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