core/testlib/testlib_test.h
Go to the documentation of this file.
00001 // This is core/testlib/testlib_test.h
00002 #ifndef testlib_test_h_
00003 #define testlib_test_h_
00004 //:
00005 // \file
00006 // \brief Testing software
00007 // \author Tim Cootes
00008 // \verbatim
00009 //  Modifications
00010 //   Apr 2002, Amitha Perera: Copied from vil_test and moved into testlib in an
00011 //                  attempt to consolidate all the repeated test functionality.
00012 //   Sep.2004, Peter Vanroose: added testlib_test_assert_near_relative().
00013 // \endverbatim
00014 
00015 #include <vcl_string.h>
00016 #include <vcl_complex.h>
00017 
00018 //: initialise test counters, check test name 'name' exists
00019 void testlib_test_start(const char* name = 0);
00020 //: increment number of tests, then output msg
00021 void testlib_test_begin(const char* msg);
00022 //: increment success/failure counters
00023 void testlib_test_perform(bool success);
00024 //: output summary of tests performed
00025 int  testlib_test_summary();
00026 
00027 //: output msg, then perform test in expr
00028 void testlib_test_assert(const vcl_string& msg, bool expr);
00029 //: output msg, then perform test to see if expr is within tol of target
00030 void testlib_test_assert_near(const vcl_string& msg, double expr,
00031                               double target = 0, double tol = 1e-12);
00032 //: output msg, then perform test to see if expr is within tol of target
00033 void testlib_test_assert_near(const vcl_string& msg, vcl_complex<double> expr,
00034                               vcl_complex<double> target, double tol = 1e-12);
00035 //: output msg, then test to see if expr is within relative tol of target
00036 void testlib_test_assert_near_relative(const vcl_string& msg, double expr,
00037                                        double target = 0, double tol = 1e-12);
00038 //: output msg, then test to see if expr is within relative tol of target
00039 void testlib_test_assert_near_relative(const vcl_string& msg,
00040                                        vcl_complex<double> expr,
00041                                        vcl_complex<double> target,
00042                                        double tol = 1e-12);
00043 //: output msg, then perform test to see if expr is not within tol of target
00044 void testlib_test_assert_far(const vcl_string& msg, double expr,
00045                              double target = 0, double tol = 1e-12);
00046 //: output msg, then perform test to see if expr is not within tol of target
00047 void testlib_test_assert_far(const vcl_string& msg, vcl_complex<double> expr,
00048                              vcl_complex<double> target, double tol = 1e-12);
00049 //: output msg, then perform test to see if expr is equal to target
00050 void testlib_test_assert_equal(const vcl_string& msg, long expr, long target);
00051 
00052 #define Assert testlib_test_assert
00053 #define AssertNear testlib_test_assert_near
00054 #define AssertFar testlib_test_assert_far
00055 
00056 //: initialise test
00057 #define START(s) testlib_test_start(s)
00058 
00059 //: TEST function, s is message, test to see if p==v
00060 #define TEST(s,p,v) \
00061 do { \
00062   testlib_test_begin(s); \
00063   testlib_test_perform((p)==(v)); \
00064 } while (false)
00065 
00066 //: TEST function, s is message, test to see if p==v for integral values
00067 #define TEST_EQUAL(s,p,v) \
00068 do { \
00069   testlib_test_begin(s); \
00070   testlib_test_assert_equal("",p,v); \
00071 } while (false)
00072 
00073 //: TEST function, s is message, test to see if p is close to v, tolerance t
00074 #define TEST_NEAR(s,p,v,t) \
00075 do { \
00076   testlib_test_begin(s); \
00077   testlib_test_assert_near("",p,v,t); \
00078 } while (false)
00079 
00080 //: TEST function, message s, test to see if (p-v)/p is small compared to t
00081 #define TEST_NEAR_REL(s,p,v,t) \
00082 do { \
00083   testlib_test_begin(s); \
00084   testlib_test_assert_near_relative("",p,v,t); \
00085 } while (false)
00086 
00087 //: TEST function, s is message, test to see if p is far from v, tolerance t
00088 #define TEST_FAR(s,p,v,t) \
00089 do { \
00090   testlib_test_begin(s); \
00091   testlib_test_assert_far("",p,v,t); \
00092 } while (false)
00093 
00094 //: run x, s is message, then test to see if p==v
00095 #define TEST_RUN(s,x,p,v) \
00096 do { \
00097   testlib_test_begin(s); \
00098   x; \
00099   testlib_test_perform((p)==(v)); \
00100 } while (false)
00101 
00102 //: Summarise test
00103 #define SUMMARY() return testlib_test_summary()
00104 
00105 //: Run a singleton test function
00106 #define RUN_TEST_FUNC(x) \
00107   testlib_test_start(#x); x(); return testlib_test_summary()
00108 
00109 //: Declare the main function.
00110 #define MAIN( testname ) \
00111   int testname ## _main(int,char*[])
00112 
00113 //: Declare the main function with parameter passing.
00114 #define MAIN_ARGS( testname ) \
00115   int testname ## _main(int argc, char* argv[])
00116 
00117 //: A simplified version of the main test, just in one line.
00118 // Avoids compiler warnings about "unused argc and argv".
00119 #define TESTMAIN( testname ) \
00120   int testname ## _main(int,char*[]) { START(#testname); testname(); SUMMARY(); }
00121 
00122 //: A simplified version of the main test, just in one line.
00123 // This (new) variant is to be used with the (new) CMake GENERATE_TEST_DRIVER()
00124 #define TEST_MAIN( testname ) \
00125   int testname(int,char*[]) { START(#testname); testname(); SUMMARY(); }
00126 
00127 //: A simplified version of the main test, with parameter passing.
00128 #define TESTMAIN_ARGS( testname ) \
00129   int testname ## _main(int argc, char*argv[]) { START(#testname); testname(argc,argv); SUMMARY(); }
00130 
00131 //: A simplified version of the main test, with parameter passing.
00132 // This (new) variant is to be used with the (new) CMake GENERATE_TEST_DRIVER()
00133 #define TEST_MAIN_ARGS( testname ) \
00134   int testname(int argc, char*argv[]) { START(#testname); testname(argc,argv); SUMMARY(); }
00135 
00136 //: Another simplified main test.  To be used in a standalone executable.
00137 #undef TESTLIB_DEFINE_MAIN
00138 #define TESTLIB_DEFINE_MAIN(testname) \
00139   int main() { START(#testname); testname(); return testlib_test_summary(); }
00140 
00141 //: A simplified main test with parameter passing.  To be used in a standalone executable.
00142 #undef TESTLIB_DEFINE_MAIN_ARGS
00143 #define TESTLIB_DEFINE_MAIN_ARGS(testname) \
00144   int main(int argc, char * argv[]) { START(#testname); testname(argc,argv); SUMMARY(); }
00145 
00146 #endif // testlib_test_h_