~ubuntu-branches/ubuntu/maverick/commons-math/maverick

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/genetics/RandomKeyTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-08-22 01:13:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090822011325-hi4peq1ua5weguwn
Tags: 2.0-1
* New upstream release.
* Set Maintainer field to Debian Java Team
* Add myself as Uploaders
* Switch to Quilt patch system:
  - Refresh all patchs
  - Remove B-D on dpatch, Add B-D on quilt
  - Include patchsys-quilt.mk in debian/rules
* Bump Standards-Version to 3.8.3:
  - Add a README.source to describe patch system
* Maven POMs:
  - Add a Build-Depends-Indep dependency on maven-repo-helper
  - Use mh_installpom and mh_installjar to install the POM and the jar to the
    Maven repository
* Use default-jdk/jre:
  - Depends on java5-runtime-headless
  - Build-Depends on default-jdk
  - Use /usr/lib/jvm/default-java as JAVA_HOME
* Move api documentation to /usr/share/doc/libcommons-math-java/api
* Build-Depends on junit4 instead of junit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 */
 
17
package org.apache.commons.math.genetics;
 
18
 
 
19
import static org.junit.Assert.*;
 
20
 
 
21
import java.util.Arrays;
 
22
import java.util.Comparator;
 
23
import java.util.List;
 
24
 
 
25
import org.junit.Test;
 
26
 
 
27
public class RandomKeyTest {
 
28
 
 
29
    @Test(expected=IllegalArgumentException.class)
 
30
    public void testConstructor1() {
 
31
        new DummyRandomKey(new Double[] {0.2, 0.3, 1.2});
 
32
    }
 
33
    
 
34
    @Test(expected=IllegalArgumentException.class)
 
35
    public void testConstructor2() {
 
36
        new DummyRandomKey(new Double[] {0.2, 0.3, -0.2});
 
37
    }
 
38
 
 
39
    @Test
 
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});
 
46
        
 
47
        assertTrue(drk1.isSame(drk2));
 
48
        assertTrue(drk2.isSame(drk3));
 
49
        assertFalse(drk3.isSame(drk4));
 
50
        assertFalse(drk4.isSame(drk5));
 
51
    }
 
52
 
 
53
    @Test
 
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"}));
 
57
        
 
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));
 
63
    }
 
64
 
 
65
    @Test
 
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));
 
70
            assertNotNull(drk);
 
71
        }
 
72
    }
 
73
 
 
74
    @Test
 
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"}));
 
78
        
 
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));
 
84
    }
 
85
 
 
86
    @Test
 
87
    public void testComparatorPermutation() {
 
88
        List<String> data = Arrays.asList(new String[] {"x", "b", "c", "z", "b"});
 
89
        
 
90
        List<Double> permutation = RandomKey.comparatorPermutation(data, new Comparator<String>() {
 
91
            public int compare(String o1, String o2) {
 
92
                return o1.compareTo(o2);
 
93
            }
 
94
        });
 
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));
 
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
}