-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathmap_from_test.dart
97 lines (81 loc) · 2.41 KB
/
map_from_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library map.from.test;
import "package:expect/expect.dart";
import 'dart:collection';
main() {
testWithConstMap();
testWithNonConstMap();
testWithHashMap();
testWithLinkedMap();
}
testWithConstMap() {
var map = const {'b': 42, 'a': 43};
var otherMap = new Map<String, int>.from(map);
Expect.isTrue(otherMap is Map);
Expect.isTrue(otherMap is LinkedHashMap);
Expect.equals(2, otherMap.length);
Expect.equals(2, otherMap.keys.length);
Expect.equals(2, otherMap.values.length);
int count(Map<String, int> map) {
int cnt = 0;
map.forEach((a, b) {
cnt += b;
});
return cnt;
}
Expect.equals(42 + 43, count(map));
Expect.equals(count(map), count(otherMap));
}
testWithNonConstMap() {
var map = {'b': 42, 'a': 43};
var otherMap = new Map<String, int>.from(map);
Expect.isTrue(otherMap is Map);
Expect.isTrue(otherMap is LinkedHashMap);
Expect.equals(2, otherMap.length);
Expect.equals(2, otherMap.keys.length);
Expect.equals(2, otherMap.values.length);
int count(Map<String, int> map) {
int count = 0;
map.forEach((a, b) {
count += b;
});
return count;
}
;
Expect.equals(42 + 43, count(map));
Expect.equals(count(map), count(otherMap));
// Test that adding to the original map does not change otherMap.
map['c'] = 44;
Expect.equals(3, map.length);
Expect.equals(2, otherMap.length);
Expect.equals(2, otherMap.keys.length);
Expect.equals(2, otherMap.values.length);
// Test that adding to otherMap does not change the original map.
otherMap['c'] = 44;
Expect.equals(3, map.length);
Expect.equals(3, otherMap.length);
Expect.equals(3, otherMap.keys.length);
Expect.equals(3, otherMap.values.length);
}
testWithHashMap() {
var map = const {'b': 1, 'a': 2, 'c': 3};
var otherMap = new HashMap.from(map);
Expect.isTrue(otherMap is Map);
Expect.isTrue(otherMap is HashMap);
var i = 1;
for (var val in map.values) {
Expect.equals(i++, val);
}
}
testWithLinkedMap() {
var map = const {'b': 1, 'a': 2, 'c': 3};
var otherMap = new LinkedHashMap.from(map);
Expect.isTrue(otherMap is Map);
Expect.isTrue(otherMap is LinkedHashMap);
var i = 1;
for (var val in map.values) {
Expect.equals(i++, val);
}
}