1 // Copyright (C) 2006  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 
4 #include <string>
5 #include "tester.h"
6 #include <cstdlib>
7 #include <dlib/threads.h>
8 
9 namespace test
10 {
11 // -----------------------------------------------------------------------------
12 
13     bool be_verbose = true;
14 
15 // -----------------------------------------------------------------------------
16 
17     static dlib::mutex spinner_mutex;
18     static dlib::mutex test_count_mutex;
19     dlib::uint64 test_count = 0;
20 
21 // -----------------------------------------------------------------------------
22 
number_of_testing_statements_executed()23     dlib::uint64 number_of_testing_statements_executed (
24     )
25     {
26         dlib::auto_mutex lock(test_count_mutex);
27         return test_count;
28     }
29 
increment_test_count()30     void increment_test_count (
31     )
32     {
33         test_count_mutex.lock();
34         ++test_count;
35         test_count_mutex.unlock();
36     }
37 
38 // -----------------------------------------------------------------------------
39 
check_test(bool _exp,long line,const char * file,const char * _exp_str)40     void check_test (
41         bool _exp,
42         long line,
43         const char* file,
44         const char* _exp_str
45     )
46     {
47         test_count_mutex.lock();
48         ++test_count;
49         test_count_mutex.unlock();
50         if ( !(_exp) )
51         {
52             std::ostringstream dlib_o_out;
53             dlib_o_out << "\n\nError occurred at line " << line << ".\n";
54             dlib_o_out << "Error occurred in file " << file << ".\n";
55             dlib_o_out << "Failing expression was " << _exp_str << ".\n";
56             throw dlib::error(dlib_o_out.str());
57         }
58     }
59 
60 // -----------------------------------------------------------------------------
61 
testers()62     map_of_testers& testers (
63     )
64     {
65         static map_of_testers t;
66         return t;
67     }
68 
69 // -----------------------------------------------------------------------------
70 
71     tester::
tester(const std::string & switch_name_x,const std::string & description_x,unsigned long num_of_args_x)72     tester (
73         const std::string& switch_name_x,
74         const std::string& description_x,
75         unsigned long num_of_args_x
76     ) :
77         switch_name(switch_name_x),
78         description_(description_x),
79         num_of_args_(num_of_args_x)
80     {
81         using namespace std;
82         if (testers().is_in_domain(switch_name))
83         {
84             cerr << "ERROR: More than one tester has been defined with the switch '" << switch_name << "'." << endl;
85             exit(1);
86         }
87 
88         string temp(switch_name);
89         tester* t = this;
90         testers().add(temp,t);
91     }
92 
93 // -----------------------------------------------------------------------------
94 
95     const std::string& tester::
cmd_line_switch() const96     cmd_line_switch (
97     ) const
98     {
99         return switch_name;
100     }
101 
102 // -----------------------------------------------------------------------------
103 
104     const std::string& tester::
description() const105     description (
106     ) const
107     {
108         return description_;
109     }
110 
111 // -----------------------------------------------------------------------------
112 
113     unsigned long tester::
num_of_args() const114     num_of_args (
115     ) const
116     {
117         return num_of_args_;
118     }
119 
120 // -----------------------------------------------------------------------------
121 
122     void tester::
perform_test()123     perform_test (
124     )
125     {
126     }
127 
128 // -----------------------------------------------------------------------------
129 
130     void tester::
perform_test(const std::string &)131     perform_test (
132         const std::string&
133     )
134     {
135     }
136 
137 // -----------------------------------------------------------------------------
138 
139     void tester::
perform_test(const std::string &,const std::string &)140     perform_test (
141         const std::string&,
142         const std::string&
143     )
144     {
145     }
146 
147 // -----------------------------------------------------------------------------
148 
print_spinner()149     void print_spinner (
150     )
151     {
152         if (be_verbose)
153         {
154             using namespace std;
155             dlib::auto_mutex M(spinner_mutex);
156             static int i = 0;
157             cout << "\b\b";
158             switch (i)
159             {
160                 case 0: cout << '|'; break;
161                 case 1: cout << '/'; break;
162                 case 2: cout << '-'; break;
163                 case 3: cout << '\\'; break;
164             }
165             cout << " " << flush;
166             i = (i+1)%4;
167         }
168     }
169 
170 // -----------------------------------------------------------------------------
171 
172 }
173 
174 
175 
176