testlib_test.h
Go to the documentation of this file.
1 // This is core/testlib/testlib_test.h
2 #ifndef testlib_test_h_
3 #define testlib_test_h_
4 //:
5 // \file
6 // \brief Testing software
7 // \author Tim Cootes
8 // \verbatim
9 // Modifications
10 // Apr 2002, Amitha Perera: Copied from vil_test and moved into testlib in an
11 // attempt to consolidate all the repeated test functionality.
12 // Sep.2004, Peter Vanroose: added testlib_test_assert_near_relative().
13 // \endverbatim
14 
15 #include <string>
16 #include <complex>
17 
18 //: initialise test counters, check test name 'name' exists
19 void testlib_test_start(const char* name = nullptr);
20 //: increment number of tests, then output msg
21 void testlib_test_begin(const char* msg);
22 //: increment success/failure counters
23 void testlib_test_perform(bool success);
24 //: output summary of tests performed
26 
27 //: output msg, then perform test in expr
28 void testlib_test_assert(const std::string& msg, bool expr);
29 //: output msg, then perform test to see if expr is within tol of target
30 void testlib_test_assert_near(const std::string& msg, double expr,
31  double target = 0, double tol = 1e-12);
32 //: output msg, then perform test to see if expr is within tol of target
33 void testlib_test_assert_near(const std::string& msg, std::complex<double> expr,
34  std::complex<double> target, double tol = 1e-12);
35 //: output msg, then test to see if expr is within relative tol of target
36 void testlib_test_assert_near_relative(const std::string& msg, double expr,
37  double target = 0, double tol = 1e-12);
38 //: output msg, then test to see if expr is within relative tol of target
39 void testlib_test_assert_near_relative(const std::string& msg,
40  std::complex<double> expr,
41  std::complex<double> target,
42  double tol = 1e-12);
43 //: output msg, then perform test to see if expr is not within tol of target
44 void testlib_test_assert_far(const std::string& msg, double expr,
45  double target = 0, double tol = 1e-12);
46 //: output msg, then perform test to see if expr is not within tol of target
47 void testlib_test_assert_far(const std::string& msg, std::complex<double> expr,
48  std::complex<double> target, double tol = 1e-12);
49 //: output msg, then perform test to see if expr is equal to target
50 void testlib_test_assert_equal(const std::string& msg, long expr, long target);
51 
52 #define Assert testlib_test_assert
53 #define AssertNear testlib_test_assert_near
54 #define AssertFar testlib_test_assert_far
55 
56 //: initialise test
57 #define START(s) testlib_test_start(s)
58 
59 //: TEST function, s is message, test to see if p==v
60 #define TEST(s,p,v) \
61 do { \
62  testlib_test_begin(s); \
63  testlib_test_perform((p)==(v)); \
64 } while (false)
65 
66 //: TEST function, s is message, test to see if p==v for integral values
67 #define TEST_EQUAL(s,p,v) \
68 do { \
69  testlib_test_begin(s); \
70  testlib_test_assert_equal("",p,v); \
71 } while (false)
72 
73 //: TEST function, s is message, test to see if p is close to v, tolerance t
74 #define TEST_NEAR(s,p,v,t) \
75 do { \
76  testlib_test_begin(s); \
77  testlib_test_assert_near("",p,v,t); \
78 } while (false)
79 
80 //: TEST function, message s, test to see if (p-v)/p is small compared to t
81 #define TEST_NEAR_REL(s,p,v,t) \
82 do { \
83  testlib_test_begin(s); \
84  testlib_test_assert_near_relative("",p,v,t); \
85 } while (false)
86 
87 //: TEST function, s is message, test to see if p is far from v, tolerance t
88 #define TEST_FAR(s,p,v,t) \
89 do { \
90  testlib_test_begin(s); \
91  testlib_test_assert_far("",p,v,t); \
92 } while (false)
93 
94 //: run x, s is message, then test to see if p==v
95 #define TEST_RUN(s,x,p,v) \
96 do { \
97  testlib_test_begin(s); \
98  x; \
99  testlib_test_perform((p)==(v)); \
100 } while (false)
101 
102 //: Summarise test
103 #define SUMMARY() return testlib_test_summary()
104 
105 //: Run a singleton test function
106 #define RUN_TEST_FUNC(x) \
107  testlib_test_start(#x); x(); return testlib_test_summary()
108 
109 //: Declare the main function.
110 #define MAIN( testname ) \
111  int testname ## _main(int,char*[])
112 
113 //: Declare the main function with parameter passing.
114 #define MAIN_ARGS( testname ) \
115  int testname ## _main(int argc, char* argv[])
116 
117 //: A simplified version of the main test, just in one line.
118 // Avoids compiler warnings about "unused argc and argv".
119 #define TESTMAIN( testname ) \
120  int testname ## _main(int,char*[]) { START(#testname); testname(); SUMMARY(); }
121 
122 //: A simplified version of the main test, just in one line.
123 // This (new) variant is to be used with the (new) CMake GENERATE_TEST_DRIVER()
124 #define TEST_MAIN( testname ) \
125  int testname(int,char*[]) { START(#testname); testname(); SUMMARY(); }
126 
127 //: A simplified version of the main test, with parameter passing.
128 #define TESTMAIN_ARGS( testname ) \
129  int testname ## _main(int argc, char*argv[]) { START(#testname); testname(argc,argv); SUMMARY(); }
130 
131 //: A simplified version of the main test, with parameter passing.
132 // This (new) variant is to be used with the (new) CMake GENERATE_TEST_DRIVER()
133 #define TEST_MAIN_ARGS( testname ) \
134  int testname(int argc, char*argv[]) { START(#testname); testname(argc,argv); SUMMARY(); }
135 
136 //: Another simplified main test. To be used in a standalone executable.
137 #undef TESTLIB_DEFINE_MAIN
138 #define TESTLIB_DEFINE_MAIN(testname) \
139  int main() { START(#testname); testname(); return testlib_test_summary(); }
140 
141 //: A simplified main test with parameter passing. To be used in a standalone executable.
142 #undef TESTLIB_DEFINE_MAIN_ARGS
143 #define TESTLIB_DEFINE_MAIN_ARGS(testname) \
144  int main(int argc, char * argv[]) { START(#testname); testname(argc,argv); SUMMARY(); }
145 
146 #endif // testlib_test_h_
void testlib_test_begin(const char *msg)
increment number of tests, then output msg.
void testlib_test_start(const char *name=nullptr)
initialise test counters, check test name 'name' exists.
void testlib_test_assert(const std::string &msg, bool expr)
output msg, then perform test in expr.
void testlib_test_assert_near_relative(const std::string &msg, double expr, double target=0, double tol=1e-12)
output msg, then test to see if expr is within relative tol of target.
void testlib_test_assert_far(const std::string &msg, double expr, double target=0, double tol=1e-12)
output msg, then perform test to see if expr is not within tol of target.
int testlib_test_summary()
output summary of tests performed.
void testlib_test_perform(bool success)
increment success/failure counters.
void testlib_test_assert_near(const std::string &msg, double expr, double target=0, double tol=1e-12)
output msg, then perform test to see if expr is within tol of target.
void testlib_test_assert_equal(const std::string &msg, long expr, long target)
output msg, then perform test to see if expr is equal to target.