1 10 package org.jgap.supergenes; 11 12 import java.util.*; 13 import org.jgap.*; 14 import org.jgap.impl.*; 15 import junit.framework.*; 16 17 23 public class SupergenePersistentRepresentationTest 24 extends JGAPTestCase { 25 26 private final static String CVS_REVISION = "$Revision: 1.15 $"; 27 28 public static Test suite() { 29 TestSuite suite = 30 new TestSuite(SupergenePersistentRepresentationTest.class); 31 return suite; 32 } 33 34 public void setUp() { 35 super.setUp(); 36 Configuration.reset(); 37 } 38 39 public void testRepresentation() 40 throws Exception { 41 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 42 Gene i1 = new IntegerGene(conf, 1, 12); 43 Gene i2 = new DoubleGene(conf, 3, 4); 44 i1.setAllele(new Integer (7)); 45 i2.setAllele(new Double (3.2)); 46 gene.addGene(i1); 47 gene.addGene(i2); 48 InstantiableSupergeneForTest nested = new InstantiableSupergeneForTest(conf); 49 Gene n1 = new IntegerGene(conf, 1, 12); 50 Gene n2 = new DoubleGene(conf, 3, 4); 51 n1.setAllele(new Integer (5)); 52 n2.setAllele(new Double (3.6)); 53 nested.addGene(n1); 54 nested.addGene(n2); 55 gene.addGene(nested); 56 InstantiableSupergeneForTest nested2 = new InstantiableSupergeneForTest(conf); 57 nested2.setValidator(new TestValidator(conf)); 58 Gene nn1 = new IntegerGene(conf, 1, 1000); 59 Gene nn2 = new DoubleGene(conf, 0, 1000); 60 nn1.setAllele(new Integer (22)); 61 nn2.setAllele(new Double (44)); 62 nested2.addGene(nn1); 63 nested2.addGene(nn2); 64 gene.addGene(nested2); 65 InstantiableSupergeneForTest nested3 = new InstantiableSupergeneForTest(conf); 66 nested3.setValidator(null); 67 Gene nnn1 = new IntegerGene(conf, 1, 1000); 68 Gene nnn2 = new DoubleGene(conf, 0, 1000); 69 nnn1.setAllele(new Integer (555)); 70 nnn2.setAllele(new Double (777)); 71 nested3.addGene(nnn1); 72 nested3.addGene(nnn2); 73 nested2.addGene(nested3); 74 String representation = gene.getPersistentRepresentation(); 75 InstantiableSupergeneForTest restored = new InstantiableSupergeneForTest(conf); 76 restored.setValueFromPersistentRepresentation(representation); 77 assertTrue(gene.equals(restored)); 78 } 79 80 public void testValidatorPers_0() { 81 Validator val = new TestValidator(conf); 82 String pers = val.getPersistent(); 83 val.setFromPersistent(null); 84 assertSame(pers, val.getPersistent()); 85 assertSame(conf, val.getConfiguration()); 86 } 87 88 public void testGetGenes_0() 89 throws Exception { 90 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 91 assertEquals(0, gene.getGenes().length); 92 Gene subGene = new BooleanGene(conf); 93 gene.addGene(subGene); 94 assertEquals(1, gene.getGenes().length); 95 assertSame(subGene, gene.geneAt(0)); 96 } 97 98 public void testSize_0() 99 throws Exception { 100 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 101 assertEquals(0, gene.size()); 102 Gene subGene = new BooleanGene(conf); 103 gene.addGene(subGene); 104 assertEquals(1, gene.size()); 105 } 106 107 public void testIsValid_0() 108 throws Exception { 109 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 110 gene.m_validator = null; 111 assertTrue(gene.isValid()); 112 gene.m_validator = gene; 113 try { 114 assertTrue(gene.isValid()); 115 fail(); 116 } 117 catch (Error e) { 118 ; } 120 } 121 122 public void testReset_0() 123 throws Exception { 124 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 125 126 gene.reset(); 127 Set[] m = (Set[]) privateAccessor.getField(gene, "m_immutable"); 128 assertEquals(1, m.length); 129 } 130 131 public void testPers_0() 132 throws Exception { 133 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 134 try { 135 gene.setValueFromPersistentRepresentation(null); 136 fail(); 137 } 138 catch (UnsupportedRepresentationException uex) { 139 ; } 141 } 142 143 public void testToString_0() 144 throws Exception { 145 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 146 gene.m_validator = null; 147 String s = gene.toString(); 148 assertEquals("Supergene " + gene.getClass().getName() 149 + " {" 150 + " non validating" 151 + "}" 152 , s); 153 } 154 155 public void testToString_1() 156 throws Exception { 157 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 158 gene.m_validator = gene; 159 String s = gene.toString(); 160 assertEquals("Supergene " + gene.getClass().getName() 161 + " {" 162 + " validator: " + gene.getClass().getName() 163 + "}" 164 , s); 165 } 166 167 public void testToString_2() 168 throws Exception { 169 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 170 Gene bgene = new BooleanGene(conf); 171 gene.addGene(bgene); 172 gene.m_validator = gene; 173 String s = gene.toString(); 174 assertEquals("Supergene " + gene.getClass().getName() 175 + " {|" 176 + bgene.toString() 177 + "|" 178 + " validator: " + gene.getClass().getName() 179 + "}" 180 , s); 181 } 182 183 public void testCompareTo_0() 184 throws Exception { 185 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 186 assertEquals(0, gene.compareTo(gene)); 187 InstantiableSupergeneForTest gene2 = new InstantiableSupergeneForTest(conf); 188 assertEquals(0, gene.compareTo(gene2)); 189 } 190 191 public void testCompareTo_1() 192 throws Exception { 193 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 194 try { 195 gene.compareTo(new Vector()); 196 fail(); 197 } 198 catch (ClassCastException ex) { 199 ; } 201 } 202 203 public void testCompareTo_2() 204 throws Exception { 205 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 206 InstantiableSupergeneForTest gene2 = new InstantiableSupergeneForTest(conf); 207 gene2.addGene(new BooleanGene(conf)); 208 assertEquals( -1, gene.compareTo(gene2)); 209 assertEquals(1, gene2.compareTo(gene)); 210 } 211 212 public void testCompareTo_3() 213 throws Exception { 214 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 215 AbstractSupergene gene2 = new TestClass(conf); 216 assertTrue(gene.compareTo(gene2) != 0); 217 assertTrue(gene2.compareTo(gene) != 0); 218 gene2.addGene(new BooleanGene(conf)); 219 assertEquals( -1, gene.compareTo(gene2)); 220 assertEquals(1, gene2.compareTo(gene)); 221 } 222 223 public void testEquals_0() 224 throws Exception { 225 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 226 assertEquals(0, gene.compareTo(gene)); 227 assertFalse(gene.equals(null)); 228 assertFalse(gene.equals(new Vector())); 229 InstantiableSupergeneForTest gene2 = new InstantiableSupergeneForTest(conf); 230 assertTrue(gene.equals(gene2)); 231 } 232 233 public void testNewGene_0() 234 throws Exception { 235 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 236 gene.m_validator = null; 237 InstantiableSupergeneForTest gene2 = (InstantiableSupergeneForTest) gene.newGene(); 238 assertEquals(gene, gene2); 239 assertEquals(gene2, gene); 240 } 241 242 public void testNewGene_1() 243 throws Exception { 244 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 245 for (int i = 0; i < 5; i++) { 246 Gene aGene = new DoubleGene(conf); 247 gene.addGene(aGene); 248 } 249 InstantiableSupergeneForTest gene2 = (InstantiableSupergeneForTest) gene.newGene(); 250 assertEquals(gene, gene2); 251 assertEquals(gene2, gene); 252 } 253 254 public void testNewGene_2() 255 throws Exception { 256 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 257 gene.m_validator = gene; 258 InstantiableSupergeneForTest gene2 = (InstantiableSupergeneForTest) gene.newGene(); 259 assertEquals(gene, gene2); 260 assertEquals(gene2, gene); 261 } 262 263 public void testSetAllele_0() 264 throws Exception { 265 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 266 gene.setAllele(new Double (23.4)); 267 assertEquals(0, gene.size()); 268 } 269 270 public void testSetAllele_1() 271 throws Exception { 272 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 273 for (int i = 0; i < 2; i++) { 274 Gene aGene = new DoubleGene(conf); 275 gene.addGene(aGene); 276 } 277 try { 278 gene.setAllele(new Double [] {new Double (23.569)}); 279 fail(); 280 } 281 catch (IllegalArgumentException iex) { 282 ; } 284 } 285 286 public void testSetAllele_2() 287 throws Exception { 288 InstantiableSupergeneForTest gene = new InstantiableSupergeneForTest(conf); 289 for (int i = 0; i < 5; i++) { 290 Gene aGene = new DoubleGene(conf); 291 gene.addGene(aGene); 292 } 293 gene.setAllele(new Double [] {new Double (03.569), new Double (13.569), 294 new Double (23.569), new Double (33.569), new Double (43.569)}); 295 for (int i = 0; i < 5; i++) { 296 DoubleGene aGene = (DoubleGene) gene.geneAt(i); 297 assertEquals(i * 10 + 3.569, aGene.doubleValue(), DELTA); 298 } 299 } 300 301 class TestClass 325 extends AbstractSupergene { 326 public TestClass(final Configuration a_conf) 327 throws InvalidConfigurationException { 328 super(a_conf, new Gene[] {}); 329 } 330 331 public boolean isValid(Gene[] a) { 332 throw new Error ("Should never be called."); 333 } 334 335 protected Gene newGeneInternal() { 336 throw new Error ("Should never be called."); 337 } 338 } 339 } 340 | Popular Tags |