001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math3.genetics;
018
019
020 import org.junit.Assert;
021 import org.junit.Test;
022
023 public class BinaryChromosomeTest {
024
025 @Test
026 public void testInvalidConstructor() {
027 Integer[][] reprs = new Integer[][] {
028 new Integer[] {0,1,0,1,2},
029 new Integer[] {0,1,0,1,-1}
030 };
031
032 for (Integer[] repr : reprs) {
033 try {
034 new DummyBinaryChromosome(repr);
035 Assert.fail("Exception not caught");
036 } catch (IllegalArgumentException e) {
037 // Expected
038 }
039 }
040 }
041
042 @Test
043 public void testRandomConstructor() {
044 for (int i=0; i<20; i++) {
045 new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(10));
046 }
047 }
048
049 @Test
050 public void testIsSame() {
051 Chromosome c1 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1});
052 Chromosome c2 = new DummyBinaryChromosome(new Integer[] {0,1,1,0,1});
053 Chromosome c3 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1,1});
054 Chromosome c4 = new DummyBinaryChromosome(new Integer[] {1,1,0,1,0,1});
055 Chromosome c5 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,0});
056 Chromosome c6 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1});
057
058 Assert.assertFalse(c1.isSame(c2));
059 Assert.assertFalse(c1.isSame(c3));
060 Assert.assertFalse(c1.isSame(c4));
061 Assert.assertFalse(c1.isSame(c5));
062 Assert.assertTrue(c1.isSame(c6));
063 }
064
065 }