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