1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.statcvs.util;
24
25 import java.util.Iterator;
26 import java.util.Set;
27
28 import junit.framework.TestCase;
29
30 /**
31 * @author Anja Jentzsch
32 * @author Richard Cyganiak
33 * @version $Id: IntegerMapTest.java,v 1.7 2002/08/23 02:03:58 cyganiak Exp $
34 */
35 public class IntegerMapTest extends TestCase {
36
37 private static final double ACCURACY = 0.00001;
38
39 private IntegerMap map;
40
41 /**
42 * Constructor for IntegerMapTest.
43 * @param arg0 input
44 */
45 public IntegerMapTest(final String arg0) {
46 super(arg0);
47 }
48
49 /**
50 * @see junit.framework.TestCase#setUp()
51 */
52 public void setUp() {
53 map = new IntegerMap();
54 }
55
56 /**
57 * Method testCreation.
58 */
59 public void testCreation() {
60 assertEquals(0, map.size());
61 assertEquals(0, map.get("some string"));
62 assertEquals(null, map.getInteger("some string"));
63 assertTrue(!map.iteratorSortedByKey().hasNext());
64 assertTrue(!map.iteratorSortedByValue().hasNext());
65 assertTrue(!map.iteratorSortedByValueReverse().hasNext());
66 assertEquals(0, map.sum());
67 assertEquals(0, map.max());
68 assertTrue("should be NaN", Double.isNaN(map.average()));
69 assertTrue("should be NaN", Double.isNaN(map.getPercent("x")));
70 }
71
72 /**
73 * Method testInsert.
74 */
75 public void testInsert() {
76 map.put("a", 42);
77 assertEquals(1, map.size());
78 assertEquals(0, map.get("some string"));
79 assertEquals(null, map.getInteger("some string"));
80 assertEquals(42, map.get("a"));
81 assertEquals(new Integer(42), map.getInteger("a"));
82 }
83
84 /**
85 * Method testAdd.
86 */
87 public void testAdd() {
88 map.put("a", 1);
89 map.addInt("a", 1);
90 assertEquals(1, map.size());
91 assertEquals(2, map.get("a"));
92 }
93
94 /**
95 * Method testRemove.
96 */
97 public void testRemove() {
98 map.put("a", 1);
99 map.remove("a");
100 assertEquals(0, map.size());
101 assertEquals(0, map.get("a"));
102 assertEquals(0, map.sum());
103 }
104
105 /**
106 * Method testRemoveUnused.
107 */
108 public void testRemoveUnused() {
109 map.remove("a");
110 assertEquals(0, map.size());
111 assertEquals(0, map.get("a"));
112 assertEquals(0, map.sum());
113 }
114
115 /**
116 * Method testMultipleKeys.
117 */
118 public void testMultipleKeys() {
119 map.put("a", 1);
120 map.put("b", 2);
121 map.put("c", 3);
122 map.put("d", 4);
123 map.put("e", 5);
124 map.addInt("b", 55);
125 map.addInt("d", -4);
126 assertEquals(5, map.size());
127 assertEquals(1, map.get("a"));
128 assertEquals(57, map.get("b"));
129 assertEquals(3, map.get("c"));
130 assertEquals(0, map.get("d"));
131 assertEquals(5, map.get("e"));
132 }
133
134 /**
135 * Method testIteratorSortedByKey.
136 */
137 public void testIteratorSortedByKey() {
138 map.put("f", 24);
139 map.put("d", 23);
140 map.put("e", 22);
141 map.put("a", 21);
142 map.put("c", 20);
143 map.put("b", 19);
144 final Iterator keys = map.iteratorSortedByKey();
145 assertTrue("should have next", keys.hasNext());
146 assertEquals("a", keys.next());
147 assertTrue("should have next", keys.hasNext());
148 assertEquals("b", keys.next());
149 assertTrue("should have next", keys.hasNext());
150 assertEquals("c", keys.next());
151 assertTrue("should have next", keys.hasNext());
152 assertEquals("d", keys.next());
153 assertTrue("should have next", keys.hasNext());
154 assertEquals("e", keys.next());
155 assertTrue("should have next", keys.hasNext());
156 assertEquals("f", keys.next());
157 assertTrue("should not have next", !keys.hasNext());
158 }
159
160 /**
161 * Method testIteratorSortedByValue.
162 */
163 public void testIteratorSortedByValue() {
164 map.put("f", 4);
165 map.put("d", 17);
166 map.put("e", -100);
167 map.put("a", 99);
168 map.put("c", 3);
169 map.put("b", 2);
170 final Iterator keys = map.iteratorSortedByValue();
171 assertTrue("should have next", keys.hasNext());
172 assertEquals("e", keys.next());
173 assertTrue("should have next", keys.hasNext());
174 assertEquals("b", keys.next());
175 assertTrue("should have next", keys.hasNext());
176 assertEquals("c", keys.next());
177 assertTrue("should have next", keys.hasNext());
178 assertEquals("f", keys.next());
179 assertTrue("should have next", keys.hasNext());
180 assertEquals("d", keys.next());
181 assertTrue("should have next", keys.hasNext());
182 assertEquals("a", keys.next());
183 assertTrue("should not have next", !keys.hasNext());
184 }
185
186 /**
187 * Method testIteratorSortedByValueReverse.
188 */
189 public void testIteratorSortedByValueReverse() {
190 map.put("f", 4);
191 map.put("d", 17);
192 map.put("e", -100);
193 map.put("a", 99);
194 map.put("c", 3);
195 map.put("b", 2);
196 final Iterator keys = map.iteratorSortedByValueReverse();
197 assertTrue("should have next", keys.hasNext());
198 assertEquals("a", keys.next());
199 assertTrue("should have next", keys.hasNext());
200 assertEquals("d", keys.next());
201 assertTrue("should have next", keys.hasNext());
202 assertEquals("f", keys.next());
203 assertTrue("should have next", keys.hasNext());
204 assertEquals("c", keys.next());
205 assertTrue("should have next", keys.hasNext());
206 assertEquals("b", keys.next());
207 assertTrue("should have next", keys.hasNext());
208 assertEquals("e", keys.next());
209 assertTrue("should not have next", !keys.hasNext());
210 }
211
212 /**
213 * Method testSumAvgMax.
214 */
215 public void testSumAvgMax() {
216 map.put("f", 4);
217 map.put("d", 0);
218 map.put("e", -1);
219 map.put("a", 5);
220 map.put("c", 3);
221 map.put("b", 2);
222 assertEquals(13, map.sum());
223 assertEquals(5, map.max());
224 assertEquals(13.0 / 6.0, map.average(), ACCURACY);
225 }
226
227 /**
228 * Method testPercent.
229 */
230 public void testPercent() {
231 map.put("a", 5);
232 map.put("b", 0);
233 map.put("c", 1);
234 map.put("d", 4);
235 assertEquals(50.0, map.getPercent("a"), ACCURACY);
236 assertEquals(0.0, map.getPercent("b"), ACCURACY);
237 assertEquals(10.0, map.getPercent("c"), ACCURACY);
238 assertEquals(40.0, map.getPercent("d"), ACCURACY);
239 assertEquals(100.0, map.getPercentOfMaximum("a"), ACCURACY);
240 assertEquals(0.0, map.getPercentOfMaximum("b"), ACCURACY);
241 assertEquals(20.0, map.getPercentOfMaximum("c"), ACCURACY);
242 assertEquals(80.0, map.getPercentOfMaximum("d"), ACCURACY);
243 }
244
245 /**
246 * Tests percentages when sum is 0
247 */
248
249 public void testPercentSum0() {
250 map.put("a", 0);
251 assertTrue(Double.isNaN(map.getPercent("a")));
252 }
253
254 /**
255 * Method testKeySetEmpty.
256 */
257 public void testKeySetEmpty() {
258 final Set keys = map.keySet();
259 assertTrue("should be empty", keys.isEmpty());
260 }
261
262 /**
263 * Method testKeySet.
264 */
265 public void testKeySet() {
266 map.put("c", 5);
267 map.put("b", 0);
268 map.put("a", 1);
269 final Set keys = map.keySet();
270 assertEquals(3, keys.size());
271 final Iterator it = keys.iterator();
272 assertTrue(it.next().equals("a"));
273 assertTrue(it.next().equals("b"));
274 assertTrue(it.next().equals("c"));
275 }
276
277 /**
278 * Method testContains.
279 */
280 public void testContains() {
281 map.put("a", 13);
282 map.put("b", 0);
283 assertTrue(map.contains("a"));
284 assertTrue(map.contains("b"));
285 assertTrue(!map.contains("c"));
286 }
287
288 /**
289 * Method testContainsRemoved.
290 */
291 public void testContainsRemoved() {
292 map.put("a", 13);
293 map.remove("a");
294 assertTrue(!map.contains("a"));
295 }
296 }