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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.genetics;
19
import static org.junit.Assert.*;
21
import java.util.Arrays;
22
import java.util.Comparator;
23
import java.util.List;
25
import org.junit.Test;
27
public class RandomKeyTest {
29
@Test(expected=IllegalArgumentException.class)
30
public void testConstructor1() {
31
new DummyRandomKey(new Double[] {0.2, 0.3, 1.2});
34
@Test(expected=IllegalArgumentException.class)
35
public void testConstructor2() {
36
new DummyRandomKey(new Double[] {0.2, 0.3, -0.2});
40
public void testIsSame() {
41
DummyRandomKey drk1 = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
42
DummyRandomKey drk2 = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
43
DummyRandomKey drk3 = new DummyRandomKey(new Double[] {0.4, 0.15, 0.5, 0.8, 0.2});
44
DummyRandomKey drk4 = new DummyRandomKey(new Double[] {0.4, 0.25, 0.5, 0.8, 0.2});
45
DummyRandomKey drk5 = new DummyRandomKey(new Double[] {0.4, 0.25, 0.5, 0.8, 0.2, 0.5});
47
assertTrue(drk1.isSame(drk2));
48
assertTrue(drk2.isSame(drk3));
49
assertFalse(drk3.isSame(drk4));
50
assertFalse(drk4.isSame(drk5));
54
public void testDecode() {
55
DummyRandomKey drk = new DummyRandomKey(new Double[] {0.4, 0.1, 0.5, 0.8, 0.2});
56
List<String> decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"}));
58
assertEquals("b", decoded.get(0));
59
assertEquals("e", decoded.get(1));
60
assertEquals("a", decoded.get(2));
61
assertEquals("c", decoded.get(3));
62
assertEquals("d", decoded.get(4));
66
public void testRandomPermutation() {
67
// never generate an invalid one
68
for (int i=0; i<10; i++) {
69
DummyRandomKey drk = new DummyRandomKey(RandomKey.randomPermutation(20));
75
public void testIdentityPermutation() {
76
DummyRandomKey drk = new DummyRandomKey(RandomKey.identityPermutation(5));
77
List<String> decoded = drk.decode(Arrays.asList(new String[] {"a", "b", "c", "d", "e"}));
79
assertEquals("a", decoded.get(0));
80
assertEquals("b", decoded.get(1));
81
assertEquals("c", decoded.get(2));
82
assertEquals("d", decoded.get(3));
83
assertEquals("e", decoded.get(4));
87
public void testComparatorPermutation() {
88
List<String> data = Arrays.asList(new String[] {"x", "b", "c", "z", "b"});
90
List<Double> permutation = RandomKey.comparatorPermutation(data, new Comparator<String>() {
91
public int compare(String o1, String o2) {
92
return o1.compareTo(o2);
95
Double[] permArr = new Double[data.size()];
96
permArr = permutation.toArray(permArr);
97
assertArrayEquals(new Double[] {0.6,0.0,0.4,0.8,0.2}, permArr);
98
List<String> decodedData = new DummyRandomKey(permutation).decode(data);
99
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));
105
permutation = RandomKey.comparatorPermutation(data, new Comparator<String>() {
106
public int compare(String o1, String o2) {
107
return o2.compareTo(o1);
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));
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"});
126
DummyRandomKey drk = new DummyRandomKey(RandomKey.inducedPermutation(origData, permutedData));
127
List<String> decoded = drk.decode(origData);
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));
136
RandomKey.inducedPermutation(
137
Arrays.asList(new String[] {"a", "b", "c", "d", "d"}),
138
Arrays.asList(new String[] {"a", "b", "c", "d"})
140
fail("Uncaught exception");
141
} catch (IllegalArgumentException e) {
145
RandomKey.inducedPermutation(
146
Arrays.asList(new String[] {"a", "b", "c", "d", "d"}),
147
Arrays.asList(new String[] {"a", "b", "c", "d", "f"})
149
fail("Uncaught exception");
150
} catch (IllegalArgumentException e) {
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));