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 |
11 void testMain(Set create()) { | 11 void testMain(Set create()) { |
12 Set set = create(); | 12 Set set = create(); |
13 Expect.equals(0, set.length); | 13 testLength(0, set); |
14 set.add(1); | 14 set.add(1); |
15 Expect.equals(1, set.length); | 15 testLength(1, set); |
16 Expect.isTrue(set.contains(1)); | 16 Expect.isTrue(set.contains(1)); |
17 | 17 |
18 set.add(1); | 18 set.add(1); |
19 Expect.equals(1, set.length); | 19 testLength(1, set); |
20 Expect.isTrue(set.contains(1)); | 20 Expect.isTrue(set.contains(1)); |
21 | 21 |
22 set.remove(1); | 22 set.remove(1); |
23 Expect.equals(0, set.length); | 23 testLength(0, set); |
24 Expect.isFalse(set.contains(1)); | 24 Expect.isFalse(set.contains(1)); |
25 | 25 |
26 for (int i = 0; i < 10; i++) { | 26 for (int i = 0; i < 10; i++) { |
27 set.add(i); | 27 set.add(i); |
28 } | 28 } |
29 | 29 |
30 Expect.equals(10, set.length); | 30 testLength(10, set); |
31 for (int i = 0; i < 10; i++) { | 31 for (int i = 0; i < 10; i++) { |
32 Expect.isTrue(set.contains(i)); | 32 Expect.isTrue(set.contains(i)); |
33 } | 33 } |
34 | 34 |
35 Expect.equals(10, set.length); | 35 testLength(10, set); |
36 | 36 |
37 for (int i = 10; i < 20; i++) { | 37 for (int i = 10; i < 20; i++) { |
38 Expect.isFalse(set.contains(i)); | 38 Expect.isFalse(set.contains(i)); |
39 } | 39 } |
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 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 Expect.equals(i.isEven && (i % 3) != 0, difference.contains(i)); | 153 Expect.equals(i.isEven && (i % 3) != 0, difference.contains(i)); |
154 } | 154 } |
155 Expect.isTrue(twice.difference(thrice).difference(twice).isEmpty); | 155 Expect.isTrue(twice.difference(thrice).difference(twice).isEmpty); |
156 | 156 |
157 // Test Set.addAll. | 157 // Test Set.addAll. |
158 List list = new List(10); | 158 List list = new List(10); |
159 for (int i = 0; i < 10; i++) { | 159 for (int i = 0; i < 10; i++) { |
160 list[i] = i + 10; | 160 list[i] = i + 10; |
161 } | 161 } |
162 set.addAll(list); | 162 set.addAll(list); |
163 Expect.equals(20, set.length); | 163 testLength(20, set); |
164 for (int i = 0; i < 20; i++) { | 164 for (int i = 0; i < 20; i++) { |
165 Expect.isTrue(set.contains(i)); | 165 Expect.isTrue(set.contains(i)); |
166 } | 166 } |
167 | 167 |
168 // Test Set.removeAll | 168 // Test Set.removeAll |
169 set.removeAll(list); | 169 set.removeAll(list); |
170 Expect.equals(10, set.length); | 170 testLength(10, set); |
171 for (int i = 0; i < 10; i++) { | 171 for (int i = 0; i < 10; i++) { |
172 Expect.isTrue(set.contains(i)); | 172 Expect.isTrue(set.contains(i)); |
173 } | 173 } |
174 for (int i = 10; i < 20; i++) { | 174 for (int i = 10; i < 20; i++) { |
175 Expect.isFalse(set.contains(i)); | 175 Expect.isFalse(set.contains(i)); |
176 } | 176 } |
177 | 177 |
178 // Test Set.clear. | 178 // Test Set.clear. |
179 set.clear(); | 179 set.clear(); |
180 Expect.equals(0, set.length); | 180 testLength(0, set); |
181 set.add(11); | 181 set.add(11); |
182 Expect.equals(1, set.length); | 182 testLength(1, set); |
| 183 } |
| 184 |
| 185 void testLength(int length, Set set) { |
| 186 Expect.equals(length, set.length); |
| 187 (length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty); |
| 188 (length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty); |
183 } | 189 } |
184 | 190 |
185 main() { | 191 main() { |
186 testMain(() => new Set()); | 192 testMain(() => new Set()); |
187 testMain(() => new HashSet()); | 193 testMain(() => new HashSet()); |
188 } | 194 } |
OLD | NEW |