1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.genetics;
18  
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import org.junit.Test;
24  
25  public class BinaryChromosomeTest {
26      
27      @Test
28      public void testInvalidConstructor() {        
29          Integer[][] reprs = new Integer[][] {
30                  new Integer[] {0,1,0,1,2},
31                  new Integer[] {0,1,0,1,-1}
32          };
33          
34          for (Integer[] repr : reprs) {
35              try {
36                  new DummyBinaryChromosome(repr);
37                  fail("Exception not caught");
38              } catch (IllegalArgumentException e) {
39                  
40              }
41          }
42      }
43      
44      @Test
45      public void testRandomConstructor() {
46          for (int i=0; i<20; i++) {
47              new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(10));
48          }
49      }
50      
51      @Test
52      public void testIsSame() {
53          Chromosome c1 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1});
54          Chromosome c2 = new DummyBinaryChromosome(new Integer[] {0,1,1,0,1});
55          Chromosome c3 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1,1});
56          Chromosome c4 = new DummyBinaryChromosome(new Integer[] {1,1,0,1,0,1});
57          Chromosome c5 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,0});
58          Chromosome c6 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1});
59          
60          assertFalse(c1.isSame(c2));
61          assertFalse(c1.isSame(c3));
62          assertFalse(c1.isSame(c4));
63          assertFalse(c1.isSame(c5));
64          assertTrue(c1.isSame(c6));
65      }
66  
67  }