Unit Test Framework Modern C++ API Reference
Assertions

A set of definitions that implements assertions for Modern C++ code. More...

Macros

#define RTI_TEST_ASSERT(expression__)
 Asserts that the given expression is true. Otherwise, throw a TestFailure exception. More...
 
#define RTI_TEST_FAIL(msg__)
 Throw an exception with a given message. More...
 
#define RTI_TEST_THROW_UNSUPPORTED(reason__)    throw rti::test::TestIncomplete(RTI_TEST_RETCODE_UNSUPPORTED, reason__)
 Throw a TestIncomplete exception with the unsupported return code. More...
 
#define RTI_TEST_ASSERT_EQUALS(expected__, actual__)
 Assert that two given values are equal. More...
 
#define RTI_TEST_ASSERT_EQUALS_STRING   RTI_TEST_ASSERT_EQUALS
 Assert that two given strings are equal. More...
 
#define RTI_TEST_ASSERT_EQUALS_INT(expected__, actual__)    RTI_TEST_ASSERT_EQUALS((int) (expected__), (int) (actual__))
 Assert that two given integers are equal. More...
 
#define RTI_TEST_ASSERT_EQUALS_LONG_LONG(expected__, actual__)    RTI_TEST_ASSERT_EQUALS((long long) (expected__), (long long) (actual__))
 Assert that two given long long values are equal. More...
 
#define RTI_TEST_ASSERT_EQUALS_DOUBLE   RTI_TEST_ASSERT_EQUALS
 Assert that two given doubles are equal. More...
 
#define RTI_TEST_ASSERT_EXCEPTION(statement__, exception_class__)
 Assert that a given statement__ throws an exception of type exception_class__. Otherwise, throw a TestFailure exception. More...
 
#define RTI_TEST_ASSERT_EXCEPTION_NO_WARN(statement__, exception_class__)
 Assert that a given statement__ throws an exception of type exception_class__. Otherwise, throw a TestFailure exception. More...
 
#define RTI_TEST_ASSERT_RANGE(expected__, count__, tolerance_percentage__)
 Assert that a given value is within a given tolerance. More...
 

Detailed Description

A set of definitions that implements assertions for Modern C++ code.

Macro Definition Documentation

◆ RTI_TEST_ASSERT

#define RTI_TEST_ASSERT (   expression__)
Value:
if (!(expression__)) { \
RTI_TEST_THROW(expression__); \
}

Asserts that the given expression is true. Otherwise, throw a TestFailure exception.

Parameters
expression__In. The expression to be evaluated.

◆ RTI_TEST_FAIL

#define RTI_TEST_FAIL (   msg__)
Value:
"" __FILE__, \
"" RTI_TEST_TOSTRING(__LINE__), \
msg__);
Represents a test failure exception.
Definition: TestAssertion.hpp:42

Throw an exception with a given message.

Parameters
msg__In. The message to be included in the exception.

◆ RTI_TEST_THROW_UNSUPPORTED

#define RTI_TEST_THROW_UNSUPPORTED (   reason__)     throw rti::test::TestIncomplete(RTI_TEST_RETCODE_UNSUPPORTED, reason__)

Throw a TestIncomplete exception with the unsupported return code.

Parameters
reason__In. The reason for the test being incomplete.

◆ RTI_TEST_ASSERT_EQUALS

#define RTI_TEST_ASSERT_EQUALS (   expected__,
  actual__ 
)
Value:
if (!check_equals(expected__, actual__)) { \
RTI_TEST_THROW(expected__ == actual__); \
}

Assert that two given values are equal.

Parameters
expected__In. The expected value.
actual__In. The actual value.
Note
This macro requires that the type provide the != and << operators.

◆ RTI_TEST_ASSERT_EQUALS_STRING

#define RTI_TEST_ASSERT_EQUALS_STRING   RTI_TEST_ASSERT_EQUALS

Assert that two given strings are equal.

Parameters
expected__In. The expected value.
actual__In. The actual value.

◆ RTI_TEST_ASSERT_EQUALS_INT

#define RTI_TEST_ASSERT_EQUALS_INT (   expected__,
  actual__ 
)     RTI_TEST_ASSERT_EQUALS((int) (expected__), (int) (actual__))

Assert that two given integers are equal.

Parameters
expected__In. The expected value.
actual__In. The actual value.

◆ RTI_TEST_ASSERT_EQUALS_LONG_LONG

#define RTI_TEST_ASSERT_EQUALS_LONG_LONG (   expected__,
  actual__ 
)     RTI_TEST_ASSERT_EQUALS((long long) (expected__), (long long) (actual__))

Assert that two given long long values are equal.

Parameters
expected__In. The expected value.
actual__In. The actual value.

◆ RTI_TEST_ASSERT_EQUALS_DOUBLE

#define RTI_TEST_ASSERT_EQUALS_DOUBLE   RTI_TEST_ASSERT_EQUALS

Assert that two given doubles are equal.

Parameters
expected__In. The expected value.
actual__In. The actual value.

◆ RTI_TEST_ASSERT_EXCEPTION

#define RTI_TEST_ASSERT_EXCEPTION (   statement__,
  exception_class__ 
)
Value:
try { \
RTITestLog_printExpectedErrorsStart(); \
statement__; \
RTI_TEST_FAIL("Expected exception not thrown"); \
} catch (const exception_class__&) { \
RTITestLog_printExpectedErrorsEnd(); \
}

Assert that a given statement__ throws an exception of type exception_class__. Otherwise, throw a TestFailure exception.

Parameters
statement__In. The statement to be evaluated.
exception_class__In. The type of exception to be thrown.

Prints expected errors start and end log messages.

◆ RTI_TEST_ASSERT_EXCEPTION_NO_WARN

#define RTI_TEST_ASSERT_EXCEPTION_NO_WARN (   statement__,
  exception_class__ 
)
Value:
try { \
statement__; \
RTI_TEST_FAIL("Expected exception not thrown"); \
} catch (const exception_class__&) { }

Assert that a given statement__ throws an exception of type exception_class__. Otherwise, throw a TestFailure exception.

Parameters
statement__In. The statement to be evaluated.
exception_class__In. The type of exception to be thrown.

This macro doesn't print the expected error's start and end log messages.

◆ RTI_TEST_ASSERT_RANGE

#define RTI_TEST_ASSERT_RANGE (   expected__,
  count__,
  tolerance_percentage__ 
)
Value:
{ \
double tolerance__ = (double) (tolerance_percentage__) / 100; \
double max_count__ = (expected__) * (1 + tolerance__); \
double min_count__ = (expected__) * (1 - tolerance__); \
try { \
RTI_TEST_ASSERT((count__) >= min_count__ && (count__) <= max_count__); \
} catch (std::exception&) { \
RTITestLog_freeForm( \
RTI_LOG_BIT_EXCEPTION, \
"%s:%d: value out of range: [%f, %f], count: %f\n", \
(__FILE__), \
(__LINE__), \
min_count__, \
max_count__, \
(double) (count__)); \
throw; \
} \
}

Assert that a given value is within a given tolerance.

Parameters
expected__In. The expected value.
count__In. The actual value.
tolerance_percentage__In. The tolerance percentage.