summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/test/example.cpp
blob: 4cf927159f77a3b7807e1a140c6304fe7b2742d0 (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
// Copyright (C) 2008  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.

#include "tester.h"

// This is called an unnamed-namespace and it has the effect of making everything 
// inside this file "private" so that everything you declare will have static linkage.  
// Thus we won't have any multiply defined symbol errors coming out of the linker when 
// we try to compile the test suite.
namespace  
{
    using namespace test;
    // Declare the logger we will use in this test.  The name of the logger 
    // should start with "test."
    dlib::logger dlog("test.example");


    class example_tester : public tester
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This object represents a unit test.  When it is constructed
                it adds itself into the testing framework.
        !*/
    public:
        example_tester (
        ) :
            tester (
                "test_example",       // the command line argument name for this test
                "Run example tests.", // the command line argument description
                0                     // the number of command line arguments for this test
            )
        {}

        void perform_test (
        )
        {
            // This message gets logged to the file debug.txt if the user has enabled logging by
            // supplying the -d option on the command line (and they haven't set the logging level
            // to something higher than LINFO).
            dlog << dlib::LINFO << "some message you want to log";

            // This test is considered a success if this function doesn't throw an exception.  
            // So we can use the DLIB_TEST_MSG macro to perform our tests since it throws an 
            // exception containing a message if its first argument is false.  

            // make sure 3 is bigger than 2
            DLIB_TEST_MSG(3 > 2,"This message prints if your compiler doesn't know 3 is bigger than 2");

            // make sure 5 is not equal to 9
            DLIB_TEST_MSG(5 != 9,"This message prints if your compiler thinks 5 is the same as 9");

            // This is a form of test you can use when you don't care about having a message
            DLIB_TEST(5 != 8);

            // If your test takes a long time to run you can also call print_spinner() 
            // periodically.  This will cause a spinning / character to display on the
            // console to indicate to the user that your test is still running (rather
            // than hung) 
            print_spinner();
        }
    };

    // Create an instance of this object.  Doing this causes this test
    // to be automatically inserted into the testing framework whenever this cpp file
    // is linked into the project.  Note that since we are inside an unnamed-namespace 
    // we won't get any linker errors about the symbol a being defined multiple times. 
    example_tester a;

}