1 10 package org.jgap.impl; 11 12 import org.jgap.*; 13 import junit.framework.*; 14 15 21 public class SetGeneTest 22 extends JGAPTestCase { 23 24 private final static String CVS_REVISION = "$Revision: 1.2 $"; 25 26 public static Test suite() { 27 TestSuite suite = new TestSuite(SetGeneTest.class); 28 return suite; 29 } 30 31 public void testConstruct_0() 32 throws Exception { 33 SetGene gene = new SetGene(conf); 34 assertSame(conf, gene.getConfiguration()); 35 } 36 37 43 public void testConstruct_1() 44 throws Exception { 45 SetGene gene = new SetGene(conf); 46 assertEquals(null, gene.getInternalValue()); 47 } 48 49 55 public void testToString_0() 56 throws Exception { 57 SetGene gene = new SetGene(conf); 58 gene.addAllele("testValue1"); 59 gene.addAllele("testValue2"); 60 gene.setAllele("testValue1"); 61 assertEquals("testValue1, Application data:null", gene.toString()); 62 } 63 64 70 public void testToString_1() 71 throws Exception { 72 SetGene gene = new SetGene(conf); 73 gene.setApplicationData("hallo"); 74 gene.addAllele("testValue1"); 75 gene.addAllele("testValue2"); 76 gene.setAllele("testValue1"); 77 assertEquals("testValue1, Application data:hallo", gene.toString()); 78 } 79 80 86 public void testGetAllele_0() 87 throws Exception { 88 SetGene gene = new SetGene(conf); 89 gene.addAllele("testValue1"); 90 gene.addAllele("testValue2"); 91 gene.setAllele("testValue2"); 92 assertEquals("testValue2", gene.getAllele()); 93 } 94 95 public void testEquals_0() 96 throws Exception { 97 Gene gene1 = new SetGene(conf); 98 Gene gene2 = new SetGene(conf); 99 assertTrue(gene1.equals(gene2)); 100 } 101 102 public void testEquals_1() 103 throws Exception { 104 Gene gene1 = new SetGene(conf); 105 assertFalse(gene1.equals(null)); 106 } 107 108 public void testEquals_2() 109 throws Exception { 110 SetGene gene1 = new SetGene(conf); 111 gene1.addAllele(new Double (2.3d)); 112 gene1.setAllele(new Double (2.3d)); 113 SetGene gene2 = new SetGene(conf); 114 gene2.addAllele(new Double (2.4d)); 115 gene2.setAllele(new Double (2.4d)); 116 assertFalse(gene1.equals(gene2)); 117 assertFalse(gene2.equals(gene1)); 118 } 119 120 public void testEquals_3() 121 throws Exception { 122 Gene gene1 = new SetGene(conf); 123 assertFalse(gene1.equals(new IntegerGene(conf))); 124 } 125 126 public void testEquals_4() 127 throws Exception { 128 Gene gene1 = new SetGene(conf); 129 Gene gene2 = new IntegerGene(conf); 130 assertFalse(gene1.equals(gene2)); 131 assertFalse(gene2.equals(gene1)); 132 } 133 134 public void testEquals_5() 135 throws Exception { 136 Gene gene1 = new SetGene(conf); 137 Gene gene2 = new FixedBinaryGene(conf, 1); 138 assertFalse(gene1.equals(gene2)); 139 assertFalse(gene2.equals(gene1)); 140 } 141 142 148 public void testSetAllele_0() 149 throws Exception { 150 SetGene gene1 = new SetGene(conf); 151 try { 152 gene1.setAllele(null); 153 fail(); 154 } 155 catch (IllegalArgumentException iex) { 156 ; } 158 } 159 160 166 public void testSetAllele_1() 167 throws Exception { 168 SetGene gene = new SetGene(conf); 169 gene.addAllele("testValue1"); 170 gene.addAllele("testValue2"); 171 try { 172 gene.setAllele("testValue3"); 173 fail(); 174 } 175 catch (IllegalArgumentException iex) { 176 ; } 178 } 179 180 186 public void testSetAllele_2() 187 throws Exception { 188 SetGene gene = new SetGene(conf); 189 gene.addAllele("testValue1"); 190 gene.addAllele("testValue2"); 191 gene.addAllele("testValue3"); 192 gene.setAllele("testValue2"); 193 } 194 195 public void testCompareTo_0() 196 throws Exception { 197 Gene gene1 = new SetGene(conf); 198 assertEquals(1, gene1.compareTo(null)); 199 } 200 201 public void testCompareTo_1() 202 throws Exception { 203 Gene gene1 = new SetGene(conf); 204 Gene gene2 = new SetGene(conf); 205 assertEquals(0, gene1.compareTo(gene2)); 206 assertEquals(0, gene2.compareTo(gene1)); 207 } 208 209 public void testCompareTo_2() 210 throws Exception { 211 SetGene gene1 = new SetGene(conf); 212 gene1.addAllele(Boolean.valueOf(true)); 213 gene1.addAllele(Boolean.valueOf(false)); 214 gene1.setAllele(Boolean.valueOf(true)); 215 SetGene gene2 = new SetGene(conf); 216 gene2.addAllele(Boolean.valueOf(true)); 217 gene2.setAllele(Boolean.valueOf(true)); 218 assertEquals(0, gene1.compareTo(gene2)); 219 assertEquals(0, gene2.compareTo(gene1)); 220 gene1.setAllele(Boolean.valueOf(false)); 221 gene2.addAllele(Boolean.valueOf(false)); 222 gene2.setAllele(Boolean.valueOf(false)); 223 assertEquals(0, gene1.compareTo(gene2)); 224 assertEquals(0, gene2.compareTo(gene1)); 225 } 226 227 public void testCompareTo_3() 228 throws Exception { 229 SetGene gene1 = new SetGene(conf); 230 gene1.addAllele(Boolean.valueOf(true)); 231 gene1.setAllele(Boolean.valueOf(true)); 232 SetGene gene2 = new SetGene(conf); 233 gene2.addAllele(Boolean.valueOf(false)); 234 gene2.setAllele(Boolean.valueOf(false)); 235 assertEquals(1, gene1.compareTo(gene2)); 236 assertEquals( -1, gene2.compareTo(gene1)); 237 } 238 239 245 public void testApplyMutation_0() 246 throws Exception { 247 conf.setRandomGenerator(new RandomGeneratorForTest(0)); 248 SetGene gene = new SetGene(conf); 249 gene.addAllele(Boolean.valueOf(true)); 250 gene.addAllele(Boolean.valueOf(false)); 251 gene.setAllele(Boolean.valueOf(true)); 252 gene.applyMutation(0, 0.0d); 253 assertEquals(true, ((Boolean )gene.getAllele()).booleanValue()); 254 } 255 256 262 public void testApplyMutation_1() 263 throws Exception { 264 conf.setRandomGenerator(new RandomGeneratorForTest(0)); 265 SetGene gene = new SetGene(conf); 266 gene.addAllele(Boolean.valueOf(true)); 267 gene.addAllele(Boolean.valueOf(false)); 268 gene.setAllele(Boolean.valueOf(true)); 269 gene.applyMutation(1, 0.000001d); assertEquals(true, ((Boolean )gene.getAllele()).booleanValue()); 271 } 272 273 279 public void testApplyMutation_2() 280 throws Exception { 281 conf.setRandomGenerator(new RandomGeneratorForTest(0)); 282 SetGene gene = new SetGene(conf); 283 gene.addAllele(Boolean.valueOf(true)); 284 gene.addAllele(Boolean.valueOf(false)); 285 gene.setAllele(Boolean.valueOf(true)); 286 gene.applyMutation(333, -0.000001d); assertEquals(true, ((Boolean )gene.getAllele()).booleanValue()); 288 } 289 290 291 292 297 public void testApplyMutation_3() 298 throws Exception { 299 BooleanGene gene = new BooleanGene(conf); 300 gene.setAllele(Boolean.valueOf(true)); 301 gene.applyMutation(0, -1.0d); 302 assertEquals(false, gene.booleanValue()); 303 } 304 305 310 public void testApplyMutation_4() 311 throws Exception { 312 BooleanGene gene = new BooleanGene(conf); 313 gene.setAllele(Boolean.valueOf(true)); 314 gene.applyMutation(0, -2.0d); 315 assertEquals(false, gene.booleanValue()); 316 } 317 318 323 public void testApplyMutation_5() 324 throws Exception { 325 BooleanGene gene = new BooleanGene(conf); 326 gene.setAllele(Boolean.valueOf(true)); 327 gene.applyMutation(0, 2.0d); 328 assertEquals(true, gene.booleanValue()); 329 } 330 331 336 public void testApplyMutation_6() 337 throws Exception { 338 BooleanGene gene = new BooleanGene(conf); 339 gene.setAllele(Boolean.valueOf(false)); 340 gene.applyMutation(0, 2.0d); 341 assertEquals(true, gene.booleanValue()); 342 } 343 344 349 public void testApplyMutation_7() 350 throws Exception { 351 BooleanGene gene = new BooleanGene(conf); 352 gene.setAllele(Boolean.valueOf(false)); 353 gene.applyMutation(0, -1.0d); 354 assertEquals(false, gene.booleanValue()); 355 } 356 357 362 public void testApplyMutation_8() 363 throws Exception { 364 BooleanGene gene = new BooleanGene(conf); 365 gene.setAllele(Boolean.valueOf(false)); 366 gene.applyMutation(22, -0.5d); assertEquals(false, gene.booleanValue()); 368 } 369 370 375 public void testApplyMutation_9() 376 throws Exception { 377 BooleanGene gene = new BooleanGene(conf); 378 gene.setAllele(Boolean.valueOf(false)); 379 gene.applyMutation(22, 0.5d); assertEquals(true, gene.booleanValue()); 381 } 382 383 389 public void testApplyMutation_10() 390 throws Exception { 391 BooleanGene gene = new BooleanGene(conf); 392 gene.applyMutation(0, 0.0d); 393 assertEquals(false, gene.booleanValue()); 394 } 395 396 403 public void testSetValueFromPersistentRepresentation_0() 404 throws Exception { 405 SetGene gene = new SetGene(conf); 406 gene.setValueFromPersistentRepresentation(null); 407 } 408 409 415 public void testSetValueFromPersistentRepresentation_1() 416 throws Exception { 417 BooleanGene gene = new BooleanGene(conf); 418 gene.setValueFromPersistentRepresentation("null"); 419 assertEquals(null, gene.getAllele()); 420 } 421 422 428 public void testSetValueFromPersistentRepresentation_2() 429 throws Exception { 430 BooleanGene gene = new BooleanGene(conf); 431 gene.setValueFromPersistentRepresentation("true"); 432 assertEquals(Boolean.TRUE, gene.getAllele()); 433 } 434 435 440 public void testSetValueFromPersistentRepresentation_3() 441 throws Exception { 442 BooleanGene gene = new BooleanGene(conf); 443 gene.setValueFromPersistentRepresentation("false"); 444 assertEquals(Boolean.FALSE, gene.getAllele()); 445 } 446 447 453 public void testSetValueFromPersistentRepresentation_4() 454 throws Exception { 455 BooleanGene gene = new BooleanGene(conf); 456 try { 457 gene.setValueFromPersistentRepresentation("True"); 458 fail(); 459 } 460 catch (UnsupportedRepresentationException uex) { 461 ; } 463 } 464 465 471 public void testSetValueFromPersistentRepresentation_5() 472 throws Exception { 473 BooleanGene gene = new BooleanGene(conf); 474 try { 475 gene.setValueFromPersistentRepresentation("False"); 476 fail(); 477 } 478 catch (UnsupportedRepresentationException uex) { 479 ; } 481 } 482 483 489 public void testSetValueFromPersistentRepresentation_6() 490 throws Exception { 491 BooleanGene gene = new BooleanGene(conf); 492 try { 493 gene.setValueFromPersistentRepresentation("X"); 494 fail(); 495 } 496 catch (UnsupportedRepresentationException uex) { 497 ; } 499 } 500 501 506 public void testGetPersistentRepresentation_0() 507 throws Exception { 508 BooleanGene gene = new BooleanGene(conf); 509 gene.setAllele(Boolean.valueOf(true)); 510 String s = gene.getPersistentRepresentation(); 511 assertEquals("true", s); 512 } 513 514 519 public void testGetPersistentRepresentation_1() 520 throws Exception { 521 BooleanGene gene = new BooleanGene(conf); 522 gene.setAllele(Boolean.valueOf(false)); 523 String s = gene.getPersistentRepresentation(); 524 assertEquals("false", s); 525 } 526 527 532 public void testGetPersistentRepresentation_2() 533 throws Exception { 534 BooleanGene gene = new BooleanGene(conf); 535 String s = gene.getPersistentRepresentation(); 536 assertEquals("null", s); 537 } 538 539 545 public void testHashCode_0() 546 throws Exception { 547 BooleanGene gene = new BooleanGene(conf); 548 assertEquals( -2, gene.hashCode()); 549 } 550 551 557 public void testSetEnergy_0() 558 throws Exception { 559 BaseGene gene = new BooleanGene(conf); 560 assertEquals(0.0, gene.getEnergy(), DELTA); 561 } 562 563 569 public void testSetEnergy_1() 570 throws Exception { 571 BaseGene gene = new BooleanGene(conf); 572 gene.setEnergy(2.3); 573 assertEquals(2.3, gene.getEnergy(), DELTA); 574 gene.setEnergy( -55.8); 575 assertEquals( -55.8, gene.getEnergy(), DELTA); 576 gene.setEnergy(0.5); 577 gene.setEnergy(0.8); 578 assertEquals(0.8, gene.getEnergy(), DELTA); 579 } 580 } 581 | Popular Tags |