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 class SetTest { | |
6 | |
7 static testMain() { | |
8 Set set = new Set(); | |
9 Expect.equals(0, set.length); | |
10 set.add(1); | |
11 Expect.equals(1, set.length); | |
12 Expect.equals(true, set.contains(1)); | |
13 | |
14 set.add(1); | |
15 Expect.equals(1, set.length); | |
16 Expect.equals(true, set.contains(1)); | |
17 | |
18 set.remove(1); | |
19 Expect.equals(0, set.length); | |
20 Expect.equals(false, set.contains(1)); | |
21 | |
22 for (int i = 0; i < 10; i++) { | |
23 set.add(i); | |
24 } | |
25 | |
26 Expect.equals(10, set.length); | |
27 for (int i = 0; i < 10; i++) { | |
28 Expect.equals(true, set.contains(i)); | |
29 } | |
30 | |
31 Expect.equals(10, set.length); | |
32 | |
33 for (int i = 10; i < 20; i++) { | |
34 Expect.equals(false, set.contains(i)); | |
35 } | |
36 | |
37 // Test Set.forEach. | |
38 int sum = 0; | |
39 testForEach(int val) { | |
40 sum += (val + 1); | |
41 } | |
42 | |
43 set.forEach(testForEach); | |
44 Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum); | |
45 | |
46 Expect.equals(true, set.isSubsetOf(set)); | |
47 Expect.equals(true, set.containsAll(set)); | |
48 | |
49 // Test Set.map. | |
50 testMap(int val) { | |
51 return val * val; | |
52 } | |
53 | |
54 Set mapped = set.map(testMap); | |
55 Expect.equals(10, mapped.length); | |
56 | |
57 Expect.equals(true, mapped.contains(0)); | |
58 Expect.equals(true, mapped.contains(1)); | |
59 Expect.equals(true, mapped.contains(4)); | |
60 Expect.equals(true, mapped.contains(9)); | |
61 Expect.equals(true, mapped.contains(16)); | |
62 Expect.equals(true, mapped.contains(25)); | |
63 Expect.equals(true, mapped.contains(36)); | |
64 Expect.equals(true, mapped.contains(49)); | |
65 Expect.equals(true, mapped.contains(64)); | |
66 Expect.equals(true, mapped.contains(81)); | |
67 | |
68 sum = 0; | |
69 set.forEach(testForEach); | |
70 Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum); | |
71 | |
72 sum = 0; | |
73 | |
74 mapped.forEach(testForEach); | |
75 Expect.equals(1 + 2 + 5 + 10 + 17 + 26 + 37 + 50 + 65 + 82, sum); | |
76 | |
77 // Test Set.filter. | |
78 testFilter(int val) { | |
79 return val.isEven(); | |
80 } | |
81 | |
82 Set filtered = set.filter(testFilter); | |
83 | |
84 Expect.equals(5, filtered.length); | |
85 | |
86 Expect.equals(true, filtered.contains(0)); | |
87 Expect.equals(true, filtered.contains(2)); | |
88 Expect.equals(true, filtered.contains(4)); | |
89 Expect.equals(true, filtered.contains(6)); | |
90 Expect.equals(true, filtered.contains(8)); | |
91 | |
92 sum = 0; | |
93 filtered.forEach(testForEach); | |
94 Expect.equals(1 + 3 + 5 + 7 + 9, sum); | |
95 | |
96 Expect.equals(true, set.containsAll(filtered)); | |
97 Expect.equals(true, filtered.isSubsetOf(set)); | |
98 | |
99 // Test Set.every. | |
100 testEvery(int val) { | |
101 return (val < 10); | |
102 } | |
103 | |
104 Expect.equals(true, set.every(testEvery)); | |
105 Expect.equals(true, filtered.every(testEvery)); | |
106 | |
107 filtered.add(10); | |
108 Expect.equals(false, filtered.every(testEvery)); | |
109 | |
110 // Test Set.some. | |
111 testSome(int val) { | |
112 return (val == 4); | |
113 } | |
114 | |
115 Expect.equals(true, set.some(testSome)); | |
116 Expect.equals(true, filtered.some(testSome)); | |
117 filtered.remove(4); | |
118 Expect.equals(false, filtered.some(testSome)); | |
119 | |
120 // Test Set.intersection. | |
121 Set intersection = set.intersection(filtered); | |
122 Expect.equals(true, set.contains(0)); | |
123 Expect.equals(true, set.contains(2)); | |
124 Expect.equals(true, set.contains(6)); | |
125 Expect.equals(true, set.contains(8)); | |
126 Expect.equals(false, intersection.contains(1)); | |
127 Expect.equals(false, intersection.contains(3)); | |
128 Expect.equals(false, intersection.contains(4)); | |
129 Expect.equals(false, intersection.contains(5)); | |
130 Expect.equals(false, intersection.contains(7)); | |
131 Expect.equals(false, intersection.contains(9)); | |
132 Expect.equals(false, intersection.contains(10)); | |
133 Expect.equals(4, intersection.length); | |
134 | |
135 Expect.equals(true, set.containsAll(intersection)); | |
136 Expect.equals(true, filtered.containsAll(intersection)); | |
137 Expect.equals(true, intersection.isSubsetOf(set)); | |
138 Expect.equals(true, intersection.isSubsetOf(filtered)); | |
139 | |
140 // Test Set.addAll. | |
141 List list = new List(10); | |
142 for (int i = 0; i < 10; i++) { | |
143 list[i] = i + 10; | |
144 } | |
145 set.addAll(list); | |
146 Expect.equals(20, set.length); | |
147 for (int i = 0; i < 20; i++) { | |
148 Expect.equals(true, set.contains(i)); | |
149 } | |
150 | |
151 // Test Set.removeAll | |
152 set.removeAll(list); | |
153 Expect.equals(10, set.length); | |
154 for (int i = 0; i < 10; i++) { | |
155 Expect.equals(true, set.contains(i)); | |
156 } | |
157 for (int i = 10; i < 20; i++) { | |
158 Expect.equals(false, set.contains(i)); | |
159 } | |
160 | |
161 // Test Set.clear. | |
162 set.clear(); | |
163 Expect.equals(0, set.length); | |
164 set.add(11); | |
165 Expect.equals(1, set.length); | |
166 } | |
167 } | |
168 | |
169 main() { | |
170 SetTest.testMain(); | |
171 } | |
OLD | NEW |