OLD | NEW |
| (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 maps_test; | |
6 | |
7 import "package:expect/expect.dart"; | |
8 import 'dart:collection'; | |
9 | |
10 main() { | |
11 final key1 = "key1"; | |
12 final key2 = "key2"; | |
13 final key3 = "key3"; | |
14 final key4 = "key4"; | |
15 final key5 = "key5"; | |
16 final key6 = "key6"; | |
17 final key7 = "key7"; | |
18 final key8 = "key8"; | |
19 | |
20 final value1 = 10; | |
21 final value2 = 20; | |
22 final value3 = 30; | |
23 final value4 = 40; | |
24 final value5 = 50; | |
25 final value6 = 60; | |
26 final value7 = 70; | |
27 final value8 = 80; | |
28 | |
29 Map map = new Map(); | |
30 | |
31 map[key1] = value1; | |
32 map[key1] = value2; | |
33 Expect.equals(false, Maps.containsKey(map, key2)); | |
34 Expect.equals(1, Maps.length(map)); | |
35 | |
36 map[key1] = value1; | |
37 // Add enough entries to make sure the table grows. | |
38 map[key2] = value2; | |
39 Expect.equals(2, Maps.length(map)); | |
40 map[key3] = value3; | |
41 map[key4] = value4; | |
42 map[key5] = value5; | |
43 map[key6] = value6; | |
44 map[key7] = value7; | |
45 map[key8] = value8; | |
46 Expect.equals(8, Maps.length(map)); | |
47 | |
48 map.remove(key4); | |
49 Expect.equals(false, Maps.containsKey(map, key4)); | |
50 Expect.equals(7, Maps.length(map)); | |
51 | |
52 // Test clearing the table. | |
53 Maps.clear(map); | |
54 Expect.equals(0, Maps.length(map)); | |
55 Expect.equals(false, Maps.containsKey(map, key1)); | |
56 Expect.equals(false, map.containsKey(key1)); | |
57 Expect.equals(false, Maps.containsKey(map, key2)); | |
58 Expect.equals(false, map.containsKey(key2)); | |
59 Expect.equals(false, Maps.containsKey(map, key3)); | |
60 Expect.equals(false, map.containsKey(key3)); | |
61 Expect.equals(false, Maps.containsKey(map, key4)); | |
62 Expect.equals(false, map.containsKey(key4)); | |
63 Expect.equals(false, Maps.containsKey(map, key5)); | |
64 Expect.equals(false, map.containsKey(key5)); | |
65 Expect.equals(false, Maps.containsKey(map, key6)); | |
66 Expect.equals(false, map.containsKey(key6)); | |
67 Expect.equals(false, Maps.containsKey(map, key7)); | |
68 Expect.equals(false, map.containsKey(key7)); | |
69 Expect.equals(false, Maps.containsKey(map, key8)); | |
70 Expect.equals(false, map.containsKey(key8)); | |
71 | |
72 // Test adding and removing again. | |
73 map[key1] = value1; | |
74 Expect.equals(1, Maps.length(map)); | |
75 map[key2] = value2; | |
76 Expect.equals(2, Maps.length(map)); | |
77 map[key3] = value3; | |
78 map.remove(key3); | |
79 Expect.equals(2, Maps.length(map)); | |
80 map[key4] = value4; | |
81 map.remove(key4); | |
82 Expect.equals(2, Maps.length(map)); | |
83 map[key5] = value5; | |
84 map.remove(key5); | |
85 Expect.equals(2, Maps.length(map)); | |
86 map[key6] = value6; | |
87 map.remove(key6); | |
88 Expect.equals(2, Maps.length(map)); | |
89 map[key7] = value7; | |
90 map.remove(key7); | |
91 Expect.equals(2, Maps.length(map)); | |
92 map[key8] = value8; | |
93 map.remove(key8); | |
94 Expect.equals(2, Maps.length(map)); | |
95 | |
96 Expect.equals(true, Maps.containsKey(map, key1)); | |
97 Expect.equals(true, Maps.containsValue(map, value1)); | |
98 | |
99 // Test Map.forEach. | |
100 Map other_map = new Map(); | |
101 void testForEachMap(key, value) { | |
102 other_map[key] = value; | |
103 } | |
104 | |
105 Maps.forEach(map, testForEachMap); | |
106 Expect.equals(true, other_map.containsKey(key1)); | |
107 Expect.equals(true, other_map.containsKey(key2)); | |
108 Expect.equals(true, other_map.containsValue(value1)); | |
109 Expect.equals(true, other_map.containsValue(value2)); | |
110 Expect.equals(2, Maps.length(other_map)); | |
111 | |
112 // Test Collection.values. | |
113 void testForEachCollection(value) { | |
114 other_map[value] = value; | |
115 } | |
116 | |
117 Iterable values = Maps.getValues(map); | |
118 other_map = new Map(); | |
119 values.forEach(testForEachCollection); | |
120 Expect.equals(true, !other_map.containsKey(key1)); | |
121 Expect.equals(true, !other_map.containsKey(key2)); | |
122 Expect.equals(true, !other_map.containsValue(key1)); | |
123 Expect.equals(true, !other_map.containsValue(key2)); | |
124 Expect.equals(true, other_map.containsKey(value1)); | |
125 Expect.equals(true, other_map.containsKey(value2)); | |
126 Expect.equals(true, other_map.containsValue(value1)); | |
127 Expect.equals(true, other_map.containsValue(value2)); | |
128 Expect.equals(2, other_map.length); | |
129 other_map.clear(); | |
130 | |
131 // Test Map.putIfAbsent. | |
132 map.clear(); | |
133 Expect.equals(false, Maps.containsKey(map, key1)); | |
134 Maps.putIfAbsent(map, key1, () => 10); | |
135 Expect.equals(true, map.containsKey(key1)); | |
136 Expect.equals(10, map[key1]); | |
137 Expect.equals(10, Maps.putIfAbsent(map, key1, () => 11)); | |
138 } | |
OLD | NEW |