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