View Javadoc
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.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                  // Expected
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  }