Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #228: CollectionTest.d

File CollectionTest.d, 12.3 kB (added by dhasenan, 1 year ago)

template to add basic unittests for any collection

Line 
1 module tango.util.collection.CollectionTest;
2
3 template GenericCollectionTest(char[] type)
4 {
5         struct Stub
6         {
7                 int i;
8                 char j;
9                 static Stub opCall(int a, char b)
10                 {
11                         Stub stub;
12                         stub.i = a;
13                         stub.j = b;
14                         return stub;
15                 }
16
17                 bool opEquals(Stub other)
18                 {
19                         return i == other.i && j == other.j;
20                 }
21         }
22
23         mixin ("alias " ~ type ~ "!(int) PrimitiveCollection;");
24         mixin ("alias " ~ type ~ "!(Stub) StructCollection;");
25         mixin ("alias " ~ type ~ "!(StubClass) ClassCollection;");
26         mixin ("alias " ~ type ~ "!(IStub) InterfaceCollection;");
27
28         /* Primitives ***********************/
29         /* Add contains */
30         unittest
31         {
32                 auto set = new PrimitiveCollection();
33                 set.add(4);
34                 set.add(5);
35                 assert (set.contains(4), "added item not found");
36                 assert (set.contains(5), "added item not found");
37         }
38         /* Remove contains */
39         unittest
40         {
41                 auto set = new PrimitiveCollection();
42                 set.add(4);
43                 set.add(5);
44                 set.remove(4);
45                 assert (!set.contains(4), "failed to remove item");
46                 set.remove(5);
47                 assert (!set.contains(5), "failed to remove item");
48         }
49         /* count */
50         unittest
51         {
52                 auto set = new PrimitiveCollection();
53                 set.add(4);
54                 assert (set.size == 1, "count not updated");
55                 set.add(5);
56                 assert (set.size == 2, "count not updated");
57                 set.remove(4);
58                 assert (set.size == 1, "count not updated");
59         }
60         /* opApply */
61         unittest
62         {
63                 int fiveFound = 0;
64                 int elevenFound = 0;
65                 auto set = new PrimitiveCollection();
66                 set.add(11);
67                 set.add(5);
68
69                 foreach (i; set)
70                 {
71                         if (i == 5)
72                         {
73                                 fiveFound++;
74                         }
75                         else if (i == 11)
76                         {
77                                 elevenFound++;
78                         }
79                         else
80                         {
81                                 assert (false, "found an element not in set");
82                         }
83                 }
84
85                 assert (fiveFound <= 1, "encountered element too many times");
86                 assert (fiveFound >= 1, "encountered element too few times");
87                 assert (elevenFound <= 1, "encountered element too many times");
88                 assert (elevenFound >= 1, "encountered element too few times");
89         }
90         /* clear */
91         unittest
92         {
93                 auto set = new PrimitiveCollection();
94                 set.add(1);
95                 set.add(2);
96                 set.add(3);
97                 assert (set.size == 3, "too few elements");
98                 set.clear();
99                 assert (set.size == 0, "did not clear all elements");
100         }
101
102         /* Structs ***********************/
103         /* Add contains */
104         unittest
105         {
106                 auto set = new StructCollection();
107                 set.add(Stub(4, 'a'));
108                 set.add(Stub(71, 'b'));
109                 assert (set.contains(Stub(4, 'a')), "added item not found");
110                 assert (set.contains(Stub(71, 'b')), "added item not found");
111         }
112         /* Remove contains */
113         unittest
114         {
115                 auto set = new StructCollection();
116                 set.add(Stub(4, 'a'));
117                 set.add(Stub(71, 'b'));
118                 set.remove(Stub(4, 'a'));
119                 assert (!set.contains(Stub(4, 'a')), "failed to remove item");
120                 set.remove(Stub(71, 'b'));
121                 assert (!set.contains(Stub(71, 'b')), "failed to remove item");
122         }
123         /* count */
124         unittest
125         {
126                 auto set = new StructCollection();
127                 set.add(Stub(4, 'a'));
128                 assert (set.size == 1, "count not updated");
129                 set.add(Stub(71, 'b'));
130                 assert (set.size == 2, "count not updated");
131                 set.remove(Stub(4, 'a'));
132                 assert (set.size == 1, "count not updated");
133         }
134         /* opApply */
135         unittest
136         {
137                 int fiveFound = 0;
138                 int elevenFound = 0;
139                 auto set = new StructCollection();
140                 set.add(Stub(11, '&'));
141                 set.add(Stub(71, 'b'));
142
143                 foreach (i; set)
144                 {
145                         if (i == Stub(71, 'b'))
146                         {
147                                 fiveFound++;
148                         }
149                         else if (i == Stub(11, '&'))
150                         {
151                                 elevenFound++;
152                         }
153                         else
154                         {
155                                 assert (false, "found an element not in set");
156                         }
157                 }
158
159                 assert (fiveFound <= 1, "encountered element too many times");
160                 assert (fiveFound >= 1, "encountered element too few times");
161                 assert (elevenFound <= 1, "encountered element too many times");
162                 assert (elevenFound >= 1, "encountered element too few times");
163         }
164         /* clear */
165         unittest
166         {
167                 auto set = new StructCollection();
168                 set.add(Stub(1, '8'));
169                 set.add(Stub(5, 'b'));
170                 set.add(Stub(0x23, '^'));
171                 assert (set.size == 3, "too few elements");
172                 set.clear();
173                 assert (set.size == 0, "did not clear all elements");
174         }
175
176         class StubClass
177         {
178                 int a;
179                 char c;
180                 this (int i, char j) {
181                         a = i;
182                         c = j;
183                 }
184         }
185
186         /* Classes ***********************/
187         /* Add contains */
188         unittest
189         {
190                 auto set = new ClassCollection();
191                 StubClass a = new StubClass(4, 'a');
192                 StubClass b = new StubClass(71, 'b');
193                 set.add(a);
194                 set.add(b);
195                 assert (set.contains(a), "added item not found");
196                 assert (set.contains(b), "added item not found");
197         }
198         /* Remove contains */
199         unittest
200         {
201                 auto set = new ClassCollection();
202                 StubClass a = new StubClass(4, 'a');
203                 StubClass b = new StubClass(71, 'b');
204                 set.add(a);
205                 set.add(b);
206                 set.remove(a);
207                 assert (!set.contains(a), "failed to remove item");
208                 assert (set.contains(b), "removed too much");
209                 set.remove(b);
210                 assert (!set.contains(b), "failed to remove item");
211         }
212         /* count */
213         unittest
214         {
215                 auto set = new ClassCollection();
216                 StubClass a = new StubClass(4, 'b');
217                 set.add(a);
218                 assert (set.size == 1, "count not updated");
219                 StubClass b = new StubClass(71, 'b');
220                 set.add(b);
221                 assert (set.size == 2, "count not updated");
222                 set.remove(b);
223                 assert (set.size == 1, "count not updated");
224         }
225         /* opApply */
226         unittest
227         {
228                 int aFound = 0;
229                 int bFound = 0;
230                 auto set = new ClassCollection();
231                 StubClass a = new StubClass(11, ' ');
232                 StubClass b = new StubClass(71, 'b');
233                 set.add(a);
234                 set.add(b);
235
236                 foreach (i; set)
237                 {
238                         if (i == a)
239                         {
240                                 aFound++;
241                         }
242                         else if (i == b)
243                         {
244                                 bFound++;
245                         }
246                         else
247                         {
248                                 assert (false, "found an element not in set");
249                         }
250                 }
251
252                 assert (aFound <= 1, "encountered element too many times");
253                 assert (aFound >= 1, "encountered element too few times");
254                 assert (bFound <= 1, "encountered element too many times");
255                 assert (bFound >= 1, "encountered element too few times");
256         }
257         /* clear */
258         unittest
259         {
260                 auto set = new ClassCollection();
261                 set.add(new StubClass(77777, 'a'));
262                 set.add(new StubClass(77778, '0'));
263                 set.add(new StubClass(77779, 'Z'));
264                 assert (set.size == 3, "too few elements");
265                 set.clear();
266                 assert (set.size == 0, "did not clear all elements");
267         }
268
269         interface IStub {}
270         class StubImpl : IStub
271         {
272                 int a, b;
273                 this (int i, int j)
274                 {
275                         a = i;
276                         b = 7 * j - 2;
277                 }
278         }
279
280         /* Interfaces ***********************/
281         /* Add contains */
282         unittest
283         {
284                 auto set = new InterfaceCollection();
285                 IStub a = new StubImpl(4, 'a');
286                 IStub b = new StubImpl(71, 'b');
287                 set.add(a);
288                 set.add(b);
289                 assert (set.contains(a), "added item not found");
290                 assert (set.contains(b), "added item not found");
291         }
292         /* Remove contains */
293         unittest
294         {
295                 auto set = new InterfaceCollection();
296                 IStub a = new StubImpl(4, 'a');
297                 IStub b = new StubImpl(71, 'b');
298                 set.add(a);
299                 set.add(b);
300                 set.remove(a);
301                 assert (!set.contains(a), "failed to remove item");
302                 assert (set.contains(b), "removed too much");
303                 set.remove(b);
304                 assert (!set.contains(b), "failed to remove item");
305         }
306         /* count */
307         unittest
308         {
309                 auto set = new InterfaceCollection();
310                 IStub a = new StubImpl(4, 'b');
311                 set.add(a);
312                 assert (set.size == 1, "count not updated");
313                 IStub b = new StubImpl(71, 'b');
314                 set.add(b);
315                 assert (set.size == 2, "count not updated");
316                 set.remove(b);
317                 assert (set.size == 1, "count not updated");
318         }
319         /* opApply */
320         unittest
321         {
322                 int aFound = 0;
323                 int bFound = 0;
324                 auto set = new InterfaceCollection();
325                 IStub a = new StubImpl(11, ' ');
326                 IStub b = new StubImpl(71, 'b');
327                 set.add(a);
328                 set.add(b);
329
330                 foreach (i; set)
331                 {
332                         if (i == a)
333                         {
334                                 aFound++;
335                         }
336                         else if (i == b)
337                         {
338                                 bFound++;
339                         }
340                         else
341                         {
342                                 assert (false, "found an element not in set");
343                         }
344                 }
345
346                 assert (aFound <= 1, "encountered element too many times");
347                 assert (aFound >= 1, "encountered element too few times");
348                 assert (bFound <= 1, "encountered element too many times");
349                 assert (bFound >= 1, "encountered element too few times");
350         }
351         /* clear */
352         unittest
353         {
354                 auto set = new InterfaceCollection();
355                 set.add(new StubImpl(77777, 'a'));
356                 set.add(new StubImpl(77778, '0'));
357                 set.add(new StubImpl(77779, 'Z'));
358                 assert (set.size == 3, "too few elements");
359                 set.clear();
360                 assert (set.size == 0, "did not clear all elements");
361         }
362 }