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