Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(349)

Side by Side Diff: tests/corelib/src/MapTest.dart

Issue 10244009: test rename overhaul: step 7 - corelib tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library("MapTest.dart");
6 #import("dart:coreimpl");
7
8 class MapTest {
9
10 static testMain() {
11 test(new HashMap());
12 test(new LinkedHashMap());
13 test(new SplayTreeMap());
14 testLinkedHashMap();
15 testMapLiteral();
16 testNullValue();
17 }
18
19 static void test(Map map) {
20 testDeletedElement(map);
21 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8);
22 map.clear();
23 testMap(map, "value1", "value2", "value3", "value4", "value5",
24 "value6", "value7", "value8");
25 }
26
27 static void testLinkedHashMap() {
28 LinkedHashMap map = new LinkedHashMap();
29 Expect.equals(false, map.containsKey(1));
30 map[1] = 1;
31 map[1] = 2;
32 Expect.equals(1, map.length);
33 }
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);
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.getKeys.
152 void testForEachCollection(value) {
153 other_map[value] = value;
154 }
155 Collection keys = map.getKeys();
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.getValues.
170 Collection values = map.getValues();
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.getKeys();
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698