1 // Copyright (C) 2010 Davis E. King (davis@dlib.net) 2 // License: Boost Software License See LICENSE.txt for the full license. 3 4 #include "tester.h" 5 #include <dlib/svm.h> 6 #include <vector> 7 #include <sstream> 8 9 namespace 10 { 11 using namespace test; 12 using namespace dlib; 13 using namespace std; 14 dlib::logger dlog("test.is_same_object"); 15 16 DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_booya_template, void, template booya<int>, (std::string)const) 17 DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_booya2_template, void, template booya2<int>, (int)const) 18 DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_funct_int, void, funct, (int)) 19 DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_funct_double, void, funct, (double)) 20 DLIB_MAKE_HAS_MEMBER_FUNCTION_TEST(has_funct_f, float, funct_f, (int)) 21 22 class htest 23 { 24 public: 25 template <typename EXP> booya(std::string) const26 void booya(std::string) const {} 27 28 template <typename EXP> booya2(EXP) const29 void booya2(EXP) const {} 30 funct(double)31 void funct(double) {} 32 }; 33 34 class htest2 35 { 36 public: 37 funct(int)38 void funct(int) {} 39 funct_f(int)40 float funct_f(int) { return 0;} 41 }; 42 test_metaprog()43 void test_metaprog() 44 { 45 DLIB_TEST(has_booya2_template<htest>::value == true); 46 DLIB_TEST(has_booya2_template<htest2>::value == false); 47 48 #if _MSC_VER > 1600 // there is a bug in visual studio 2010 and older that prevents this test from working 49 DLIB_TEST(has_booya_template<htest>::value == true); 50 #endif 51 52 DLIB_TEST(has_booya_template<htest2>::value == false); 53 54 DLIB_TEST(has_funct_int<htest>::value == false); 55 DLIB_TEST(has_funct_int<htest2>::value == true); 56 DLIB_TEST(has_funct_double<htest>::value == true); 57 DLIB_TEST(has_funct_double<htest2>::value == false); 58 59 DLIB_TEST(has_funct_f<htest>::value == false); 60 DLIB_TEST(has_funct_f<htest2>::value == true); 61 } 62 63 class is_same_object_tester : public tester 64 { 65 /*! 66 WHAT THIS OBJECT REPRESENTS 67 This object represents a unit test. When it is constructed 68 it adds itself into the testing framework. 69 !*/ 70 public: is_same_object_tester()71 is_same_object_tester ( 72 ) : 73 tester ( 74 "test_is_same_object", // the command line argument name for this test 75 "Run tests on the is_same_object function.", // the command line argument description 76 0 // the number of command line arguments for this test 77 ) 78 { 79 } 80 81 struct base {}; 82 struct derived : public base {}; 83 84 template <bool truth> go(const base & a,const base & b)85 void go(const base& a, const base& b) 86 { 87 DLIB_TEST( is_same_object(a,b) == truth) ; 88 DLIB_TEST( is_same_object(b,a) == truth) ; 89 } 90 91 92 template <bool truth> go2(const base & a,const derived & b)93 void go2(const base& a, const derived& b) 94 { 95 DLIB_TEST( is_same_object(a,b) == truth) ; 96 DLIB_TEST( is_same_object(b,a) == truth) ; 97 } 98 99 perform_test()100 void perform_test ( 101 ) 102 { 103 print_spinner(); 104 105 int a, b; 106 double d; 107 DLIB_TEST( is_same_object(a,a) == true) ; 108 DLIB_TEST( is_same_object(a,b) == false) ; 109 DLIB_TEST( is_same_object(d,b) == false) ; 110 DLIB_TEST( is_same_object(d,d) == true) ; 111 112 base sb; 113 derived sd, sd2; 114 115 DLIB_TEST( is_same_object(sb,sd) == false) ; 116 DLIB_TEST( is_same_object(sd,sb) == false) ; 117 118 go<true>(sd, sd); 119 go<false>(sd, sd2); 120 go<true>(sb, sb); 121 go<false>(sd, sb); 122 123 go2<true>(sd, sd); 124 go2<false>(sd2, sd); 125 go2<false>(sd, sd2); 126 go2<false>(sb, sd); 127 128 test_metaprog(); 129 } 130 }; 131 132 // Create an instance of this object. Doing this causes this test 133 // to be automatically inserted into the testing framework whenever this cpp file 134 // is linked into the project. Note that since we are inside an unnamed-namespace 135 // we won't get any linker errors about the symbol a being defined multiple times. 136 is_same_object_tester a; 137 138 } 139 140 141 142