Chromium Code Reviews| 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 testWeirdKeys(new Map()); | |
| 19 testWeirdKeys(new Map<String, String>()); | |
| 20 testWeirdKeys(new HashMap()); | |
| 21 testWeirdKeys(new HashMap<String, String>()); | |
| 22 testWeirdKeys(new LinkedHashMap()); | |
| 23 testWeirdKeys(new LinkedHashMap<String, String>()); | |
| 24 testWeirdKeys(new SplayTreeMap()); | |
| 25 testWeirdKeys(new SplayTreeMap<String, String>()); | |
| 26 | |
| 27 // BUG(9368): Enable these tests. | |
| 28 if (false) { | |
| 29 Expect.isFalse(new Map<int, num>() is Map<String, num>); | |
| 30 Expect.isFalse(new HashMap<int, num>() is Map<String, num>); | |
| 31 Expect.isFalse(new LinkedHashMap<int, num>() is Map<String, num>); | |
| 32 } | |
| 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 testWeirdKeys(Map map) { | |
| 286 // Test weird keys. | |
| 287 var weirdKeys = const [ | |
| 288 'hasOwnProperty', | |
| 289 'constructor', | |
|
erikcorry
2013/03/22 12:59:24
Please add __proto__, __count__, __parent__, NaN a
| |
| 290 'toLocaleString', | |
| 291 'propertyIsEnumerable', | |
| 292 '__defineGetter__', | |
| 293 '__defineSetter__', | |
| 294 '__lookupGetter__', | |
| 295 '__lookupSetter__', | |
| 296 'isPrototypeOf', | |
| 297 'toString', | |
| 298 'valueOf' ]; | |
| 299 Expect.isTrue(map.isEmpty); | |
| 300 for (var key in weirdKeys) { | |
| 301 Expect.isFalse(map.containsKey(key)); | |
| 302 Expect.equals(null, map[key]); | |
| 303 var value = 'value:$key'; | |
| 304 map[key] = value; | |
| 305 Expect.isTrue(map.containsKey(key)); | |
| 306 Expect.equals(value, map[key]); | |
| 307 Expect.equals(value, map.remove(key)); | |
| 308 Expect.isFalse(map.containsKey(key)); | |
| 309 Expect.equals(null, map[key]); | |
| 310 } | |
| 311 Expect.isTrue(map.isEmpty); | |
| 312 } | |
| OLD | NEW |