Teh first one
[mldemos:kalians-mldemos.git] / _AlgorithmsPlugins / KernelMethods / dlib / test / config_reader.cpp
1 // Copyright (C) 2007  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 <cstdlib>\r
8 #include <ctime>\r
9 #include <dlib/config_reader.h>\r
10 \r
11 #include "tester.h"\r
12 \r
13 // This is called an unnamed-namespace and it has the effect of making everything inside this file "private"\r
14 // so that everything you declare will have static linkage.  Thus we won't have any multiply\r
15 // defined symbol errors coming out of the linker when we try to compile the test suite.\r
16 namespace  \r
17 {\r
18 \r
19     using namespace test;\r
20     using namespace dlib;\r
21     using namespace std;\r
22 \r
23     // Declare the logger we will use in this test.  The name of the tester \r
24     // should start with "test."\r
25     logger dlog("test.config_reader");\r
26 \r
27     template <\r
28         typename config_reader\r
29         >\r
30     void do_the_tests (\r
31         config_reader& cr\r
32     )\r
33     {\r
34         DLIB_TEST(cr.is_key_defined("global"));\r
35         DLIB_TEST(cr.is_block_defined("all"));\r
36         DLIB_TEST(cr.is_key_defined("globalasfd") == false);\r
37         DLIB_TEST(cr.is_block_defined("all!") == false);\r
38         DLIB_TEST(cr["global"] == "hmm");\r
39         DLIB_TEST(cr["global2"] == "hmm2");\r
40 \r
41         std_vector_c<string> blocks;\r
42         cr.block("all").get_blocks(blocks);\r
43         DLIB_TEST(blocks.size() == 4); \r
44         cr.block("all").block("block1").get_blocks(blocks); DLIB_TEST(blocks.size() == 0); \r
45         cr.block("all").block("block2").get_blocks(blocks); DLIB_TEST(blocks.size() == 0); \r
46         cr.block("all").block("block3").get_blocks(blocks); DLIB_TEST(blocks.size() == 0); \r
47         cr.block("all").block("block4").get_blocks(blocks); DLIB_TEST(blocks.size() == 0); \r
48 \r
49         DLIB_TEST(cr.block("all").block("block1").is_key_defined("name")); \r
50         DLIB_TEST(cr.block("all").block("block2").is_key_defined("name")); \r
51         DLIB_TEST(cr.block("all").block("block3").is_key_defined("name")); \r
52         DLIB_TEST(cr.block("all").block("block4").is_key_defined("name")); \r
53         DLIB_TEST(cr.block("all").block("block1").is_key_defined("age")); \r
54         DLIB_TEST(cr.block("all").block("block2").is_key_defined("age")); \r
55         DLIB_TEST(cr.block("all").block("block3").is_key_defined("age")); \r
56         DLIB_TEST(cr.block("all").block("block4").is_key_defined("age")); \r
57 \r
58         DLIB_TEST(cr.block("all").block("block1")["name"] == "davis king"); \r
59         DLIB_TEST(cr.block("all").block("block2")["name"] == "joel"); \r
60         DLIB_TEST(cr.block("all").block("block3")["name"] == "john"); \r
61         DLIB_TEST(cr.block("all").block("block4")["name"] == "dude"); \r
62         DLIB_TEST(cr.block("all").block("block1")["age"] == "24"); \r
63         DLIB_TEST(cr.block("all").block("block2")["age"] == "24"); \r
64         DLIB_TEST(cr.block("all").block("block3")["age"] == "24"); \r
65         DLIB_TEST(cr.block("all").block("block4")["age"] == "53"); \r
66 \r
67 \r
68         int count2 = 0;\r
69         cr.get_blocks(blocks);\r
70         DLIB_TEST(blocks.size() == 1);\r
71         DLIB_TEST(blocks[0] == "all");\r
72 \r
73 \r
74         DLIB_TEST(cr.block("all").is_key_defined("global") == false);\r
75         DLIB_TEST(cr.block("all").is_key_defined("global2") == false);\r
76         DLIB_TEST(cr.block("all").is_key_defined("name") == false);\r
77         DLIB_TEST(cr.block("all").is_key_defined("age") == false);\r
78 \r
79         cr.block("all").get_blocks(blocks);\r
80         DLIB_TEST(blocks.size() == 4);\r
81         std::vector<string> temp_blocks;\r
82         for (unsigned long i = 0; i < blocks.size(); ++i)\r
83         {\r
84             ++count2;\r
85             ostringstream sout;\r
86             sout << "block" << count2;\r
87             DLIB_TEST(blocks[i] == sout.str());\r
88 \r
89             cr.block("all").block(blocks[i]).get_blocks(temp_blocks);\r
90             DLIB_TEST(temp_blocks.size() == 0);\r
91 \r
92             DLIB_TEST(cr.block("all").block(blocks[i]).is_key_defined("name"));\r
93             DLIB_TEST(cr.block("all").block(blocks[i]).is_key_defined("age"));\r
94         }\r
95 \r
96 \r
97 \r
98         bool found_error = false;\r
99         try\r
100         {\r
101             cr.block("bogus_block");\r
102         }\r
103         catch (typename config_reader::config_reader_access_error& e)\r
104         {\r
105             DLIB_TEST(e.block_name == "bogus_block");\r
106             DLIB_TEST(e.key_name == "");\r
107             found_error = true;\r
108         }\r
109         DLIB_TEST(found_error);\r
110 \r
111         found_error = false;\r
112         try\r
113         {\r
114             cr["bogus_key"];\r
115         }\r
116         catch (typename config_reader::config_reader_access_error& e)\r
117         {\r
118             DLIB_TEST(e.block_name == "");\r
119             DLIB_TEST(e.key_name == "bogus_key");\r
120             found_error = true;\r
121         }\r
122         DLIB_TEST(found_error);\r
123 \r
124 \r
125         found_error = false;\r
126         try\r
127         {\r
128             cr.block("all").block("block10");\r
129         }\r
130         catch (typename config_reader::config_reader_access_error& e)\r
131         {\r
132             DLIB_TEST(e.block_name == "block10");\r
133             DLIB_TEST(e.key_name == "");\r
134             found_error = true;\r
135         }\r
136         DLIB_TEST(found_error);\r
137 \r
138         found_error = false;\r
139         try\r
140         {\r
141             cr.block("all")["msdofg"];\r
142         }\r
143         catch (typename config_reader::config_reader_access_error& e)\r
144         {\r
145             DLIB_TEST(e.block_name == "");\r
146             DLIB_TEST(e.key_name == "msdofg");\r
147             found_error = true;\r
148         }\r
149         DLIB_TEST(found_error);\r
150 \r
151     }\r
152 \r
153 \r
154 \r
155     template <\r
156         typename config_reader\r
157         >\r
158     void config_reader_test (\r
159     )\r
160     /*!\r
161         requires\r
162             - config_reader is an implementation of config_reader/config_reader_kernel_abstract.h \r
163               is instantiated with int\r
164         ensures\r
165             - runs tests on config_reader for compliance with the specs\r
166     !*/\r
167     {        \r
168 \r
169 \r
170 \r
171         ostringstream sout;\r
172 \r
173         sout << "all#comment { { } \n";\r
174         sout << "{ \n";\r
175         sout << "    block1 \n";\r
176         sout << "    { \n";\r
177         sout << "        name = davis king \n";\r
178         sout << "        age = 24 \n";\r
179         sout << "    } \n";\r
180         sout << " \n";\r
181         sout << "    block2 \n";\r
182         sout << "    { \n";\r
183         sout << "        name= joel \n";\r
184         sout << "        age =24 \n";\r
185         sout << "    } \n";\r
186         sout << " \n";\r
187         sout << "    block3 \n";\r
188         sout << "    { \n";\r
189         sout << "        name = john \n";\r
190         sout << "        age = 24 \n";\r
191         sout << "    } \n";\r
192         sout << "  #comment \n";\r
193         sout << "#comment \n";\r
194         sout << "    block4{  # comment";\r
195         sout << "     \n";\r
196         sout << "        name = dude \n";\r
197         sout << "        age = 53}\n";\r
198         sout << "     \n";\r
199         sout << "} \n";\r
200         sout << " \n";\r
201         sout << " \n";\r
202         sout << "global=hmm#comment \n";\r
203         sout << "global2=hmm2 \n";\r
204         sout << " # comment \n";\r
205 \r
206         string data = sout.str();\r
207 \r
208         config_reader cr2;\r
209         for (int i = 0; i < 3; ++i)\r
210         {\r
211             istringstream sin;\r
212 \r
213             sin.clear();\r
214             sin.str(data);\r
215 \r
216             config_reader cr(sin);\r
217             sin.clear();\r
218             sin.str(data);\r
219 \r
220             cr2.load_from(sin);\r
221 \r
222             do_the_tests(cr);\r
223             do_the_tests(cr2);\r
224 \r
225             cr.clear();\r
226             DLIB_TEST(cr.is_key_defined("global") == false);\r
227         }\r
228 \r
229 \r
230         sout.clear();\r
231         sout.str("");\r
232 \r
233         {\r
234             sout << "all#comment { { } \n";\r
235             sout << "{ \n";\r
236             sout << "    block1 \n";\r
237             sout << "    { \n";\r
238             sout << "        name = davis king \n";\r
239             sout << "        age = 24 \n";\r
240             sout << "    } \n";\r
241             sout << " \n";\r
242             sout << "    block2 \n";\r
243             sout << "    { \n";\r
244             sout << "        name= joel \n";\r
245             sout << "        age =24 \n";\r
246             sout << "    } \n";\r
247             sout << " \n";\r
248             sout << "    block3 \n";\r
249             sout << "    {{ \n";  // error on this line\r
250             sout << "        name = john \n";\r
251             sout << "        age = 24 \n";\r
252             sout << "    } \n";\r
253             sout << "  #comment \n";\r
254             sout << "#comment \n";\r
255             sout << "    block4{  # comment";\r
256             sout << "     \n";\r
257             sout << "        name = dude \n";\r
258             sout << "        age = 53}\n";\r
259             sout << "     \n";\r
260             sout << "} \n";\r
261             sout << " \n";\r
262             sout << " \n";\r
263             sout << "global=hmm#comment \n";\r
264             sout << "global2=hmm2 \n";\r
265             sout << " # comment \n";\r
266 \r
267             istringstream sin(sout.str());\r
268 \r
269             bool error_found = false;\r
270             try\r
271             {\r
272                 cr2.load_from(sin);\r
273             }\r
274             catch (typename config_reader::config_reader_error& e)\r
275             {\r
276                 error_found = true;\r
277                 DLIB_TEST(e.line_number == 16);\r
278                 DLIB_TEST(e.redefinition == false);\r
279             }\r
280             DLIB_TEST(error_found);\r
281         }\r
282 \r
283         {\r
284             sout.str("");\r
285             sout.clear();\r
286             sout << "all#comment { { } \n";\r
287             sout << "{ \n";\r
288             sout << "    block1 \n";\r
289             sout << "    { \n";\r
290             sout << "        name = davis king \n";\r
291             sout << "        age = 24 \n";\r
292             sout << "    } \n";\r
293             sout << " \n";\r
294             sout << "    block2 \n";\r
295             sout << "    { \n";\r
296             sout << "        name= joel \n";\r
297             sout << "        age =24 \n";\r
298             sout << "    } \n";\r
299             sout << " \n";\r
300             sout << "    block3 \n";\r
301             sout << "    { \n";\r
302             sout << "        name = john \n";\r
303             sout << "        age = 24 \n";\r
304             sout << "    } \n";\r
305             sout << "  #comment \n";\r
306             sout << "#comment \n";\r
307             sout << "    block4{  # comment";\r
308             sout << "     \n";\r
309             sout << "        name = dude \n";\r
310             sout << "        age = 53}\n";\r
311             sout << "     \n";\r
312             sout << "} \n";\r
313             sout << " \n";\r
314             sout << " \n";\r
315             sout << "global=hmm#comment \n";\r
316             sout << " \n";\r
317             sout << "global=hmm2 \n";  // error on this line\r
318             sout << " # comment \n";\r
319 \r
320             istringstream sin(sout.str());\r
321 \r
322             bool error_found = false;\r
323             try\r
324             {\r
325                 cr2.load_from(sin);\r
326             }\r
327             catch (typename config_reader::config_reader_error& e)\r
328             {\r
329                 error_found = true;\r
330                 DLIB_TEST_MSG(e.line_number == 31,e.line_number);\r
331                 DLIB_TEST(e.redefinition == true);\r
332             }\r
333             DLIB_TEST(error_found);\r
334         }\r
335 \r
336 \r
337         {\r
338             sout.str("");\r
339             sout.clear();\r
340             sout << "all#comment { { } \n";\r
341             sout << "{ \n";\r
342             sout << "    block1 \n";\r
343             sout << "    { \n";\r
344             sout << "        name = davis king \n";\r
345             sout << "        age = 24 \n";\r
346             sout << "    } \n";\r
347             sout << " \n";\r
348             sout << "    block2 \n";\r
349             sout << "    { \n";\r
350             sout << "        name= joel \n";\r
351             sout << "        age =24 \n";\r
352             sout << "    } block2{} \n";  // error on this line\r
353             sout << " \n";\r
354             sout << "    block3 \n";\r
355             sout << "    { \n";\r
356             sout << "        name = john \n";\r
357             sout << "        age = 24 \n";\r
358             sout << "    } \n";\r
359             sout << "  #comment \n";\r
360             sout << "#comment \n";\r
361             sout << "    block4{  # comment";\r
362             sout << "     \n";\r
363             sout << "        name = dude \n";\r
364             sout << "        age = 53}\n";\r
365             sout << "     \n";\r
366             sout << "} \n";\r
367             sout << " \n";\r
368             sout << " \n";\r
369             sout << "global=hmm#comment \n";\r
370             sout << " \n";\r
371             sout << " # comment \n";\r
372 \r
373             istringstream sin(sout.str());\r
374 \r
375             bool error_found = false;\r
376             try\r
377             {\r
378                 cr2.load_from(sin);\r
379             }\r
380             catch (typename config_reader::config_reader_error& e)\r
381             {\r
382                 error_found = true;\r
383                 DLIB_TEST_MSG(e.line_number == 13,e.line_number);\r
384                 DLIB_TEST(e.redefinition == true);\r
385             }\r
386             DLIB_TEST(error_found);\r
387         }\r
388 \r
389 \r
390 \r
391     }\r
392 \r
393 \r
394 \r
395     class config_reader_tester : public tester\r
396     {\r
397         /*!\r
398             WHAT THIS OBJECT REPRESENTS\r
399                 This object represents a test for the config_reader object.  When it is constructed\r
400                 it adds itself into the testing framework.  The command line switch is\r
401                 specified as test_config_reader by passing that string to the tester constructor.\r
402         !*/\r
403     public:\r
404         config_reader_tester (\r
405         ) :\r
406             tester ("test_config_reader",\r
407                     "Runs tests on the config_reader component.")\r
408         {}\r
409 \r
410         void perform_test (\r
411         )\r
412         {\r
413             dlog << LINFO << "testing kernel_1a";\r
414             print_spinner();\r
415             config_reader_test<config_reader::kernel_1a>();\r
416 \r
417             dlog << LINFO << "testing thread_safe_1a";\r
418             print_spinner();\r
419             config_reader_test<config_reader::thread_safe_1a>();\r
420         }\r
421     } a;\r
422 \r
423 }\r
424 \r
425 \r