Teh first one
[mldemos:kalians-mldemos.git] / _AlgorithmsPlugins / KernelMethods / dlib / test / main.cpp
1 // Copyright (C) 2006  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 <iostream>\r
6 #include <fstream>\r
7 #include <dlib/cmd_line_parser.h>\r
8 #include "tester.h"\r
9 #include <dlib/string.h>\r
10 \r
11 \r
12 using namespace std;\r
13 using namespace dlib;\r
14 using namespace test;\r
15 \r
16 typedef cmd_line_parser<char>::check_1a_c clp;\r
17 \r
18 static logger dlog("test.main");\r
19 \r
20 int main (int argc, char** argv)\r
21 {\r
22     try\r
23     {\r
24         clp parser;\r
25 \r
26         parser.add_option("runall","Run all the tests that don't take any arguments.");\r
27         parser.add_option("h","Displays this information.");\r
28         parser.add_option("n","How many times to run the selected tests. The default is 1.",1);\r
29         parser.add_option("d","log debugging statements to file debug.txt.");\r
30         parser.add_option("l","Set the logging level (all, trace, debug, info, warn, error, or fatal), the default is all.",1);\r
31         parser.add_option("a","Append debugging messages to debug.txt rather than clearing the file at program startup.");\r
32         parser.add_option("q","Be quiet.  Don't print the testing progress or results to standard out.");\r
33 \r
34         unsigned long num = 1;\r
35 \r
36         // add the options for all the different tests\r
37         testers().reset();\r
38         while (testers().move_next())\r
39         {\r
40             tester& test = *testers().element().value();\r
41             parser.add_option(test.cmd_line_switch(), test.description(), test.num_of_args());\r
42         }\r
43 \r
44         parser.parse(argc,argv);\r
45 \r
46         parser.check_option_arg_range("n",1,1000000000);\r
47         const char* singles[] = {"d","l","a","n","h","runall","q"};\r
48         parser.check_one_time_options(singles);\r
49         const char* d_sub[] = {"l","a"};\r
50         const char* l_args[] = {"all", "trace", "debug", "info", "warn", "error", "fatal"};\r
51         parser.check_sub_options("d",d_sub);\r
52         parser.check_option_arg_range("l",l_args);\r
53 \r
54 \r
55         if (parser.option("n"))\r
56         {\r
57             num = string_cast<unsigned long>(parser.option("n").argument());\r
58         }\r
59 \r
60         if (parser.option("q"))\r
61         {\r
62             be_verbose = false;\r
63         }\r
64 \r
65         if (parser.option("h"))\r
66         {\r
67             cout << "Usage: test [options]\n";\r
68             parser.print_options(cout);\r
69             cout << "\n\n";\r
70             return 0;\r
71         }\r
72 \r
73         ofstream fout;\r
74         if (parser.option("d"))\r
75         {\r
76             if (parser.option("a"))\r
77                 fout.open("debug.txt",ios::app);\r
78             else\r
79                 fout.open("debug.txt");\r
80 \r
81             set_all_logging_output_streams(fout);\r
82 \r
83             if (parser.option("l").count() == 0)\r
84                 set_all_logging_levels(LALL);\r
85             else if (parser.option("l").argument() == "all")\r
86                 set_all_logging_levels(LALL);\r
87             else if (parser.option("l").argument() == "trace")\r
88                 set_all_logging_levels(LTRACE);\r
89             else if (parser.option("l").argument() == "debug")\r
90                 set_all_logging_levels(LDEBUG);\r
91             else if (parser.option("l").argument() == "info")\r
92                 set_all_logging_levels(LINFO);\r
93             else if (parser.option("l").argument() == "warn")\r
94                 set_all_logging_levels(LWARN);\r
95             else if (parser.option("l").argument() == "error")\r
96                 set_all_logging_levels(LERROR);\r
97             else if (parser.option("l").argument() == "fatal")\r
98                 set_all_logging_levels(LFATAL);\r
99         }\r
100         else\r
101         {\r
102             set_all_logging_levels(LNONE);\r
103         }\r
104 \r
105         unsigned long num_of_failed_tests = 0;\r
106         unsigned long num_of_passed_tests = 0;\r
107         for (unsigned long i = 0; i < num; ++i)\r
108         {\r
109             dlog << LINFO << "************ Starting Test Run " << i+1 << " of " << num << ". ************";\r
110 \r
111             // loop over all the testers and see if they are supposed to run\r
112             testers().reset();\r
113             while (testers().move_next())\r
114             {\r
115                 tester& test= *testers().element().value();\r
116                 const clp::option_type& opt = parser.option(test.cmd_line_switch());\r
117                 // run the test for this option as many times as the user has requested.\r
118                 for (unsigned long j = 0; j < parser.option("runall").count() + opt.count(); ++j)\r
119                 {\r
120                     // quit this loop if this option has arguments and this round through the loop is\r
121                     // from the runall option being present.\r
122                     if (test.num_of_args() > 0 && j == opt.count())\r
123                         break;\r
124 \r
125                     if (be_verbose)\r
126                         cout << "Running " << test.cmd_line_switch() << "   " << flush;\r
127 \r
128                     dlog << LINFO << "Running " << test.cmd_line_switch();\r
129                     try\r
130                     {\r
131                         switch (test.num_of_args())\r
132                         {\r
133                             case 0:\r
134                                 test.perform_test();\r
135                                 break;\r
136                             case 1:\r
137                                 test.perform_test(opt.argument(0,j));\r
138                                 break;\r
139                             case 2:\r
140                                 test.perform_test(opt.argument(0,j), opt.argument(1,j));\r
141                                 break;\r
142                             default:\r
143                                 cerr << "\n\nThe test '" << test.cmd_line_switch() << "' requested " << test.num_of_args()\r
144                                     << " arguments but only 2 are supported." << endl;\r
145                                 dlog << LINFO << "The test '" << test.cmd_line_switch() << "' requested " << test.num_of_args()\r
146                                     << " arguments but only 2 are supported.";\r
147                                 break;\r
148                         }\r
149                         if (be_verbose)\r
150                             cout << "\r                                                                               \r";\r
151 \r
152                         ++num_of_passed_tests;\r
153 \r
154                     }\r
155                     catch (std::exception& e)\r
156                     {\r
157                         if (be_verbose)\r
158                         {\r
159                             cout << "\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";\r
160                             cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TEST FAILED: " << test.cmd_line_switch() \r
161                                 << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!";\r
162                             cout << "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n";\r
163                             cout << "Failure message from test: " << e.what() << endl;\r
164                         }\r
165 \r
166 \r
167                         dlog << LERROR << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";\r
168                         dlog << LERROR << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!! TEST FAILED: " << test.cmd_line_switch() \r
169                             << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!";\r
170                         dlog << LERROR << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";\r
171                         dlog << LERROR << "Failure message from test: " << e.what();\r
172                         ++num_of_failed_tests;\r
173                     }\r
174                 }\r
175             }\r
176         }\r
177         dlog << LINFO << "Testing Finished";\r
178         if (num_of_passed_tests == 0 && num_of_failed_tests == 0)\r
179         {\r
180             cout << "You didn't select any tests to run.\n";\r
181             cout << "Try the -h option for more information.\n";\r
182         }\r
183         else if (num_of_failed_tests == 0)\r
184         {\r
185             if (be_verbose)\r
186             {\r
187                 cout << "\n\nTesting Finished\n";\r
188                 cout << "All tests completed successfully\n\n";\r
189             }\r
190             dlog << LINFO << "All tests completed successfully";\r
191         }\r
192         else\r
193         {\r
194             if (be_verbose)\r
195             {\r
196                 cout << "\n\nTesting Finished\n";\r
197                 cout << "Number of failed tests: " << num_of_failed_tests << "\n";\r
198                 cout << "Number of passed tests: " << num_of_passed_tests << "\n\n";\r
199             }\r
200             dlog << LWARN << "Number of failed tests: " << num_of_failed_tests;\r
201             dlog << LWARN << "Number of passed tests: " << num_of_passed_tests;\r
202         }\r
203 \r
204         return num_of_failed_tests;\r
205     }\r
206     catch (exception& e)\r
207     {\r
208         cout << e.what() << endl;\r
209         cout << "\nTry the -h option for more information.\n";\r
210         cout << endl;\r
211     }\r
212 }\r
213 \r