OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library map_test; | 5 library map_test; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 | 7 |
8 void main() { | 8 void main() { |
9 test(new HashMap()); | 9 test(new HashMap()); |
10 test(new LinkedHashMap()); | 10 test(new LinkedHashMap()); |
11 test(new SplayTreeMap()); | 11 test(new SplayTreeMap()); |
12 test(new SplayTreeMap(Comparable.compare)); | 12 test(new SplayTreeMap(Comparable.compare)); |
13 testLinkedHashMap(); | 13 testLinkedHashMap(); |
14 testMapLiteral(); | 14 testMapLiteral(); |
15 testNullValue(); | 15 testNullValue(); |
16 testTypes(); | 16 testTypes(); |
| 17 |
| 18 testWeirdStringKeys(new Map()); |
| 19 testWeirdStringKeys(new Map<String, String>()); |
| 20 testWeirdStringKeys(new HashMap()); |
| 21 testWeirdStringKeys(new HashMap<String, String>()); |
| 22 testWeirdStringKeys(new LinkedHashMap()); |
| 23 testWeirdStringKeys(new LinkedHashMap<String, String>()); |
| 24 testWeirdStringKeys(new SplayTreeMap()); |
| 25 testWeirdStringKeys(new SplayTreeMap<String, String>()); |
| 26 |
| 27 testNumericKeys(new Map()); |
| 28 testNumericKeys(new Map<num, String>()); |
| 29 testNumericKeys(new HashMap()); |
| 30 testNumericKeys(new HashMap<num, String>()); |
| 31 testNumericKeys(new LinkedHashMap()); |
| 32 testNumericKeys(new LinkedHashMap<num, String>()); |
17 } | 33 } |
18 | 34 |
| 35 |
19 void test(Map map) { | 36 void test(Map map) { |
20 testDeletedElement(map); | 37 testDeletedElement(map); |
21 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); | 38 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); |
22 map.clear(); | 39 map.clear(); |
23 testMap(map, "value1", "value2", "value3", "value4", "value5", | 40 testMap(map, "value1", "value2", "value3", "value4", "value5", |
24 "value6", "value7", "value8"); | 41 "value6", "value7", "value8"); |
25 } | 42 } |
26 | 43 |
27 void testLinkedHashMap() { | 44 void testLinkedHashMap() { |
28 LinkedHashMap map = new LinkedHashMap(); | 45 LinkedHashMap map = new LinkedHashMap(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 Expect.equals(2, map.length); | 144 Expect.equals(2, map.length); |
128 map[key8] = value8; | 145 map[key8] = value8; |
129 Expect.equals(value8, map[key8]); | 146 Expect.equals(value8, map[key8]); |
130 map.remove(key8); | 147 map.remove(key8); |
131 Expect.equals(2, map.length); | 148 Expect.equals(2, map.length); |
132 | 149 |
133 Expect.equals(true, map.containsKey(key1)); | 150 Expect.equals(true, map.containsKey(key1)); |
134 Expect.equals(true, map.containsValue(value1)); | 151 Expect.equals(true, map.containsValue(value1)); |
135 | 152 |
136 // Test Map.forEach. | 153 // Test Map.forEach. |
137 Map other_map = new Map(); | 154 Map otherMap = new Map(); |
138 void testForEachMap(key, value) { | 155 void testForEachMap(key, value) { |
139 other_map[key] = value; | 156 otherMap[key] = value; |
140 } | 157 } |
141 map.forEach(testForEachMap); | 158 map.forEach(testForEachMap); |
142 Expect.equals(true, other_map.containsKey(key1)); | 159 Expect.equals(true, otherMap.containsKey(key1)); |
143 Expect.equals(true, other_map.containsKey(key2)); | 160 Expect.equals(true, otherMap.containsKey(key2)); |
144 Expect.equals(true, other_map.containsValue(value1)); | 161 Expect.equals(true, otherMap.containsValue(value1)); |
145 Expect.equals(true, other_map.containsValue(value2)); | 162 Expect.equals(true, otherMap.containsValue(value2)); |
146 Expect.equals(2, other_map.length); | 163 Expect.equals(2, otherMap.length); |
147 | 164 |
148 other_map.clear(); | 165 otherMap.clear(); |
149 Expect.equals(0, other_map.length); | 166 Expect.equals(0, otherMap.length); |
150 | 167 |
151 // Test Collection.keys. | 168 // Test Collection.keys. |
152 void testForEachCollection(value) { | 169 void testForEachCollection(value) { |
153 other_map[value] = value; | 170 otherMap[value] = value; |
154 } | 171 } |
155 Iterable keys = map.keys; | 172 Iterable keys = map.keys; |
156 keys.forEach(testForEachCollection); | 173 keys.forEach(testForEachCollection); |
157 Expect.equals(true, other_map.containsKey(key1)); | 174 Expect.equals(true, otherMap.containsKey(key1)); |
158 Expect.equals(true, other_map.containsKey(key2)); | 175 Expect.equals(true, otherMap.containsKey(key2)); |
159 Expect.equals(true, other_map.containsValue(key1)); | 176 Expect.equals(true, otherMap.containsValue(key1)); |
160 Expect.equals(true, other_map.containsValue(key2)); | 177 Expect.equals(true, otherMap.containsValue(key2)); |
161 Expect.equals(true, !other_map.containsKey(value1)); | 178 Expect.equals(true, !otherMap.containsKey(value1)); |
162 Expect.equals(true, !other_map.containsKey(value2)); | 179 Expect.equals(true, !otherMap.containsKey(value2)); |
163 Expect.equals(true, !other_map.containsValue(value1)); | 180 Expect.equals(true, !otherMap.containsValue(value1)); |
164 Expect.equals(true, !other_map.containsValue(value2)); | 181 Expect.equals(true, !otherMap.containsValue(value2)); |
165 Expect.equals(2, other_map.length); | 182 Expect.equals(2, otherMap.length); |
166 other_map.clear(); | 183 otherMap.clear(); |
167 Expect.equals(0, other_map.length); | 184 Expect.equals(0, otherMap.length); |
168 | 185 |
169 // Test Collection.values. | 186 // Test Collection.values. |
170 Iterable values = map.values; | 187 Iterable values = map.values; |
171 values.forEach(testForEachCollection); | 188 values.forEach(testForEachCollection); |
172 Expect.equals(true, !other_map.containsKey(key1)); | 189 Expect.equals(true, !otherMap.containsKey(key1)); |
173 Expect.equals(true, !other_map.containsKey(key2)); | 190 Expect.equals(true, !otherMap.containsKey(key2)); |
174 Expect.equals(true, !other_map.containsValue(key1)); | 191 Expect.equals(true, !otherMap.containsValue(key1)); |
175 Expect.equals(true, !other_map.containsValue(key2)); | 192 Expect.equals(true, !otherMap.containsValue(key2)); |
176 Expect.equals(true, other_map.containsKey(value1)); | 193 Expect.equals(true, otherMap.containsKey(value1)); |
177 Expect.equals(true, other_map.containsKey(value2)); | 194 Expect.equals(true, otherMap.containsKey(value2)); |
178 Expect.equals(true, other_map.containsValue(value1)); | 195 Expect.equals(true, otherMap.containsValue(value1)); |
179 Expect.equals(true, other_map.containsValue(value2)); | 196 Expect.equals(true, otherMap.containsValue(value2)); |
180 Expect.equals(2, other_map.length); | 197 Expect.equals(2, otherMap.length); |
181 other_map.clear(); | 198 otherMap.clear(); |
182 Expect.equals(0, other_map.length); | 199 Expect.equals(0, otherMap.length); |
183 | 200 |
184 // Test Map.putIfAbsent. | 201 // Test Map.putIfAbsent. |
185 map.clear(); | 202 map.clear(); |
186 Expect.equals(false, map.containsKey(key1)); | 203 Expect.equals(false, map.containsKey(key1)); |
187 map.putIfAbsent(key1, () => 10); | 204 map.putIfAbsent(key1, () => 10); |
188 Expect.equals(true, map.containsKey(key1)); | 205 Expect.equals(true, map.containsKey(key1)); |
189 Expect.equals(10, map[key1]); | 206 Expect.equals(10, map[key1]); |
190 Expect.equals(10, | 207 Expect.equals(10, |
191 map.putIfAbsent(key1, () => 11)); | 208 map.putIfAbsent(key1, () => 11)); |
192 } | 209 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 Expect.equals(true, m.containsKey("c")); | 255 Expect.equals(true, m.containsKey("c")); |
239 Expect.equals(3, m.length); | 256 Expect.equals(3, m.length); |
240 | 257 |
241 m.remove("a"); | 258 m.remove("a"); |
242 Expect.equals(2, m.length); | 259 Expect.equals(2, m.length); |
243 Expect.equals(null, m["a"]); | 260 Expect.equals(null, m["a"]); |
244 Expect.equals(false, m.containsKey("a")); | 261 Expect.equals(false, m.containsKey("a")); |
245 } | 262 } |
246 | 263 |
247 void testTypes() { | 264 void testTypes() { |
248 Map<int> map; | 265 Map<int, dynamic> map; |
249 testMap(Map map) { | 266 testMap(Map map) { |
250 map[42] = "text"; | 267 map[42] = "text"; |
251 map[43] = "text"; | 268 map[43] = "text"; |
252 map[42] = "text"; | 269 map[42] = "text"; |
253 map.remove(42); | 270 map.remove(42); |
254 map[42] = "text"; | 271 map[42] = "text"; |
255 } | 272 } |
256 testMap(new HashMap<int, String>()); | 273 testMap(new HashMap<int, String>()); |
257 testMap(new LinkedHashMap<int, String>()); | 274 testMap(new LinkedHashMap<int, String>()); |
258 testMap(new SplayTreeMap<int, String>()); | 275 testMap(new SplayTreeMap<int, String>()); |
259 testMap(new SplayTreeMap<int, String>(Comparable.compare)); | 276 testMap(new SplayTreeMap<int, String>(Comparable.compare)); |
260 testMap(new SplayTreeMap<int, String>((int a, int b) => a.compareTo(b))); | 277 testMap(new SplayTreeMap<int, String>((int a, int b) => a.compareTo(b))); |
261 testMap(new HashMap<num, String>()); | 278 testMap(new HashMap<num, String>()); |
262 testMap(new LinkedHashMap<num, String>()); | 279 testMap(new LinkedHashMap<num, String>()); |
263 testMap(new SplayTreeMap<num, String>()); | 280 testMap(new SplayTreeMap<num, String>()); |
264 testMap(new SplayTreeMap<num, String>(Comparable.compare)); | 281 testMap(new SplayTreeMap<num, String>(Comparable.compare)); |
265 testMap(new SplayTreeMap<num, String>((num a, num b) => a.compareTo(b))); | 282 testMap(new SplayTreeMap<num, String>((num a, num b) => a.compareTo(b))); |
266 } | 283 } |
| 284 |
| 285 void testWeirdStringKeys(Map map) { |
| 286 // Test weird keys. |
| 287 var weirdKeys = const [ |
| 288 'hasOwnProperty', |
| 289 'constructor', |
| 290 'toLocaleString', |
| 291 'propertyIsEnumerable', |
| 292 '__defineGetter__', |
| 293 '__defineSetter__', |
| 294 '__lookupGetter__', |
| 295 '__lookupSetter__', |
| 296 'isPrototypeOf', |
| 297 'toString', |
| 298 'valueOf', |
| 299 '__proto__', |
| 300 '__count__', |
| 301 '__parent__', |
| 302 '']; |
| 303 Expect.isTrue(map.isEmpty); |
| 304 for (var key in weirdKeys) { |
| 305 Expect.isFalse(map.containsKey(key)); |
| 306 Expect.equals(null, map[key]); |
| 307 var value = 'value:$key'; |
| 308 map[key] = value; |
| 309 Expect.isTrue(map.containsKey(key)); |
| 310 Expect.equals(value, map[key]); |
| 311 Expect.equals(value, map.remove(key)); |
| 312 Expect.isFalse(map.containsKey(key)); |
| 313 Expect.equals(null, map[key]); |
| 314 } |
| 315 Expect.isTrue(map.isEmpty); |
| 316 |
| 317 } |
| 318 |
| 319 void testNumericKeys(Map map) { |
| 320 var numericKeys = const [ |
| 321 double.INFINITY, |
| 322 double.NEGATIVE_INFINITY, |
| 323 0, |
| 324 0.0, |
| 325 -0.0 ]; |
| 326 |
| 327 Expect.isTrue(map.isEmpty); |
| 328 for (var key in numericKeys) { |
| 329 Expect.isFalse(map.containsKey(key)); |
| 330 Expect.equals(null, map[key]); |
| 331 var value = 'value:$key'; |
| 332 map[key] = value; |
| 333 Expect.isTrue(map.containsKey(key)); |
| 334 Expect.equals(value, map[key]); |
| 335 Expect.equals(value, map.remove(key)); |
| 336 Expect.isFalse(map.containsKey(key)); |
| 337 Expect.equals(null, map[key]); |
| 338 } |
| 339 Expect.isTrue(map.isEmpty); |
| 340 |
| 341 // Test NaN. |
| 342 var nan = double.NAN; |
| 343 Expect.isFalse(map.containsKey(nan)); |
| 344 Expect.equals(null, map[nan]); |
| 345 |
| 346 map[nan] = 'value:0'; |
| 347 Expect.isFalse(map.containsKey(nan)); |
| 348 Expect.equals(null, map[nan]); |
| 349 Expect.equals(1, map.length); |
| 350 |
| 351 map[nan] = 'value:1'; |
| 352 Expect.isFalse(map.containsKey(nan)); |
| 353 Expect.equals(null, map[nan]); |
| 354 Expect.equals(2, map.length); |
| 355 |
| 356 Expect.equals(null, map.remove(nan)); |
| 357 Expect.equals(2, map.length); |
| 358 |
| 359 var count = 0; |
| 360 map.forEach((key, value) { |
| 361 if (key.isNaN) count++; |
| 362 }); |
| 363 Expect.equals(2, count); |
| 364 |
| 365 map.clear(); |
| 366 Expect.isTrue(map.isEmpty); |
| 367 } |
OLD | NEW |