KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
l_test.h
Go to the documentation of this file.
1 /* ======================================================================== *
2  | |
3  | Copyright (c) 2010, by members of University of Arizona Computer Vision |
4  | group (the authors) including |
5  | Kyle Simek, Andrew Predoehl |
6  | |
7  | For use outside the University of Arizona Computer Vision group please |
8  | contact Kobus Barnard. |
9  | |
10  * ======================================================================== */
11 
12 /* $Id: test.h 5908 2010-05-21 23:31:54Z ksimek $ */
13 
14 #ifndef TEST_H
15 #define TEST_H
16 
17 #include <l/l_sys_lib.h>
18 #include <l_cpp/l_exception.h>
19 
20 //#define DB(x) std::cout << #x << ": \n" << x << std::endl;
21 
22 void down_in_flames(
23  const char* test_kind,
24  const char* filename,
25  int line_num,
26  const char* bad_line
27 );
28 
29 void print_victory(const char*);
30 
31 /* The "do STUFF while(0)" pattern provides a few benefits:
32  * 1. provides a closed scope for any local variable(s);
33  * 2. wraps up an "if" so that a stray "else" cannot bind to it; and
34  * 3. makes the action a single grammatical statement, thus it forces one to
35  * use (correctly) a semicolon after the macro invocation --
36  * the idea being that we don't want to mask errors, even small ones.
37  */
38 
39 #define DOWN_IN_FLAMES(tst,src) \
40  down_in_flames( (tst), __FILE__, __LINE__, (src) )
41 
42 #define RETURN_VICTORIOUSLY() \
43  do { print_victory(__FILE__); \
44  return EXIT_SUCCESS; \
45  } while( 0 )
46 
47 #define TEST_TRUE(line) \
48  do if (!(line)) { DOWN_IN_FLAMES( "TEST_TRUE", #line ); } while( 0 )
49 
50 #define TEST_FALSE(line) \
51  do if ( line ) { DOWN_IN_FLAMES( "TEST_FALSE", #line ); } while( 0 )
52 
53 #define TEST_SUCCESS(line) \
54  do { \
55  try { line; } \
56  catch(...) \
57  { \
58  DOWN_IN_FLAMES( "TEST_SUCCESS", #line ); \
59  } \
60  } while ( 0 )
61 
62 
63 #define TEST_APPROX_EQUALITY(x,y) \
64  do { \
65  const double a(x), b(y); \
66  if(!(a==b || fabs(a-b) <= (fabs(a)+fabs(b))*0.005 )) \
67  { \
68  DOWN_IN_FLAMES( "TEST_APPROX_EQUALITY", #x " approx == " #y ); \
69  } \
70  } while(0)
71 
72 
73 #define TEST_APPROX_ZERO(z) TEST_ZERO_WITH_TOLERANCE(z, 1e-10)
74 
75 
76 #define TEST_ZERO_WITH_TOLERANCE(z, t) \
77  do { \
78  if (!(fabs(z) <= (t))) \
79  { \
80  DOWN_IN_FLAMES( "TEST_ZERO_WITH_TOLERANCE", "fabs(" #z ")<=" #t );\
81  } \
82  } while(0)
83 
84 
85 #define TEST_FAIL(line) \
86  do { \
87  bool assert_fail_failed = false; \
88  try { line; } \
89  catch( kjb::Index_out_of_bounds& iob ) \
90  { \
91  /* std::cout <<"Correctly caught: "; */ \
92  /* iob.print( std::cout ); */ \
93  assert_fail_failed = true; \
94  } \
95  catch(...) \
96  { \
97  assert_fail_failed = true; \
98  } \
99  if (!assert_fail_failed) \
100  { \
101  DOWN_IN_FLAMES( "TEST_FAIL", #line ); \
102  } \
103  } while( 0 )
104 
105 #endif
void print_victory(const char *)
Definition: l_test.cpp:39
void down_in_flames(const char *test_kind, const char *filename, int line_num, const char *bad_line)
Definition: l_test.cpp:26
Support for error handling exception classes in libKJB.