Unit Test Framework Modern C++ API Reference
TestAssertion.hpp
1/*
2 * (c) Copyright, Real-Time Innovations, 2014-2016.
3 * All rights reserved.
4 *
5 * No duplications, whole or partial, manual or electronic, may be made
6 * without express written permission. Any such copies, or
7 * revisions thereof, must display this notice unaltered.
8 * This code contains trade secrets of Real-Time Innovations, Inc.
9 */
10
11#ifndef RTI_TEST_ASSERTIONS_HPP_
12#define RTI_TEST_ASSERTIONS_HPP_
13
14#include <cmath>
15#include <iostream>
16#include <limits>
17#include <stdio.h>
18#include <stdlib.h>
19#include <stdexcept>
20#include <string>
21#include <sstream>
22
23#include "rtitest/test_log.h"
24#include "rtitest/test_time.h"
25
26namespace rti { namespace test {
27
33// --- TestFailure exception: -------------------------------------------------
34
42class TestFailure : public std::logic_error {
43public:
51 const std::string& the_file,
52 const std::string& the_line,
53 const std::string& the_msg)
54 : std::logic_error(the_file + ":" + the_line + ": " + the_msg),
55 _file(the_file),
56 _line(the_line),
57 _msg(the_msg)
58 { }
59
68 const std::string& the_file,
69 const std::string& the_line,
70 const std::string& the_msg,
71 const std::exception& nested_ex)
72 : std::logic_error(
73 the_file + ":" + the_line + ": " + the_msg
74 + " - caused by:\n " + nested_ex.what()),
75 _file(the_file),
76 _line(the_line),
77 _msg(the_msg)
78 { }
79
86 ~TestFailure() throw()
87 { }
88
93 const std::string& file() const
94 {
95 return _file;
96 }
97
102 const std::string& line() const
103 {
104 return _line;
105 }
106
111 const std::string& msg() const
112 {
113 return _msg;
114 }
115
116private:
117 std::string _file;
118 std::string _line;
119 std::string _msg;
120};
121
129class TestIncomplete : public std::logic_error {
130public:
136 TestIncomplete(int retcode, const std::string& reason)
137 : std::logic_error(reason), retcode_(retcode)
138 { }
139
147 ~TestIncomplete() throw()
148 { }
149
154 int retcode() const
155 {
156 return retcode_;
157 }
158
159private:
160 int retcode_;
161};
162
163}} // namespace rti::test
164
170// --- Assertion macros: ------------------------------------------------------
171
177#define RTI_TEST_THROW(expression__) \
178 throw rti::test::TestFailure( \
179 "" __FILE__, \
180 "" RTI_TEST_TOSTRING(__LINE__), \
181 "assertion failed: " #expression__)
182
188#define RTI_TEST_ASSERT(expression__) \
189 if (!(expression__)) { \
190 RTI_TEST_THROW(expression__); \
191 }
192
198#define RTI_TEST_FAIL(msg__) \
199 throw rti::test::TestFailure( \
200 "" __FILE__, \
201 "" RTI_TEST_TOSTRING(__LINE__), \
202 msg__);
203
209#define RTI_TEST_THROW_UNSUPPORTED(reason__) \
210 throw rti::test::TestIncomplete(RTI_TEST_RETCODE_UNSUPPORTED, reason__)
211
217#define RTI_TEST_THROW_UNIMPLEMENTED(reason__) \
218 throw rti::test::TestIncomplete(RTI_TEST_RETCODE_UNIMPLEMENTED, reason__)
219
220template <typename T, typename U>
229void print_assert_equals_failure(const T& a, const U& b)
230{
231 std::ostringstream os;
232 os << "expected: " << a << ", actual: " << b << "\n";
233 RTITestLog_logParamString(
234 RTI_TEST_LOG_BIT_EXCEPTION,
235 "%s",
236 os.str().c_str());
237}
238
239template <
240 typename T,
241 typename U,
242 typename std::enable_if<
243 !std::is_floating_point<T>::value
244 && !std::is_floating_point<U>::value,
245 bool>::type = true>
255bool check_equals(const T& a, const U& b)
256{
257 if (a != b) {
258 print_assert_equals_failure(a, b);
259 return false;
260 }
261
262 return true;
263}
264
265template <
266 typename T,
267 typename U,
268 typename std::enable_if<
269 std::is_floating_point<T>::value
270 || std::is_floating_point<U>::value,
271 bool>::type = true>
281bool check_equals(const T& a, const U& b)
282{
283 long double diff =
284 std::fabs(static_cast<long double>(a) - static_cast<long double>(b));
285 bool condition_check = diff > std::numeric_limits<double>::epsilon();
286
287 if (condition_check) {
288 print_assert_equals_failure(a, b);
289 return false;
290 }
291
292 return true;
293}
294
302#define RTI_TEST_ASSERT_EQUALS(expected__, actual__) \
303 if (!check_equals(expected__, actual__)) { \
304 RTI_TEST_THROW(expected__ == actual__); \
305 }
306
313#define RTI_TEST_ASSERT_EQUALS_STRING RTI_TEST_ASSERT_EQUALS
314
321#define RTI_TEST_ASSERT_EQUALS_INT(expected__, actual__) \
322 RTI_TEST_ASSERT_EQUALS((int) (expected__), (int) (actual__))
323
330#define RTI_TEST_ASSERT_EQUALS_LONG_LONG(expected__, actual__) \
331 RTI_TEST_ASSERT_EQUALS((long long) (expected__), (long long) (actual__))
332
339#define RTI_TEST_ASSERT_EQUALS_DOUBLE RTI_TEST_ASSERT_EQUALS
340
346#define RTI_TEST_WAIT(max_seconds__) \
347 { \
348 const struct RTITestTime sleepTime__ = { 0, 429496730 }; \
349 int sleeps__ = 0; \
350 while (sleeps__ < max_seconds__ * 10) { \
351 RTITest_sleep(&sleepTime__); \
352 ++sleeps__; \
353 } \
354 }
355
362#define RTI_TEST_WAIT_FOR(expression__, max_seconds__) \
363 { \
364 /* 100ms */ \
365 const struct RTITestTime sleepTime__ = { 0, 429496730 }; \
366 int sleeps__ = 0; \
367 bool result__ = (expression__); \
368 while (!result__ && sleeps__ < max_seconds__ * 10) { \
369 RTITest_sleep(&sleepTime__); \
370 ++sleeps__; \
371 result__ = (expression__); \
372 } \
373 if (!result__) { \
374 throw rti::test::TestFailure( \
375 "" __FILE__, \
376 "" RTI_TEST_TOSTRING(__LINE__), \
377 "wait for condition failed: " #expression__); \
378 } \
379 }
380
388#define RTI_TEST_WAIT_FOR_EQUALS(expected__, actual__, max_seconds__) \
389 { \
390 try { \
391 RTI_TEST_WAIT_FOR((expected__) == (actual__), (max_seconds__)); \
392 } catch (const rti::test::TestFailure&) { \
393 print_assert_equals_failure(expected__, actual__); \
394 throw; \
395 } \
396 }
397
406#define RTI_TEST_WAIT_FOR_GREATER_OR_EQUALS(actual__, limit__, max_seconds__) \
407 { \
408 try { \
409 RTI_TEST_WAIT_FOR((actual__) >= (limit__), (max_seconds__)); \
410 } catch (const rti::test::TestFailure&) { \
411 print_assert_equals_failure(limit__, actual__); \
412 throw; \
413 } \
414 }
415
424#define RTI_TEST_WAIT_FOR_GREATER_OR_EQUALS_INT \
425 RTI_TEST_WAIT_FOR_GREATER_OR_EQUALS
426
434#define RTI_TEST_WAIT_FOR_EQUALS_INT RTI_TEST_WAIT_FOR_EQUALS
435
444#define RTI_TEST_ASSERT_EXCEPTION(statement__, exception_class__) \
445 try { \
446 RTITestLog_printExpectedErrorsStart(); \
447 statement__; \
448 RTI_TEST_FAIL("Expected exception not thrown"); \
449 } catch (const exception_class__&) { \
450 RTITestLog_printExpectedErrorsEnd(); \
451 }
452
461#define RTI_TEST_ASSERT_EXCEPTION_NO_WARN(statement__, exception_class__) \
462 try { \
463 statement__; \
464 RTI_TEST_FAIL("Expected exception not thrown"); \
465 } catch (const exception_class__&) { }
466
473#define RTI_TEST_TRACE_W_MSG(statement__, msg__) \
474 try { \
475 (statement__); \
476 } catch (const std::exception& ex) { \
477 throw rti::test::TestFailure( \
478 "" __FILE__, \
479 "" RTI_TEST_TOSTRING(__LINE__), \
480 msg__, \
481 ex); \
482 }
483
491#define RTI_TEST_ASSERT_RANGE(expected__, count__, tolerance_percentage__) \
492 { \
493 double tolerance__ = (double) (tolerance_percentage__) / 100; \
494 double max_count__ = (expected__) * (1 + tolerance__); \
495 double min_count__ = (expected__) * (1 - tolerance__); \
496 try { \
497 RTI_TEST_ASSERT((count__) >= min_count__ && (count__) <= max_count__); \
498 } catch (std::exception&) { \
499 RTITestLog_freeForm( \
500 RTI_LOG_BIT_EXCEPTION, \
501 "%s:%d: value out of range: [%f, %f], count: %f\n", \
502 (__FILE__), \
503 (__LINE__), \
504 min_count__, \
505 max_count__, \
506 (double) (count__)); \
507 throw; \
508 } \
509 }
510
521#define RTI_TEST_TRACE(statement__) \
522 RTI_TEST_TRACE_W_MSG(statement__, "test failure: " #statement__)
523
524#endif // RTI_TEST_ASSERTIONS_HPP_
Represents a test failure exception.
Definition: TestAssertion.hpp:42
Exception class for indicating that a test is unsupported.
Definition: TestAssertion.hpp:129