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