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 "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 void main() { | 9 void main() { |
10 test(new HashMap()); | 10 test(new HashMap()); |
(...skipping 29 matching lines...) Expand all Loading... |
40 map.clear(); | 40 map.clear(); |
41 testMap(map, "value1", "value2", "value3", "value4", "value5", | 41 testMap(map, "value1", "value2", "value3", "value4", "value5", |
42 "value6", "value7", "value8"); | 42 "value6", "value7", "value8"); |
43 } | 43 } |
44 | 44 |
45 void testLinkedHashMap() { | 45 void testLinkedHashMap() { |
46 LinkedHashMap map = new LinkedHashMap(); | 46 LinkedHashMap map = new LinkedHashMap(); |
47 Expect.equals(false, map.containsKey(1)); | 47 Expect.equals(false, map.containsKey(1)); |
48 map[1] = 1; | 48 map[1] = 1; |
49 map[1] = 2; | 49 map[1] = 2; |
50 Expect.equals(1, map.length); | 50 testLength(1, map); |
51 } | 51 } |
52 | 52 |
53 void testMap(Map map, key1, key2, key3, key4, key5, key6, key7, key8) { | 53 void testMap(Map map, key1, key2, key3, key4, key5, key6, key7, key8) { |
54 int value1 = 10; | 54 int value1 = 10; |
55 int value2 = 20; | 55 int value2 = 20; |
56 int value3 = 30; | 56 int value3 = 30; |
57 int value4 = 40; | 57 int value4 = 40; |
58 int value5 = 50; | 58 int value5 = 50; |
59 int value6 = 60; | 59 int value6 = 60; |
60 int value7 = 70; | 60 int value7 = 70; |
61 int value8 = 80; | 61 int value8 = 80; |
62 | 62 |
63 Expect.equals(0, map.length); | 63 testLength(0, map); |
64 | 64 |
65 map[key1] = value1; | 65 map[key1] = value1; |
66 Expect.equals(value1, map[key1]); | 66 Expect.equals(value1, map[key1]); |
67 map[key1] = value2; | 67 map[key1] = value2; |
68 Expect.equals(false, map.containsKey(key2)); | 68 Expect.equals(false, map.containsKey(key2)); |
69 Expect.equals(1, map.length); | 69 testLength(1, map); |
70 | 70 |
71 map[key1] = value1; | 71 map[key1] = value1; |
72 Expect.equals(value1, map[key1]); | 72 Expect.equals(value1, map[key1]); |
73 // Add enough entries to make sure the table grows. | 73 // Add enough entries to make sure the table grows. |
74 map[key2] = value2; | 74 map[key2] = value2; |
75 Expect.equals(value2, map[key2]); | 75 Expect.equals(value2, map[key2]); |
76 Expect.equals(2, map.length); | 76 testLength(2, map); |
77 map[key3] = value3; | 77 map[key3] = value3; |
78 Expect.equals(value2, map[key2]); | 78 Expect.equals(value2, map[key2]); |
79 Expect.equals(value3, map[key3]); | 79 Expect.equals(value3, map[key3]); |
80 map[key4] = value4; | 80 map[key4] = value4; |
81 Expect.equals(value3, map[key3]); | 81 Expect.equals(value3, map[key3]); |
82 Expect.equals(value4, map[key4]); | 82 Expect.equals(value4, map[key4]); |
83 map[key5] = value5; | 83 map[key5] = value5; |
84 Expect.equals(value4, map[key4]); | 84 Expect.equals(value4, map[key4]); |
85 Expect.equals(value5, map[key5]); | 85 Expect.equals(value5, map[key5]); |
86 map[key6] = value6; | 86 map[key6] = value6; |
87 Expect.equals(value5, map[key5]); | 87 Expect.equals(value5, map[key5]); |
88 Expect.equals(value6, map[key6]); | 88 Expect.equals(value6, map[key6]); |
89 map[key7] = value7; | 89 map[key7] = value7; |
90 Expect.equals(value6, map[key6]); | 90 Expect.equals(value6, map[key6]); |
91 Expect.equals(value7, map[key7]); | 91 Expect.equals(value7, map[key7]); |
92 map[key8] = value8; | 92 map[key8] = value8; |
93 Expect.equals(value1, map[key1]); | 93 Expect.equals(value1, map[key1]); |
94 Expect.equals(value2, map[key2]); | 94 Expect.equals(value2, map[key2]); |
95 Expect.equals(value3, map[key3]); | 95 Expect.equals(value3, map[key3]); |
96 Expect.equals(value4, map[key4]); | 96 Expect.equals(value4, map[key4]); |
97 Expect.equals(value5, map[key5]); | 97 Expect.equals(value5, map[key5]); |
98 Expect.equals(value6, map[key6]); | 98 Expect.equals(value6, map[key6]); |
99 Expect.equals(value7, map[key7]); | 99 Expect.equals(value7, map[key7]); |
100 Expect.equals(value8, map[key8]); | 100 Expect.equals(value8, map[key8]); |
101 Expect.equals(8, map.length); | 101 testLength(8, map); |
102 | 102 |
103 map.remove(key4); | 103 map.remove(key4); |
104 Expect.equals(false, map.containsKey(key4)); | 104 Expect.equals(false, map.containsKey(key4)); |
105 Expect.equals(7, map.length); | 105 testLength(7, map); |
106 | 106 |
107 // Test clearing the table. | 107 // Test clearing the table. |
108 map.clear(); | 108 map.clear(); |
109 Expect.equals(0, map.length); | 109 testLength(0, map); |
110 Expect.equals(false, map.containsKey(key1)); | 110 Expect.equals(false, map.containsKey(key1)); |
111 Expect.equals(false, map.containsKey(key2)); | 111 Expect.equals(false, map.containsKey(key2)); |
112 Expect.equals(false, map.containsKey(key3)); | 112 Expect.equals(false, map.containsKey(key3)); |
113 Expect.equals(false, map.containsKey(key4)); | 113 Expect.equals(false, map.containsKey(key4)); |
114 Expect.equals(false, map.containsKey(key5)); | 114 Expect.equals(false, map.containsKey(key5)); |
115 Expect.equals(false, map.containsKey(key6)); | 115 Expect.equals(false, map.containsKey(key6)); |
116 Expect.equals(false, map.containsKey(key7)); | 116 Expect.equals(false, map.containsKey(key7)); |
117 Expect.equals(false, map.containsKey(key8)); | 117 Expect.equals(false, map.containsKey(key8)); |
118 | 118 |
119 // Test adding and removing again. | 119 // Test adding and removing again. |
120 map[key1] = value1; | 120 map[key1] = value1; |
121 Expect.equals(value1, map[key1]); | 121 Expect.equals(value1, map[key1]); |
122 Expect.equals(1, map.length); | 122 testLength(1, map); |
123 map[key2] = value2; | 123 map[key2] = value2; |
124 Expect.equals(value2, map[key2]); | 124 Expect.equals(value2, map[key2]); |
125 Expect.equals(2, map.length); | 125 testLength(2, map); |
126 map[key3] = value3; | 126 map[key3] = value3; |
127 Expect.equals(value3, map[key3]); | 127 Expect.equals(value3, map[key3]); |
128 map.remove(key3); | 128 map.remove(key3); |
129 Expect.equals(2, map.length); | 129 testLength(2, map); |
130 map[key4] = value4; | 130 map[key4] = value4; |
131 Expect.equals(value4, map[key4]); | 131 Expect.equals(value4, map[key4]); |
132 map.remove(key4); | 132 map.remove(key4); |
133 Expect.equals(2, map.length); | 133 testLength(2, map); |
134 map[key5] = value5; | 134 map[key5] = value5; |
135 Expect.equals(value5, map[key5]); | 135 Expect.equals(value5, map[key5]); |
136 map.remove(key5); | 136 map.remove(key5); |
137 Expect.equals(2, map.length); | 137 testLength(2, map); |
138 map[key6] = value6; | 138 map[key6] = value6; |
139 Expect.equals(value6, map[key6]); | 139 Expect.equals(value6, map[key6]); |
140 map.remove(key6); | 140 map.remove(key6); |
141 Expect.equals(2, map.length); | 141 testLength(2, map); |
142 map[key7] = value7; | 142 map[key7] = value7; |
143 Expect.equals(value7, map[key7]); | 143 Expect.equals(value7, map[key7]); |
144 map.remove(key7); | 144 map.remove(key7); |
145 Expect.equals(2, map.length); | 145 testLength(2, map); |
146 map[key8] = value8; | 146 map[key8] = value8; |
147 Expect.equals(value8, map[key8]); | 147 Expect.equals(value8, map[key8]); |
148 map.remove(key8); | 148 map.remove(key8); |
149 Expect.equals(2, map.length); | 149 testLength(2, map); |
150 | 150 |
151 Expect.equals(true, map.containsKey(key1)); | 151 Expect.equals(true, map.containsKey(key1)); |
152 Expect.equals(true, map.containsValue(value1)); | 152 Expect.equals(true, map.containsValue(value1)); |
153 | 153 |
154 // Test Map.forEach. | 154 // Test Map.forEach. |
155 Map otherMap = new Map(); | 155 Map otherMap = new Map(); |
156 void testForEachMap(key, value) { | 156 void testForEachMap(key, value) { |
157 otherMap[key] = value; | 157 otherMap[key] = value; |
158 } | 158 } |
159 map.forEach(testForEachMap); | 159 map.forEach(testForEachMap); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 Expect.equals(true, map.containsKey(key1)); | 206 Expect.equals(true, map.containsKey(key1)); |
207 Expect.equals(10, map[key1]); | 207 Expect.equals(10, map[key1]); |
208 Expect.equals(10, | 208 Expect.equals(10, |
209 map.putIfAbsent(key1, () => 11)); | 209 map.putIfAbsent(key1, () => 11)); |
210 } | 210 } |
211 | 211 |
212 void testDeletedElement(Map map) { | 212 void testDeletedElement(Map map) { |
213 map.clear(); | 213 map.clear(); |
214 for (int i = 0; i < 100; i++) { | 214 for (int i = 0; i < 100; i++) { |
215 map[1] = 2; | 215 map[1] = 2; |
216 Expect.equals(1, map.length); | 216 testLength(1, map); |
217 map.remove(1); | 217 map.remove(1); |
218 Expect.equals(0, map.length); | 218 testLength(0, map); |
219 } | 219 } |
220 Expect.equals(0, map.length); | 220 testLength(0, map); |
221 } | 221 } |
222 | 222 |
223 void testMapLiteral() { | 223 void testMapLiteral() { |
224 Map m = {"a": 1, "b" : 2, "c": 3 }; | 224 Map m = {"a": 1, "b" : 2, "c": 3 }; |
225 Expect.equals(3, m.length); | 225 Expect.equals(3, m.length); |
226 int sum = 0; | 226 int sum = 0; |
227 m.forEach((a, b) { | 227 m.forEach((a, b) { |
228 sum += b; | 228 sum += b; |
229 }); | 229 }); |
230 Expect.equals(6, sum); | 230 Expect.equals(6, sum); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 Expect.isTrue(map.isEmpty); | 340 Expect.isTrue(map.isEmpty); |
341 | 341 |
342 // Test NaN. | 342 // Test NaN. |
343 var nan = double.NAN; | 343 var nan = double.NAN; |
344 Expect.isFalse(map.containsKey(nan)); | 344 Expect.isFalse(map.containsKey(nan)); |
345 Expect.equals(null, map[nan]); | 345 Expect.equals(null, map[nan]); |
346 | 346 |
347 map[nan] = 'value:0'; | 347 map[nan] = 'value:0'; |
348 Expect.isFalse(map.containsKey(nan)); | 348 Expect.isFalse(map.containsKey(nan)); |
349 Expect.equals(null, map[nan]); | 349 Expect.equals(null, map[nan]); |
350 Expect.equals(1, map.length); | 350 testLength(1, map); |
351 | 351 |
352 map[nan] = 'value:1'; | 352 map[nan] = 'value:1'; |
353 Expect.isFalse(map.containsKey(nan)); | 353 Expect.isFalse(map.containsKey(nan)); |
354 Expect.equals(null, map[nan]); | 354 Expect.equals(null, map[nan]); |
355 Expect.equals(2, map.length); | 355 testLength(2, map); |
356 | 356 |
357 Expect.equals(null, map.remove(nan)); | 357 Expect.equals(null, map.remove(nan)); |
358 Expect.equals(2, map.length); | 358 testLength(2, map); |
359 | 359 |
360 var count = 0; | 360 var count = 0; |
361 map.forEach((key, value) { | 361 map.forEach((key, value) { |
362 if (key.isNaN) count++; | 362 if (key.isNaN) count++; |
363 }); | 363 }); |
364 Expect.equals(2, count); | 364 Expect.equals(2, count); |
365 | 365 |
366 map.clear(); | 366 map.clear(); |
367 Expect.isTrue(map.isEmpty); | 367 Expect.isTrue(map.isEmpty); |
368 } | 368 } |
| 369 |
| 370 void testLength(int length, Map map) { |
| 371 Expect.equals(length, map.length); |
| 372 (length == 0 ? Expect.isTrue : Expect.isFalse)(map.isEmpty); |
| 373 (length != 0 ? Expect.isTrue : Expect.isFalse)(map.isNotEmpty); |
| 374 } |
OLD | NEW |