Unit Test Framework C and Traditional C++ API Reference
test_assert.h
1/*
2 * (c) Copyright, Real-Time Innovations, 2002-2022.
3 * All rights reserved.
4 * No duplications, whole or partial, manual or electronic, may be made
5 * without express written permission. Any such copies, or
6 * revisions thereof, must display this notice unaltered.
7 * This code contains trade secrets of Real-Time Innovations, Inc.
8 */
9
10#ifndef rtitest_assert_h
11#define rtitest_assert_h
12
13#include <stdio.h>
14#include <float.h>
15
16#include "rtitest/test_assert_types.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
36#ifdef RTI_TEST_ENABLE_DEBUG_ASSERT
37 #define RTITest_debugAssert(str__) \
38 RTITestLogger_log( \
39 RTI_TEST_LOG_BIT_EXCEPTION, \
40 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
41 "[DEBUG %s:%d] executing command: %s", \
42 __FILE__, \
43 __LINE__, \
44 #str__)
45#else
46 #define RTITest_debugAssert(str__)
47#endif
48
57#define RTITest_setLastAssertErrorMessage(...) \
58 if (RTITest_g_lastAssertErrorMessage != NULL) { \
59 int unusedReturnValue__; \
60 unusedReturnValue__ = RTITestLog_snprintf( \
61 RTITest_g_lastAssertErrorMessage, \
62 RTI_TEST_MAX_ASSERT_ERROR_MESSAGE_SIZE, \
63 __VA_ARGS__); \
64 (void) unusedReturnValue__; \
65 }
66
73#define RTITest_assertLog(logMessage__, ...) \
74 (void) METHOD_NAME; \
75 RTITest_setLastAssertErrorMessage(__VA_ARGS__); \
76 RTITestLogger_log(RTI_TEST_LOG_BIT_EXCEPTION, (logMessage__), __VA_ARGS__)
77
85#define RTITest_assertLogWithFunctionName(functionName__, logMessage__, ...) \
86 RTITest_setLastAssertErrorMessage(__VA_ARGS__); \
87 RTITestLogger_logWithFunctionName( \
88 RTI_TEST_LOG_BIT_EXCEPTION, \
89 (functionName__), \
90 (logMessage__), \
91 __VA_ARGS__)
92
100#define RTITest_assertWithStr(statement__, str__) \
101 RTITest_debugAssert(statement__); \
102 if (!(statement__)) { \
103 const char *strConst__ = str__; \
104 RTITest_assertLog( \
105 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
106 "[%s:%d] assertion failed: %s reason: %s", \
107 __FILE__, \
108 __LINE__, \
109 #statement__, \
110 strConst__); \
111 goto done; \
112 }
113
122#define RTITest_assertWithAction(statement__, failAction__) \
123 RTITest_debugAssert(statement__); \
124 if (!(statement__)) { \
125 RTITest_assertLog( \
126 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
127 "[%s:%d] assertion failed: %s", \
128 __FILE__, \
129 __LINE__, \
130 #statement__); \
131 failAction__; \
132 }
133
140#define RTITest_assert(statement__) \
141 RTITest_assertWithAction(statement__, goto done)
142
149#define RTITest_assertAfterDone(statement__) \
150 RTITest_assertWithAction(statement__, result = RTI_TEST_RETCODE_FAILED)
151
158#define RTITest_assertWithNoAction(statement__) \
159 RTITest_assertWithAction(statement__,)
160
161/*
162 * Use almost equals macros only if you cannot get the right precision. Use
163 * regular assert equals macros instead.
164 */
165
173#define RTITest_assertAlmostEqualsFloat(limit__, actual__) \
174 RTITest_assertComparisonForFloatingPointTypeWithAction( \
175 limit__, \
176 actual__, \
177 float, \
178 ==, \
179 0.0000007, \
180 goto done)
181
189#define RTITest_assertAlmostEqualsDouble(limit__, actual__) \
190 RTITest_assertComparisonForFloatingPointTypeWithAction( \
191 limit__, \
192 actual__, \
193 double, \
194 ==, \
195 0.0000007, \
196 goto done)
197
205#define RTITest_assertEqualsSN(expected__, actual__) \
206 { \
207 RTITest_assertComparisonForType( \
208 (expected__).low, \
209 (actual__).low, \
210 unsigned int, \
211 "%u", \
212 ==); \
213 RTITest_assertComparisonForType( \
214 (expected__).high, \
215 (actual__).high, \
216 int, \
217 "%d", \
218 ==); \
219 }
220
229#define RTITest_assertComparisonForDDS_Duration_tWithAction(expected__, actual__, operation__, failAction__) \
230 { \
231 RTITest_assertComparisonForTypeWithAction( \
232 expected__.sec, \
233 actual__.sec, \
234 int, \
235 "%d", \
236 operation__, \
237 failAction__); \
238 RTITest_assertComparisonForTypeWithAction( \
239 expected__.nanosec, \
240 actual__.nanosec, \
241 unsigned int, \
242 "%d", \
243 operation__, \
244 failAction__); \
245 }
246
255#define RTITest_assertComparisonForDDS_Duration_t(expected__, actual__, operation__) \
256 RTITest_assertComparisonForDDS_Duration_tWithAction( \
257 expected__, \
258 actual__, \
259 operation__, \
260 goto done)
261
269#define RTITest_assertEqualsDDS_Duration_t(expected__, actual__) \
270 RTITest_assertComparisonForDDS_Duration_t(expected__, actual__, ==)
271
280#define RTITest_assertEqualsString(expected__, actual__) \
281 RTITest_debugAssert(strcmp((expected__), (actual__)) == 0); \
282 if (strcmp((expected__), (actual__)) != 0) { \
283 const char *expectedConst__ = expected__; \
284 const char *actualConst__ = actual__; \
285 RTITest_assertLog( \
286 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
287 "[%s:%d] assertion failed: strcmp(%s, %s) == 0: expected: %s, " \
288 "actual: %s", \
289 __FILE__, \
290 __LINE__, \
291 #expected__, \
292 #actual__, \
293 expectedConst__, \
294 actualConst__); \
295 goto done; \
296 }
297
306#define RTITest_assertNotEqualsString(expected__, actual__) \
307 RTITest_debugAssert(strcmp((expected__), (actual__)) != 0); \
308 if (strcmp((expected__), (actual__)) == 0) { \
309 const char *expectedConst__ = expected__; \
310 const char *actualConst__ = actual__; \
311 RTITest_assertLog( \
312 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
313 "[%s:%d] assertion failed: strcmp(%s, %s) != 0: not expected: %s, " \
314 "actual: %s", \
315 __FILE__, \
316 __LINE__, \
317 #expected__, \
318 #actual__, \
319 expectedConst__, \
320 actualConst__); \
321 goto done; \
322 }
323
333#define RTITest_assertSubString(expectedSubStr__, actualStr__) \
334 RTITest_debugAssert(strstr((actualStr__), (expectedSubStr__)) == NULL); \
335 if (strstr((actualStr__), (expectedSubStr__)) == NULL) { \
336 RTITest_assertLog( \
337 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
338 "[%s:%d] assertion failed: expected to find string %s in %s", \
339 __FILE__, \
340 __LINE__, \
341 expectedSubStr__, \
342 actualStr__); \
343 goto done; \
344 }
345
352#define RTITest_assertNotNull(object__) \
353 RTITest_assertNotNullWithAction(object__, goto done)
354
361#define RTITest_assertNotNullWithAction(object__, failAction__) \
362 RTITest_debugAssert((object__) != NULL); \
363 if ((object__) == NULL) { \
364 RTITest_assertLog( \
365 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
366 "[%s:%d] pointer is null: %s", \
367 __FILE__, \
368 __LINE__, \
369 #object__); \
370 failAction__; \
371 }
372
379#define RTITest_assertNull(object__) \
380 RTITest_assertNullWithAction(object__, goto done)
381
388#define RTITest_assertNullWithAction(object__, failAction__) \
389 RTITest_debugAssert((object__) == NULL); \
390 if ((object__) != NULL) { \
391 RTITest_assertLog( \
392 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
393 "[%s:%d] pointer is not null: %s", \
394 __FILE__, \
395 __LINE__, \
396 #object__); \
397 failAction__; \
398 }
399
400
408#define RTITest_assertRetcodeWithAction( \
409 retcode__, \
410 failAction__, \
411 expectedRetcode__) \
412{ \
413 DDS_ReturnCode_t retCodeTmp__ = (retcode__); \
414 RTITest_debugAssert(retCodeTmp__ == (expectedRetcode__)); \
415 if (retCodeTmp__ != (expectedRetcode__)) { \
416 if ((int) retCodeTmp__ < 0) { \
417 RTITest_assertLog( \
418 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
419 "[%s:%d] assertion failed: %s == %s", \
420 __FILE__, \
421 __LINE__, \
422 #retcode__, \
423 #expectedRetcode__); \
424 } else { \
425 unsigned int retCodeAsUInt__ = (unsigned int) retCodeTmp__; \
426 unsigned int index__ = \
427 (retCodeAsUInt__ < RTI_TEST_NUM_RETCODES - 1 \
428 ? retCodeAsUInt__ \
429 : RTI_TEST_NUM_RETCODES); \
430 index__ = \
431 (retCodeTmp__ == DDS_RETCODE_NOT_ALLOWED_BY_SECURITY \
432 ? RTI_TEST_NUM_RETCODES - 1 \
433 : index__); \
434 RTITest_assertLog( \
435 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
436 "[%s:%d] assertion failed: %s == %s: got %s", \
437 __FILE__, \
438 __LINE__, \
439 #retcode__, \
440 #expectedRetcode__, \
441 RTITest_g_returnCodeStrings[index__]); \
442 } \
443 failAction__; \
444 } \
445}
446
454#define RTITest_assertOkWithAction(retcode__, failAction__) \
455 RTITest_assertRetcodeWithAction(retcode__, failAction__, DDS_RETCODE_OK)
456
463#define RTITest_assertOk(retcode__) \
464 RTITest_assertOkWithAction((retcode__), goto done)
465
466#define RTITest_assertPassed(retcode__) \
467 RTITest_assertEqualsInt(RTI_TEST_RETCODE_PASSED, (retcode__))
468
476#define RTITest_assertErrorWithAction(retcode__, failAction__) \
477 RTITest_assertRetcodeWithAction(retcode__, failAction__, DDS_RETCODE_ERROR)
478
486#define RTITest_assertEqualsRetcode(expected__, retcode__) \
487 RTITest_assertRetcodeWithAction(retcode__, goto done, expected__)
488
495#define RTITest_assertError(retcode__) \
496 RTITest_assertErrorWithAction((retcode__), goto done)
497
498/*
499 * Use after the done label to set the test result to failed if the assert fails
500 * requires the test result variable to be named 'result'
501 */
502
509#define RTITest_assertOkAfterDone(retcode__) \
510 RTITest_assertOkWithAction((retcode__), result = RTI_TEST_RETCODE_FAILED)
511
517#define RTITest_assertFalseWithAction(statement__, failAction__) \
518 RTITest_debugAssert(statement__); \
519 if ((statement__)) { \
520 RTITest_assertLog( \
521 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
522 "[%s:%d] assertion failed: !%s", \
523 __FILE__, \
524 __LINE__, \
525 #statement__); \
526 failAction__; \
527 }
528
535#define RTITest_assertFalse(statement__) \
536 RTITest_assertFalseWithAction(statement__, goto done)
537
544#define RTITest_assertFalseAfterDone(statement__) \
545 RTITest_assertFalseWithAction(statement__, result = RTI_TEST_RETCODE_FAILED)
546
558#define RTITest_assertMarginWithAction(expected__, count__, tolerance_percentage__, failAction__) \
559 { \
560 double tolerance = (double) (tolerance_percentage__) / 100; \
561 double max_count = (double) (expected__) * (1 + tolerance); \
562 double min_count = (double) (expected__) * (1 - tolerance); \
563 if (!((double) (count__) >= min_count \
564 && (double) (count__) <= max_count)) { \
565 RTITest_assertLog( \
566 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
567 "%s:[%s:%d]: value out of range: [%f, %f], count: %f\n", \
568 RTI_FUNCTION_NAME, \
569 __FILE__, \
570 __LINE__, \
571 min_count, \
572 max_count, \
573 (double) (count__)); \
574 failAction__; \
575 } \
576 }
577
588#define RTITest_assertMargin(expected__, count__, tolerance_percentage__) \
589 RTITest_assertMarginWithAction( \
590 expected__, \
591 count__, \
592 tolerance_percentage__, \
593 goto done)
594
601#define RTITest_fail(msg__) \
602 RTITest_assertLog( \
603 &RTI_TEST_LOG_EMPTY_TEMPLATE, \
604 "%s[%s:%d] %s\n", \
605 RTI_FUNCTION_NAME, \
606 __FILE__, \
607 __LINE__, \
608 msg__); \
609 goto done;
610
616#define RTITest_isSpdp2Enabled(participantQos__) \
617 (participantQos__.discovery_config.builtin_discovery_plugins \
618 & DDS_DISCOVERYCONFIG_BUILTIN_SPDP2)
619
620#ifdef __cplusplus
621} /* extern "C" */
622#endif
623
624#endif /* rtitest_assert_h */