1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.genetics;
18
19
20 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
21 import org.junit.Assert;
22 import org.junit.Test;
23
24 public class BinaryChromosomeTest {
25
26 @Test
27 public void testInvalidConstructor() {
28 Integer[][] reprs = new Integer[][] {
29 new Integer[] {0,1,0,1,2},
30 new Integer[] {0,1,0,1,-1}
31 };
32
33 for (Integer[] repr : reprs) {
34 try {
35 new DummyBinaryChromosome(repr);
36 Assert.fail("Exception not caught");
37 } catch (MathIllegalArgumentException e) {
38
39 }
40 }
41 }
42
43 @Test
44 public void testRandomConstructor() {
45 for (int i=0; i<20; i++) {
46 new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(10));
47 }
48 }
49
50 @Test
51 public void testIsSame() {
52 Chromosome c1 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1});
53 Chromosome c2 = new DummyBinaryChromosome(new Integer[] {0,1,1,0,1});
54 Chromosome c3 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1,1});
55 Chromosome c4 = new DummyBinaryChromosome(new Integer[] {1,1,0,1,0,1});
56 Chromosome c5 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,0});
57 Chromosome c6 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1});
58
59 Assert.assertFalse(c1.isSame(c2));
60 Assert.assertFalse(c1.isSame(c3));
61 Assert.assertFalse(c1.isSame(c4));
62 Assert.assertFalse(c1.isSame(c5));
63 Assert.assertTrue(c1.isSame(c6));
64 }
65 }