KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > audit > KeyedValuesTest


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.audit;
11
12 import java.util.*;
13 import org.jgap.*;
14 import junit.framework.*;
15
16 /**
17  * Tests for KeyedValues class
18  *
19  * @author Klaus Meffert
20  * @since 3.0
21  */

22 public class KeyedValuesTest
23     extends JGAPTestCase {
24   /** String containing the CVS revision. Read out via reflection!*/
25   private final static String JavaDoc CVS_REVISION = "$Revision: 1.3 $";
26
27   public void setUp() {
28     super.setUp();
29   }
30
31   public static Test suite() {
32     TestSuite suite = new TestSuite(KeyedValuesTest.class);
33     return suite;
34   }
35
36   /**
37    * @throws Exception
38    *
39    * @author Klaus Meffert
40    * @since 3.0
41    */

42   public void testConstruct_0()
43       throws Exception JavaDoc {
44     KeyedValues kv = new KeyedValues();
45     assertEquals(0, kv.size());
46     assertTrue(kv.hashCode() < 0);
47   }
48
49   /**
50    * @throws Exception
51    *
52    * @author Klaus Meffert
53    * @since 3.0
54    */

55   public void testSetValue_0()
56       throws Exception JavaDoc {
57     KeyedValues kv = new KeyedValues();
58     Comparable JavaDoc key = new Double JavaDoc(2.3d);
59     Number JavaDoc value = new Double JavaDoc(4.5d);
60     kv.setValue(key, value);
61     Comparable JavaDoc key2 = new Double JavaDoc(2.4d);
62     Number JavaDoc value2 = new Double JavaDoc(4.6d);
63     kv.setValue(key2, value2);
64     assertSame(key, kv.getKey(0));
65     assertSame(value, kv.getValue(0));
66     assertSame(key2, kv.getKey(1));
67     assertSame(value2, kv.getValue(1));
68   }
69
70   /**
71    * @throws Exception
72    *
73    * @author Klaus Meffert
74    * @since 3.0
75    */

76   public void testSetValue_1()
77       throws Exception JavaDoc {
78     KeyedValues kv = new KeyedValues();
79     Comparable JavaDoc key = new Double JavaDoc(2.3d);
80     Number JavaDoc value = new Double JavaDoc(4.5d);
81     kv.setValue(key, value);
82     value = new Double JavaDoc(23.11d);
83     kv.setValue(key, value);
84     assertSame(value, kv.getValue(key));
85   }
86
87   /**
88    * @throws Exception
89    *
90    * @author Klaus Meffert
91    * @since 3.0
92    */

93   public void testGetKeys_0()
94       throws Exception JavaDoc {
95     KeyedValues kv = new KeyedValues();
96     Comparable JavaDoc key = new Double JavaDoc(2.3d);
97     Number JavaDoc value = new Double JavaDoc(4.5d);
98     kv.setValue(key, value);
99     List keys = kv.getKeys();
100     assertEquals(1, keys.size());
101     assertSame(key, keys.get(0));
102     key = new Double JavaDoc(22.3d);
103     value = new Double JavaDoc(42.5d);
104     kv.setValue(key, value);
105     keys = kv.getKeys();
106     assertEquals(2, keys.size());
107     assertSame(key, keys.get(1));
108   }
109
110   /**
111    * @throws Exception
112    *
113    * @author Klaus Meffert
114    * @since 3.0
115    */

116   public void testGetValue_0()
117       throws Exception JavaDoc {
118     KeyedValues kv = new KeyedValues();
119     Comparable JavaDoc key = new Double JavaDoc(2.3d);
120     Number JavaDoc value = new Double JavaDoc(4.5d);
121     kv.setValue(key, value);
122     Comparable JavaDoc key2 = new Double JavaDoc(22.3d);
123     Number JavaDoc value2 = new Double JavaDoc(42.5d);
124     kv.setValue(key2, value2);
125     assertSame(value, kv.getValue(key));
126     assertSame(value2, kv.getValue(key2));
127     Comparable JavaDoc key3 = new Double JavaDoc( -94.3d);
128     assertNull(kv.getValue(key3));
129   }
130
131   /**
132    * @throws Exception
133    *
134    * @author Klaus Meffert
135    * @since 3.0
136    */

137   public void testGetKey_0()
138       throws Exception JavaDoc {
139     KeyedValues kv = new KeyedValues();
140     try {
141       kv.getKey(0);
142       fail();
143     } catch (IndexOutOfBoundsException JavaDoc iex) {
144       ; //this is OK
145
}
146   }
147
148   /**
149    * @throws Exception
150    *
151    * @author Klaus Meffert
152    * @since 3.0
153    */

154   public void testGetKey_1()
155       throws Exception JavaDoc {
156     KeyedValues kv = new KeyedValues();
157     Number JavaDoc value = new Double JavaDoc(2.3d);
158     kv.setValue(null, value);
159     assertNull(kv.getKey(0));
160   }
161
162   /**
163    * @throws Exception
164    *
165    * @author Klaus Meffert
166    * @since 3.1
167    */

168   public void testGetKey_2()
169       throws Exception JavaDoc {
170     KeyedValues kv = new KeyedValues();
171     kv.setValue(null, null);
172     assertNull(kv.getKey(0));
173   }
174
175   /**
176    * @throws Exception
177    *
178    * @author Klaus Meffert
179    * @since 3.0
180    */

181   public void testGetValue_1()
182       throws Exception JavaDoc {
183     KeyedValues kv = new KeyedValues();
184     try {
185       kv.getValue(0);
186       fail();
187     } catch (IndexOutOfBoundsException JavaDoc iex) {
188       ; //this is OK
189
}
190   }
191
192   /**
193    * @throws Exception
194    *
195    * @author Klaus Meffert
196    * @since 3.0
197    */

198   public void testGetValue_2()
199       throws Exception JavaDoc {
200     KeyedValues kv = new KeyedValues();
201     Comparable JavaDoc key = new Double JavaDoc(2.3d);
202     kv.setValue(key, null);
203     assertNull(kv.getValue(key));
204   }
205
206   /**
207    * @throws Exception
208    *
209    * @author Klaus Meffert
210    * @since 3.1
211    */

212   public void testGetValue_3()
213       throws Exception JavaDoc {
214     KeyedValues kv = new KeyedValues();
215     Comparable JavaDoc key = new Double JavaDoc(2.3d);
216     assertNull(kv.getValue(key));
217   }
218
219   /**
220    * @throws Exception
221    *
222    * @author Klaus Meffert
223    * @since 3.0
224    */

225   public void testClone_0()
226       throws Exception JavaDoc {
227     KeyedValues kv = new KeyedValues();
228     Comparable JavaDoc key = new Double JavaDoc(2.3d);
229     Number JavaDoc value = new Double JavaDoc(4.5d);
230     kv.setValue(key, value);
231     Comparable JavaDoc key2 = new Double JavaDoc(2.4d);
232     Number JavaDoc value2 = new Double JavaDoc(4.6d);
233     kv.setValue(key2, value2);
234     KeyedValues clone = (KeyedValues) kv.clone();
235     assertEquals(clone, kv);
236     assertSame(key, clone.getKey(0));
237     assertSame(value, clone.getValue(0));
238     assertSame(key2, clone.getKey(1));
239     assertSame(value2, clone.getValue(1));
240   }
241
242   /**
243    * @throws Exception
244    *
245    * @author Klaus Meffert
246    * @since 3.0
247    */

248   public void testEquals_0()
249       throws Exception JavaDoc {
250     KeyedValues kv = new KeyedValues();
251     assertFalse(kv.equals(null));
252     assertTrue(kv.equals(kv));
253     assertFalse(kv.equals(new Vector()));
254     Comparable JavaDoc key = new Double JavaDoc(2.3d);
255     Number JavaDoc value = new Double JavaDoc(4.5d);
256     kv.setValue(key, value);
257   }
258
259   /**
260    * @throws Exception
261    *
262    * @author Klaus Meffert
263    * @since 3.0
264    */

265   public void testEquals_1()
266       throws Exception JavaDoc {
267     KeyedValues kv = new KeyedValues();
268     Comparable JavaDoc key = new Double JavaDoc(2.3d);
269     Number JavaDoc value = new Double JavaDoc(4.5d);
270     kv.setValue(key, value);
271     KeyedValues kv2 = new KeyedValues();
272     assertFalse(kv.equals(kv2));
273     assertFalse(kv2.equals(kv));
274   }
275
276   /**
277    * @throws Exception
278    *
279    * @author Klaus Meffert
280    * @since 3.0
281    */

282   public void testHashcode_0()
283       throws Exception JavaDoc {
284     KeyedValues kv = new KeyedValues();
285     Comparable JavaDoc key = new Double JavaDoc(2.3d);
286     Number JavaDoc value = new Double JavaDoc(4.5d);
287     kv.setValue(key, value);
288     Object JavaDoc data = privateAccessor.getField(kv, "m_data");
289     assertEquals(data.hashCode(), kv.hashCode());
290   }
291
292   /**
293    * @throws Exception
294    *
295    * @author Klaus Meffert
296    * @since 3.0
297    */

298   public void testGetIndex_0()
299       throws Exception JavaDoc {
300     KeyedValues kv = new KeyedValues();
301     Comparable JavaDoc key = null;
302     Number JavaDoc value = new Double JavaDoc(4.5d);
303     kv.setValue(key, value);
304     assertEquals(0, kv.getIndex(null));
305   }
306
307   /**
308    * @throws Exception
309    *
310    * @author Klaus Meffert
311    * @since 3.0
312    */

313   public void testGetIndex_1()
314       throws Exception JavaDoc {
315     KeyedValues kv = new KeyedValues();
316     Comparable JavaDoc key = new Double JavaDoc(23.11d);
317     Number JavaDoc value = new Double JavaDoc(4.5d);
318     kv.setValue(key, value);
319     assertEquals( -1, kv.getIndex(null));
320   }
321
322   /**
323    * @throws Exception
324    *
325    * @author Klaus Meffert
326    * @since 3.0
327    */

328   public void testGetIndex_2()
329       throws Exception JavaDoc {
330     KeyedValues kv = new KeyedValues();
331     Number JavaDoc value = new Double JavaDoc(4.5d);
332     kv.setValue(null, value);
333     assertEquals( -1, kv.getIndex(new Double JavaDoc(23.11d)));
334   }
335 }
336
Popular Tags