1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.genetics;
18
19
20 import java.util.Arrays;
21 import java.util.Comparator;
22 import java.util.List;
23
24 import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
25 import org.junit.Assert;
26 import org.junit.Test;
27
28 @SuppressWarnings("boxing")
29 public class RandomKeyTest {
30
31 @Test(expected=MathIllegalArgumentException.class)
32 public void testConstructor1() {
33 new DummyRandomKey(new Double[] {0.2, 0.3, 1.2});
34 }
35
36 @Test(expected=MathIllegalArgumentException.class)
37 public void testConstructor2() {
38 new DummyRandomKey(new Double[] {0.2, 0.3, -0.2});
39 }
40
41 @Test
42 public void testIsSame() {
43 DummyRandomKey drk1 = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
44 DummyRandomKey drk2 = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
45 DummyRandomKey drk3 = new DummyRandomKey(new Double[] {0.4, 0.15, 0.5, 0.8, 0.2});
46 DummyRandomKey drk4 = new DummyRandomKey(new Double[] {0.4, 0.25, 0.5, 0.8, 0.2});
47 DummyRandomKey drk5 = new DummyRandomKey(new Double[] {0.4, 0.25, 0.5, 0.8, 0.2, 0.5});
48
49 Assert.assertTrue(drk1.isSame(drk2));
50 Assert.assertTrue(drk2.isSame(drk3));
51 Assert.assertFalse(drk3.isSame(drk4));
52 Assert.assertFalse(drk4.isSame(drk5));
53 }
54
55 @Test
56 public void testDecode() {
57 DummyRandomKey drk = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
58 List<String> decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"}));
59
60 Assert.assertEquals("b", decoded.get(0));
61 Assert.assertEquals("e", decoded.get(1));
62 Assert.assertEquals("a", decoded.get(2));
63 Assert.assertEquals("c", decoded.get(3));
64 Assert.assertEquals("d", decoded.get(4));
65 }
66
67 @Test(expected=MathIllegalArgumentException.class)
68 public void testInvalidRepresentation() {
69 new DummyRandomKey(new Double[] {0.1, 0.1, 2d, 0.8, 0.2});
70 }
71
72 @Test
73 public void testRandomPermutation() {
74
75 for (int i=0; i<10; i++) {
76 DummyRandomKey drk = new DummyRandomKey(RandomKey.randomPermutation(20));
77 Assert.assertNotNull(drk);
78 }
79 }
80
81 @Test
82 public void testIdentityPermutation() {
83 DummyRandomKey drk = new DummyRandomKey(RandomKey.identityPermutation(5));
84 List<String> decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"}));
85
86 Assert.assertEquals("a", decoded.get(0));
87 Assert.assertEquals("b", decoded.get(1));
88 Assert.assertEquals("c", decoded.get(2));
89 Assert.assertEquals("d", decoded.get(3));
90 Assert.assertEquals("e", decoded.get(4));
91 }
92
93 @Test
94 public void testComparatorPermutation() {
95 List<String> data = Arrays.asList(new String[] {"x", "b", "c", "z", "b"});
96
97 List<Double> permutation = RandomKey.comparatorPermutation(data, new Comparator<String>() {
98 @Override
99 public int compare(String o1, String o2) {
100 return o1.compareTo(o2);
101 }
102 });
103 Double[] permArr = new Double[data.size()];
104 permArr = permutation.toArray(permArr);
105 Assert.assertArrayEquals(new Double[] {0.6,0.0,0.4,0.8,0.2}, permArr);
106 List<String> decodedData = new DummyRandomKey(permutation).decode(data);
107 Assert.assertEquals("b", decodedData.get(0));
108 Assert.assertEquals("b", decodedData.get(1));
109 Assert.assertEquals("c", decodedData.get(2));
110 Assert.assertEquals("x", decodedData.get(3));
111 Assert.assertEquals("z", decodedData.get(4));
112
113 permutation = RandomKey.comparatorPermutation(data, new Comparator<String>() {
114 @Override
115 public int compare(String o1, String o2) {
116 return o2.compareTo(o1);
117 }
118 });
119 permArr = new Double[data.size()];
120 permArr = permutation.toArray(permArr);
121 Assert.assertArrayEquals(new Double[] {0.2,0.6,0.4,0.0,0.8}, permArr);
122 decodedData = new DummyRandomKey(permutation).decode(data);
123 Assert.assertEquals("z", decodedData.get(0));
124 Assert.assertEquals("x", decodedData.get(1));
125 Assert.assertEquals("c", decodedData.get(2));
126 Assert.assertEquals("b", decodedData.get(3));
127 Assert.assertEquals("b", decodedData.get(4));
128 }
129
130 @Test
131 public void testInducedPermutation() {
132 List<String> origData = Arrays.asList(new String[] {"a", "b", "c", "d", "d"});
133 List<String> permutedData = Arrays.asList(new String[] {"d", "b", "c", "a", "d"});
134
135 DummyRandomKey drk = new DummyRandomKey(RandomKey.inducedPermutation(origData, permutedData));
136 List<String> decoded = drk.decode(origData);
137
138 Assert.assertEquals("d", decoded.get(0));
139 Assert.assertEquals("b", decoded.get(1));
140 Assert.assertEquals("c", decoded.get(2));
141 Assert.assertEquals("a", decoded.get(3));
142 Assert.assertEquals("d", decoded.get(4));
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"})
148 );
149 Assert.fail("Uncaught exception");
150 } catch (MathIllegalArgumentException e) {
151
152 }
153 try {
154 RandomKey.inducedPermutation(
155 Arrays.asList(new String[] {"a", "b", "c", "d", "d"}),
156 Arrays.asList(new String[] {"a", "b", "c", "d", "f"})
157 );
158 Assert.fail("Uncaught exception");
159 } catch (MathIllegalArgumentException e) {
160
161 }
162 }
163
164 @Test
165 public void testEqualRepr() {
166 DummyRandomKey drk = new DummyRandomKey(new Double[] {0.2, 0.2, 0.5});
167 List<String> decodedData = drk.decode(Arrays.asList(new String[] {"a", "b", "c"}));
168 Assert.assertEquals("a", decodedData.get(0));
169 Assert.assertEquals("b", decodedData.get(1));
170 Assert.assertEquals("c", decodedData.get(2));
171 }
172 }