OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // A subtest of the larger MapTest. Will eliminate once the full | |
5 // test is running. | |
6 | |
7 class MapTest { | |
8 | |
9 static void testDeletedElement(Map map) { | |
10 map.clear(); | |
11 for (int i = 0; i < 100; i++) { | |
12 map[1] = 2; | |
13 Expect.equals(1, map.length); | |
14 int x = map.remove(1); | |
15 Expect.equals(2, x); | |
16 Expect.equals(0, map.length); | |
17 } | |
18 Expect.equals(0, map.length); | |
19 for (int i = 0; i < 100; i++) { | |
20 map[i] = 2; | |
21 Expect.equals(1, map.length); | |
22 int x = map.remove(105); | |
23 Expect.equals(null, x); | |
24 Expect.equals(1, map.length); | |
25 x = map.remove(i); | |
26 Expect.equals(2, x); | |
27 Expect.equals(0, map.length); | |
28 } | |
29 Expect.equals(0, map.length); | |
30 map.remove(105); | |
31 } | |
32 | |
33 static void test(Map map) { | |
34 testDeletedElement(map); | |
35 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); | |
36 map.clear(); | |
37 testMap(map, "value1", "value2", "value3", "value4", "value5", | |
38 "value6", "value7", "value8"); | |
39 } | |
40 | |
41 static void testMap(Map map, key1, key2, key3, key4, key5, key6, key7, key8) { | |
42 int value1 = 10; | |
43 int value2 = 20; | |
44 int value3 = 30; | |
45 int value4 = 40; | |
46 int value5 = 50; | |
47 int value6 = 60; | |
48 int value7 = 70; | |
49 int value8 = 80; | |
50 | |
51 Expect.equals(0, map.length); | |
52 | |
53 map[key1] = value1; | |
54 Expect.equals(value1, map[key1]); | |
55 map[key1] = value2; | |
56 Expect.equals(false, map.containsKey(key2)); | |
57 Expect.equals(1, map.length); | |
58 | |
59 map[key1] = value1; | |
60 Expect.equals(value1, map[key1]); | |
61 // Add enough entries to make sure the table grows. | |
62 map[key2] = value2; | |
63 Expect.equals(value2, map[key2]); | |
64 Expect.equals(2, map.length); | |
65 map[key3] = value3; | |
66 Expect.equals(value2, map[key2]); | |
67 Expect.equals(value3, map[key3]); | |
68 map[key4] = value4; | |
69 Expect.equals(value3, map[key3]); | |
70 Expect.equals(value4, map[key4]); | |
71 map[key5] = value5; | |
72 Expect.equals(value4, map[key4]); | |
73 Expect.equals(value5, map[key5]); | |
74 map[key6] = value6; | |
75 Expect.equals(value5, map[key5]); | |
76 Expect.equals(value6, map[key6]); | |
77 map[key7] = value7; | |
78 Expect.equals(value6, map[key6]); | |
79 Expect.equals(value7, map[key7]); | |
80 map[key8] = value8; | |
81 Expect.equals(value1, map[key1]); | |
82 Expect.equals(value2, map[key2]); | |
83 Expect.equals(value3, map[key3]); | |
84 Expect.equals(value4, map[key4]); | |
85 Expect.equals(value5, map[key5]); | |
86 Expect.equals(value6, map[key6]); | |
87 Expect.equals(value7, map[key7]); | |
88 Expect.equals(value8, map[key8]); | |
89 Expect.equals(8, map.length); | |
90 | |
91 map.remove(key4); | |
92 Expect.equals(false, map.containsKey(key4)); | |
93 Expect.equals(7, map.length); | |
94 | |
95 // Test clearing the table. | |
96 map.clear(); | |
97 Expect.equals(0, map.length); | |
98 Expect.equals(false, map.containsKey(key1)); | |
99 Expect.equals(false, map.containsKey(key2)); | |
100 Expect.equals(false, map.containsKey(key3)); | |
101 Expect.equals(false, map.containsKey(key4)); | |
102 Expect.equals(false, map.containsKey(key5)); | |
103 Expect.equals(false, map.containsKey(key6)); | |
104 Expect.equals(false, map.containsKey(key7)); | |
105 Expect.equals(false, map.containsKey(key8)); | |
106 | |
107 // Test adding and removing again. | |
108 map[key1] = value1; | |
109 Expect.equals(value1, map[key1]); | |
110 Expect.equals(1, map.length); | |
111 map[key2] = value2; | |
112 Expect.equals(value2, map[key2]); | |
113 Expect.equals(2, map.length); | |
114 map[key3] = value3; | |
115 Expect.equals(value3, map[key3]); | |
116 map.remove(key3); | |
117 Expect.equals(2, map.length); | |
118 map[key4] = value4; | |
119 Expect.equals(value4, map[key4]); | |
120 map.remove(key4); | |
121 Expect.equals(2, map.length); | |
122 map[key5] = value5; | |
123 Expect.equals(value5, map[key5]); | |
124 map.remove(key5); | |
125 Expect.equals(2, map.length); | |
126 map[key6] = value6; | |
127 Expect.equals(value6, map[key6]); | |
128 map.remove(key6); | |
129 Expect.equals(2, map.length); | |
130 map[key7] = value7; | |
131 Expect.equals(value7, map[key7]); | |
132 map.remove(key7); | |
133 Expect.equals(2, map.length); | |
134 map[key8] = value8; | |
135 Expect.equals(value8, map[key8]); | |
136 map.remove(key8); | |
137 Expect.equals(2, map.length); | |
138 | |
139 Expect.equals(true, map.containsKey(key1)); | |
140 Expect.equals(true, map.containsValue(value1)); | |
141 | |
142 // Test Map.forEach. | |
143 Map other_map = new Map(); | |
144 void testForEachMap(key, value) { | |
145 other_map[key] = value; | |
146 } | |
147 map.forEach(testForEachMap); | |
148 Expect.equals(true, other_map.containsKey(key1)); | |
149 Expect.equals(true, other_map.containsKey(key2)); | |
150 Expect.equals(true, other_map.containsValue(value1)); | |
151 Expect.equals(true, other_map.containsValue(value2)); | |
152 Expect.equals(2, other_map.length); | |
153 | |
154 other_map.clear(); | |
155 Expect.equals(0, other_map.length); | |
156 | |
157 // Test Collection.getKeys. | |
158 void testForEachCollection(value) { | |
159 other_map[value] = value; | |
160 } | |
161 Collection keys = map.getKeys(); | |
162 keys.forEach(testForEachCollection); | |
163 Expect.equals(true, other_map.containsKey(key1)); | |
164 Expect.equals(true, other_map.containsKey(key2)); | |
165 Expect.equals(true, other_map.containsValue(key1)); | |
166 Expect.equals(true, other_map.containsValue(key2)); | |
167 Expect.equals(true, !other_map.containsKey(value1)); | |
168 Expect.equals(true, !other_map.containsKey(value2)); | |
169 Expect.equals(true, !other_map.containsValue(value1)); | |
170 Expect.equals(true, !other_map.containsValue(value2)); | |
171 Expect.equals(2, other_map.length); | |
172 other_map.clear(); | |
173 Expect.equals(0, other_map.length); | |
174 | |
175 // Test Collection.getValues. | |
176 Collection values = map.getValues(); | |
177 values.forEach(testForEachCollection); | |
178 Expect.equals(true, !other_map.containsKey(key1)); | |
179 Expect.equals(true, !other_map.containsKey(key2)); | |
180 Expect.equals(true, !other_map.containsValue(key1)); | |
181 Expect.equals(true, !other_map.containsValue(key2)); | |
182 Expect.equals(true, other_map.containsKey(value1)); | |
183 Expect.equals(true, other_map.containsKey(value2)); | |
184 Expect.equals(true, other_map.containsValue(value1)); | |
185 Expect.equals(true, other_map.containsValue(value2)); | |
186 Expect.equals(2, other_map.length); | |
187 other_map.clear(); | |
188 Expect.equals(0, other_map.length); | |
189 | |
190 // Test Map.putIfAbsent. | |
191 map.clear(); | |
192 Expect.equals(false, map.containsKey(key1)); | |
193 map.putIfAbsent(key1, () => 10); | |
194 Expect.equals(true, map.containsKey(key1)); | |
195 Expect.equals(10, map[key1]); | |
196 Expect.equals(10, | |
197 map.putIfAbsent(key1, () => 11)); | |
198 } | |
199 | |
200 static testKeys(Map map) { | |
201 map[1] = 101; | |
202 map[2] = 102; | |
203 Collection k = map.getKeys(); | |
204 Expect.equals(2, k.length); | |
205 Collection v = map.getValues(); | |
206 Expect.equals(2, v.length); | |
207 Expect.equals(true, map.containsValue(101)); | |
208 Expect.equals(true, map.containsValue(102)); | |
209 Expect.equals(false, map.containsValue(103)); | |
210 } | |
211 | |
212 static testMain() { | |
213 test(new Map()); | |
214 testKeys(new Map()); | |
215 } | |
216 } | |
217 | |
218 | |
219 main() { | |
220 MapTest.testMain(); | |
221 } | |
OLD | NEW |