versadac  1
versadac - Scalable Recorder Firmware
UnitTest.h
1 #ifndef _TEST_SUITE_H_
2 #define _TEST_SUITE_H_
3 
4 #include <string>
5 #include <sstream>
6 #include <cstring>
7 
8 #ifdef __GNUC__
9  #define TEST_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100)
10  #if (TEST_GCC_VERSION >= 29600)
11  #define test_likely(x) __builtin_expect((long)((bool)(x)),1)
12  #define test_unlikely(x) __builtin_expect((long)((bool)(x)),0)
13  #else
14  #define test_likely(x) x
15  #define test_unlikely(x) x
16  #endif
17 #else
18  #define test_likely(x) x
19  #define test_unlikely(x) x
20 #endif
21 
22 #include <limits>
23 
24 template <typename T>
26  unittest_numUnion(T _v) : val(_v){}
27  T val;
28  unsigned char c[sizeof(T)];
29 };
30 
31 template<typename T>
32 static bool unittest_isNAN(T num){
33  unittest_numUnion<T> orig(num);
34 
35  static unittest_numUnion<T> sig_nan(std::numeric_limits<T>::signaling_NaN());
36 
37  bool isNAN = true;
38  for(size_t i = 0; i < sizeof(T); ++i){
39  if (orig.c[i] != sig_nan.c[i]){
40  isNAN = false;
41  break;
42  }
43  }
44  if (isNAN) return true;
45 
46  static unittest_numUnion<T> quiet_nan(std::numeric_limits<T>::quiet_NaN());
47 
48  for(size_t i = 0; i < sizeof(T); ++i){
49  if (orig.c[i] != quiet_nan.c[i]){
50  return false;
51  }
52  }
53  return true;
54 }
55 
56 class UnitTest {
57 public:
58  static void SelfCheck(void);
59  static void PushFailure(const std::string & fail);
60  static void PushSuccess(const std::string & pass);
61  static void echo_(const std::string & out);
62  static std::string ToString(void);
63  static std::string ToHTML(void);
64  static void SaveTo(const std::string & location);
65  static void SetReturnOnFail(bool option);
66  static bool GetReturnOnFail(void);
67  static void SetEcho(bool option);
68  static void SetPrefix(const std::string & prefix);
69  static std::string GetPrefix(void);
70  static void StartTime(void);
71  static inline bool _floatsAreEqual(const double & one, const double & two){
72  return (one > two) ? (one - two) < .000001 : (one - two) > -.000001;
73  }
74 };
75 
76 #define MakePre()\
77  std::string pre = UnitTest::GetPrefix();\
78  if (test_unlikely(pre.empty())){\
79  std::stringstream out;\
80  out << __FILE__ << ":" << __LINE__;\
81  pre = out.str();\
82  }\
83  pre += ": ";
84 
85 #define FAIL(stri)\
86  MakePre()\
87  UnitTest::PushFailure(pre + std::string(stri));\
88  if (UnitTest::GetReturnOnFail()) return;
89 
90 #define PASS(stri)\
91  MakePre();\
92  UnitTest::PushSuccess(pre + std::string(stri));\
93 
94 #define assertUnitTest()\
95  UnitTest::SelfCheck();
96 
97 #define assertTrue(cond)\
98  if (test_unlikely(!(cond))){\
99  FAIL(#cond);\
100  } else {\
101  PASS(#cond);\
102  }
103 
104 #define assertFalse(cond)\
105  if (test_unlikely(cond)){\
106  FAIL(#cond);\
107  } else {\
108  PASS(#cond);\
109  }
110 
111 #define assertTrue_Primitive(cond, leftside, rightside)\
112  if (test_unlikely(!(cond))){\
113  std::stringstream unit_out;\
114  unit_out << #cond;\
115  unit_out << ", Left side: " << leftside;\
116  unit_out << ", Right side: " << rightside;\
117  FAIL(unit_out.str());\
118  } else {\
119  PASS(#cond);\
120  }
121 
122 //needs to copy it so that if its a function call it only does it once
123 #define assertNAN(type, one)\
124  {\
125  type val = (type)one;\
126  std::string lag(#one);\
127  lag += " not a number";\
128  if (test_likely(unittest_isNAN<type>(one))){\
129  PASS(lag)\
130  } else {\
131  FAIL(lag)\
132  }\
133  }
134 
135 #define assertFloatEquals(one, two)\
136  assertTrue(UnitTest::_floatsAreEqual(one, two))
137 
138 #define assertEquals(one, two)\
139  assertTrue((one) == (two))
140 
141 #define assertNotEquals(one, two)\
142  assertTrue((one) != (two))
143 
144 #define assertGreaterThan(one, two)\
145  assertTrue((one) > (two))
146 
147 #define assertGreaterThanEqualTo(one, two)\
148  assertTrue((one) >= (two))
149 
150 #define assertLessThan(one, two)\
151  assertTrue((one) < (two))
152 
153 #define assertLessThanEqualTo(one, two)\
154  assertTrue((one) <= (two))
155 
156 
157 
158 #define assertEquals_Primitive(one, two)\
159  assertTrue_Primitive((one) == (two), one, two)
160 
161 #define assertNotEquals_Primitive(one, two)\
162  assertTrue_Primitive((one) != (two), one, two)
163 
164 #define assertGreaterThan_Primitive(one, two)\
165  assertTrue_Primitive((one) > (two), one, two)
166 
167 #define assertGreaterThanEqualTo_Primitive(one, two)\
168  assertTrue_Primitive((one) >= (two), one, two)
169 
170 #define assertLessThan_Primitive(one, two)\
171  assertTrue_Primitive((one) < (two), one, two)
172 
173 #define assertLessThanEqualTo_Primitive(one, two)\
174  assertTrue_Primitive((one) <= (two), one, two)
175 
176 #define assertNull(one)\
177  assertTrue(one == NULL);
178 
179 #define assertNotNull(one)\
180  assertTrue(one != NULL);
181 
182 #define assertCStringEquals(one, two)\
183  if (test_unlikely(strcmp(one, two))){\
184  FAIL(std::string(#one) + "==" + #two);\
185  } else {\
186  PASS(std::string(#one) + "==" + #two);\
187  }
188 
189 #define assertCStringNotEquals(one, two)\
190  if (test_unlikely(!strcmp(one, two))){\
191  FAIL(std::string(#one) + "!=" + #two);\
192  } else {\
193  PASS(std::string(#one) + "!=" + #two);\
194  }
195 
196 #define assertCStringEqualsW(one, two)\
197  if (test_unlikely(wcscmp(one, two))){\
198  FAIL(std::string(#one) + "==" + #two);\
199  } else {\
200  PASS(std::string(#one) + "==" + #two);\
201  }
202 
203 #define assertCStringNotEqualsW(one, two)\
204  if (test_unlikely(!wcscmp(one, two))){\
205  FAIL(std::string(#one) + "!=" + #two);\
206  } else {\
207  PASS(std::string(#one) + "!=" + #two);\
208  }
209 
210 #define assertException(code, exc)\
211  {\
212  bool failed = false;\
213  try {\
214  code;\
215  } catch (exc){\
216  PASS(std::string(#exc) + " caught");\
217  failed = true;\
218  }\
219  if (test_unlikely(!failed)){ FAIL(std::string(#exc) + " not caught");}\
220  }
221 
222 #define echo(something)\
223  {\
224  std::stringstream somet;\
225  somet << something;\
226  UnitTest::echo_(somet.str());\
227  }
228 
229 #endif
Definition: UnitTest.h:56
Definition: UnitTest.h:25