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

#include "tester.h"
#include <dlib/svm.h>
#include <vector>
#include <sstream>

namespace  
{
    using namespace test;
    using namespace dlib;
    using namespace std;
    dlib::logger dlog("test.is_same_object");

    DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_booya_template, void, template booya<int>, (std::string)const);
    DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_booya2_template, void, template booya2<int>, (int)const);
    DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_funct_int, void, funct, (int));
    DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_funct_double, void, funct, (double));
    DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_funct_f, float, funct_f, (int));

    class htest
    {
    public:
        template <typename EXP>
        void booya(std::string) const {}

        template <typename EXP>
        void booya2(EXP) const {}

        void funct(double) {}
    };

    class htest2
    {
    public:

        void funct(int) {}

        float funct_f(int) { return 0;}
    };

    void test_metaprog()
    {
        DLIB_TEST(has_booya2_template<htest>::value  == true);
        DLIB_TEST(has_booya2_template<htest2>::value == false);

#if _MSC_VER > 1600 // there is a bug in visual studio 2010 and older that prevents this test from working
        DLIB_TEST(has_booya_template<htest>::value  == true);
#endif

        DLIB_TEST(has_booya_template<htest2>::value == false);

        DLIB_TEST(has_funct_int<htest>::value  == false);
        DLIB_TEST(has_funct_int<htest2>::value == true);
        DLIB_TEST(has_funct_double<htest>::value  == true);
        DLIB_TEST(has_funct_double<htest2>::value == false);

        DLIB_TEST(has_funct_f<htest>::value  == false);
        DLIB_TEST(has_funct_f<htest2>::value == true);
    }

    class is_same_object_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:
        is_same_object_tester (
        ) :
            tester (
                "test_is_same_object",       // the command line argument name for this test
                "Run tests on the is_same_object function.", // the command line argument description
                0                     // the number of command line arguments for this test
            )
        {
        }

        struct base {};
        struct derived : public base {};

        template <bool truth>
        void go(const base& a, const base& b)
        {
            DLIB_TEST( is_same_object(a,b) == truth) ;
            DLIB_TEST( is_same_object(b,a) == truth) ;
        }


        template <bool truth>
        void go2(const base& a, const derived& b)
        {
            DLIB_TEST( is_same_object(a,b) == truth) ;
            DLIB_TEST( is_same_object(b,a) == truth) ;
        }


        void perform_test (
        )
        {
            print_spinner();

            int a, b;
            double d;
            DLIB_TEST( is_same_object(a,a) == true) ;
            DLIB_TEST( is_same_object(a,b) == false) ;
            DLIB_TEST( is_same_object(d,b) == false) ;
            DLIB_TEST( is_same_object(d,d) == true) ;

            base sb;
            derived sd, sd2;

            DLIB_TEST( is_same_object(sb,sd) == false) ;
            DLIB_TEST( is_same_object(sd,sb) == false) ;

            go<true>(sd, sd);
            go<false>(sd, sd2);
            go<true>(sb, sb);
            go<false>(sd, sb);

            go2<true>(sd, sd);
            go2<false>(sd2, sd);
            go2<false>(sd, sd2);
            go2<false>(sb, sd);

            test_metaprog();
        }
    };

    // 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. 
    is_same_object_tester a;

}