1 // Copyright (C) 2003  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 
4 
5 #include <sstream>
6 #include <string>
7 #include <cstdlib>
8 #include <ctime>
9 
10 #include <dlib/hash_set.h>
11 #include "tester.h"
12 
13 namespace
14 {
15 
16     using namespace test;
17     using namespace std;
18     using namespace dlib;
19 
20     logger dlog("test.hash_set");
21 
22     template <
23         typename hash_set
24         >
hash_set_kernel_test()25     void hash_set_kernel_test (
26     )
27     /*!
28         requires
29             - hash_set is an implementation of hash_set/hash_set_kernel_abstract.h and
30               is instantiated with int
31         ensures
32             - runs tests on hash_set for compliance with the specs
33     !*/
34     {
35 
36 
37         srand(static_cast<unsigned int>(time(0)));
38 
39 
40         print_spinner();
41 
42         hash_set test, test2;
43 
44 
45         enumerable<const int>& e = test;
46         DLIB_TEST(e.at_start() == true);
47 
48 
49         for (int j = 0; j < 4; ++j)
50         {
51             print_spinner();
52 
53             DLIB_TEST(test.at_start() == true);
54             DLIB_TEST(test.current_element_valid() == false);
55             DLIB_TEST(test.move_next() == false);
56             DLIB_TEST(test.move_next() == false);
57             DLIB_TEST(test.move_next() == false);
58             DLIB_TEST(test.move_next() == false);
59             DLIB_TEST(test.move_next() == false);
60             DLIB_TEST(test.move_next() == false);
61             DLIB_TEST(test.move_next() == false);
62             DLIB_TEST(test.at_start() == false);
63             DLIB_TEST(test.current_element_valid() == false);
64 
65 
66             DLIB_TEST(test.size() == 0);
67             DLIB_TEST(test.is_member(5) == false);
68             DLIB_TEST(test.is_member(0) == false);
69             DLIB_TEST(test.is_member(-999) == false);
70             DLIB_TEST(test.is_member(4999) == false);
71 
72 
73             int a,b = 0;
74             a = 8;
75             test.add(a);
76             DLIB_TEST(test.size() == 1);
77             DLIB_TEST(test.is_member(8) == true);
78             DLIB_TEST(test.is_member(5) == false);
79             DLIB_TEST(test.is_member(0) == false);
80             DLIB_TEST(test.is_member(-999) == false);
81             DLIB_TEST(test.is_member(4999) == false);
82             a = 53;
83             test.add(a);
84             DLIB_TEST(test.size() == 2);
85             DLIB_TEST(test.is_member(53) == true);
86             DLIB_TEST(test.is_member(5) == false);
87             DLIB_TEST(test.is_member(0) == false);
88             DLIB_TEST(test.is_member(-999) == false);
89             DLIB_TEST(test.is_member(4999) == false);
90 
91 
92             swap(test,test2);
93 
94 
95 
96             DLIB_TEST(test2.is_member(8) == true);
97             DLIB_TEST(test2.is_member(5) == false);
98             DLIB_TEST(test2.is_member(0) == false);
99             DLIB_TEST(test2.is_member(-999) == false);
100             DLIB_TEST(test2.is_member(4999) == false);
101             DLIB_TEST(test2.size() == 2);
102             DLIB_TEST(test2.is_member(53) == true);
103             DLIB_TEST(test2.is_member(5) == false);
104             DLIB_TEST(test2.is_member(0) == false);
105             DLIB_TEST(test2.is_member(-999) == false);
106             DLIB_TEST(test2.is_member(4999) == false);
107 
108 
109             DLIB_TEST(test.size() == 0);
110             DLIB_TEST(test.is_member(8) == false);
111             DLIB_TEST(test.is_member(5) == false);
112             DLIB_TEST(test.is_member(0) == false);
113             DLIB_TEST(test.is_member(-999) == false);
114             DLIB_TEST(test.is_member(4999) == false);
115             DLIB_TEST(test.size() == 0);
116             DLIB_TEST(test.is_member(53) == false);
117             DLIB_TEST(test.is_member(5) == false);
118             DLIB_TEST(test.is_member(0) == false);
119             DLIB_TEST(test.is_member(-999) == false);
120             DLIB_TEST(test.is_member(4999) == false);
121 
122 
123             test.clear();
124             DLIB_TEST(test.at_start() == true);
125             DLIB_TEST(test.move_next() == false);
126             DLIB_TEST(test.move_next() == false);
127             DLIB_TEST(test.move_next() == false);
128             DLIB_TEST(test.move_next() == false);
129             DLIB_TEST(test.move_next() == false);
130             DLIB_TEST(test.at_start() == false);
131 
132 
133             DLIB_TEST(test.size() == 0);
134 
135             while (test.size() < 10000)
136             {
137                 a = ::rand();
138                 if (!test.is_member(a))
139                     test.add(a);
140             }
141 
142             DLIB_TEST(test.size() == 10000);
143             test.clear();
144             DLIB_TEST(test.size() == 0);
145 
146             while (test.size() < 10000)
147             {
148                 a = ::rand();
149                 if (!test.is_member(a))
150                     test.add(a);
151             }
152 
153             DLIB_TEST(test.size() == 10000);
154 
155             int count = 0;
156             while (test.move_next())
157             {
158                 DLIB_TEST(test.element() == test.element());
159                 DLIB_TEST(test.element() == test.element());
160                 DLIB_TEST(test.element() == test.element());
161 
162 
163                 ++count;
164             }
165             DLIB_TEST(test.current_element_valid() == false);
166             DLIB_TEST(test.at_start() == false);
167             DLIB_TEST(test.move_next() == false);
168             DLIB_TEST(test.current_element_valid() == false);
169             DLIB_TEST(test.at_start() == false);
170             DLIB_TEST(test.move_next() == false);
171 
172             DLIB_TEST(count == 10000);
173 
174             test.swap(test2);
175 
176             DLIB_TEST(test.size() == 2);
177             DLIB_TEST(test2.size() == 10000);
178             count = 0;
179             test2.reset();
180             while (test2.move_next())
181             {
182                 DLIB_TEST(test2.element() == test2.element());
183                 DLIB_TEST(test2.element() == test2.element());
184                 DLIB_TEST(test2.element() == test2.element());
185 
186                 ++count;
187             }
188             DLIB_TEST(test2.size() == 10000);
189             DLIB_TEST(count == 10000);
190             DLIB_TEST(test2.current_element_valid() == false);
191             DLIB_TEST(test2.at_start() == false);
192             DLIB_TEST(test2.move_next() == false);
193             DLIB_TEST(test2.current_element_valid() == false);
194             DLIB_TEST(test2.at_start() == false);
195             DLIB_TEST(test2.move_next() == false);
196 
197 
198 
199             test2.clear();
200             DLIB_TEST(test2.size() == 0);
201             DLIB_TEST(test2.at_start() == true);
202 
203             while (test.size() < 20000)
204             {
205                 a = ::rand();
206                 if (!test.is_member(a))
207                     test.add(a);
208             }
209 
210             DLIB_TEST(test.at_start() == true);
211 
212             {
213                 int* array = new int[test.size()];
214                 int* tmp = array;
215 
216                 // serialize the state of test, then clear test, then
217                 // load the state back into test.
218                 ostringstream sout;
219                 serialize(test,sout);
220                 DLIB_TEST(test.at_start() == true);
221                 istringstream sin(sout.str());
222                 test.clear();
223                 deserialize(test,sin);
224 
225 
226 
227                 count = 0;
228                 while (test.move_next())
229                 {
230                     DLIB_TEST(test.element() == test.element());
231                     DLIB_TEST(test.element() == test.element());
232                     DLIB_TEST(test.element() == test.element());
233                     *tmp = test.element();
234                     ++tmp;
235                     ++count;
236                 }
237                 DLIB_TEST(count == 20000);
238 
239                 tmp = array;
240                 for (int i = 0; i < 20000; ++i)
241                 {
242                     DLIB_TEST(test.is_member(*tmp) == true);
243                     ++tmp;
244                 }
245 
246                 DLIB_TEST(test.size() == 20000);
247 
248                 tmp = array;
249                 count = 0;
250                 while (test.size() > 10000)
251                 {
252                     test.remove(*tmp,a);
253                     DLIB_TEST(*tmp == a);
254                     ++tmp;
255                     ++count;
256                 }
257                 DLIB_TEST(count == 10000);
258                 DLIB_TEST(test.size() == 10000);
259 
260                 while (test.move_next())
261                 {
262                     ++count;
263                 }
264                 DLIB_TEST(count == 20000);
265                 DLIB_TEST(test.size() == 10000);
266 
267                 while (test.size() < 20000)
268                 {
269                     a = ::rand();
270                     if (!test.is_member(a))
271                         test.add(a);
272                 }
273 
274                 test2.swap(test);
275 
276                 count = 0;
277                 while (test2.move_next())
278                 {
279                     DLIB_TEST(test2.element() == test2.element());
280                     DLIB_TEST(test2.element() == test2.element());
281                     DLIB_TEST(test2.element() == test2.element());
282 
283                     ++count;
284                 }
285 
286                 DLIB_TEST(count == 20000);
287                 DLIB_TEST(test2.size() == 20000);
288 
289 
290                 while (test2.size()>0)
291                 {
292                     test2.remove_any(b);
293                 }
294 
295                 DLIB_TEST(test2.size() == 0);
296                 delete [] array;
297             }
298 
299             test.clear();
300             test2.clear();
301             while (test.size() < 10000)
302             {
303                 a = ::rand();
304                 if (!test.is_member(a))
305                     test.add(a);
306             }
307 
308             count = 0;
309             while (test.move_next())
310             {
311                 ++count;
312                 if (count == 5000)
313                     break;
314                 DLIB_TEST(test.current_element_valid() == true);
315             }
316 
317             test.reset();
318 
319             count = 0;
320             while (test.move_next())
321             {
322                 ++count;
323                 DLIB_TEST(test.current_element_valid() == true);
324             }
325 
326             DLIB_TEST(count == 10000);
327 
328 
329             test.clear();
330             test2.clear();
331         }
332 
333 
334         {
335             test.clear();
336             DLIB_TEST(test.size() == 0);
337             int a = 5;
338             test.add(a);
339             a = 7;
340             test.add(a);
341             DLIB_TEST(test.size() == 2);
342             DLIB_TEST(test.is_member(7));
343             DLIB_TEST(test.is_member(5));
344             test.destroy(7);
345             DLIB_TEST(test.size() == 1);
346             DLIB_TEST(!test.is_member(7));
347             DLIB_TEST(test.is_member(5));
348             test.destroy(5);
349             DLIB_TEST(test.size() == 0);
350             DLIB_TEST(!test.is_member(7));
351             DLIB_TEST(!test.is_member(5));
352         }
353 
354     }
355 
356 
357 
358 
359     class hash_set_tester : public tester
360     {
361     public:
hash_set_tester()362         hash_set_tester (
363         ) :
364             tester ("test_hash_set",
365                     "Runs tests on the hash_set component.")
366         {}
367 
perform_test()368         void perform_test (
369         )
370         {
371             dlog << LINFO << "testing kernel_1a";
372             hash_set_kernel_test<hash_set<int,14>::kernel_1a>();
373             dlog << LINFO << "testing kernel_1a_c";
374             hash_set_kernel_test<hash_set<int,14>::kernel_1a_c>();
375             dlog << LINFO << "testing kernel_1b";
376             hash_set_kernel_test<hash_set<int,14>::kernel_1b>();
377             dlog << LINFO << "testing kernel_1b_c";
378             hash_set_kernel_test<hash_set<int,14>::kernel_1b_c>();
379             dlog << LINFO << "testing kernel_1c";
380             hash_set_kernel_test<hash_set<int,14>::kernel_1c>();
381             dlog << LINFO << "testing kernel_1c_c";
382             hash_set_kernel_test<hash_set<int,14>::kernel_1c_c>();
383         }
384     } a;
385 
386 }
387 
388