NetBurner 3.1
UtestMacros.h
1 /*
2  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef D_UTestMacros_h
29 #define D_UTestMacros_h
30 
31 #include "CppUTest/config.h"
41 #include "CppUTest/SimpleString.h"
42 extern SimpleString gSystemOut;
43 
44 
45 #define TEST_GROUP_BASE(testGroup, baseclass) \
46  extern int externTestGroup##testGroup; \
47  int externTestGroup##testGroup = 0; \
48  struct TEST_GROUP_##CppUTestGroup##testGroup : public baseclass
49 
50 #define TEST_BASE(testBaseClass) \
51  struct testBaseClass : public Utest
52 
53 #define TEST_GROUP(testGroup) \
54  TEST_GROUP_BASE(testGroup, Utest)
55 
56 #define TEST_SETUP() \
57  virtual void setup()
58 
59 #define TEST_TEARDOWN() \
60  virtual void teardown()
61 
62 #ifdef SB800EX
63 #define TEST(testGroup, testName) TEST_PLATFORM( testGroup, testName, SB800EX_ )
64 #elif defined MOD5441X
65 #define TEST(testGroup, testName) TEST_PLATFORM( testGroup, testName, MOD5441X_ )
66 #elif defined SB70LC
67 #define TEST(testGroup, testName) TEST_PLATFORM( testGroup, testName, SB70LC_ )
68 #elif defined MODM7AE70
69 #define TEST(testGroup, testName) TEST_PLATFORM( testGroup, testName, MODM7AE70_ )
70 #else
71 #define TEST(testGroup, testName) TEST_PLATFORM( testGroup, testName, UNKNOWN_ )
72 #endif
73 
74 #define TEST_PLATFORM(testGroup, testName, platform) \
75  /* External declarations for strict compilers */ \
76  class TEST_##platform####testGroup##_##testName##_TestShell; \
77  extern TEST_##platform####testGroup##_##testName##_TestShell TEST_##platform####testGroup##_##testName##_TestShell_instance; \
78  \
79  class TEST_##platform####testGroup##_##testName##_Test : public TEST_GROUP_##CppUTestGroup##testGroup \
80 { public: TEST_##platform####testGroup##_##testName##_Test () : TEST_GROUP_##CppUTestGroup##testGroup () {} \
81  void testBody(); }; \
82  class TEST_##platform####testGroup##_##testName##_TestShell : public UtestShell { \
83  virtual Utest* createTest() _override { return new TEST_##platform####testGroup##_##testName##_Test; } \
84  } TEST_##platform####testGroup##_##testName##_TestShell_instance; \
85  static TestInstaller TEST_##platform####testGroup##_##testName##_Installer(TEST_##platform####testGroup##_##testName##_TestShell_instance, #platform#testGroup, #testName, __FILE__,__LINE__); \
86  void TEST_##platform####testGroup##_##testName##_Test::testBody()
87 
88 #define IGNORE_TEST(testGroup, testName)\
89  /* External declarations for strict compilers */ \
90  class IGNORE##testGroup##_##testName##_TestShell; \
91  extern IGNORE##testGroup##_##testName##_TestShell IGNORE##testGroup##_##testName##_TestShell_instance; \
92  \
93  class IGNORE##testGroup##_##testName##_Test : public TEST_GROUP_##CppUTestGroup##testGroup \
94 { public: IGNORE##testGroup##_##testName##_Test () : TEST_GROUP_##CppUTestGroup##testGroup () {} \
95  public: void testBody (); }; \
96  class IGNORE##testGroup##_##testName##_TestShell : public IgnoredUtestShell { \
97  virtual Utest* createTest() _override { return new IGNORE##testGroup##_##testName##_Test; } \
98  } IGNORE##testGroup##_##testName##_TestShell_instance; \
99  static TestInstaller TEST_##testGroup##testName##_Installer(IGNORE##testGroup##_##testName##_TestShell_instance, #testGroup, #testName, __FILE__,__LINE__); \
100  void IGNORE##testGroup##_##testName##_Test::testBody ()
101 
102 #define IMPORT_TEST_GROUP(testGroup) \
103  extern int externTestGroup##testGroup;\
104  extern int* p##testGroup; \
105  int* p##testGroup = &externTestGroup##testGroup
106 
107 #define CPPUTEST_DEFAULT_MAIN \
108  /*#include <CppUTest/CommandLineTestRunner.h>*/ \
109  int main(int argc, char** argv) \
110  { \
111  return CommandLineTestRunner::RunAllTests(argc, argv); \
112  }
113 
114 
115 // Different checking macros
116 
117 #define CHECK(condition)\
118  CHECK_TRUE_LOCATION(condition, "CHECK", #condition, NULL, __FILE__, __LINE__)
119 
120 #define CHECK_TEXT(condition, text) \
121  CHECK_TRUE_LOCATION(condition, "CHECK", #condition, text, __FILE__, __LINE__)
122 
123 #define CHECK_TRUE(condition)\
124  CHECK_TRUE_LOCATION(condition, "CHECK_TRUE", #condition, NULL, __FILE__, __LINE__)
125 
126 #define CHECK_TRUE_TEXT(condition, text)\
127  CHECK_TRUE_LOCATION(condition, "CHECK_TRUE", #condition, text, __FILE__, __LINE__)
128 
129 #define CHECK_FALSE(condition)\
130  CHECK_FALSE_LOCATION(condition, "CHECK_FALSE", #condition, NULL, __FILE__, __LINE__)
131 
132 #define CHECK_FALSE_TEXT(condition, text)\
133  CHECK_FALSE_LOCATION(condition, "CHECK_FALSE", #condition, text, __FILE__, __LINE__)
134 
135 #define CHECK_TRUE_LOCATION(condition, checkString, conditionString, text, file, line)\
136  { UtestShell::getCurrent()->assertTrue((condition) != 0, checkString, conditionString, text, file, line); }
137 
138 #define CHECK_FALSE_LOCATION(condition, checkString, conditionString, text, file, line)\
139  { UtestShell::getCurrent()->assertTrue((condition) == 0, checkString, conditionString, text, file, line); }
140 
141 //This check needs the operator!=(), and a StringFrom(YourType) function
142 #define CHECK_EQUAL(expected, actual)\
143  CHECK_EQUAL_LOCATION(expected, actual, NULL, __FILE__, __LINE__)
144 
145 #define CHECK_EQUAL_TEXT(expected, actual, text)\
146  CHECK_EQUAL_LOCATION(expected, actual, text, __FILE__, __LINE__)
147 
148 #define CHECK_EQUAL_LOCATION(expected, actual, text, file, line)\
149  { if ((expected) != (actual)) { \
150  if ((actual) != (actual)) \
151  UtestShell::getCurrent()->print("WARNING:\n\tThe \"Actual Parameter\" parameter is evaluated multiple times resulting in different values.\n\tThus the value in the error message is probably incorrect.", file, line); \
152  if ((expected) != (expected)) \
153  UtestShell::getCurrent()->print("WARNING:\n\tThe \"Expected Parameter\" parameter is evaluated multiple times resulting in different values.\n\tThus the value in the error message is probably incorrect.", file, line); \
154  UtestShell::getCurrent()->assertEquals(true, StringFrom(expected).asCharString(), StringFrom(actual).asCharString(), text, file, line); \
155  } \
156  else \
157  { \
158  UtestShell::getCurrent()->assertLongsEqual((long)0, (long)0, NULL, file, line); \
159  } }
160 
161 //This check checks for char* string equality using strcmp.
162 //This makes up for the fact that CHECK_EQUAL only compares the pointers to char*'s
163 #define STRCMP_EQUAL(expected, actual)\
164  STRCMP_EQUAL_LOCATION(expected, actual, NULL, __FILE__, __LINE__)
165 
166 #define STRCMP_EQUAL_TEXT(expected, actual, text)\
167  STRCMP_EQUAL_LOCATION(expected, actual, text, __FILE__, __LINE__)
168 
169 #define STRCMP_EQUAL_LOCATION(expected, actual, text, file, line)\
170  { UtestShell::getCurrent()->assertCstrEqual(expected, actual, text, file, line); }
171 
172 #define STRNCMP_EQUAL(expected, actual, length)\
173  STRNCMP_EQUAL_LOCATION(expected, actual, length, NULL, __FILE__, __LINE__)
174 
175 #define STRNCMP_EQUAL_TEXT(expected, actual, length, text)\
176  STRNCMP_EQUAL_LOCATION(expected, actual, length, text, __FILE__, __LINE__)
177 
178 #define STRNCMP_EQUAL_LOCATION(expected, actual, length, text, file, line)\
179  { UtestShell::getCurrent()->assertCstrNEqual(expected, actual, length, text, file, line); }
180 
181 #define STRCMP_NOCASE_EQUAL(expected, actual)\
182  STRCMP_NOCASE_EQUAL_LOCATION(expected, actual, NULL, __FILE__, __LINE__)
183 
184 #define STRCMP_NOCASE_EQUAL_TEXT(expected, actual, text)\
185  STRCMP_NOCASE_EQUAL_LOCATION(expected, actual, text, __FILE__, __LINE__)
186 
187 #define STRCMP_NOCASE_EQUAL_LOCATION(expected, actual, text, file, line)\
188  { UtestShell::getCurrent()->assertCstrNoCaseEqual(expected, actual, text, file, line); }
189 
190 #define STRCMP_CONTAINS(expected, actual)\
191  STRCMP_CONTAINS_LOCATION(expected, actual, NULL, __FILE__, __LINE__)
192 
193 #define STRCMP_CONTAINS_TEXT(expected, actual, text)\
194  STRCMP_CONTAINS_LOCATION(expected, actual, text, __FILE__, __LINE__)
195 
196 #define STRCMP_CONTAINS_LOCATION(expected, actual, text, file, line)\
197  { UtestShell::getCurrent()->assertCstrContains(expected, actual, text, file, line); }
198 
199 #define STRCMP_NOCASE_CONTAINS(expected, actual)\
200  STRCMP_NOCASE_CONTAINS_LOCATION(expected, actual, NULL, __FILE__, __LINE__)
201 
202 #define STRCMP_NOCASE_CONTAINS_TEXT(expected, actual, text)\
203  STRCMP_NOCASE_CONTAINS_LOCATION(expected, actual, text, __FILE__, __LINE__)
204 
205 #define STRCMP_NOCASE_CONTAINS_LOCATION(expected, actual, text, file, line)\
206  { UtestShell::getCurrent()->assertCstrNoCaseContains(expected, actual, text, file, line); }
207 
208 //Check two long integers for equality
209 #define LONGS_EQUAL(expected, actual)\
210  LONGS_EQUAL_LOCATION((expected), (actual), "LONGS_EQUAL(" #expected ", " #actual ") failed", __FILE__, __LINE__)
211 
212 #define LONGS_EQUAL_TEXT(expected, actual, text)\
213  LONGS_EQUAL_LOCATION((expected), (actual), text, __FILE__, __LINE__)
214 
215 #define UNSIGNED_LONGS_EQUAL(expected, actual)\
216  UNSIGNED_LONGS_EQUAL_LOCATION((expected), (actual), NULL, __FILE__, __LINE__)
217 
218 #define UNSIGNED_LONGS_EQUAL_TEXT(expected, actual, text)\
219  UNSIGNED_LONGS_EQUAL_LOCATION((expected), (actual), text, __FILE__, __LINE__)
220 
221 #define LONGS_EQUAL_LOCATION(expected, actual, text, file, line)\
222  { UtestShell::getCurrent()->assertLongsEqual((long)expected, (long)actual, text, file, line); }
223 
224 #define UNSIGNED_LONGS_EQUAL_LOCATION(expected, actual, text, file, line)\
225  { UtestShell::getCurrent()->assertUnsignedLongsEqual((unsigned long)expected, (unsigned long)actual, text, file, line); }
226 
227 #define LONGLONGS_EQUAL(expected, actual)\
228  LONGLONGS_EQUAL_LOCATION(expected, actual, NULL, __FILE__, __LINE__)
229 
230 #define LONGLONGS_EQUAL_TEXT(expected, actual, text)\
231  LONGLONGS_EQUAL_LOCATION(expected, actual, text, __FILE__, __LINE__)
232 
233 #define UNSIGNED_LONGLONGS_EQUAL(expected, actual)\
234  UNSIGNED_LONGLONGS_EQUAL_LOCATION(expected, actual, NULL, __FILE__, __LINE__)
235 
236 #define UNSIGNED_LONGLONGS_EQUAL_TEXT(expected, actual, text)\
237  UNSIGNED_LONGLONGS_EQUAL_LOCATION(expected, actual, text, __FILE__, __LINE__)
238 
239 #define LONGLONGS_EQUAL_LOCATION(expected, actual, text, file, line)\
240  { UtestShell::getCurrent()->assertLongLongsEqual(expected, actual, text, file, line); }
241 
242 #define UNSIGNED_LONGLONGS_EQUAL_LOCATION(expected, actual, text, file, line)\
243  { UtestShell::getCurrent()->assertUnsignedLongLongsEqual(expected, actual, text, file, line); }
244 
245 #define BYTES_EQUAL(expected, actual)\
246  LONGS_EQUAL((expected) & 0xff,(actual) & 0xff)
247 
248 #define BYTES_EQUAL_TEXT(expected, actual, text)\
249  LONGS_EQUAL_TEXT((expected) & 0xff, (actual) & 0xff, text)
250 
251 #define SIGNED_BYTES_EQUAL(expected, actual)\
252  SIGNED_BYTES_EQUAL_LOCATION(expected, actual, __FILE__, __LINE__)
253 
254 #define SIGNED_BYTES_EQUAL_LOCATION(expected, actual, file, line) \
255  { UtestShell::getCurrent()->assertSignedBytesEqual(expected, actual, NULL, file, line); }
256 
257 #define SIGNED_BYTES_EQUAL_TEXT(expected, actual, text)\
258  SIGNED_BYTES_EQUAL_TEXT_LOCATION(expected, actual, text, __FILE__, __LINE__)
259 
260 #define SIGNED_BYTES_EQUAL_TEXT_LOCATION(expected, actual, text, file, line) \
261  { UtestShell::getCurrent()->assertSignedBytesEqual(expected, actual, text, file, line); }
262 
263 #define POINTERS_EQUAL(expected, actual)\
264  POINTERS_EQUAL_LOCATION((expected), (actual), NULL, __FILE__, __LINE__)
265 
266 #define POINTERS_EQUAL_TEXT(expected, actual, text)\
267  POINTERS_EQUAL_LOCATION((expected), (actual), text, __FILE__, __LINE__)
268 
269 #define POINTERS_EQUAL_LOCATION(expected, actual, text, file, line)\
270  { UtestShell::getCurrent()->assertPointersEqual((void *)expected, (void *)actual, text, file, line); }
271 
272 #define FUNCTIONPOINTERS_EQUAL(expected, actual)\
273  FUNCTIONPOINTERS_EQUAL_LOCATION((expected), (actual), NULL, __FILE__, __LINE__)
274 
275 #define FUNCTIONPOINTERS_EQUAL_TEXT(expected, actual, text)\
276  FUNCTIONPOINTERS_EQUAL_LOCATION((expected), (actual), text, __FILE__, __LINE__)
277 
278 #define FUNCTIONPOINTERS_EQUAL_LOCATION(expected, actual, text, file, line)\
279  { UtestShell::getCurrent()->assertFunctionPointersEqual((void (*)())expected, (void (*)())actual, text, file, line); }
280 
281 //Check two doubles for equality within a tolerance threshold
282 #define DOUBLES_EQUAL(expected, actual, threshold)\
283  DOUBLES_EQUAL_LOCATION(expected, actual, threshold, NULL, __FILE__, __LINE__)
284 
285 #define DOUBLES_EQUAL_TEXT(expected, actual, threshold, text)\
286  DOUBLES_EQUAL_LOCATION(expected, actual, threshold, text, __FILE__, __LINE__)
287 
288 #define DOUBLES_EQUAL_LOCATION(expected, actual, threshold, text, file, line)\
289  { UtestShell::getCurrent()->assertDoublesEqual(expected, actual, threshold, text, file, line); }
290 
291 #define MEMCMP_EQUAL(expected, actual, size)\
292  MEMCMP_EQUAL_LOCATION(expected, actual, size, NULL, __FILE__, __LINE__)
293 
294 #define MEMCMP_EQUAL_TEXT(expected, actual, size, text)\
295  MEMCMP_EQUAL_LOCATION(expected, actual, size, text, __FILE__, __LINE__)
296 
297 #define MEMCMP_EQUAL_LOCATION(expected, actual, size, text, file, line)\
298  { UtestShell::getCurrent()->assertBinaryEqual(expected, actual, size, text, file, line); }
299 
300 #define BITS_EQUAL(expected, actual, mask)\
301  BITS_LOCATION(expected, actual, mask, NULL, __FILE__, __LINE__)
302 
303 #define BITS_EQUAL_TEXT(expected, actual, mask, text)\
304  BITS_LOCATION(expected, actual, mask, text, __FILE__, __LINE__)
305 
306 #define BITS_LOCATION(expected, actual, mask, text, file, line)\
307  { UtestShell::getCurrent()->assertBitsEqual(expected, actual, mask, sizeof(actual), text, file, line); }
308 
309 //Fail if you get to this macro
310 //The macro FAIL may already be taken, so allow FAIL_TEST too
311 #ifndef FAIL
312 #define FAIL(text)\
313  FAIL_LOCATION(text, __FILE__,__LINE__)
314 
315 #define FAIL_LOCATION(text, file, line)\
316  { UtestShell::getCurrent()->fail(text, file, line); }
317 #endif
318 
319 #define FAIL_TEST(text)\
320  FAIL_TEST_LOCATION(text, __FILE__,__LINE__)
321 
322 #define FAIL_TEST_LOCATION(text, file,line)\
323  { UtestShell::getCurrent()->fail(text, file, line); }
324 
325 #define TEST_EXIT\
326  { UtestShell::getCurrent()->exitTest(); }
327 
328 #define UT_PRINT_LOCATION(text, file, line) \
329  { UtestShell::getCurrent()->print(text, file, line); }
330 
331 #define UT_PRINT(text) \
332  UT_PRINT_LOCATION(text, __FILE__, __LINE__)
333 
334 #if JENKINS_OUTPUT
335 #define UT_MEASURE_DOUBLE(key,value) \
336  { \
337  double val = value; \
338  SimpleString buff = StringFromFormat( "&lt;measurement&gt;&lt;name&gt;%s&lt;/name&gt;&lt;value&gt;%f&lt;/value&gt;&lt;/measurement&gt;", key, val ); \
339  gSystemOut += buff; \
340  }
341 #define UT_MEASURE_INT(key,value) \
342  { \
343  int val = value; \
344  char buffer[120]; \
345  snprintf(buffer,120,"&lt;measurement&gt;&lt;name&gt;%s&lt;/name&gt;&lt;value&gt;%d&lt;/value&gt;&lt;/measurement&gt;",key,val); \
346  UT_PRINT_LOCATION(buffer, __FILE__, __LINE__); \
347  }
348 #else
349 #define UT_MEASURE_DOUBLE(key,value) \
350  { \
351  double val = value; \
352  char buffer[120]; \
353  snprintf(buffer,120,"\n%s: %f\n",key,val); \
354  UT_PRINT_LOCATION(buffer, __FILE__, __LINE__); \
355  }
356 #define UT_MEASURE_INT(key,value) \
357  { \
358  int val = value; \
359  char buffer[120]; \
360  snprintf(buffer,120,"\n%s: %d\n",key,val); \
361  UT_PRINT_LOCATION(buffer, __FILE__, __LINE__); \
362  }
363 #endif
364 
365 #if CPPUTEST_USE_STD_CPP_LIB
366 #define CHECK_THROWS(expected, expression) \
367  { \
368  SimpleString failure_msg("expected to throw "#expected "\nbut threw nothing"); \
369  bool caught_expected = false; \
370  try { \
371  (expression); \
372  } catch(const expected &) { \
373  caught_expected = true; \
374  } catch(...) { \
375  failure_msg = "expected to throw " #expected "\nbut threw a different type"; \
376  } \
377  if (!caught_expected) { \
378  UtestShell::getCurrent()->fail(failure_msg.asCharString(), __FILE__, __LINE__); \
379  } \
380  else { \
381  UtestShell::getCurrent()->countCheck(); \
382  } \
383  }
384 #endif /* CPPUTEST_USE_STD_CPP_LIB */
385 
386 #define UT_CRASH() { UtestShell::crash(); }
387 #define RUN_ALL_TESTS(ac, av) CommandLineTestRunner::RunAllTests(ac, av)
388 
389 #endif /*D_UTestMacros_h*/