KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > sosnoski > util > hashmap > IntStringHashMapTest


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 package tests.sosnoski.util.hashmap;
24
25 import java.util.Iterator JavaDoc;
26
27 import com.sosnoski.util.hashmap.IntStringHashMap;
28
29 import junit.framework.*;
30
31 public class IntStringHashMapTest extends TestCase
32 {
33     private static final int TEST_ITEMS = 80;
34
35     private IntStringHashMap m_hashmap;
36
37     public IntStringHashMapTest(String JavaDoc name) {
38         super(name);
39     }
40
41     public void setUp() {
42         m_hashmap = new IntStringHashMap();
43     }
44
45     public void tearDown() {
46         m_hashmap.clear();
47     }
48
49     private String JavaDoc gen(int index) {
50         return Integer.toString(index);
51     }
52
53     private boolean ifMatch(String JavaDoc a, int b) {
54         return gen(b).equals(a);
55     }
56
57     public void fillSequential(int count) {
58         for (int i = 0; i < count; i++) {
59             m_hashmap.add(i, gen(i));
60         }
61     }
62
63     public void testAdd() {
64         fillSequential(TEST_ITEMS);
65         assert(m_hashmap.size() == TEST_ITEMS);
66         for (int i = 0; i < TEST_ITEMS; i++) {
67             assert(ifMatch(m_hashmap.get(i), i));
68         }
69         assert(m_hashmap.add(-1, gen(-1)) == null);
70     }
71
72     public void testClear() {
73         fillSequential(TEST_ITEMS);
74         m_hashmap.clear();
75         assert(m_hashmap.size() == 0);
76     }
77
78     public void testContains() {
79         fillSequential(TEST_ITEMS);
80         assert(m_hashmap.size() == TEST_ITEMS);
81         assert(!m_hashmap.containsKey(-1));
82         for (int i = 0; i < TEST_ITEMS; i++) {
83             assert(m_hashmap.containsKey(i));
84         }
85         for (int i = TEST_ITEMS; i < TEST_ITEMS*20; i++) {
86             assert(!m_hashmap.containsKey(i));
87         }
88     }
89
90     public void testRemove() {
91         fillSequential(TEST_ITEMS);
92         assert(m_hashmap.remove(TEST_ITEMS/2).equals(gen(TEST_ITEMS/2)));
93         assert(m_hashmap.size() == TEST_ITEMS-1);
94         for (int i = 0; i < TEST_ITEMS/2; i++) {
95             assert(ifMatch(m_hashmap.get(i), i));
96         }
97         assert(!m_hashmap.containsKey(TEST_ITEMS/2));
98         for (int i = TEST_ITEMS/2+1; i < TEST_ITEMS; i++) {
99             assert(ifMatch(m_hashmap.get(i), i));
100         }
101         assert(m_hashmap.remove(-1) == null);
102         assert(m_hashmap.size() == TEST_ITEMS-1);
103     }
104
105     public void testReplace() {
106         fillSequential(TEST_ITEMS);
107         for (int i = 0; i < TEST_ITEMS; i++) {
108             assert(ifMatch(m_hashmap.add(i, gen(TEST_ITEMS-i)), i));
109         }
110         assert(m_hashmap.size() == TEST_ITEMS);
111         for (int i = 0; i < TEST_ITEMS; i++) {
112             assert(ifMatch(m_hashmap.get(i), TEST_ITEMS-i));
113         }
114     }
115
116     public void testValueIterator() {
117         fillSequential(TEST_ITEMS);
118         Iterator JavaDoc iter = m_hashmap.valueIterator();
119         int count = 0;
120         while (iter.hasNext()) {
121             assert(m_hashmap.containsKey(Integer.parseInt((String JavaDoc)iter.next())));
122             count++;
123         }
124         assert(count == TEST_ITEMS);
125     }
126
127     public void testClone() {
128         fillSequential(TEST_ITEMS);
129         IntStringHashMap clone = (IntStringHashMap)m_hashmap.clone();
130         assert(clone.size() == TEST_ITEMS);
131         for (int i = 0; i < TEST_ITEMS; i++) {
132             assert(ifMatch(m_hashmap.get(i), i));
133         }
134     }
135
136     public static Test suite() {
137         return new TestSuite(IntStringHashMapTest.class);
138     }
139
140     public static void main(String JavaDoc[] args) {
141         String JavaDoc[] names = { IntStringHashMapTest.class.getName() };
142         junit.textui.TestRunner.main(names);
143     }
144 }
145
Popular Tags