MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
testing_macros.hpp
Go to the documentation of this file.
1  /* ==============================================================================
2  *
3  * Filename: testing_macros.hpp
4  *
5  * Description: This file contains some macros useful in testing.
6  *
7  * Version: 1.0
8  * Created: Monday 19 May 2014 05:04:41 IST
9  * Revision: none
10  * Compiler: gcc
11  *
12  * Author: Dilawar Singh (), dilawars@ncbs.res.in
13  * Organization:
14  *
15  * ==============================================================================
16  */
17 
18 #ifndef TESTING_MACROS_INC
19 #define TESTING_MACROS_INC
20 
21 
22 #include <sstream>
23 #include <exception>
24 #include <iostream>
25 #include <stdexcept>
26 #include <limits>
27 #include <cmath>
28 
29 #include "current_function.hpp"
30 #include "print_function.hpp"
31 
32 using namespace std;
33 
34 inline bool doubleEq(double a, double b)
35 {
36  return std::fabs(a-b) < 1e-7;
37 }
38 
39 static ostringstream assertStream;
40 
41 #define LOCATION(ss) \
42  ss << "In function: " << SIMPLE_CURRENT_FUNCTION; \
43  ss << " file: " << __FILE__ << ":" << __LINE__ << endl;
44 
45 #define EXPECT_TRUE( condition, msg) \
46  if( !(condition) ) {\
47  assertStream.str(""); \
48  LOCATION( assertStream ); \
49  assertStream << msg << endl; \
50  moose::__dump__(assertStream.str(), moose::failed); \
51  }
52 
53 #define EXPECT_FALSE( condition, msg) \
54  if( (condition) ) {\
55  assertStream.str(""); \
56  LOCATION( assertStream ); \
57  assertStream << msg << endl; \
58  moose::__dump__(assertStream.str(), moose::failed); \
59  }
60 
61 #define EXPECT_EQ(a, b, token) \
62  if( (a) != (b)) { \
63  assertStream.str(""); \
64  LOCATION(assertStream) \
65  assertStream << "Expected " << b << ", received " << a ; \
66  assertStream << token; \
67  moose::__dump__(assertStream.str(), moose::failed); \
68  }
69 
70 #define EXPECT_NEQ(a, b, token) \
71  if( (a) == (b)) { \
72  assertStream.str(""); \
73  LOCATION(assertStream); \
74  assertStream << "Not expected " << a << endl; \
75  assertStream << token << endl; \
76  moose::__dump__(assertStream.str(), moose::failed); \
77  }
78 
79 #define EXPECT_GT(a, b, token) \
80  if( (a) <= (b)) { \
81  assertStream.str(""); \
82  LOCATION(assertStream); \
83  assertStream << "Expected greater than " << a << ", received " << b << endl; \
84  assertStream << token << endl; \
85  moose::__dump__(assertStream.str(), moose::failed); \
86  }
87 
88 #define EXPECT_GTE(a, b, token) \
89  if( (a) < (b)) { \
90  assertStream.str(""); \
91  LOCATION(assertStream); \
92  assertStream << "Expected greater than or equal to " << a \
93  << ", received " << b << endl; \
94  assertStream << token << endl; \
95  moose::__dump__(assertStream.str(), moose::failed); \
96  }
97 
98 #define EXPECT_LT(a, b, token) \
99  if( (a) >= (b)) { \
100  assertStream.str(""); \
101  LOCATION(assertStream); \
102  assertStream << "Expected less than " << a << ", received " << b << endl; \
103  assertStream << token << endl; \
104  moose::__dump__(assertStream.str(), moose::failed); \
105  }
106 
107 #define EXPECT_LTE(a, b, token) \
108  if( (a) < (b)) { \
109  assertStream.str(""); \
110  LOCATION(assertStream); \
111  assertStream << "Expected less than or equal to " << a \
112  << ", received " << b << endl; \
113  assertStream << token << endl; \
114  moose::__dump__(assertStream.str(), moose::failed); \
115  }
116 
117 #define ASSERT_TRUE( condition, msg) \
118  if( !(condition) ) {\
119  assertStream.str(""); \
120  assertStream << msg << endl; \
121  throw std::runtime_error( assertStream.str() );\
122  }
123 
124 #define ASSERT_FALSE( condition, msg) \
125  if( (condition) ) {\
126  assertStream.str(""); \
127  assertStream.precision( 9 ); \
128  assertStream << msg << endl; \
129  throw std::runtime_error(assertStream.str()); \
130  }
131 
132 #define ASSERT_LT( a, b, msg) \
133  EXPECT_LT(a, b, msg); \
134  assertStream.str(""); \
135  assertStream.precision( 9 ); \
136  assertStream << msg; \
137  throw std::runtime_error( assertStream.str() ); \
138 
139 #define ASSERT_EQ(a, b, token) \
140  if( ! doubleEq((a), (b)) ) { \
141  assertStream.str(""); \
142  assertStream.precision( 9 ); \
143  LOCATION(assertStream) \
144  assertStream << "Expected " << a << ", received " << b << endl; \
145  assertStream << token << endl; \
146  throw std::runtime_error(assertStream.str()); \
147  }
148 
149 #define ASSERT_DOUBLE_EQ(token, a, b) \
150  if(! doubleEq(a, b) ) { \
151  assertStream.str(""); \
152  LOCATION(assertStream); \
153  assertStream << "Expected " << std::fixed << b << ", received " << a << endl; \
154  assertStream << token; \
155  moose::__dump__(assertStream.str(), moose::failed); \
156  throw std::runtime_error( "float equality test failed" ); \
157  }
158 
159 #define ASSERT_NEQ(a, b, token) \
160  if( (a) == (b)) { \
161  assertStream.str(""); \
162  LOCATION(assertStream); \
163  assertStream << "Not expected " << a << endl; \
164  assertStream << token << endl; \
165  throw std::runtime_error(assertStream.str()); \
166  }
167 
168 
169 #endif /* ----- #ifndef TESTING_MACROS_INC ----- */
static ostringstream assertStream
bool doubleEq(double a, double b)