summaryrefslogtreecommitdiffstats
path: root/src/ml/dlib/dlib/test/tester.h
blob: e16647cf5f81f9af8e82d95431d63aa686615a3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_TESTEr_
#define DLIB_TESTEr_

#include <iostream>
#include <string>
#include <dlib/map.h>
#include <dlib/logger.h>
#include <dlib/assert.h>
#include <dlib/algs.h>
#include <typeinfo>

#ifdef  __INTEL_COMPILER
// ignore the bogus warning about not overloading perform_test() all the way
#pragma warning (disable: 654)
#endif


#define DLIB_TEST(_exp) check_test(bool(_exp), __LINE__, __FILE__, #_exp)

#define DLIB_TEST_MSG(_exp,_message)                                        \
    do{increment_test_count(); if ( !(_exp) )                                 \
    {                                                                       \
        std::ostringstream dlib_o_out;                                       \
        dlib_o_out << "\n\nError occurred at line " << __LINE__ << ".\n";    \
        dlib_o_out << "Error occurred in file " << __FILE__ << ".\n";        \
        dlib_o_out << "Failing expression was " << #_exp << ".\n";           \
        dlib_o_out << _message << "\n";                                      \
        throw dlib::error(dlib_o_out.str());                                 \
    }}while(0)

namespace test
{
    class tester;
    typedef dlib::map<std::string,tester*>::kernel_1a_c map_of_testers;

    map_of_testers& testers (
    );

// -----------------------------------------------------------------------------

    void check_test (
        bool _exp,
        long line,
        const char* file,
        const char* _exp_str
    );
    
// -----------------------------------------------------------------------------

// This bool controls any cout statements in this program.  Only print to 
// standard out if we should be verbose.  The default is true
    extern bool be_verbose;

// -----------------------------------------------------------------------------

    dlib::uint64 number_of_testing_statements_executed (
    );
    /*!
        ensures
            - returns the total number of DLIB_TEST and DLIB_TEST_MSG
              statements executed since program startup.
    !*/

    void increment_test_count (
    );
    /*!
        ensures
            - increments number_of_testing_statements_executed()
    !*/

// -----------------------------------------------------------------------------

    void print_spinner (
    );
    /*!
        ensures
            - reprints the spinner
    !*/

// -----------------------------------------------------------------------------

    class tester
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object represents a generic regression test.
        !*/

    public:

        tester (
            const std::string& switch_name,
            const std::string& description_,
            unsigned long num_of_args_ = 0
        );
        /*!
            requires
                - testers().is_in_domain(switch_name) == false
            ensures
                - #cmd_line_switch() == switch_name
                - #description() == description_
                - #num_of_args() == num_of_args_
                - adds this tester to the testers() map.
        !*/

        virtual ~tester (
        ){}

        const std::string& cmd_line_switch (
        ) const;
        /*!
            ensures
                - returns the name of the command line switch for this tester.
        !*/

        const std::string& description (
        ) const;
        /*!
            ensures
                - returns the description of what this tester tests.
        !*/

        unsigned long num_of_args (
        ) const;
        /*!
            ensures
                - returns the number of arguments this test expects
        !*/

        virtual void perform_test (
        );
        /*!
            requires
                - is invoked when number_of_args() == 0
            ensures
                - performs the test and throws an exception 
                  derived from std::exception if the test fails.
        !*/

        virtual void perform_test (
            const std::string& arg 
        );
        /*!
            requires
                - is invoked when number_of_args() == 1
            ensures
                - performs the test and throws an exception 
                  derived from std::exception if the test fails.
        !*/

        virtual void perform_test (
            const std::string& arg1, 
            const std::string& arg2 
        );
        /*!
            requires
                - is invoked when number_of_args() == 2
            ensures
                - performs the test and throws an exception 
                  derived from std::exception if the test fails.
        !*/

    private:

    // ---------------------------------------------------------------------------
    //             Implementation Details
    // ---------------------------------------------------------------------------

        /*!
            CONVENTION
                - switch_name == cmd_line_switch()
                - description_ == description()
                - num_of_args_ == num_of_args()
                - test::tester[switch_name] == this
        !*/

        const std::string switch_name;
        const std::string description_;
        const unsigned long num_of_args_;
    };

}

#endif // DLIB_TESTEr_