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 // Dart test for linked hash-maps. | |
6 library linkedHashMap.test; | |
7 | |
8 import "package:expect/expect.dart"; | |
9 import 'dart:collection' show LinkedHashMap; | |
10 | |
11 class LinkedHashMapTest { | |
12 static void testMain() { | |
13 Map map = new LinkedHashMap(); | |
14 map["a"] = 1; | |
15 map["b"] = 2; | |
16 map["c"] = 3; | |
17 map["d"] = 4; | |
18 map["e"] = 5; | |
19 | |
20 List<String> keys = new List<String>(5); | |
21 List<int> values = new List<int>(5); | |
22 | |
23 int index; | |
24 | |
25 clear() { | |
26 index = 0; | |
27 for (int i = 0; i < keys.length; i++) { | |
28 keys[i] = null; | |
29 values[i] = null; | |
30 } | |
31 } | |
32 | |
33 verifyKeys(List<String> correctKeys) { | |
34 for (int i = 0; i < correctKeys.length; i++) { | |
35 Expect.equals(correctKeys[i], keys[i]); | |
36 } | |
37 } | |
38 | |
39 verifyValues(List<int> correctValues) { | |
40 for (int i = 0; i < correctValues.length; i++) { | |
41 Expect.equals(correctValues[i], values[i]); | |
42 } | |
43 } | |
44 | |
45 testForEachMap(Object key, Object value) { | |
46 Expect.equals(map[key], value); | |
47 keys[index] = key; | |
48 values[index] = value; | |
49 index++; | |
50 } | |
51 | |
52 testForEachValue(Object v) { | |
53 values[index++] = v; | |
54 } | |
55 | |
56 testForEachKey(Object v) { | |
57 keys[index++] = v; | |
58 } | |
59 | |
60 final keysInOrder = const ["a", "b", "c", "d", "e"]; | |
61 final valuesInOrder = const [1, 2, 3, 4, 5]; | |
62 | |
63 clear(); | |
64 map.forEach(testForEachMap); | |
65 verifyKeys(keysInOrder); | |
66 verifyValues(valuesInOrder); | |
67 | |
68 clear(); | |
69 map.keys.forEach(testForEachKey); | |
70 verifyKeys(keysInOrder); | |
71 | |
72 clear(); | |
73 map.values.forEach(testForEachValue); | |
74 verifyValues(valuesInOrder); | |
75 | |
76 // Remove and then insert. | |
77 map.remove("b"); | |
78 map["b"] = 6; | |
79 final keysAfterBMove = const ["a", "c", "d", "e", "b"]; | |
80 final valuesAfterBMove = const [1, 3, 4, 5, 6]; | |
81 | |
82 clear(); | |
83 map.forEach(testForEachMap); | |
84 verifyKeys(keysAfterBMove); | |
85 verifyValues(valuesAfterBMove); | |
86 | |
87 clear(); | |
88 map.keys.forEach(testForEachKey); | |
89 verifyKeys(keysAfterBMove); | |
90 | |
91 clear(); | |
92 map.values.forEach(testForEachValue); | |
93 verifyValues(valuesAfterBMove); | |
94 | |
95 // Update. | |
96 map["a"] = 0; | |
97 final valuesAfterAUpdate = const [0, 3, 4, 5, 6]; | |
98 | |
99 clear(); | |
100 map.forEach(testForEachMap); | |
101 verifyKeys(keysAfterBMove); | |
102 verifyValues(valuesAfterAUpdate); | |
103 | |
104 clear(); | |
105 map.keys.forEach(testForEachKey); | |
106 verifyKeys(keysAfterBMove); | |
107 | |
108 clear(); | |
109 map.values.forEach(testForEachValue); | |
110 verifyValues(valuesAfterAUpdate); | |
111 } | |
112 } | |
113 | |
114 main() { | |
115 LinkedHashMapTest.testMain(); | |
116 } | |
OLD | NEW |