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.math.genetics;
018
019 import static org.junit.Assert.assertFalse;
020 import static org.junit.Assert.assertTrue;
021 import static org.junit.Assert.fail;
022
023 import org.junit.Test;
024
025 public class BinaryChromosomeTest {
026
027 @Test
028 public void testInvalidConstructor() {
029 Integer[][] reprs = new Integer[][] {
030 new Integer[] {0,1,0,1,2},
031 new Integer[] {0,1,0,1,-1}
032 };
033
034 for (Integer[] repr : reprs) {
035 try {
036 new DummyBinaryChromosome(repr);
037 fail("Exception not caught");
038 } catch (IllegalArgumentException e) {
039
040 }
041 }
042 }
043
044 @Test
045 public void testRandomConstructor() {
046 for (int i=0; i<20; i++) {
047 new DummyBinaryChromosome(BinaryChromosome.randomBinaryRepresentation(10));
048 }
049 }
050
051 @Test
052 public void testIsSame() {
053 Chromosome c1 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1});
054 Chromosome c2 = new DummyBinaryChromosome(new Integer[] {0,1,1,0,1});
055 Chromosome c3 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1,1});
056 Chromosome c4 = new DummyBinaryChromosome(new Integer[] {1,1,0,1,0,1});
057 Chromosome c5 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,0});
058 Chromosome c6 = new DummyBinaryChromosome(new Integer[] {0,1,0,1,0,1});
059
060 assertFalse(c1.isSame(c2));
061 assertFalse(c1.isSame(c3));
062 assertFalse(c1.isSame(c4));
063 assertFalse(c1.isSame(c5));
064 assertTrue(c1.isSame(c6));
065 }
066
067 }