11#ifndef RTI_TEST_ASSERTIONS_HPP_
12#define RTI_TEST_ASSERTIONS_HPP_
23#include "rtitest/test_log.h"
24#include "rtitest/test_time.h"
26namespace rti {
namespace test {
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),
68 const std::string& the_file,
69 const std::string& the_line,
70 const std::string& the_msg,
71 const std::exception& nested_ex)
73 the_file +
":" + the_line +
": " + the_msg
74 +
" - caused by:\n " + nested_ex.what()),
93 const std::string& file()
const
102 const std::string& line()
const
111 const std::string& msg()
const
137 : std::logic_error(reason), retcode_(retcode)
177#define RTI_TEST_THROW(expression__) \
178 throw rti::test::TestFailure( \
180 "" RTI_TEST_TOSTRING(__LINE__), \
181 "assertion failed: " #expression__)
188#define RTI_TEST_ASSERT(expression__) \
189 if (!(expression__)) { \
190 RTI_TEST_THROW(expression__); \
198#define RTI_TEST_FAIL(msg__) \
199 throw rti::test::TestFailure( \
201 "" RTI_TEST_TOSTRING(__LINE__), \
209#define RTI_TEST_THROW_UNSUPPORTED(reason__) \
210 throw rti::test::TestIncomplete(RTI_TEST_RETCODE_UNSUPPORTED, reason__)
217#define RTI_TEST_THROW_UNIMPLEMENTED(reason__) \
218 throw rti::test::TestIncomplete(RTI_TEST_RETCODE_UNIMPLEMENTED, reason__)
220template <
typename T,
typename U>
229void print_assert_equals_failure(
const T& a,
const U& b)
231 std::ostringstream os;
232 os <<
"expected: " << a <<
", actual: " << b <<
"\n";
233 RTITestLog_logParamString(
234 RTI_TEST_LOG_BIT_EXCEPTION,
242 typename std::enable_if<
243 !std::is_floating_point<T>::value
244 && !std::is_floating_point<U>::value,
255bool check_equals(
const T& a,
const U& b)
258 print_assert_equals_failure(a, b);
268 typename std::enable_if<
269 std::is_floating_point<T>::value
270 || std::is_floating_point<U>::value,
281bool check_equals(
const T& a,
const U& b)
284 std::fabs(
static_cast<long double>(a) -
static_cast<long double>(b));
285 bool condition_check = diff > std::numeric_limits<double>::epsilon();
287 if (condition_check) {
288 print_assert_equals_failure(a, b);
302#define RTI_TEST_ASSERT_EQUALS(expected__, actual__) \
303 if (!check_equals(expected__, actual__)) { \
304 RTI_TEST_THROW(expected__ == actual__); \
313#define RTI_TEST_ASSERT_EQUALS_STRING RTI_TEST_ASSERT_EQUALS
321#define RTI_TEST_ASSERT_EQUALS_INT(expected__, actual__) \
322 RTI_TEST_ASSERT_EQUALS((int) (expected__), (int) (actual__))
330#define RTI_TEST_ASSERT_EQUALS_LONG_LONG(expected__, actual__) \
331 RTI_TEST_ASSERT_EQUALS((long long) (expected__), (long long) (actual__))
339#define RTI_TEST_ASSERT_EQUALS_DOUBLE RTI_TEST_ASSERT_EQUALS
346#define RTI_TEST_WAIT(max_seconds__) \
348 const struct RTITestTime sleepTime__ = { 0, 429496730 }; \
350 while (sleeps__ < max_seconds__ * 10) { \
351 RTITest_sleep(&sleepTime__); \
362#define RTI_TEST_WAIT_FOR(expression__, max_seconds__) \
365 const struct RTITestTime sleepTime__ = { 0, 429496730 }; \
367 bool result__ = (expression__); \
368 while (!result__ && sleeps__ < max_seconds__ * 10) { \
369 RTITest_sleep(&sleepTime__); \
371 result__ = (expression__); \
374 throw rti::test::TestFailure( \
376 "" RTI_TEST_TOSTRING(__LINE__), \
377 "wait for condition failed: " #expression__); \
388#define RTI_TEST_WAIT_FOR_EQUALS(expected__, actual__, max_seconds__) \
391 RTI_TEST_WAIT_FOR((expected__) == (actual__), (max_seconds__)); \
392 } catch (const rti::test::TestFailure&) { \
393 print_assert_equals_failure(expected__, actual__); \
406#define RTI_TEST_WAIT_FOR_GREATER_OR_EQUALS(actual__, limit__, max_seconds__) \
409 RTI_TEST_WAIT_FOR((actual__) >= (limit__), (max_seconds__)); \
410 } catch (const rti::test::TestFailure&) { \
411 print_assert_equals_failure(limit__, actual__); \
424#define RTI_TEST_WAIT_FOR_GREATER_OR_EQUALS_INT \
425 RTI_TEST_WAIT_FOR_GREATER_OR_EQUALS
434#define RTI_TEST_WAIT_FOR_EQUALS_INT RTI_TEST_WAIT_FOR_EQUALS
444#define RTI_TEST_ASSERT_EXCEPTION(statement__, exception_class__) \
446 RTITestLog_printExpectedErrorsStart(); \
448 RTI_TEST_FAIL("Expected exception not thrown"); \
449 } catch (const exception_class__&) { \
450 RTITestLog_printExpectedErrorsEnd(); \
461#define RTI_TEST_ASSERT_EXCEPTION_NO_WARN(statement__, exception_class__) \
464 RTI_TEST_FAIL("Expected exception not thrown"); \
465 } catch (const exception_class__&) { }
473#define RTI_TEST_TRACE_W_MSG(statement__, msg__) \
476 } catch (const std::exception& ex) { \
477 throw rti::test::TestFailure( \
479 "" RTI_TEST_TOSTRING(__LINE__), \
491#define RTI_TEST_ASSERT_RANGE(expected__, count__, tolerance_percentage__) \
493 double tolerance__ = (double) (tolerance_percentage__) / 100; \
494 double max_count__ = (expected__) * (1 + tolerance__); \
495 double min_count__ = (expected__) * (1 - tolerance__); \
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", \
506 (double) (count__)); \
521#define RTI_TEST_TRACE(statement__) \
522 RTI_TEST_TRACE_W_MSG(statement__, "test failure: " #statement__)
Represents a test failure exception.
Definition: TestAssertion.hpp:42
Exception class for indicating that a test is unsupported.
Definition: TestAssertion.hpp:129