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 set_test; | 5 library set_test; |
6 | 6 |
7 | 7 |
8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
9 import "dart:collection"; | 9 import "dart:collection"; |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 // Test Set.forEach. | 41 // Test Set.forEach. |
42 int sum = 0; | 42 int sum = 0; |
43 testForEach(int val) { | 43 testForEach(int val) { |
44 sum += (val + 1); | 44 sum += (val + 1); |
45 } | 45 } |
46 | 46 |
47 set.forEach(testForEach); | 47 set.forEach(testForEach); |
48 Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum); | 48 Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum); |
49 | 49 |
50 Expect.isTrue(set.isSubsetOf(set)); | |
51 Expect.isTrue(set.containsAll(set)); | 50 Expect.isTrue(set.containsAll(set)); |
52 | 51 |
53 // Test Set.map. | 52 // Test Set.map. |
54 testMap(int val) { | 53 testMap(int val) { |
55 return val * val; | 54 return val * val; |
56 } | 55 } |
57 | 56 |
58 Set mapped = set.map(testMap).toSet(); | 57 Set mapped = set.map(testMap).toSet(); |
59 Expect.equals(10, mapped.length); | 58 Expect.equals(10, mapped.length); |
60 | 59 |
(...skipping 30 matching lines...) Expand all Loading... |
91 Expect.isTrue(filtered.contains(2)); | 90 Expect.isTrue(filtered.contains(2)); |
92 Expect.isTrue(filtered.contains(4)); | 91 Expect.isTrue(filtered.contains(4)); |
93 Expect.isTrue(filtered.contains(6)); | 92 Expect.isTrue(filtered.contains(6)); |
94 Expect.isTrue(filtered.contains(8)); | 93 Expect.isTrue(filtered.contains(8)); |
95 | 94 |
96 sum = 0; | 95 sum = 0; |
97 filtered.forEach(testForEach); | 96 filtered.forEach(testForEach); |
98 Expect.equals(1 + 3 + 5 + 7 + 9, sum); | 97 Expect.equals(1 + 3 + 5 + 7 + 9, sum); |
99 | 98 |
100 Expect.isTrue(set.containsAll(filtered)); | 99 Expect.isTrue(set.containsAll(filtered)); |
101 Expect.isTrue(filtered.isSubsetOf(set)); | |
102 | 100 |
103 // Test Set.every. | 101 // Test Set.every. |
104 testEvery(int val) { | 102 testEvery(int val) { |
105 return (val < 10); | 103 return (val < 10); |
106 } | 104 } |
107 | 105 |
108 Expect.isTrue(set.every(testEvery)); | 106 Expect.isTrue(set.every(testEvery)); |
109 Expect.isTrue(filtered.every(testEvery)); | 107 Expect.isTrue(filtered.every(testEvery)); |
110 | 108 |
111 filtered.add(10); | 109 filtered.add(10); |
(...skipping 19 matching lines...) Expand all Loading... |
131 Expect.isFalse(intersection.contains(3)); | 129 Expect.isFalse(intersection.contains(3)); |
132 Expect.isFalse(intersection.contains(4)); | 130 Expect.isFalse(intersection.contains(4)); |
133 Expect.isFalse(intersection.contains(5)); | 131 Expect.isFalse(intersection.contains(5)); |
134 Expect.isFalse(intersection.contains(7)); | 132 Expect.isFalse(intersection.contains(7)); |
135 Expect.isFalse(intersection.contains(9)); | 133 Expect.isFalse(intersection.contains(9)); |
136 Expect.isFalse(intersection.contains(10)); | 134 Expect.isFalse(intersection.contains(10)); |
137 Expect.equals(4, intersection.length); | 135 Expect.equals(4, intersection.length); |
138 | 136 |
139 Expect.isTrue(set.containsAll(intersection)); | 137 Expect.isTrue(set.containsAll(intersection)); |
140 Expect.isTrue(filtered.containsAll(intersection)); | 138 Expect.isTrue(filtered.containsAll(intersection)); |
141 Expect.isTrue(intersection.isSubsetOf(set)); | |
142 Expect.isTrue(intersection.isSubsetOf(filtered)); | |
143 | 139 |
144 // Test Set.union. | 140 // Test Set.union. |
145 Set twice = create()..addAll([0, 2, 4, 6, 8, 10, 12, 14]); | 141 Set twice = create()..addAll([0, 2, 4, 6, 8, 10, 12, 14]); |
146 Set thrice = create()..addAll([0, 3, 6, 9, 12, 15]); | 142 Set thrice = create()..addAll([0, 3, 6, 9, 12, 15]); |
147 Set union = twice.union(thrice); | 143 Set union = twice.union(thrice); |
148 Expect.equals(11, union.length); | 144 Expect.equals(11, union.length); |
149 for (int i = 0; i < 16; i++) { | 145 for (int i = 0; i < 16; i++) { |
150 Expect.equals(i.isEven || (i % 3) == 0, union.contains(i)); | 146 Expect.equals(i.isEven || (i % 3) == 0, union.contains(i)); |
151 } | 147 } |
152 | 148 |
153 // Test Set.difference. | 149 // Test Set.difference. |
154 Set difference = twice.difference(thrice); | 150 Set difference = twice.difference(thrice); |
155 Expect.equals(5, difference.length); | 151 Expect.equals(5, difference.length); |
156 for (int i = 0; i < 16; i++) { | 152 for (int i = 0; i < 16; i++) { |
157 Expect.equals(i.isEven && (i % 3) != 0, difference.contains(i)); | 153 Expect.equals(i.isEven && (i % 3) != 0, difference.contains(i)); |
158 } | 154 } |
159 Expect.isTrue(twice.difference(thrice).difference(twice).isEmpty); | 155 Expect.isTrue(twice.difference(thrice).difference(twice).isEmpty); |
160 | 156 |
161 // Test Set.addAll. | 157 // Test Set.addAll. |
162 List list = new List.fixedLength(10); | 158 List list = new List(10); |
163 for (int i = 0; i < 10; i++) { | 159 for (int i = 0; i < 10; i++) { |
164 list[i] = i + 10; | 160 list[i] = i + 10; |
165 } | 161 } |
166 set.addAll(list); | 162 set.addAll(list); |
167 Expect.equals(20, set.length); | 163 Expect.equals(20, set.length); |
168 for (int i = 0; i < 20; i++) { | 164 for (int i = 0; i < 20; i++) { |
169 Expect.isTrue(set.contains(i)); | 165 Expect.isTrue(set.contains(i)); |
170 } | 166 } |
171 | 167 |
172 // Test Set.removeAll | 168 // Test Set.removeAll |
(...skipping 10 matching lines...) Expand all Loading... |
183 set.clear(); | 179 set.clear(); |
184 Expect.equals(0, set.length); | 180 Expect.equals(0, set.length); |
185 set.add(11); | 181 set.add(11); |
186 Expect.equals(1, set.length); | 182 Expect.equals(1, set.length); |
187 } | 183 } |
188 | 184 |
189 main() { | 185 main() { |
190 testMain(() => new Set()); | 186 testMain(() => new Set()); |
191 testMain(() => new HashSet()); | 187 testMain(() => new HashSet()); |
192 } | 188 } |
OLD | NEW |