StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
test_handlers.h File Reference
#include <exception>
#include <filesystem>
#include <iostream>
#include <ostream>
#include <string_view>
Include dependency graph for test_handlers.h:

Go to the source code of this file.

Namespaces

namespace  StormByte
 Root namespace of the StormByte suite.
namespace  StormByte::Test
namespace  StormByte::Test::Detail

Macros

#define RETURN_TEST(fn_result)
 Prints FAILED to stderr when fn_result is not 0, then returns that value.
#define CurrentFileDirectory   std::filesystem::path(__FILE__).parent_path()
 Directory of the translation unit (std::filesystem::path of __FILE__).
#define ASSERT_EQUAL(expected, actual)
 Fails the test (return 1) when expected != actual.
#define ASSERT_NOT_EQUAL(expected, actual)
 Fails the test (return 1) when expected == actual.
#define ASSERT_FALSE(condition)
 Fails the test (return 1) when condition is true.
#define ASSERT_TRUE(condition)
 Fails the test (return 1) when condition is false.
#define ASSERT_THROWS(expression, exception_type)
 Fails when expression does not throw exception_type.
#define ASSERT_NO_THROW(expression)
 Fails when expression throws any exception.
#define ASSERT_NEAR(expected, actual, tolerance)
 Fails when two arithmetic values differ by more than tolerance.
#define ASSERT_CONTAINS(haystack, needle)
 Fails when haystack does not contain needle.
#define ASSERT_NOT_CONTAINS(haystack, needle)
 Fails when haystack contains needle.
#define ASSERT_NOT_NULL(pointer)
 Fails when pointer is null.
#define ASSERT_NULL(pointer)
 Fails when pointer is not null.
#define ASSERT_EMPTY(container)
 Fails when container is not empty.
#define ASSERT_NOT_EMPTY(container)
 Fails when container is empty.
#define ASSERT_SIZE(container, expected)
 Fails when container size() is not expected.
#define ASSERT_FAIL(reason)
 Fails the test.

Functions

template<typename T>
void StormByte::Test::Detail::PrintValue (std::ostream &stream, const T &value)

Macro Definition Documentation

◆ ASSERT_CONTAINS

#define ASSERT_CONTAINS ( haystack,
needle )
Value:
do { \
const auto& stormbyte_haystack = (haystack); \
const auto& stormbyte_needle = (needle); \
if (stormbyte_haystack.find(stormbyte_needle) == std::string_view::npos) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected text to contain \"" << stormbyte_needle << "\"" << std::endl; \
return 1; \
} \
} while (false)

Fails when haystack does not contain needle.

Parameters
haystackText that must contain the needle.
needleText that must be found.

◆ ASSERT_EMPTY

#define ASSERT_EMPTY ( container)
Value:
do { \
const auto& stormbyte_container = (container); \
if (!stormbyte_container.empty()) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected empty, size is "; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_container.size()); \
std::cerr << std::endl; \
return 1; \
} \
} while (false)

Fails when container is not empty.

Parameters
containerContainer with empty() and size().

◆ ASSERT_EQUAL

#define ASSERT_EQUAL ( expected,
actual )
Value:
do { \
const auto& stormbyte_expected = (expected); \
const auto& stormbyte_actual = (actual); \
if (!(stormbyte_expected == stormbyte_actual)) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected \""; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_expected); \
std::cerr << "\", got \""; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_actual); \
std::cerr << "\"" << std::endl; \
return 1; \
} \
} while (false)

Fails the test (return 1) when expected != actual.

Parameters
expectedExpected value.
actualObserved value.

◆ ASSERT_FAIL

#define ASSERT_FAIL ( reason)
Value:
do { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": " << (reason) << std::endl; \
return 1; \
} while (false)

Fails the test.

For a branch that must not be reached.

Parameters
reasonText written to the log.

◆ ASSERT_FALSE

#define ASSERT_FALSE ( condition)
Value:
do { \
if ((condition)) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": condition is true, expected false" << std::endl; \
return 1; \
} \
} while (false)

Fails the test (return 1) when condition is true.

Parameters
conditionExpression that must be false.

◆ ASSERT_NEAR

#define ASSERT_NEAR ( expected,
actual,
tolerance )
Value:
do { \
const auto stormbyte_expected = (expected); \
const auto stormbyte_actual = (actual); \
const auto stormbyte_tolerance = (tolerance); \
const auto stormbyte_difference = stormbyte_expected > stormbyte_actual ? \
stormbyte_expected - stormbyte_actual : stormbyte_actual - stormbyte_expected; \
if (stormbyte_difference > stormbyte_tolerance) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": values differ beyond tolerance" << std::endl; \
return 1; \
} \
} while (false)

Fails when two arithmetic values differ by more than tolerance.

Parameters
expectedExpected value.
actualObserved value.
toleranceMaximum accepted difference.

◆ ASSERT_NO_THROW

#define ASSERT_NO_THROW ( expression)
Value:
do { \
try { \
(void)(expression); \
} catch (const std::exception& stormbyte_exception) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": unexpected exception: " << stormbyte_exception.what() << std::endl; \
return 1; \
} catch (...) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": unexpected non-standard exception" << std::endl; \
return 1; \
} \
} while (false)

Fails when expression throws any exception.

Parameters
expressionExpression that must not throw.

◆ ASSERT_NOT_CONTAINS

#define ASSERT_NOT_CONTAINS ( haystack,
needle )
Value:
do { \
const auto& stormbyte_haystack = (haystack); \
const auto& stormbyte_needle = (needle); \
if (stormbyte_haystack.find(stormbyte_needle) != std::string_view::npos) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected text not to contain \"" << stormbyte_needle << "\"" << std::endl; \
return 1; \
} \
} while (false)

Fails when haystack contains needle.

Parameters
haystackText that must not contain the needle.
needleText that must be absent.

◆ ASSERT_NOT_EMPTY

#define ASSERT_NOT_EMPTY ( container)
Value:
do { \
const auto& stormbyte_container = (container); \
if (stormbyte_container.empty()) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected not empty" << std::endl; \
return 1; \
} \
} while (false)

Fails when container is empty.

Parameters
containerContainer with empty().

◆ ASSERT_NOT_EQUAL

#define ASSERT_NOT_EQUAL ( expected,
actual )
Value:
do { \
const auto& stormbyte_expected = (expected); \
const auto& stormbyte_actual = (actual); \
if (stormbyte_expected == stormbyte_actual) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": values should differ; both were \""; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_actual); \
std::cerr << "\"" << std::endl; \
return 1; \
} \
} while (false)

Fails the test (return 1) when expected == actual.

Parameters
expectedValue that must differ.
actualObserved value.

◆ ASSERT_NOT_NULL

#define ASSERT_NOT_NULL ( pointer)
Value:
do { \
if ((pointer) == nullptr) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": pointer is null" << std::endl; \
return 1; \
} \
} while (false)

Fails when pointer is null.

Parameters
pointerPointer that must not be null.

◆ ASSERT_NULL

#define ASSERT_NULL ( pointer)
Value:
do { \
if ((pointer) != nullptr) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": pointer is not null" << std::endl; \
return 1; \
} \
} while (false)

Fails when pointer is not null.

Parameters
pointerPointer that must be null.

◆ ASSERT_SIZE

#define ASSERT_SIZE ( container,
expected )
Value:
do { \
const auto& stormbyte_container = (container); \
const auto stormbyte_expected = (expected); \
if (!(stormbyte_container.size() == stormbyte_expected)) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected size \""; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_expected); \
std::cerr << "\", got \""; \
::StormByte::Test::Detail::PrintValue(std::cerr, stormbyte_container.size()); \
std::cerr << "\"" << std::endl; \
return 1; \
} \
} while (false)

Fails when container size() is not expected.

Parameters
containerContainer with size().
expectedExpected size.

◆ ASSERT_THROWS

#define ASSERT_THROWS ( expression,
exception_type )
Value:
do { \
bool stormbyte_threw = false; \
try { \
(void)(expression); \
} catch (const exception_type&) { \
stormbyte_threw = true; \
} catch (...) { \
} \
if (!stormbyte_threw) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": expected " << #exception_type << " to be thrown" << std::endl; \
return 1; \
} \
} while (false)

Fails when expression does not throw exception_type.

Parameters
expressionExpression that must throw.
exception_typeException type that must be caught.

◆ ASSERT_TRUE

#define ASSERT_TRUE ( condition)
Value:
do { \
if (!(condition)) { \
std::cerr << __func__ << ": Assertion failed at " << __FILE__ << ":" << __LINE__ << ": condition is false, expected true" << std::endl; \
return 1; \
} \
} while (false)

Fails the test (return 1) when condition is false.

Parameters
conditionExpression that must be true.

◆ CurrentFileDirectory

#define CurrentFileDirectory   std::filesystem::path(__FILE__).parent_path()

Directory of the translation unit (std::filesystem::path of __FILE__).

◆ RETURN_TEST

#define RETURN_TEST ( fn_result)
Value:
do { \
const int stormbyte_test_result = (fn_result); \
if (stormbyte_test_result != 0) { \
std::cerr << "Test " << __func__ << " FAILED!" << std::endl; \
} \
return stormbyte_test_result; \
} while (false)

Prints FAILED to stderr when fn_result is not 0, then returns that value.

Parameters
fn_resultInteger status (0 = pass).

The test name is __func__ of the function that expands the macro.