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 map.from.test; | |
6 | |
7 import "package:expect/expect.dart"; | |
8 import 'dart:collection'; | |
9 | |
10 main() { | |
11 testWithConstMap(); | |
12 testWithNonConstMap(); | |
13 testWithHashMap(); | |
14 testWithLinkedMap(); | |
15 } | |
16 | |
17 testWithConstMap() { | |
18 var map = const {'b': 42, 'a': 43}; | |
19 var otherMap = new Map.from(map); | |
20 Expect.isTrue(otherMap is Map); | |
21 Expect.isTrue(otherMap is HashMap); | |
22 Expect.isTrue(otherMap is LinkedHashMap); | |
23 | |
24 Expect.equals(2, otherMap.length); | |
25 Expect.equals(2, otherMap.keys.length); | |
26 Expect.equals(2, otherMap.values.length); | |
27 | |
28 var count = (map) { | |
29 int cnt = 0; | |
30 map.forEach((a, b) { | |
31 cnt += b; | |
32 }); | |
33 return cnt; | |
34 }; | |
35 | |
36 Expect.equals(42 + 43, count(map)); | |
37 Expect.equals(count(map), count(otherMap)); | |
38 } | |
39 | |
40 testWithNonConstMap() { | |
41 var map = {'b': 42, 'a': 43}; | |
42 var otherMap = new Map.from(map); | |
43 Expect.isTrue(otherMap is Map); | |
44 Expect.isTrue(otherMap is HashMap); | |
45 Expect.isTrue(otherMap is LinkedHashMap); | |
46 | |
47 Expect.equals(2, otherMap.length); | |
48 Expect.equals(2, otherMap.keys.length); | |
49 Expect.equals(2, otherMap.values.length); | |
50 | |
51 int count(map) { | |
52 int count = 0; | |
53 map.forEach((a, b) { | |
54 count += b; | |
55 }); | |
56 return count; | |
57 } | |
58 | |
59 ; | |
60 | |
61 Expect.equals(42 + 43, count(map)); | |
62 Expect.equals(count(map), count(otherMap)); | |
63 | |
64 // Test that adding to the original map does not change otherMap. | |
65 map['c'] = 44; | |
66 Expect.equals(3, map.length); | |
67 Expect.equals(2, otherMap.length); | |
68 Expect.equals(2, otherMap.keys.length); | |
69 Expect.equals(2, otherMap.values.length); | |
70 | |
71 // Test that adding to otherMap does not change the original map. | |
72 otherMap['c'] = 44; | |
73 Expect.equals(3, map.length); | |
74 Expect.equals(3, otherMap.length); | |
75 Expect.equals(3, otherMap.keys.length); | |
76 Expect.equals(3, otherMap.values.length); | |
77 } | |
78 | |
79 testWithHashMap() { | |
80 var map = const {'b': 1, 'a': 2, 'c': 3}; | |
81 var otherMap = new HashMap.from(map); | |
82 Expect.isTrue(otherMap is Map); | |
83 Expect.isTrue(otherMap is HashMap); | |
84 Expect.isTrue(otherMap is! LinkedHashMap); | |
85 var i = 1; | |
86 for (var val in map.values) { | |
87 Expect.equals(i++, val); | |
88 } | |
89 } | |
90 | |
91 testWithLinkedMap() { | |
92 var map = const {'b': 1, 'a': 2, 'c': 3}; | |
93 var otherMap = new LinkedHashMap.from(map); | |
94 Expect.isTrue(otherMap is Map); | |
95 Expect.isTrue(otherMap is HashMap); | |
96 Expect.isTrue(otherMap is LinkedHashMap); | |
97 var i = 1; | |
98 for (var val in map.values) { | |
99 Expect.equals(i++, val); | |
100 } | |
101 } | |
OLD | NEW |