Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement SNITCH_DISABLE feature #192

Merged
merged 21 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ set(SNITCH_MAX_PATH_LENGTH 1024 CACHE STRING "Maximum length of a file
set(SNITCH_MAX_REPORTER_SIZE_BYTES 128 CACHE STRING "Maximum size (in bytes) of a reporter object.")

# Feature toggles.
set(SNITCH_DISABLE OFF CACHE BOOL "Disable Snitch at build time")
NicoMuleo marked this conversation as resolved.
Show resolved Hide resolved
set(SNITCH_DEFINE_MAIN ON CACHE BOOL "Define main() in snitch -- disable to provide your own main() function.")
set(SNITCH_WITH_EXCEPTIONS ON CACHE BOOL "Use exceptions in snitch implementation -- will be forced OFF if exceptions are not available.")
set(SNITCH_WITH_MULTITHREADING ON CACHE BOOL "Make the testing framework thread-safe -- disable if multithreading is not needed.")
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ The goal of _snitch_ is to be a simple, cheap, non-invasive, and user-friendly t
- Additional API not in _Catch2_, or different from _Catch2_:
- Matchers use a different API (see [Matchers](#matchers) below).
- Additional macros for testing [`constexpr`](#run-time-and-compile-time) and [`consteval`](#compile-time) expressions.
- Can be disabled at build time. As a side effect, this will reduce the size of the executable, as most, if not all, the Snitch symbols will be removed by the compiler and/or linker (especially when LTO is enabled).
NicoMuleo marked this conversation as resolved.
Show resolved Hide resolved

If you need features that are not in the list above, please use _Catch2_ or _doctest_.

Expand Down
3 changes: 3 additions & 0 deletions include/snitch/snitch_config.hpp.config
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
#if !defined(SNITCH_SHARED_LIBRARY)
#cmakedefine01 SNITCH_SHARED_LIBRARY
#endif
#if !defined(SNITCH_DISABLE)
#cmakedefine01 SNITCH_DISABLE
#endif
cschreib marked this conversation as resolved.
Show resolved Hide resolved
// clang-format on

#if defined(_MSC_VER)
Expand Down
166 changes: 94 additions & 72 deletions include/snitch/snitch_macros_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,97 +4,119 @@
#include "snitch/snitch_config.hpp"
#include "snitch/snitch_expression.hpp"
#include "snitch/snitch_macros_check_base.hpp"
#include "snitch/snitch_macros_utility.hpp"
#include "snitch/snitch_macros_warnings.hpp"
#include "snitch/snitch_matcher.hpp"
#include "snitch/snitch_registry.hpp"
#include "snitch/snitch_test_data.hpp"

#define SNITCH_REQUIRE_IMPL(CHECK, EXPECTED, MAYBE_ABORT, ...) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
SNITCH_WARNING_PUSH \
SNITCH_WARNING_DISABLE_PARENTHESES \
SNITCH_WARNING_DISABLE_CONSTANT_COMPARISON \
if constexpr (SNITCH_IS_DECOMPOSABLE(__VA_ARGS__)) { \
SNITCH_EXPR(CHECK, EXPECTED, __VA_ARGS__); \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} else { \
const auto SNITCH_CURRENT_EXPRESSION = snitch::impl::expression{ \
CHECK, #__VA_ARGS__, {}, static_cast<bool>(__VA_ARGS__) == EXPECTED}; \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} \
SNITCH_WARNING_POP \
} while (0)
#if !(SNITCH_DISABLE)
cschreib marked this conversation as resolved.
Show resolved Hide resolved

# define SNITCH_REQUIRE_IMPL(CHECK, EXPECTED, MAYBE_ABORT, ...) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
SNITCH_WARNING_PUSH \
SNITCH_WARNING_DISABLE_PARENTHESES \
SNITCH_WARNING_DISABLE_CONSTANT_COMPARISON \
if constexpr (SNITCH_IS_DECOMPOSABLE(__VA_ARGS__)) { \
SNITCH_EXPR(CHECK, EXPECTED, __VA_ARGS__); \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} else { \
const auto SNITCH_CURRENT_EXPRESSION = snitch::impl::expression{ \
CHECK, #__VA_ARGS__, {}, static_cast<bool>(__VA_ARGS__) == EXPECTED}; \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} \
SNITCH_WARNING_POP \
} while (0)

// clang-format off
#define SNITCH_REQUIRE(...) SNITCH_REQUIRE_IMPL("REQUIRE", true, SNITCH_TESTING_ABORT, __VA_ARGS__)
#define SNITCH_CHECK(...) SNITCH_REQUIRE_IMPL("CHECK", true, (void)0, __VA_ARGS__)
#define SNITCH_REQUIRE_FALSE(...) SNITCH_REQUIRE_IMPL("REQUIRE_FALSE", false, SNITCH_TESTING_ABORT, __VA_ARGS__)
#define SNITCH_CHECK_FALSE(...) SNITCH_REQUIRE_IMPL("CHECK_FALSE", false, (void)0, __VA_ARGS__)
# define SNITCH_REQUIRE(...) SNITCH_REQUIRE_IMPL("REQUIRE", true, SNITCH_TESTING_ABORT, __VA_ARGS__)
# define SNITCH_CHECK(...) SNITCH_REQUIRE_IMPL("CHECK", true, (void)0, __VA_ARGS__)
# define SNITCH_REQUIRE_FALSE(...) SNITCH_REQUIRE_IMPL("REQUIRE_FALSE", false, SNITCH_TESTING_ABORT, __VA_ARGS__)
# define SNITCH_CHECK_FALSE(...) SNITCH_REQUIRE_IMPL("CHECK_FALSE", false, (void)0, __VA_ARGS__)
// clang-format on

#define SNITCH_SUCCEED(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(true, (MESSAGE)); \
} while (0)

#define SNITCH_FAIL(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(false, (MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

#define SNITCH_FAIL_CHECK(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(false, (MESSAGE)); \
} while (0)

#define SNITCH_SKIP(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_skipped((MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

#define SNITCH_SKIP_CHECK(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_skipped((MESSAGE)); \
} while (0)

#define SNITCH_REQUIRE_THAT_IMPL(CHECK, MAYBE_ABORT, EXPR, ...) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
const auto SNITCH_TEMP_RESULT = snitch::impl::match(EXPR, __VA_ARGS__); \
const auto SNITCH_CURRENT_EXPRESSION = snitch::impl::expression{ \
CHECK, #EXPR ", " #__VA_ARGS__, \
snitch::resize_or_truncate<snitch::max_expr_length>(SNITCH_TEMP_RESULT.second), \
SNITCH_TEMP_RESULT.first}; \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} while (0)
# define SNITCH_SUCCEED(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(true, (MESSAGE)); \
} while (0)

# define SNITCH_FAIL(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(false, (MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

# define SNITCH_FAIL_CHECK(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_assertion(false, (MESSAGE)); \
} while (0)

# define SNITCH_SKIP(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_skipped((MESSAGE)); \
SNITCH_TESTING_ABORT; \
} while (0)

# define SNITCH_SKIP_CHECK(MESSAGE) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
snitch::registry::report_skipped((MESSAGE)); \
} while (0)

# define SNITCH_REQUIRE_THAT_IMPL(CHECK, MAYBE_ABORT, EXPR, ...) \
do { \
auto SNITCH_CURRENT_CHECK = SNITCH_NEW_CHECK; \
const auto SNITCH_TEMP_RESULT = snitch::impl::match(EXPR, __VA_ARGS__); \
const auto SNITCH_CURRENT_EXPRESSION = snitch::impl::expression{ \
CHECK, #EXPR ", " #__VA_ARGS__, \
snitch::resize_or_truncate<snitch::max_expr_length>(SNITCH_TEMP_RESULT.second), \
SNITCH_TEMP_RESULT.first}; \
SNITCH_REPORT_EXPRESSION(MAYBE_ABORT); \
} while (0)

// clang-format off
#define SNITCH_REQUIRE_THAT(EXPR, ...) SNITCH_REQUIRE_THAT_IMPL("REQUIRE_THAT", SNITCH_TESTING_ABORT, EXPR, __VA_ARGS__)
#define SNITCH_CHECK_THAT(EXPR, ...) SNITCH_REQUIRE_THAT_IMPL("CHECK_THAT", (void)0, EXPR, __VA_ARGS__)
# define SNITCH_REQUIRE_THAT(EXPR, ...) SNITCH_REQUIRE_THAT_IMPL("REQUIRE_THAT", SNITCH_TESTING_ABORT, EXPR, __VA_ARGS__)
# define SNITCH_CHECK_THAT(EXPR, ...) SNITCH_REQUIRE_THAT_IMPL("CHECK_THAT", (void)0, EXPR, __VA_ARGS__)
// clang-format on

#else // SNITCH_DISABLE
// clang-format off
# define SNITCH_REQUIRE(...) SNITCH_DISCARD_ARGS(__VA_ARGS__)
# define SNITCH_CHECK(...) SNITCH_DISCARD_ARGS(__VA_ARGS__)
# define SNITCH_REQUIRE_FALSE(...) SNITCH_DISCARD_ARGS(__VA_ARGS__)
# define SNITCH_CHECK_FALSE(...) SNITCH_DISCARD_ARGS(__VA_ARGS__)

# define SNITCH_SUCCEED(MESSAGE) SNITCH_VOID_STATEMENT
# define SNITCH_FAIL(MESSAGE) SNITCH_VOID_STATEMENT
# define SNITCH_FAIL_CHECK(MESSAGE) SNITCH_VOID_STATEMENT
# define SNITCH_SKIP(MESSAGE) SNITCH_VOID_STATEMENT
# define SNITCH_SKIP_CHECK(MESSAGE) SNITCH_VOID_STATEMENT

# define SNITCH_REQUIRE_THAT(EXPR, ...) SNITCH_DISCARD_ARGS(EXPR, __VA_ARGS__)
# define SNITCH_CHECK_THAT(EXPR, ...) SNITCH_DISCARD_ARGS(EXPR, __VA_ARGS__)
// clang-format on

#endif // SNITCH_DISABLE

// clang-format on
#if SNITCH_WITH_SHORTHAND_MACROS
# define SUCCEED(MESSAGE) SNITCH_SUCCEED(MESSAGE)
# define FAIL(MESSAGE) SNITCH_FAIL(MESSAGE)
# define SUCCEED(MESSAGE) SNITCH_SUCCEED(MESSAGE)
# define FAIL(MESSAGE) SNITCH_FAIL(MESSAGE)
# define FAIL_CHECK(MESSAGE) SNITCH_FAIL_CHECK(MESSAGE)
# define SKIP(MESSAGE) SNITCH_SKIP(MESSAGE)
# define SKIP(MESSAGE) SNITCH_SKIP(MESSAGE)
# define SKIP_CHECK(MESSAGE) SNITCH_SKIP_CHECK(MESSAGE)

# define REQUIRE(...) SNITCH_REQUIRE(__VA_ARGS__)
# define CHECK(...) SNITCH_CHECK(__VA_ARGS__)
# define REQUIRE_FALSE(...) SNITCH_REQUIRE_FALSE(__VA_ARGS__)
# define CHECK_FALSE(...) SNITCH_CHECK_FALSE(__VA_ARGS__)
# define REQUIRE(...) SNITCH_REQUIRE(__VA_ARGS__)
# define CHECK(...) SNITCH_CHECK(__VA_ARGS__)
# define REQUIRE_FALSE(...) SNITCH_REQUIRE_FALSE(__VA_ARGS__)
# define CHECK_FALSE(...) SNITCH_CHECK_FALSE(__VA_ARGS__)
# define REQUIRE_THAT(EXP, ...) SNITCH_REQUIRE_THAT(EXP, __VA_ARGS__)
# define CHECK_THAT(EXP, ...) SNITCH_CHECK_THAT(EXP, __VA_ARGS__)
# define CHECK_THAT(EXP, ...) SNITCH_CHECK_THAT(EXP, __VA_ARGS__)
#endif
// clang-format on

Expand Down
4 changes: 3 additions & 1 deletion include/snitch/snitch_macros_check_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#endif

#define SNITCH_NEW_CHECK \
snitch::impl::scoped_test_check { SNITCH_CURRENT_LOCATION }
snitch::impl::scoped_test_check { \
SNITCH_CURRENT_LOCATION \
}

#define SNITCH_EXPR(TYPE, EXPECTED, ...) \
auto SNITCH_CURRENT_EXPRESSION = \
Expand Down
Loading
Loading