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.*;
020
021 import java.util.Arrays;
022 import java.util.Comparator;
023 import java.util.List;
024
025 import org.junit.Test;
026
027 public class RandomKeyTest {
028
029 @Test(expected=IllegalArgumentException.class)
030 public void testConstructor1() {
031 new DummyRandomKey(new Double[] {0.2, 0.3, 1.2});
032 }
033
034 @Test(expected=IllegalArgumentException.class)
035 public void testConstructor2() {
036 new DummyRandomKey(new Double[] {0.2, 0.3, -0.2});
037 }
038
039 @Test
040 public void testIsSame() {
041 DummyRandomKey drk1 = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
042 DummyRandomKey drk2 = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
043 DummyRandomKey drk3 = new DummyRandomKey(new Double[] {0.4, 0.15, 0.5, 0.8, 0.2});
044 DummyRandomKey drk4 = new DummyRandomKey(new Double[] {0.4, 0.25, 0.5, 0.8, 0.2});
045 DummyRandomKey drk5 = new DummyRandomKey(new Double[] {0.4, 0.25, 0.5, 0.8, 0.2, 0.5});
046
047 assertTrue(drk1.isSame(drk2));
048 assertTrue(drk2.isSame(drk3));
049 assertFalse(drk3.isSame(drk4));
050 assertFalse(drk4.isSame(drk5));
051 }
052
053 @Test
054 public void testDecode() {
055 DummyRandomKey drk = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
056 List<String> decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"}));
057
058 assertEquals("b", decoded.get(0));
059 assertEquals("e", decoded.get(1));
060 assertEquals("a", decoded.get(2));
061 assertEquals("c", decoded.get(3));
062 assertEquals("d", decoded.get(4));
063 }
064
065 @Test
066 public void testRandomPermutation() {
067 // never generate an invalid one
068 for (int i=0; i<10; i++) {
069 DummyRandomKey drk = new DummyRandomKey(RandomKey.randomPermutation(20));
070 assertNotNull(drk);
071 }
072 }
073
074 @Test
075 public void testIdentityPermutation() {
076 DummyRandomKey drk = new DummyRandomKey(RandomKey.identityPermutation(5));
077 List<String> decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"}));
078
079 assertEquals("a", decoded.get(0));
080 assertEquals("b", decoded.get(1));
081 assertEquals("c", decoded.get(2));
082 assertEquals("d", decoded.get(3));
083 assertEquals("e", decoded.get(4));
084 }
085
086 @Test
087 public void testComparatorPermutation() {
088 List<String> data = Arrays.asList(new String[] {"x", "b", "c", "z", "b"});
089
090 List<Double> permutation = RandomKey.comparatorPermutation(data, new Comparator<String>() {
091 public int compare(String o1, String o2) {
092 return o1.compareTo(o2);
093 }
094 });
095 Double[] permArr = new Double[data.size()];
096 permArr = permutation.toArray(permArr);
097 assertArrayEquals(new Double[] {0.6,0.0,0.4,0.8,0.2}, permArr);
098 List<String> decodedData = new DummyRandomKey(permutation).decode(data);
099 assertEquals("b", decodedData.get(0));
100 assertEquals("b", decodedData.get(1));
101 assertEquals("c", decodedData.get(2));
102 assertEquals("x", decodedData.get(3));
103 assertEquals("z", decodedData.get(4));
104
105 permutation = RandomKey.comparatorPermutation(data, new Comparator<String>() {
106 public int compare(String o1, String o2) {
107 return o2.compareTo(o1);
108 }
109 });
110 permArr = new Double[data.size()];
111 permArr = permutation.toArray(permArr);
112 assertArrayEquals(new Double[] {0.2,0.6,0.4,0.0,0.8}, permArr);
113 decodedData = new DummyRandomKey(permutation).decode(data);
114 assertEquals("z", decodedData.get(0));
115 assertEquals("x", decodedData.get(1));
116 assertEquals("c", decodedData.get(2));
117 assertEquals("b", decodedData.get(3));
118 assertEquals("b", decodedData.get(4));
119 }
120
121 @Test
122 public void testInducedPermutation() {
123 List<String> origData = Arrays.asList(new String[] {"a", "b", "c", "d", "d"});
124 List<String> permutedData = Arrays.asList(new String[] {"d", "b", "c", "a", "d"});
125
126 DummyRandomKey drk = new DummyRandomKey(RandomKey.inducedPermutation(origData, permutedData));
127 List<String> decoded = drk.decode(origData);
128
129 assertEquals("d", decoded.get(0));
130 assertEquals("b", decoded.get(1));
131 assertEquals("c", decoded.get(2));
132 assertEquals("a", decoded.get(3));
133 assertEquals("d", decoded.get(4));
134
135 try {
136 RandomKey.inducedPermutation(
137 Arrays.asList(new String[] {"a", "b", "c", "d", "d"}),
138 Arrays.asList(new String[] {"a", "b", "c", "d"})
139 );
140 fail("Uncaught exception");
141 } catch (IllegalArgumentException e) {
142 // no-op
143 }
144 try {
145 RandomKey.inducedPermutation(
146 Arrays.asList(new String[] {"a", "b", "c", "d", "d"}),
147 Arrays.asList(new String[] {"a", "b", "c", "d", "f"})
148 );
149 fail("Uncaught exception");
150 } catch (IllegalArgumentException e) {
151 // no-op
152 }
153 }
154
155 @Test
156 public void testEqualRepr() {
157 DummyRandomKey drk = new DummyRandomKey(new Double[] {0.2, 0.2, 0.5});
158 List<String> decodedData = drk.decode(Arrays.asList(new String[] {"a", "b", "c"}));
159 assertEquals("a", decodedData.get(0));
160 assertEquals("b", decodedData.get(1));
161 assertEquals("c", decodedData.get(2));
162 }
163
164 }