Teh first one
[mldemos:kalians-mldemos.git] / _AlgorithmsPlugins / KernelMethods / dlib / test / sliding_buffer.cpp
1 // Copyright (C) 2004  Davis E. King (davis@dlib.net)\r
2 // License: Boost Software License   See LICENSE.txt for the full license.\r
3 \r
4 \r
5 #include <sstream>\r
6 #include <string>\r
7 #include <ctime>\r
8 #include <cstdlib>\r
9 \r
10 #include <dlib/sliding_buffer.h>\r
11 #include "tester.h"\r
12 \r
13 namespace  \r
14 {\r
15 \r
16     using namespace test;\r
17     using namespace std;\r
18     using namespace dlib;\r
19 \r
20     logger dlog("test.sliding_buffer");\r
21 \r
22     template <\r
23         typename buf\r
24         >\r
25     void sliding_buffer_kernel_test (\r
26     )\r
27     /*!\r
28         requires\r
29             - buf is an implementation of sliding_buffer/sliding_buffer_kernel_abstract.h \r
30             - buf is instantiated with T=unsigned char\r
31         ensures\r
32             - runs tests on buf for compliance with the specs \r
33     !*/\r
34     {        \r
35 \r
36         print_spinner();\r
37 \r
38         buf test;\r
39 \r
40         DLIB_TEST(test.size() == 0);\r
41 \r
42         test.set_size(3);\r
43         buf test2;\r
44 \r
45         DLIB_TEST(test.size() == 8);\r
46 \r
47         for (int g = 0; g < 2; ++g)\r
48         {\r
49 \r
50             test.clear();\r
51 \r
52             DLIB_TEST(test.size() == 0);\r
53             test.set_size(2);\r
54 \r
55             DLIB_TEST(test.size() == 4);\r
56 \r
57 \r
58 \r
59             test[0] = 'a';\r
60             test[1] = 's';\r
61             test[2] = 'd';\r
62             test[3] = 'f';\r
63 \r
64             unsigned long id = test.get_element_id(2);\r
65             DLIB_TEST(test[test.get_element_index(id)] == 'd');\r
66 \r
67 \r
68             DLIB_TEST(test[0] == 'a');\r
69             DLIB_TEST(test[1] == 's');\r
70             DLIB_TEST(test[2] == 'd');\r
71             DLIB_TEST(test[3] == 'f');\r
72 \r
73             DLIB_TEST(test2.size() == 0);\r
74             swap(test,test2);\r
75             DLIB_TEST(test2.size() == 4);\r
76 \r
77             DLIB_TEST(test2[0] == 'a');\r
78             DLIB_TEST(test2[1] == 's');\r
79             DLIB_TEST(test2[2] == 'd');\r
80             DLIB_TEST(test2[3] == 'f');\r
81 \r
82             swap(test,test2);\r
83 \r
84             test.rotate_left(4);\r
85 \r
86 \r
87             DLIB_TEST(test[test.get_element_index(id)] == 'd');\r
88 \r
89             DLIB_TEST(test[0] == 'a');\r
90             DLIB_TEST(test[1] == 's');\r
91             DLIB_TEST(test[2] == 'd');\r
92             DLIB_TEST(test[3] == 'f');\r
93 \r
94             test.rotate_right(1);\r
95 \r
96             DLIB_TEST(test[test.get_element_index(id)] == 'd');\r
97 \r
98             DLIB_TEST(test[0] == 's');\r
99             DLIB_TEST(test[1] == 'd');\r
100             DLIB_TEST(test[2] == 'f');\r
101             DLIB_TEST(test[3] == 'a');   \r
102 \r
103 \r
104             test.rotate_left(1);\r
105 \r
106             DLIB_TEST(test[test.get_element_index(id)] == 'd');\r
107             DLIB_TEST(test[0] == 'a');\r
108             DLIB_TEST(test[1] == 's');\r
109             DLIB_TEST(test[2] == 'd');\r
110             DLIB_TEST(test[3] == 'f');\r
111 \r
112 \r
113             test.rotate_left(16);\r
114 \r
115             DLIB_TEST(test[test.get_element_index(id)] == 'd');\r
116             DLIB_TEST(test[0] == 'a');\r
117             DLIB_TEST(test[1] == 's');\r
118             DLIB_TEST(test[2] == 'd');\r
119             DLIB_TEST(test[3] == 'f');\r
120 \r
121 \r
122             test.rotate_left(2);\r
123 \r
124             DLIB_TEST(test[test.get_element_index(id)] == 'd');\r
125 \r
126             DLIB_TEST(test[0] == 'd');\r
127             DLIB_TEST(test[1] == 'f');\r
128             DLIB_TEST(test[2] == 'a');\r
129             DLIB_TEST(test[3] == 's');\r
130 \r
131             test.rotate_left(1);\r
132 \r
133             DLIB_TEST(test[test.get_element_index(id)] == 'd');\r
134             DLIB_TEST(test[0] == 's');\r
135             DLIB_TEST(test[1] == 'd');\r
136             DLIB_TEST(test[2] == 'f');\r
137             DLIB_TEST(test[3] == 'a');\r
138 \r
139             test.rotate_left(1);\r
140 \r
141             DLIB_TEST(test[test.get_element_index(id)] == 'd');\r
142             DLIB_TEST(test[0] == 'a');\r
143             DLIB_TEST(test[1] == 's');\r
144             DLIB_TEST(test[2] == 'd');\r
145             DLIB_TEST(test[3] == 'f');\r
146 \r
147             DLIB_TEST(test.size() == 4);\r
148 \r
149             test[0] = 'x';\r
150 \r
151             DLIB_TEST(test[0] == 'x');\r
152             DLIB_TEST(test[1] == 's');\r
153             DLIB_TEST(test[2] == 'd');\r
154             DLIB_TEST(test[3] == 'f');\r
155 \r
156             test.rotate_left(1);\r
157 \r
158             DLIB_TEST_MSG(test[0] == 'f',test[0]);\r
159             DLIB_TEST(test[1] == 'x');\r
160             DLIB_TEST(test[2] == 's');\r
161             DLIB_TEST(test[3] == 'd');\r
162 \r
163 \r
164             test[0] = 'x';\r
165 \r
166             DLIB_TEST(test[0] == 'x');\r
167             DLIB_TEST(test[1] == 'x');\r
168             DLIB_TEST(test[2] == 's');\r
169             DLIB_TEST(test[3] == 'd');\r
170 \r
171 \r
172             test.rotate_left(1);\r
173 \r
174 \r
175             DLIB_TEST(test[0] == 'd');\r
176             DLIB_TEST(test[1] == 'x');\r
177             DLIB_TEST(test[2] == 'x');\r
178             DLIB_TEST(test[3] == 's');\r
179 \r
180 \r
181 \r
182             DLIB_TEST(test.at_start() == true);\r
183             DLIB_TEST(test.current_element_valid() == false);\r
184             DLIB_TEST(test.move_next() == true);\r
185             DLIB_TEST(test.at_start() == false);\r
186             DLIB_TEST(test.current_element_valid() == true);\r
187 \r
188             test.clear();\r
189             test2.clear();\r
190 \r
191 \r
192             DLIB_TEST(test.size() == 0);\r
193             DLIB_TEST(test.at_start() == true);\r
194             DLIB_TEST(test.current_element_valid() == false);\r
195             DLIB_TEST(test.move_next() == false);\r
196             DLIB_TEST(test.at_start() == false);\r
197             DLIB_TEST(test.current_element_valid() == false);\r
198 \r
199             swap(test,test2);\r
200 \r
201             DLIB_TEST(test2.at_start() == false);\r
202             DLIB_TEST(test2.current_element_valid() == false);\r
203             DLIB_TEST(test2.move_next() == false);\r
204             DLIB_TEST(test2.at_start() == false);\r
205             DLIB_TEST(test2.current_element_valid() == false);\r
206 \r
207 \r
208             DLIB_TEST(test.at_start() == true);\r
209             DLIB_TEST(test.current_element_valid() == false);\r
210             DLIB_TEST(test.move_next() == false);\r
211             DLIB_TEST(test.at_start() == false);\r
212 \r
213             test.set_size(3);\r
214             DLIB_TEST(test.size() == 8);\r
215 \r
216             DLIB_TEST(test.at_start() == true);\r
217             DLIB_TEST(test.current_element_valid() == false);\r
218             DLIB_TEST(test.move_next() == true);\r
219             DLIB_TEST(test.at_start() == false);\r
220             DLIB_TEST(test.current_element_valid() == true);\r
221             test.reset();\r
222             DLIB_TEST(test.size() == 8);\r
223             DLIB_TEST(test.at_start() == true);\r
224             DLIB_TEST(test.current_element_valid() == false);\r
225             DLIB_TEST(test.move_next() == true);\r
226             DLIB_TEST(test.at_start() == false);\r
227             DLIB_TEST(test.current_element_valid() == true);\r
228 \r
229 \r
230             test.rotate_right(1);\r
231             DLIB_TEST(test.size() == 8);\r
232             DLIB_TEST(test.at_start() == true);\r
233             DLIB_TEST(test.current_element_valid() == false);\r
234             DLIB_TEST(test.move_next() == true);\r
235             DLIB_TEST(test.at_start() == false);\r
236             DLIB_TEST(test.current_element_valid() == true);\r
237 \r
238             test.rotate_left(1);\r
239             DLIB_TEST(test.size() == 8);\r
240             DLIB_TEST(test.at_start() == true);\r
241             DLIB_TEST(test.current_element_valid() == false);\r
242             DLIB_TEST(test.move_next() == true);\r
243             DLIB_TEST(test.at_start() == false);\r
244             DLIB_TEST(test.current_element_valid() == true);\r
245             test.reset();\r
246 \r
247 \r
248             for (unsigned long i = 0; i < test.size(); ++i)\r
249             {\r
250                 test[i] = static_cast<unsigned char>(i);\r
251             }\r
252 \r
253             unsigned long count = 0;\r
254             while (test.move_next())\r
255             {\r
256                 DLIB_TEST(test.element() == count);\r
257                 ++count;\r
258             }\r
259 \r
260             DLIB_TEST(count == test.size());\r
261 \r
262 \r
263             test2.clear();\r
264             ostringstream sout;\r
265             istringstream sin;\r
266 \r
267             serialize(test,sout);\r
268             sin.str(sout.str());\r
269             deserialize(test2,sin);\r
270 \r
271             char ch;\r
272             sin >> ch;\r
273             DLIB_TEST( !sin);\r
274 \r
275             DLIB_TEST(test2.size() == test.size());\r
276 \r
277 \r
278             for (unsigned long i = 0; i < test.size(); ++i)\r
279             {\r
280                 DLIB_TEST_MSG(test[i] == test2[i],\r
281                              "\ni:        " << i <<\r
282                              "\ntest[i]:  " << test[i] <<\r
283                              "\ntest2[i]: " << test2[i]);\r
284             }\r
285 \r
286             count = 0;\r
287             while (test.move_next() && test2.move_next())\r
288             {\r
289                 DLIB_TEST(test.element() == count);\r
290                 DLIB_TEST(test2.element() == count);\r
291                 ++count;\r
292             }\r
293 \r
294             DLIB_TEST(test2.size() == count);\r
295             DLIB_TEST(test.size() == count);\r
296 \r
297             test2.clear();\r
298 \r
299 \r
300         } // for (int g = 0; g < 2; ++g)\r
301 \r
302 \r
303     }\r
304 \r
305 \r
306 \r
307 \r
308 \r
309 \r
310     class sliding_buffer_tester : public tester\r
311     {\r
312     public:\r
313         sliding_buffer_tester (\r
314         ) :\r
315             tester ("test_sliding_buffer",\r
316                     "Runs tests on the sliding_buffer component.")\r
317         {}\r
318 \r
319         void perform_test (\r
320         )\r
321         {\r
322             dlog << LINFO << "testing kernel_1a";\r
323             sliding_buffer_kernel_test<sliding_buffer<unsigned char>::kernel_1a>  ();\r
324             dlog << LINFO << "testing kernel_1a_c";\r
325             sliding_buffer_kernel_test<sliding_buffer<unsigned char>::kernel_1a_c>();\r
326         }\r
327     } a;\r
328 \r
329 }\r
330 \r