| 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 class MapTest { | 8 void main() { |
| 9 | 9 test(new HashMap()); |
| 10 static testMain() { | 10 test(new LinkedHashMap()); |
| 11 test(new HashMap()); | 11 test(new SplayTreeMap()); |
| 12 test(new LinkedHashMap()); | 12 test(new SplayTreeMap(Comparable.compare)); |
| 13 test(new SplayTreeMap()); | 13 testLinkedHashMap(); |
| 14 testLinkedHashMap(); | 14 testMapLiteral(); |
| 15 testMapLiteral(); | 15 testNullValue(); |
| 16 testNullValue(); | 16 testTypes(); |
| 17 } | 17 } |
| 18 | 18 |
| 19 static void test(Map map) { | 19 void test(Map map) { |
| 20 testDeletedElement(map); | 20 testDeletedElement(map); |
| 21 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); | 21 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); |
| 22 map.clear(); | 22 map.clear(); |
| 23 testMap(map, "value1", "value2", "value3", "value4", "value5", | 23 testMap(map, "value1", "value2", "value3", "value4", "value5", |
| 24 "value6", "value7", "value8"); | 24 "value6", "value7", "value8"); |
| 25 } | 25 } |
| 26 | 26 |
| 27 static void testLinkedHashMap() { | 27 void testLinkedHashMap() { |
| 28 LinkedHashMap map = new LinkedHashMap(); | 28 LinkedHashMap map = new LinkedHashMap(); |
| 29 Expect.equals(false, map.containsKey(1)); | 29 Expect.equals(false, map.containsKey(1)); |
| 30 map[1] = 1; | 30 map[1] = 1; |
| 31 map[1] = 2; |
| 32 Expect.equals(1, map.length); |
| 33 } |
| 34 |
| 35 void testMap(Map map, key1, key2, key3, key4, key5, key6, key7, key8) { |
| 36 int value1 = 10; |
| 37 int value2 = 20; |
| 38 int value3 = 30; |
| 39 int value4 = 40; |
| 40 int value5 = 50; |
| 41 int value6 = 60; |
| 42 int value7 = 70; |
| 43 int value8 = 80; |
| 44 |
| 45 Expect.equals(0, map.length); |
| 46 |
| 47 map[key1] = value1; |
| 48 Expect.equals(value1, map[key1]); |
| 49 map[key1] = value2; |
| 50 Expect.equals(false, map.containsKey(key2)); |
| 51 Expect.equals(1, map.length); |
| 52 |
| 53 map[key1] = value1; |
| 54 Expect.equals(value1, map[key1]); |
| 55 // Add enough entries to make sure the table grows. |
| 56 map[key2] = value2; |
| 57 Expect.equals(value2, map[key2]); |
| 58 Expect.equals(2, map.length); |
| 59 map[key3] = value3; |
| 60 Expect.equals(value2, map[key2]); |
| 61 Expect.equals(value3, map[key3]); |
| 62 map[key4] = value4; |
| 63 Expect.equals(value3, map[key3]); |
| 64 Expect.equals(value4, map[key4]); |
| 65 map[key5] = value5; |
| 66 Expect.equals(value4, map[key4]); |
| 67 Expect.equals(value5, map[key5]); |
| 68 map[key6] = value6; |
| 69 Expect.equals(value5, map[key5]); |
| 70 Expect.equals(value6, map[key6]); |
| 71 map[key7] = value7; |
| 72 Expect.equals(value6, map[key6]); |
| 73 Expect.equals(value7, map[key7]); |
| 74 map[key8] = value8; |
| 75 Expect.equals(value1, map[key1]); |
| 76 Expect.equals(value2, map[key2]); |
| 77 Expect.equals(value3, map[key3]); |
| 78 Expect.equals(value4, map[key4]); |
| 79 Expect.equals(value5, map[key5]); |
| 80 Expect.equals(value6, map[key6]); |
| 81 Expect.equals(value7, map[key7]); |
| 82 Expect.equals(value8, map[key8]); |
| 83 Expect.equals(8, map.length); |
| 84 |
| 85 map.remove(key4); |
| 86 Expect.equals(false, map.containsKey(key4)); |
| 87 Expect.equals(7, map.length); |
| 88 |
| 89 // Test clearing the table. |
| 90 map.clear(); |
| 91 Expect.equals(0, map.length); |
| 92 Expect.equals(false, map.containsKey(key1)); |
| 93 Expect.equals(false, map.containsKey(key2)); |
| 94 Expect.equals(false, map.containsKey(key3)); |
| 95 Expect.equals(false, map.containsKey(key4)); |
| 96 Expect.equals(false, map.containsKey(key5)); |
| 97 Expect.equals(false, map.containsKey(key6)); |
| 98 Expect.equals(false, map.containsKey(key7)); |
| 99 Expect.equals(false, map.containsKey(key8)); |
| 100 |
| 101 // Test adding and removing again. |
| 102 map[key1] = value1; |
| 103 Expect.equals(value1, map[key1]); |
| 104 Expect.equals(1, map.length); |
| 105 map[key2] = value2; |
| 106 Expect.equals(value2, map[key2]); |
| 107 Expect.equals(2, map.length); |
| 108 map[key3] = value3; |
| 109 Expect.equals(value3, map[key3]); |
| 110 map.remove(key3); |
| 111 Expect.equals(2, map.length); |
| 112 map[key4] = value4; |
| 113 Expect.equals(value4, map[key4]); |
| 114 map.remove(key4); |
| 115 Expect.equals(2, map.length); |
| 116 map[key5] = value5; |
| 117 Expect.equals(value5, map[key5]); |
| 118 map.remove(key5); |
| 119 Expect.equals(2, map.length); |
| 120 map[key6] = value6; |
| 121 Expect.equals(value6, map[key6]); |
| 122 map.remove(key6); |
| 123 Expect.equals(2, map.length); |
| 124 map[key7] = value7; |
| 125 Expect.equals(value7, map[key7]); |
| 126 map.remove(key7); |
| 127 Expect.equals(2, map.length); |
| 128 map[key8] = value8; |
| 129 Expect.equals(value8, map[key8]); |
| 130 map.remove(key8); |
| 131 Expect.equals(2, map.length); |
| 132 |
| 133 Expect.equals(true, map.containsKey(key1)); |
| 134 Expect.equals(true, map.containsValue(value1)); |
| 135 |
| 136 // Test Map.forEach. |
| 137 Map other_map = new Map(); |
| 138 void testForEachMap(key, value) { |
| 139 other_map[key] = value; |
| 140 } |
| 141 map.forEach(testForEachMap); |
| 142 Expect.equals(true, other_map.containsKey(key1)); |
| 143 Expect.equals(true, other_map.containsKey(key2)); |
| 144 Expect.equals(true, other_map.containsValue(value1)); |
| 145 Expect.equals(true, other_map.containsValue(value2)); |
| 146 Expect.equals(2, other_map.length); |
| 147 |
| 148 other_map.clear(); |
| 149 Expect.equals(0, other_map.length); |
| 150 |
| 151 // Test Collection.keys. |
| 152 void testForEachCollection(value) { |
| 153 other_map[value] = value; |
| 154 } |
| 155 Iterable keys = map.keys; |
| 156 keys.forEach(testForEachCollection); |
| 157 Expect.equals(true, other_map.containsKey(key1)); |
| 158 Expect.equals(true, other_map.containsKey(key2)); |
| 159 Expect.equals(true, other_map.containsValue(key1)); |
| 160 Expect.equals(true, other_map.containsValue(key2)); |
| 161 Expect.equals(true, !other_map.containsKey(value1)); |
| 162 Expect.equals(true, !other_map.containsKey(value2)); |
| 163 Expect.equals(true, !other_map.containsValue(value1)); |
| 164 Expect.equals(true, !other_map.containsValue(value2)); |
| 165 Expect.equals(2, other_map.length); |
| 166 other_map.clear(); |
| 167 Expect.equals(0, other_map.length); |
| 168 |
| 169 // Test Collection.values. |
| 170 Iterable values = map.values; |
| 171 values.forEach(testForEachCollection); |
| 172 Expect.equals(true, !other_map.containsKey(key1)); |
| 173 Expect.equals(true, !other_map.containsKey(key2)); |
| 174 Expect.equals(true, !other_map.containsValue(key1)); |
| 175 Expect.equals(true, !other_map.containsValue(key2)); |
| 176 Expect.equals(true, other_map.containsKey(value1)); |
| 177 Expect.equals(true, other_map.containsKey(value2)); |
| 178 Expect.equals(true, other_map.containsValue(value1)); |
| 179 Expect.equals(true, other_map.containsValue(value2)); |
| 180 Expect.equals(2, other_map.length); |
| 181 other_map.clear(); |
| 182 Expect.equals(0, other_map.length); |
| 183 |
| 184 // Test Map.putIfAbsent. |
| 185 map.clear(); |
| 186 Expect.equals(false, map.containsKey(key1)); |
| 187 map.putIfAbsent(key1, () => 10); |
| 188 Expect.equals(true, map.containsKey(key1)); |
| 189 Expect.equals(10, map[key1]); |
| 190 Expect.equals(10, |
| 191 map.putIfAbsent(key1, () => 11)); |
| 192 } |
| 193 |
| 194 void testDeletedElement(Map map) { |
| 195 map.clear(); |
| 196 for (int i = 0; i < 100; i++) { |
| 31 map[1] = 2; | 197 map[1] = 2; |
| 32 Expect.equals(1, map.length); | 198 Expect.equals(1, map.length); |
| 33 } | 199 map.remove(1); |
| 34 | |
| 35 static void testMap(Map map, key1, key2, key3, key4, key5, key6, key7, key8) { | |
| 36 int value1 = 10; | |
| 37 int value2 = 20; | |
| 38 int value3 = 30; | |
| 39 int value4 = 40; | |
| 40 int value5 = 50; | |
| 41 int value6 = 60; | |
| 42 int value7 = 70; | |
| 43 int value8 = 80; | |
| 44 | |
| 45 Expect.equals(0, map.length); | 200 Expect.equals(0, map.length); |
| 46 | 201 } |
| 47 map[key1] = value1; | 202 Expect.equals(0, map.length); |
| 48 Expect.equals(value1, map[key1]); | 203 } |
| 49 map[key1] = value2; | 204 |
| 50 Expect.equals(false, map.containsKey(key2)); | 205 void testMapLiteral() { |
| 51 Expect.equals(1, map.length); | 206 Map m = {"a": 1, "b" : 2, "c": 3 }; |
| 52 | 207 Expect.equals(3, m.length); |
| 53 map[key1] = value1; | 208 int sum = 0; |
| 54 Expect.equals(value1, map[key1]); | 209 m.forEach((a, b) { |
| 55 // Add enough entries to make sure the table grows. | 210 sum += b; |
| 56 map[key2] = value2; | 211 }); |
| 57 Expect.equals(value2, map[key2]); | 212 Expect.equals(6, sum); |
| 58 Expect.equals(2, map.length); | 213 |
| 59 map[key3] = value3; | 214 List values = m.keys.toList(); |
| 60 Expect.equals(value2, map[key2]); | 215 Expect.equals(3, values.length); |
| 61 Expect.equals(value3, map[key3]); | 216 String first = values[0]; |
| 62 map[key4] = value4; | 217 String second = values[1]; |
| 63 Expect.equals(value3, map[key3]); | 218 String third = values[2]; |
| 64 Expect.equals(value4, map[key4]); | 219 String all = "${first}${second}${third}"; |
| 65 map[key5] = value5; | 220 Expect.equals(3, all.length); |
| 66 Expect.equals(value4, map[key4]); | 221 Expect.equals(true, all.contains("a", 0)); |
| 67 Expect.equals(value5, map[key5]); | 222 Expect.equals(true, all.contains("b", 0)); |
| 68 map[key6] = value6; | 223 Expect.equals(true, all.contains("c", 0)); |
| 69 Expect.equals(value5, map[key5]); | 224 } |
| 70 Expect.equals(value6, map[key6]); | 225 |
| 71 map[key7] = value7; | 226 void testNullValue() { |
| 72 Expect.equals(value6, map[key6]); | 227 Map m = {"a": 1, "b" : null, "c": 3 }; |
| 73 Expect.equals(value7, map[key7]); | 228 |
| 74 map[key8] = value8; | 229 Expect.equals(null, m["b"]); |
| 75 Expect.equals(value1, map[key1]); | 230 Expect.equals(true, m.containsKey("b")); |
| 76 Expect.equals(value2, map[key2]); | 231 Expect.equals(3, m.length); |
| 77 Expect.equals(value3, map[key3]); | 232 |
| 78 Expect.equals(value4, map[key4]); | 233 m["a"] = null; |
| 79 Expect.equals(value5, map[key5]); | 234 m["c"] = null; |
| 80 Expect.equals(value6, map[key6]); | 235 Expect.equals(null, m["a"]); |
| 81 Expect.equals(value7, map[key7]); | 236 Expect.equals(true, m.containsKey("a")); |
| 82 Expect.equals(value8, map[key8]); | 237 Expect.equals(null, m["c"]); |
| 83 Expect.equals(8, map.length); | 238 Expect.equals(true, m.containsKey("c")); |
| 84 | 239 Expect.equals(3, m.length); |
| 85 map.remove(key4); | 240 |
| 86 Expect.equals(false, map.containsKey(key4)); | 241 m.remove("a"); |
| 87 Expect.equals(7, map.length); | 242 Expect.equals(2, m.length); |
| 88 | 243 Expect.equals(null, m["a"]); |
| 89 // Test clearing the table. | 244 Expect.equals(false, m.containsKey("a")); |
| 90 map.clear(); | 245 } |
| 91 Expect.equals(0, map.length); | 246 |
| 92 Expect.equals(false, map.containsKey(key1)); | 247 void testTypes() { |
| 93 Expect.equals(false, map.containsKey(key2)); | 248 Map<int> map; |
| 94 Expect.equals(false, map.containsKey(key3)); | 249 testMap(Map map) { |
| 95 Expect.equals(false, map.containsKey(key4)); | 250 map[42] = "text"; |
| 96 Expect.equals(false, map.containsKey(key5)); | 251 map[43] = "text"; |
| 97 Expect.equals(false, map.containsKey(key6)); | 252 map[42] = "text"; |
| 98 Expect.equals(false, map.containsKey(key7)); | 253 map.remove(42); |
| 99 Expect.equals(false, map.containsKey(key8)); | 254 map[42] = "text"; |
| 100 | 255 } |
| 101 // Test adding and removing again. | 256 testMap(new HashMap<int, String>()); |
| 102 map[key1] = value1; | 257 testMap(new LinkedHashMap<int, String>()); |
| 103 Expect.equals(value1, map[key1]); | 258 testMap(new SplayTreeMap<int, String>()); |
| 104 Expect.equals(1, map.length); | 259 testMap(new SplayTreeMap<int, String>(Comparable.compare)); |
| 105 map[key2] = value2; | 260 testMap(new SplayTreeMap<int, String>((int a, int b) => a.compareTo(b))); |
| 106 Expect.equals(value2, map[key2]); | 261 testMap(new HashMap<num, String>()); |
| 107 Expect.equals(2, map.length); | 262 testMap(new LinkedHashMap<num, String>()); |
| 108 map[key3] = value3; | 263 testMap(new SplayTreeMap<num, String>()); |
| 109 Expect.equals(value3, map[key3]); | 264 testMap(new SplayTreeMap<num, String>(Comparable.compare)); |
| 110 map.remove(key3); | 265 testMap(new SplayTreeMap<num, String>((num a, num b) => a.compareTo(b))); |
| 111 Expect.equals(2, map.length); | 266 } |
| 112 map[key4] = value4; | |
| 113 Expect.equals(value4, map[key4]); | |
| 114 map.remove(key4); | |
| 115 Expect.equals(2, map.length); | |
| 116 map[key5] = value5; | |
| 117 Expect.equals(value5, map[key5]); | |
| 118 map.remove(key5); | |
| 119 Expect.equals(2, map.length); | |
| 120 map[key6] = value6; | |
| 121 Expect.equals(value6, map[key6]); | |
| 122 map.remove(key6); | |
| 123 Expect.equals(2, map.length); | |
| 124 map[key7] = value7; | |
| 125 Expect.equals(value7, map[key7]); | |
| 126 map.remove(key7); | |
| 127 Expect.equals(2, map.length); | |
| 128 map[key8] = value8; | |
| 129 Expect.equals(value8, map[key8]); | |
| 130 map.remove(key8); | |
| 131 Expect.equals(2, map.length); | |
| 132 | |
| 133 Expect.equals(true, map.containsKey(key1)); | |
| 134 Expect.equals(true, map.containsValue(value1)); | |
| 135 | |
| 136 // Test Map.forEach. | |
| 137 Map other_map = new Map(); | |
| 138 void testForEachMap(key, value) { | |
| 139 other_map[key] = value; | |
| 140 } | |
| 141 map.forEach(testForEachMap); | |
| 142 Expect.equals(true, other_map.containsKey(key1)); | |
| 143 Expect.equals(true, other_map.containsKey(key2)); | |
| 144 Expect.equals(true, other_map.containsValue(value1)); | |
| 145 Expect.equals(true, other_map.containsValue(value2)); | |
| 146 Expect.equals(2, other_map.length); | |
| 147 | |
| 148 other_map.clear(); | |
| 149 Expect.equals(0, other_map.length); | |
| 150 | |
| 151 // Test Collection.keys. | |
| 152 void testForEachCollection(value) { | |
| 153 other_map[value] = value; | |
| 154 } | |
| 155 Iterable keys = map.keys; | |
| 156 keys.forEach(testForEachCollection); | |
| 157 Expect.equals(true, other_map.containsKey(key1)); | |
| 158 Expect.equals(true, other_map.containsKey(key2)); | |
| 159 Expect.equals(true, other_map.containsValue(key1)); | |
| 160 Expect.equals(true, other_map.containsValue(key2)); | |
| 161 Expect.equals(true, !other_map.containsKey(value1)); | |
| 162 Expect.equals(true, !other_map.containsKey(value2)); | |
| 163 Expect.equals(true, !other_map.containsValue(value1)); | |
| 164 Expect.equals(true, !other_map.containsValue(value2)); | |
| 165 Expect.equals(2, other_map.length); | |
| 166 other_map.clear(); | |
| 167 Expect.equals(0, other_map.length); | |
| 168 | |
| 169 // Test Collection.values. | |
| 170 Iterable values = map.values; | |
| 171 values.forEach(testForEachCollection); | |
| 172 Expect.equals(true, !other_map.containsKey(key1)); | |
| 173 Expect.equals(true, !other_map.containsKey(key2)); | |
| 174 Expect.equals(true, !other_map.containsValue(key1)); | |
| 175 Expect.equals(true, !other_map.containsValue(key2)); | |
| 176 Expect.equals(true, other_map.containsKey(value1)); | |
| 177 Expect.equals(true, other_map.containsKey(value2)); | |
| 178 Expect.equals(true, other_map.containsValue(value1)); | |
| 179 Expect.equals(true, other_map.containsValue(value2)); | |
| 180 Expect.equals(2, other_map.length); | |
| 181 other_map.clear(); | |
| 182 Expect.equals(0, other_map.length); | |
| 183 | |
| 184 // Test Map.putIfAbsent. | |
| 185 map.clear(); | |
| 186 Expect.equals(false, map.containsKey(key1)); | |
| 187 map.putIfAbsent(key1, () => 10); | |
| 188 Expect.equals(true, map.containsKey(key1)); | |
| 189 Expect.equals(10, map[key1]); | |
| 190 Expect.equals(10, | |
| 191 map.putIfAbsent(key1, () => 11)); | |
| 192 } | |
| 193 | |
| 194 static void testDeletedElement(Map map) { | |
| 195 map.clear(); | |
| 196 for (int i = 0; i < 100; i++) { | |
| 197 map[1] = 2; | |
| 198 Expect.equals(1, map.length); | |
| 199 map.remove(1); | |
| 200 Expect.equals(0, map.length); | |
| 201 } | |
| 202 Expect.equals(0, map.length); | |
| 203 } | |
| 204 | |
| 205 static void testMapLiteral() { | |
| 206 Map m = {"a": 1, "b" : 2, "c": 3 }; | |
| 207 Expect.equals(3, m.length); | |
| 208 int sum = 0; | |
| 209 m.forEach((a, b) { | |
| 210 sum += b; | |
| 211 }); | |
| 212 Expect.equals(6, sum); | |
| 213 | |
| 214 List values = m.keys.toList(); | |
| 215 Expect.equals(3, values.length); | |
| 216 String first = values[0]; | |
| 217 String second = values[1]; | |
| 218 String third = values[2]; | |
| 219 String all = "${first}${second}${third}"; | |
| 220 Expect.equals(3, all.length); | |
| 221 Expect.equals(true, all.contains("a", 0)); | |
| 222 Expect.equals(true, all.contains("b", 0)); | |
| 223 Expect.equals(true, all.contains("c", 0)); | |
| 224 } | |
| 225 | |
| 226 static void testNullValue() { | |
| 227 Map m = {"a": 1, "b" : null, "c": 3 }; | |
| 228 | |
| 229 Expect.equals(null, m["b"]); | |
| 230 Expect.equals(true, m.containsKey("b")); | |
| 231 Expect.equals(3, m.length); | |
| 232 | |
| 233 m["a"] = null; | |
| 234 m["c"] = null; | |
| 235 Expect.equals(null, m["a"]); | |
| 236 Expect.equals(true, m.containsKey("a")); | |
| 237 Expect.equals(null, m["c"]); | |
| 238 Expect.equals(true, m.containsKey("c")); | |
| 239 Expect.equals(3, m.length); | |
| 240 | |
| 241 m.remove("a"); | |
| 242 Expect.equals(2, m.length); | |
| 243 Expect.equals(null, m["a"]); | |
| 244 Expect.equals(false, m.containsKey("a")); | |
| 245 } | |
| 246 } | |
| 247 | |
| 248 main() { | |
| 249 MapTest.testMain(); | |
| 250 } | |
| OLD | NEW |