#include <exception>
#include <filesystem>
#include <iostream>
#include <ostream>
#include <string_view>
Go to the source code of this file.
|
| #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.
|
◆ 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
-
| haystack | Text that must contain the needle. |
| needle | Text 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
-
| container | Container 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
-
| expected | Expected value. |
| actual | Observed 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
-
| reason | Text 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
-
| condition | Expression 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
-
| expected | Expected value. |
| actual | Observed value. |
| tolerance | Maximum 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
-
| expression | Expression 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
-
| haystack | Text that must not contain the needle. |
| needle | Text 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
-
| container | Container 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
-
| expected | Value that must differ. |
| actual | Observed 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
-
| pointer | Pointer 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
-
| pointer | Pointer 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
-
| container | Container with size(). |
| expected | Expected 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
-
| expression | Expression that must throw. |
| exception_type | Exception 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
-
| condition | Expression 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_result | Integer status (0 = pass). |
The test name is __func__ of the function that expands the macro.