diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d71f0e2..420af0a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,12 +1,12 @@ name: Build -on: [push, pull_request_target] +on: [push, pull_request] jobs: build-linux: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v7 - name: Build tests run: ./scripts/compile_clang.sh - name: Run Tests @@ -15,7 +15,7 @@ jobs: build-macos: runs-on: macos-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v7 - name: Build tests run: ./scripts/compile_clang.sh - name: Run Tests @@ -27,7 +27,7 @@ jobs: arch: [amd64, amd64_x86] #, amd64_arm64] runs-on: windows-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v7 - uses: ilammy/msvc-dev-cmd@v1 with: arch: ${{ matrix.arch }} diff --git a/test/jc_test.h b/test/jc_test.h index 49854d9..8df3ffc 100644 --- a/test/jc_test.h +++ b/test/jc_test.h @@ -1,1752 +1,1143 @@ -/* test.h Copyright 2018-2022 Mathias Westerdahl +/* jc_test.h - single-header C11 test framework + * Copyright 2018-2026 Mathias Westerdahl + * SPDX-License-Identifier: MIT * - * https://github.com/JCash/jctest - * https://jcash.github.io/jctest - * - * BRIEF: - * - * A small, single header only C++-11 test framework - * Made sure to compile with highest warning/error levels possible - * - * HISTORY: - * 0.9 2022-12-22 Fixed proper printout for pointer values - * Minimum version is now C++11 due to usage of - * Removed doctest support - * 0.8 2021-04-03 Added fflush to logging to prevent test output becoming out of order - * 0.7 2021-02-07 Fixed null pointer warning on C++0x and above - * Test filtering now works on parameterized tests - * 0.6 2020-03-12 Fixed bootstrap issue w/static initializers - * Added support for JC_TEST_USE_COLORS to force color on/off - * Added support for JC_TEST_USE_DEFAULT_MAIN - * 0.5 2019-11-10 Added support for logging enum values - * Added ASSERT_ARRAY_EQ - * 0.4 2019-08-10 Fix for outputting 64 bit integer values upon error - * Skipping tests now doesn't output extraneous info - * 0.3 2019-04-25 Ansi colors for Win32 - * Msys2 + Cygwin support - * setjmp fix for Emscripten - * Removed limit on number of tests - * 0.2 2019-04-14 Fixed ASSERT_EQ for single precision floats - * 0.1 2019-01-19 Added GTEST-like C++ interface - * - * LICENSE: - * - * The MIT License (MIT) - * - * Copyright (c) 2018-2022 Mathias Westerdahl - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - * DISCLAIMER: - * - * This software is supplied "AS IS" without any warranties and support - * - * This software was designed to be a (non complete) replacement for GTEST, - * with the intent to replace the library in an existing code base. - * Although the supported features were implemented in the same spirit as the GTEST - * fixtures/functions, there will be discprepancies. However, those differences have - * been chosen from a pragmatic standpoint, in favor of making porting of the existing - * tests feasible with minimal changes, as well as keeping this library - * as light weight as possible. - * - * - * USAGE: - * For more use cases, see end of document - -// Smallest test case -#define JC_TEST_IMPLEMENTATION -#include - -TEST(FixtureName, TestName) { - ASSERT_EQ(4, 2*2); -} - -int main(int argc, char** argv) { - jc_test_init(&argc, argv); - return jc_test_run_all(); -} + * C++ translation units are forwarded to jc_test.hpp for source compatibility. + * Define JC_TEST_IMPLEMENTATION in exactly one C translation unit. */ -#ifndef JC_TEST_H -#define JC_TEST_H - -// *************************************************************************************** -// PUBLIC API - -// May modify the argument list, to remove the test specific arguments -extern void jc_test_init(int* argc, char** argv); - -// Runs all registered tests -extern int jc_test_run_all(); - -// The standard test class -struct jc_test_base_class { - virtual ~jc_test_base_class(); - static void SetUpTestCase() {} // The UserClass::SetUpTestCase is called before each test case runs - static void TearDownTestCase() {} // The UserClass::TearDownTestCase is called after all tests have run - virtual void SetUp(); // Called before each test - virtual void TearDown(); // Called after each test - virtual void TestBody() = 0; // Implemented by TEST_F and TEST_P -private: - struct Setup_should_be_spelled_SetUp {}; - virtual Setup_should_be_spelled_SetUp* Setup(); // Trick from GTEST to make sure users don't accidentally misspell the function -}; +#ifndef JC_TEST_C_DISPATCH_H +#define JC_TEST_C_DISPATCH_H -// A parameterized test class, to use with TEST_P and INSTANTIATE_TEST_CASE_P -template -struct jc_test_params_class : public jc_test_base_class { - typedef ParamType param_t; - jc_test_params_class() {} - static const ParamType& GetParam() { return *param; } - static void SetParam(const ParamType* _param) { param = _param; } - static const ParamType* param; -}; +#if defined(__cplusplus) -// *************************************************************************************** -// TEST API - -// Basic test -// #define TEST(testfixture,testfn) -// -// Basic tests using a user defined fixture class (jc_test_base_class) -// #define TEST_F(testfixture,testfn) -// -// Parameterized tests using a user defined fixture class (jc_test_params_class) -// #define TEST_P(testfixture,testfn) -// -// Instantiation of parameterisaed test -// #define INSTANTIATE_TEST_CASE_P(prefix,testfixture,testvalues) - -// *************************************************************************************** -// ASSERTION API - -// #define SKIP() Skips the test - -// Fatal failures are prefixed ASSERT_ -// Non fatal failures are prefixed EXPECT_ - -// #define ASSERT_TRUE( VALUE ) value -// #define ASSERT_FALSE( VALUE ) !value -// #define ASSERT_EQ( A, B ) A == B -// #define ASSERT_NE( A, B ) A != B -// #define ASSERT_LT( A, B ) A < B -// #define ASSERT_GT( A, B ) A > B -// #define ASSERT_LE( A, B ) A <= B -// #define ASSERT_GE( A, B ) A >= B -// #define ASSERT_STREQ( A, B ) strcmp(A,B) == 0 -// #define ASSERT_STRNE( A, B ) strcmp(A,B) != 0 -// #define ASSERT_NEAR( A, B, EPSILON ) abs(a - b) < epsilon -// #define ASSERT_DEATH(S, RE) -// #define ASSERT_ARRAY_EQ( A, B ) memcmp(A, B, sizeof(A)) == 0 -// #define ASSERT_ARRAY_EQ_LEN( A, B, LENGTH) memcmp(A, B, LENGTH) == 0 - -// #define SCOPED_TRACE(_MSG) // nop - - -// *************************************************************************************** -// Possible modifications for the included files - -// Can be used to override the logging entirely (e.g. for writing html output) -#ifndef JC_TEST_LOGF - #include //va_list - // Can be overridden to log in a different way - #define JC_TEST_LOGF jc_test_logf -#endif +#include "jc_test.hpp" -#ifndef JC_TEST_SNPRINTF - #include //snprintf - #if defined(_MSC_VER) - #define JC_TEST_SNPRINTF _snprintf - #else - #define JC_TEST_SNPRINTF snprintf - #endif -#endif +#else -#ifndef JC_TEST_ASSERT_FN - #include - #define JC_TEST_ASSERT_FN assert +#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 201112L) +#error "jc_test.h requires C11 or newer" #endif -#ifndef JC_TEST_EXIT - #include - #define JC_TEST_EXIT exit +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(_WIN32) +#include +#include +#else +#include +#include #endif -#ifndef JC_TEST_NO_DEATH_TEST - #include - #include // setjmp+longjmp -#if defined(__EMSCRIPTEN__) || defined(__MINGW32__) - #define JC_TEST_SETJMP setjmp +#if !defined(JC_TEST_SNPRINTF) +#if defined(_MSC_VER) +#define JC_TEST_SNPRINTF _snprintf #else - #define JC_TEST_SETJMP _setjmp +#define JC_TEST_SNPRINTF snprintf #endif #endif -#include // painful - -// C++0x and above -#if !defined(_MSC_VER) - #pragma GCC diagnostic push - #if !defined(__GNUC__) - #if __cplusplus >= 199711L - // Silencing them made the code unreadable, so I opted to disable them instead - #pragma GCC diagnostic ignored "-Wc++98-compat" - #endif - #endif - #if __cplusplus >= 201103L - #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" - #endif +#if !defined(JC_TEST_EXIT) +#define JC_TEST_EXIT exit #endif -#if __cplusplus > 199711L - #define JC_OVERRIDE override -#else - #define JC_OVERRIDE +#if !defined(JC_TEST_ASSERT_FN) +#include +#define JC_TEST_ASSERT_FN assert #endif -#if defined(__GNUC__) || defined(__clang__) - #define JC_TEST_UNUSED __attribute__ ((unused)) +#if !defined(JC_TEST_DBG_BREAK) +#if defined(_MSC_VER) +#include +#define JC_TEST_DBG_BREAK() __debugbreak() +#elif defined(__has_builtin) +#if __has_builtin(__builtin_debugtrap) +#define JC_TEST_DBG_BREAK() __builtin_debugtrap() #else - #define JC_TEST_UNUSED +#define JC_TEST_DBG_BREAK() abort() #endif - -#if !defined(JC_TEST_NO_STDINT_H) - #include +#else +#define JC_TEST_DBG_BREAK() abort() #endif - -#if !defined(JC_FMT_U64) - #if defined(__ANDROID__) - #define JC_FMT_U64 "%llu" - #define JC_FMT_I64 "%lld" - #else - #if __cplusplus == 199711L && (defined(__GNUC__) && !defined(__clang__)) - // For some reason GCC would always warn about the format not working in c++98 (it does though) - #pragma GCC diagnostic ignored "-Wformat" - #endif - #include - #define JC_FMT_U64 "%" PRIu64 - #define JC_FMT_I64 "%" PRId64 - #endif #endif -#if defined(__x86_64__) || defined(__arm64) || defined(__aarch64__) || defined(__ppc64__) || defined(_WIN64) - #define JC_TEST_64BIT +#if defined(__GNUC__) || defined(__clang__) +#define JC_TEST_C_UNUSED __attribute__((unused)) +#else +#define JC_TEST_C_UNUSED #endif -enum jc_test_event -{ - JC_TEST_EVENT_FIXTURE_SETUP = 0, - JC_TEST_EVENT_FIXTURE_TEARDOWN = 1, - JC_TEST_EVENT_TEST_SETUP = 2, - JC_TEST_EVENT_TEST_TEARDOWN = 3, - JC_TEST_EVENT_ASSERT_FAILED = 4, - JC_TEST_EVENT_SUMMARY = 5, - JC_TEST_EVENT_GENERIC = 6 -}; - -#ifndef JC_TEST_TIMING_FUNC - #define JC_TEST_TIMING_FUNC jc_test_get_time // returns micro seconds +typedef uint64_t jc_test_time_t; - typedef unsigned long jc_test_time_t; - extern jc_test_time_t jc_test_get_time(void); +#if !defined(JC_TEST_TIMING_FUNC) +#define JC_TEST_TIMING_FUNC jc_test_get_time +jc_test_time_t jc_test_get_time(void); +#define JC_TEST_C_DEFAULT_TIMING 1 #endif -// Returns the user defined context for the fixture -typedef void* (*jc_test_fixture_setup_func)(); - -struct jc_test_factory_base_interface; -typedef void (*jc_test_void_staticfunc)(); -typedef void (jc_test_base_class::*jc_test_void_memberfunc)(); - -typedef struct jc_test_entry { - jc_test_entry* next; // linked list - const char* name; - jc_test_base_class* instance; - jc_test_factory_base_interface* factory; // Factory for parameterized tests - uint32_t fail:1; - uint32_t skipped:1; - uint32_t :30; - #if defined(JC_TEST_64BIT) - uint32_t :32; - #endif -} jc_test_entry; +#if !defined(JCT_IS_DEBUGGER_ATTACHED) +#define JCT_IS_DEBUGGER_ATTACHED() 0 +#endif typedef struct jc_test_stats { int num_pass; - int num_fail:16; - int num_skipped:16; + int num_fail; + int num_skipped; int num_assertions; int num_tests; jc_test_time_t totaltime; } jc_test_stats; -typedef struct jc_test_fixture { - virtual ~jc_test_fixture(); - virtual void SetParam(); - virtual void Instantiate(); - jc_test_fixture* next; // linked list - jc_test_entry* tests; // linked list - const char* name; // The name of the fixture - const char* filename; // The filename of the current ASSERT/EXPECT - const char* prototype; // The name of any prototype fixture - struct jc_test_fixture* parent; // In case of parameterized tests, this points to the first test - jc_test_void_staticfunc fixture_setup; - jc_test_void_staticfunc fixture_teardown; - jc_test_stats stats; - unsigned int fail:26; - unsigned int type:2; // 0: function, 1: class, 2: params class, 3: params instance - unsigned int first:1; // If it's the first in a range of fixtures - unsigned int last:1; // If it's the last in a range of fixtures - unsigned int fatal:1; // If set, it aborts the test - unsigned int skipped:1; // All tests are skipped, we skip the whole fixture - unsigned int index; // the index of the param in the original params array - int signum:8; // If we're checking for a signal - int line:16; // The line of the current ASSERT/EXPECT - int _pad:8; - int num_tests; -} jc_test_fixture; - -typedef struct jc_test_state { - #if !defined(JC_TEST_NO_DEATH_TEST) - jmp_buf jumpenv; // Set before trying to catch exceptions - // remove padding warning due to jmp_buf struct alignment - unsigned char _pad[sizeof(jmp_buf)+sizeof(void*) - (sizeof(jmp_buf)/sizeof(void*))*sizeof(void*)]; - #endif - - jc_test_entry* current_test; - jc_test_fixture* current_fixture; - jc_test_fixture* fixtures; - jc_test_stats stats; - char** filter_patterns; - unsigned int num_filter_patterns:8; - unsigned int :24; - int num_fixtures:31; - unsigned int use_colors:1; -} jc_test_state; - -// *************************************************************************************** -// Private functions - -extern jc_test_state* jc_test_get_state(); -extern void jc_test_exit(); // called by jc_test_run_all -extern void jc_test_set_test_fail(int fatal); -extern void jc_test_set_test_skipped(); -extern void jc_test_increment_assertions(); -extern void jc_test_set_signal_handler(); -extern void jc_test_unset_signal_handler(); -extern int jc_test_streq(const char* a, const char* b); -extern void jc_test_logf(const jc_test_fixture* fixture, const jc_test_entry* test, const jc_test_stats* stats, jc_test_event event, const char* format, ...); -extern int jc_test_cmp_double_eq(double, double); -extern int jc_test_cmp_float_eq(float, float); -extern int jc_test_cmp_STREQ(const char* a, const char* b, const char* exprA, const char* exprB); -extern int jc_test_cmp_STRNE(const char* a, const char* b, const char* exprA, const char* exprB); -extern int jc_test_cmp_NEAR(double a, double b, double epsilon, const char* exprA, const char* exprB, const char* exprC); - -#define JC_TEST_CAST(_TYPE_, _EXPR_) reinterpret_cast< _TYPE_ >( _EXPR_ ) -#define JC_TEST_STATIC_CAST(_TYPE_, _EXPR_) static_cast< _TYPE_ >( _EXPR_ ) - -static inline jc_test_fixture* jc_test_get_fixture() { - return jc_test_get_state()->current_fixture; -} -static inline jc_test_entry* jc_test_get_test() { - return jc_test_get_state()->current_test; -} - -template -typename std::enable_if::value, char*>::type -jc_test_print_value(char* buffer, size_t buffer_len, const T value) { - return buffer + JC_TEST_SNPRINTF(buffer, buffer_len, "%d", JC_TEST_STATIC_CAST(int, value)); -} - -template -typename std::enable_if< !std::is_enum::value && std::is_pointer::value, char*>::type -jc_test_print_value(char* buffer, size_t buffer_len, const T value) { - return buffer + JC_TEST_SNPRINTF(buffer, buffer_len, "%p", JC_TEST_CAST(const void*, value)); -} - -template -typename std::enable_if< !std::is_enum::value && !std::is_pointer::value, char*>::type -jc_test_print_value(char* buffer, size_t, const T) { - buffer[0] = '?'; buffer[1] = 0; - return buffer+2; -} - -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const double value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const float value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const int8_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const uint8_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const int16_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const uint16_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const int32_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const uint32_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const int64_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const uint64_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const char* value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const std::nullptr_t value); -template <> char* jc_test_print_value(char* buffer, size_t buffer_len, const struct _jc_test_null_literal* value); - -template -static inline void jc_test_log_failure(T1 a, T2 b, const char* exprA, const char* exprB, const char* op) { - char bufferA[64]; jc_test_print_value(bufferA, sizeof(bufferA), a); - char bufferB[64]; jc_test_print_value(bufferB, sizeof(bufferB), b); - JC_TEST_LOGF(jc_test_get_fixture(), jc_test_get_test(), 0, JC_TEST_EVENT_ASSERT_FAILED, "\nExpected: (%s) %s (%s), actual: %s vs %s\n", exprA, op, exprB, bufferA, bufferB); -} - -template -static inline void jc_test_log_failure_boolean(T v, const char* expr) { - JC_TEST_LOGF(jc_test_get_fixture(), jc_test_get_test(), 0, JC_TEST_EVENT_ASSERT_FAILED, "\nValue of: %s\nExpected: %s\n Actual: %s\n", expr, (!v)?"true":"false", v?"true":"false"); -} +typedef enum jc_test_value_kind { + JC_TEST_VALUE_SIGNED, + JC_TEST_VALUE_UNSIGNED, + JC_TEST_VALUE_FLOAT, + JC_TEST_VALUE_DOUBLE, + JC_TEST_VALUE_LONG_DOUBLE, + JC_TEST_VALUE_POINTER +} jc_test_value_kind; + +typedef struct jc_test_value { + jc_test_value_kind kind; + unsigned int bits; + union { + intmax_t signed_value; + uintmax_t unsigned_value; + long double float_value; + const void* pointer_value; + } data; +} jc_test_value; -template -static inline void jc_test_log_failure_str(const T* a, const T* b, const char* exprA, const char* exprB, const char* op) { - JC_TEST_LOGF(jc_test_get_fixture(), jc_test_get_test(), 0, JC_TEST_EVENT_ASSERT_FAILED, "\nValue of: %s %s %s\nExpected: %s\n Actual: %s\n", exprA, op, exprB, a, b); -} +typedef void (*jc_test_c_func)(void); -template -int jc_test_cmp_TRUE(T v, const char* expr) { - if (v) return 1; - jc_test_log_failure_boolean(v, expr); - return 0; -} -template -int jc_test_cmp_FALSE(T v, const char* expr) { - if (!v) return 1; - jc_test_log_failure_boolean(v, expr); - return 0; -} +typedef struct jc_test_entry { + struct jc_test_entry* next; + const char* suite_name; + const char* test_name; + const char* filename; + int line; + jc_test_c_func run; + jc_test_time_t time; + unsigned int fail:1; + unsigned int skipped:1; +} jc_test_entry; -#define JC_TEST_COMPARE_FUNC(OP_NAME, OP) \ - template \ - int jc_test_cmp_ ## OP_NAME(T1 a, T2 b, const char* exprA, const char* exprB) { \ - if (a OP b) return 1; \ - jc_test_log_failure(a, b, exprA, exprB, #OP); \ - return 0; \ +void jc_test_init(int* argc, char** argv); +int jc_test_run_all(void); +void jc_test_register(jc_test_entry* test); + +void jc_test_assert_boolean(int value, int expected, const char* expression, + const char* file, int line, int fatal); +void jc_test_assert_compare(jc_test_value a, jc_test_value b, int operation, + const char* expression_a, const char* expression_b, + const char* file, int line, int fatal); +void jc_test_assert_string(const char* a, const char* b, int equal, + const char* expression_a, const char* expression_b, + const char* file, int line, int fatal); +void jc_test_assert_near(long double a, long double b, long double epsilon, + const char* expression_a, const char* expression_b, + const char* expression_epsilon, + const char* file, int line, int fatal); +void jc_test_assert_array(const void* a, const void* b, size_t length, + size_t element_size, const char* expression_a, + const char* expression_b, + const char* file, int line, int fatal); +int jc_test_current_fatal(void); +int jc_test_current_skipped(void); +void jc_test_set_test_skipped(void); +void jc_test_clear_fatal(void); + +static inline jc_test_value jc_test_value_signed(intmax_t value, unsigned int bits) { + jc_test_value result; + result.kind = JC_TEST_VALUE_SIGNED; + result.bits = bits; + result.data.signed_value = value; + return result; +} + +static inline jc_test_value jc_test_value_unsigned(uintmax_t value, unsigned int bits) { + jc_test_value result; + if (bits < (unsigned int)(sizeof(int) * CHAR_BIT)) { + result.kind = JC_TEST_VALUE_SIGNED; + result.bits = (unsigned int)(sizeof(int) * CHAR_BIT); + result.data.signed_value = (intmax_t)value; + } else { + result.kind = JC_TEST_VALUE_UNSIGNED; + result.bits = bits; + result.data.unsigned_value = value; } - -JC_TEST_COMPARE_FUNC(EQ, ==) -JC_TEST_COMPARE_FUNC(NE, !=) -JC_TEST_COMPARE_FUNC(LE, <=) -JC_TEST_COMPARE_FUNC(LT, <) -JC_TEST_COMPARE_FUNC(GE, >=) -JC_TEST_COMPARE_FUNC(GT, >) - -template int jc_test_cmp_EQ(double a, T b, const char* exprA, const char* exprB) { - if (jc_test_cmp_double_eq(a, JC_TEST_STATIC_CAST(double, b))) return 1; - jc_test_log_failure(a, b, exprA, exprB, "=="); - return 0; -} -template int jc_test_cmp_EQ(float a, T b, const char* exprA, const char* exprB) { - if (jc_test_cmp_float_eq(a, JC_TEST_STATIC_CAST(float, b))) return 1; - jc_test_log_failure(a, b, exprA, exprB, "=="); - return 0; + return result; } -template int jc_test_cmp_NE(double a, T b, const char* exprA, const char* exprB) { - if (!jc_test_cmp_double_eq(a, JC_TEST_STATIC_CAST(double, b))) return 1; - jc_test_log_failure(a, b, exprA, exprB, "!="); - return 0; -} -template int jc_test_cmp_NE(float a, T b, const char* exprA, const char* exprB) { - if (!jc_test_cmp_float_eq(a, JC_TEST_STATIC_CAST(float, b))) return 1; - jc_test_log_failure(a, b, exprA, exprB, "!="); - return 0; +static inline jc_test_value jc_test_value_float(float value, unsigned int bits) { + jc_test_value result; + (void)bits; + result.kind = JC_TEST_VALUE_FLOAT; + result.bits = (unsigned int)(sizeof(float) * CHAR_BIT); + result.data.float_value = (long double)value; + return result; } -int jc_test_cmp_array(const uint8_t* a, const uint8_t* b, size_t len, size_t typesize, int valuetype, const char* exprA, const char* exprB); - -template -int jc_test_cmp_ARRAY_EQ(const char (&a)[N], const char (&b)[N], const char* exprA, const char* exprB) { - return jc_test_cmp_array(JC_TEST_CAST(const uint8_t*, a), JC_TEST_CAST(const uint8_t*, b), N, sizeof(char), 1, exprA, exprB); -} - -template -int jc_test_cmp_ARRAY_EQ(T (&a)[N], T (&b)[N], const char* exprA, const char* exprB) { - return jc_test_cmp_array(JC_TEST_CAST(const uint8_t*, a), JC_TEST_CAST(const uint8_t*, b), N, sizeof(T), 0, exprA, exprB); +static inline jc_test_value jc_test_value_double(double value, unsigned int bits) { + jc_test_value result; + (void)bits; + result.kind = JC_TEST_VALUE_DOUBLE; + result.bits = (unsigned int)(sizeof(double) * CHAR_BIT); + result.data.float_value = (long double)value; + return result; } -template -int jc_test_cmp_ARRAY_EQ_LEN(const T* a, const T* b, size_t length, const char* exprA, const char* exprB, const char*) { - return jc_test_cmp_array(JC_TEST_CAST(const uint8_t*, a), JC_TEST_CAST(const uint8_t*, b), length, sizeof(T), 0, exprA, exprB); +static inline jc_test_value jc_test_value_long_double(long double value, unsigned int bits) { + jc_test_value result; + (void)bits; + result.kind = JC_TEST_VALUE_LONG_DOUBLE; + result.bits = (unsigned int)(sizeof(long double) * CHAR_BIT); + result.data.float_value = value; + return result; } -template<> -inline int jc_test_cmp_ARRAY_EQ_LEN(const char* a, const char* b, size_t length, const char* exprA, const char* exprB, const char*) { - return jc_test_cmp_array(JC_TEST_CAST(const uint8_t*, a), JC_TEST_CAST(const uint8_t*, b), length, sizeof(char), 1, exprA, exprB); +static inline jc_test_value jc_test_value_pointer(const void* value, unsigned int bits) { + jc_test_value result; + (void)bits; + result.kind = JC_TEST_VALUE_POINTER; + result.bits = (unsigned int)(sizeof(void*) * CHAR_BIT); + result.data.pointer_value = value; + return result; } -struct jc_test_cmp_eq_helper { - -#if __cplusplus > 199711L - template ::value || - !std::is_pointer::value>::type* = nullptr> - static int compare(const T1& a, const T2& b, const char* exprA, const char* exprB) { - return jc_test_cmp_EQ(a, b, exprA, exprB); - } +#if CHAR_MIN < 0 +#define JC_TEST_C_CHAR_VALUE jc_test_value_signed +#else +#define JC_TEST_C_CHAR_VALUE jc_test_value_unsigned #endif - static int compare(const uint32_t& a, const int32_t& b, const char* exprA, const char* exprB) { - return jc_test_cmp_EQ(a, (uint32_t)b, exprA, exprB); - } - static int compare(const int32_t& a, const uint32_t& b, const char* exprA, const char* exprB) { - return jc_test_cmp_EQ((uint32_t)a, b, exprA, exprB); - } - static int compare(const uint32_t& a, const uint32_t& b, const char* exprA, const char* exprB) { - return jc_test_cmp_EQ(a, b, exprA, exprB); - } - static int compare(const int32_t& a, const int32_t& b, const char* exprA, const char* exprB) { - return jc_test_cmp_EQ(a, b, exprA, exprB); - } - - template - static int compare(_jc_test_null_literal*, T* b, const char* exprA, const char* exprB) { - return jc_test_cmp_EQ(JC_TEST_STATIC_CAST(T*, 0), b, exprA, exprB); - } +#define JC_TEST_C_VALUE(_VALUE) \ + _Generic((1 ? (_VALUE) : 0), \ + _Bool: jc_test_value_unsigned, \ + char: JC_TEST_C_CHAR_VALUE, \ + signed char: jc_test_value_signed, \ + unsigned char: jc_test_value_unsigned, \ + short: jc_test_value_signed, \ + unsigned short: jc_test_value_unsigned, \ + int: jc_test_value_signed, \ + unsigned int: jc_test_value_unsigned, \ + long: jc_test_value_signed, \ + unsigned long: jc_test_value_unsigned, \ + long long: jc_test_value_signed, \ + unsigned long long: jc_test_value_unsigned, \ + float: jc_test_value_float, \ + double: jc_test_value_double, \ + long double: jc_test_value_long_double, \ + default: jc_test_value_pointer \ + )((1 ? (_VALUE) : 0), \ + (unsigned int)(sizeof(1 ? (_VALUE) : 0) * CHAR_BIT)) + +#define JC_TEST_C_IS_NUMERIC(_VALUE) \ + _Generic((1 ? (_VALUE) : 0), \ + _Bool: 1, char: 1, signed char: 1, unsigned char: 1, \ + short: 1, unsigned short: 1, int: 1, unsigned int: 1, \ + long: 1, unsigned long: 1, long long: 1, \ + unsigned long long: 1, float: 1, double: 1, long double: 1, \ + default: 0) + +enum { + JC_TEST_COMPARE_EQ, + JC_TEST_COMPARE_NE, + JC_TEST_COMPARE_LT, + JC_TEST_COMPARE_GT, + JC_TEST_COMPARE_LE, + JC_TEST_COMPARE_GE }; -#define JC_TEST_ASSERT_SETUP \ - jc_test_get_fixture()->line = __LINE__; \ - jc_test_get_fixture()->filename = __FILE__; \ - jc_test_increment_assertions() - -#define JC_TEST_FATAL_FAILURE jc_test_set_test_fail(1); return -#define JC_TEST_NON_FATAL_FAILURE jc_test_set_test_fail(0); - -#define JC_ASSERT_TEST_BOOLEAN(OP, VALUE, FAIL_FUNC) \ - do { \ - JC_TEST_ASSERT_SETUP; \ - if ( jc_test_cmp_##OP (VALUE, #VALUE) == 0 ) { \ - FAIL_FUNC; \ - } \ - } while(0) -#define JC_ASSERT_TEST_EQ(A, B, FAIL_FUNC) \ - do { \ - JC_TEST_ASSERT_SETUP; \ - if ( jc_test_cmp_eq_helper::compare(A, B, #A, #B) == 0 ) { \ - FAIL_FUNC; \ - } \ - } while(0) -#define JC_ASSERT_TEST_OP(OP, A, B, FAIL_FUNC) \ - do { \ - JC_TEST_ASSERT_SETUP; \ - if ( jc_test_cmp_##OP (A, B, #A, #B) == 0 ) { \ - FAIL_FUNC; \ - } \ - } while(0) -#define JC_ASSERT_TEST_3OP(OP, A, B, C, FAIL_FUNC) \ - do { \ - JC_TEST_ASSERT_SETUP; \ - if ( jc_test_cmp_##OP (A, B, C, #A, #B, #C) == 0 ) { \ - FAIL_FUNC; \ - } \ - } while(0) -#if !defined(JC_TEST_NO_DEATH_TEST) -#define JC_ASSERT_TEST_DEATH_OP(STATEMENT, RE, FAIL_FUNC) \ - do { \ - JC_TEST_ASSERT_SETUP; \ - if (JC_TEST_SETJMP(jc_test_get_state()->jumpenv) == 0) { \ - jc_test_set_signal_handler(); \ - JC_TEST_LOGF(jc_test_get_fixture(), jc_test_get_test(), 0, JC_TEST_EVENT_GENERIC, "\njc_test: Death test begin ->\n"); \ - STATEMENT; \ - JC_TEST_LOGF(jc_test_get_fixture(), jc_test_get_test(), 0, JC_TEST_EVENT_ASSERT_FAILED, "\nExpected this to fail: %s", #STATEMENT ); \ - jc_test_unset_signal_handler(); \ - FAIL_FUNC; \ - } \ - jc_test_unset_signal_handler(); \ - JC_TEST_LOGF(jc_test_get_fixture(), jc_test_get_test(), 0, JC_TEST_EVENT_GENERIC, "jc_test: <- Death test end\n"); \ - } while(0) +#define JC_TEST_C_FATAL 1 +#define JC_TEST_C_NON_FATAL 0 + +#define JC_TEST_C_ASSERT_BOOLEAN(_VALUE, _EXPECTED, _FATAL) \ + do { \ + int jc_test_c_boolean = !!(_VALUE); \ + jc_test_assert_boolean(jc_test_c_boolean, (_EXPECTED), #_VALUE, \ + __FILE__, __LINE__, (_FATAL)); \ + if ((_FATAL) && jc_test_current_fatal()) return; \ + } while (0) + +#define JC_TEST_C_ASSERT_COMPARE(_A, _B, _OPERATION, _FATAL) \ + do { \ + jc_test_value jc_test_c_a = JC_TEST_C_VALUE(_A); \ + jc_test_value jc_test_c_b = JC_TEST_C_VALUE(_B); \ + jc_test_assert_compare(jc_test_c_a, jc_test_c_b, (_OPERATION), \ + #_A, #_B, __FILE__, __LINE__, (_FATAL)); \ + if ((_FATAL) && jc_test_current_fatal()) return; \ + } while (0) + +#define JC_TEST_C_ASSERT_ORDERED(_A, _B, _OPERATION, _FATAL) \ + do { \ + _Static_assert(JC_TEST_C_IS_NUMERIC(_A) && JC_TEST_C_IS_NUMERIC(_B), \ + "ordered assertions require numeric operands"); \ + JC_TEST_C_ASSERT_COMPARE(_A, _B, _OPERATION, _FATAL); \ + } while (0) + +#define JC_TEST_C_ASSERT_STRING(_A, _B, _EQUAL, _FATAL) \ + do { \ + const char* jc_test_c_a = (_A); \ + const char* jc_test_c_b = (_B); \ + jc_test_assert_string(jc_test_c_a, jc_test_c_b, (_EQUAL), #_A, #_B, \ + __FILE__, __LINE__, (_FATAL)); \ + if ((_FATAL) && jc_test_current_fatal()) return; \ + } while (0) + +#define JC_TEST_C_ASSERT_NEAR(_A, _B, _EPSILON, _FATAL) \ + do { \ + long double jc_test_c_a = (long double)(_A); \ + long double jc_test_c_b = (long double)(_B); \ + long double jc_test_c_epsilon = (long double)(_EPSILON); \ + jc_test_assert_near(jc_test_c_a, jc_test_c_b, jc_test_c_epsilon, \ + #_A, #_B, #_EPSILON, __FILE__, __LINE__, (_FATAL)); \ + if ((_FATAL) && jc_test_current_fatal()) return; \ + } while (0) + +#define JC_TEST_C_ASSERT_ARRAY(_A, _B, _LENGTH, _ELEMENT_SIZE, _FATAL) \ + do { \ + const void* jc_test_c_a = (const void*)(_A); \ + const void* jc_test_c_b = (const void*)(_B); \ + size_t jc_test_c_length = (size_t)(_LENGTH); \ + jc_test_assert_array(jc_test_c_a, jc_test_c_b, jc_test_c_length, \ + (size_t)(_ELEMENT_SIZE), #_A, #_B, \ + __FILE__, __LINE__, (_FATAL)); \ + if ((_FATAL) && jc_test_current_fatal()) return; \ + } while (0) + +#define ASSERT_TRUE(_VALUE) JC_TEST_C_ASSERT_BOOLEAN(_VALUE, 1, JC_TEST_C_FATAL) +#define ASSERT_FALSE(_VALUE) JC_TEST_C_ASSERT_BOOLEAN(_VALUE, 0, JC_TEST_C_FATAL) +#define ASSERT_EQ(_A, _B) JC_TEST_C_ASSERT_COMPARE(_A, _B, JC_TEST_COMPARE_EQ, JC_TEST_C_FATAL) +#define ASSERT_NE(_A, _B) JC_TEST_C_ASSERT_COMPARE(_A, _B, JC_TEST_COMPARE_NE, JC_TEST_C_FATAL) +#define ASSERT_LT(_A, _B) JC_TEST_C_ASSERT_ORDERED(_A, _B, JC_TEST_COMPARE_LT, JC_TEST_C_FATAL) +#define ASSERT_GT(_A, _B) JC_TEST_C_ASSERT_ORDERED(_A, _B, JC_TEST_COMPARE_GT, JC_TEST_C_FATAL) +#define ASSERT_LE(_A, _B) JC_TEST_C_ASSERT_ORDERED(_A, _B, JC_TEST_COMPARE_LE, JC_TEST_C_FATAL) +#define ASSERT_GE(_A, _B) JC_TEST_C_ASSERT_ORDERED(_A, _B, JC_TEST_COMPARE_GE, JC_TEST_C_FATAL) +#define ASSERT_STREQ(_A, _B) JC_TEST_C_ASSERT_STRING(_A, _B, 1, JC_TEST_C_FATAL) +#define ASSERT_STRNE(_A, _B) JC_TEST_C_ASSERT_STRING(_A, _B, 0, JC_TEST_C_FATAL) +#define ASSERT_NEAR(_A, _B, _EPSILON) JC_TEST_C_ASSERT_NEAR(_A, _B, _EPSILON, JC_TEST_C_FATAL) +#define ASSERT_ARRAY_EQ(_A, _B) \ + do { \ + _Static_assert(sizeof(_A) == sizeof(_B), "array sizes must match"); \ + JC_TEST_C_ASSERT_ARRAY(_A, _B, sizeof(_A) / sizeof((_A)[0]), \ + sizeof((_A)[0]), JC_TEST_C_FATAL); \ + } while (0) +#define ASSERT_ARRAY_EQ_LEN(_A, _B, _LENGTH) \ + JC_TEST_C_ASSERT_ARRAY(_A, _B, _LENGTH, sizeof((_A)[0]), JC_TEST_C_FATAL) + +#define EXPECT_TRUE(_VALUE) JC_TEST_C_ASSERT_BOOLEAN(_VALUE, 1, JC_TEST_C_NON_FATAL) +#define EXPECT_FALSE(_VALUE) JC_TEST_C_ASSERT_BOOLEAN(_VALUE, 0, JC_TEST_C_NON_FATAL) +#define EXPECT_EQ(_A, _B) JC_TEST_C_ASSERT_COMPARE(_A, _B, JC_TEST_COMPARE_EQ, JC_TEST_C_NON_FATAL) +#define EXPECT_NE(_A, _B) JC_TEST_C_ASSERT_COMPARE(_A, _B, JC_TEST_COMPARE_NE, JC_TEST_C_NON_FATAL) +#define EXPECT_LT(_A, _B) JC_TEST_C_ASSERT_ORDERED(_A, _B, JC_TEST_COMPARE_LT, JC_TEST_C_NON_FATAL) +#define EXPECT_GT(_A, _B) JC_TEST_C_ASSERT_ORDERED(_A, _B, JC_TEST_COMPARE_GT, JC_TEST_C_NON_FATAL) +#define EXPECT_LE(_A, _B) JC_TEST_C_ASSERT_ORDERED(_A, _B, JC_TEST_COMPARE_LE, JC_TEST_C_NON_FATAL) +#define EXPECT_GE(_A, _B) JC_TEST_C_ASSERT_ORDERED(_A, _B, JC_TEST_COMPARE_GE, JC_TEST_C_NON_FATAL) +#define EXPECT_STREQ(_A, _B) JC_TEST_C_ASSERT_STRING(_A, _B, 1, JC_TEST_C_NON_FATAL) +#define EXPECT_STRNE(_A, _B) JC_TEST_C_ASSERT_STRING(_A, _B, 0, JC_TEST_C_NON_FATAL) +#define EXPECT_NEAR(_A, _B, _EPSILON) JC_TEST_C_ASSERT_NEAR(_A, _B, _EPSILON, JC_TEST_C_NON_FATAL) +#define EXPECT_ARRAY_EQ(_A, _B) \ + do { \ + _Static_assert(sizeof(_A) == sizeof(_B), "array sizes must match"); \ + JC_TEST_C_ASSERT_ARRAY(_A, _B, sizeof(_A) / sizeof((_A)[0]), \ + sizeof((_A)[0]), JC_TEST_C_NON_FATAL); \ + } while (0) +#define EXPECT_ARRAY_EQ_LEN(_A, _B, _LENGTH) \ + JC_TEST_C_ASSERT_ARRAY(_A, _B, _LENGTH, sizeof((_A)[0]), JC_TEST_C_NON_FATAL) + +#define SKIP() do { jc_test_set_test_skipped(); return; } while (0) +#define SCOPED_TRACE(_MESSAGE) ((void)0) + +#if defined(_MSC_VER) +#pragma section(".CRT$XCU", read) +typedef void (__cdecl *jc_test_initializer_func)(void); +#define JC_TEST_C_INITIALIZER(_NAME) \ + static void __cdecl _NAME(void); \ + __declspec(allocate(".CRT$XCU")) static jc_test_initializer_func \ + JC_TEST_C_JOIN(_NAME, _initializer) = _NAME; \ + static void __cdecl _NAME(void) +#elif defined(__GNUC__) || defined(__clang__) +#define JC_TEST_C_INITIALIZER(_NAME) \ + static void _NAME(void) __attribute__((constructor)); \ + static void _NAME(void) #else -#define JC_ASSERT_TEST_DEATH_OP(STATEMENT, RE, FAIL_FUNC) +#error "jc_test automatic registration requires GCC, Clang, or MSVC" #endif -// TEST API Begin --> - -#define SKIP() { jc_test_set_test_skipped(); return; } - -#define ASSERT_TRUE( VALUE ) JC_ASSERT_TEST_BOOLEAN( TRUE, VALUE, JC_TEST_FATAL_FAILURE ) -#define ASSERT_FALSE( VALUE ) JC_ASSERT_TEST_BOOLEAN( FALSE, VALUE, JC_TEST_FATAL_FAILURE ) -#define ASSERT_EQ( A, B ) JC_ASSERT_TEST_EQ( A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_NE( A, B ) JC_ASSERT_TEST_OP( NE, A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_LT( A, B ) JC_ASSERT_TEST_OP( LT, A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_GT( A, B ) JC_ASSERT_TEST_OP( GT, A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_LE( A, B ) JC_ASSERT_TEST_OP( LE, A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_GE( A, B ) JC_ASSERT_TEST_OP( GE, A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_STREQ( A, B ) JC_ASSERT_TEST_OP( STREQ, A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_STRNE( A, B ) JC_ASSERT_TEST_OP( STRNE, A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_NEAR( A, B, EPS ) JC_ASSERT_TEST_3OP( NEAR, A, B, EPS, JC_TEST_FATAL_FAILURE ) -#define ASSERT_DEATH(S, RE) JC_ASSERT_TEST_DEATH_OP( S, RE, JC_TEST_FATAL_FAILURE ) -#define ASSERT_ARRAY_EQ( A, B ) JC_ASSERT_TEST_OP( ARRAY_EQ, A, B, JC_TEST_FATAL_FAILURE ) -#define ASSERT_ARRAY_EQ_LEN( A, B, LEN )JC_ASSERT_TEST_3OP( ARRAY_EQ_LEN, A, B, LEN, JC_TEST_FATAL_FAILURE ) - -#define EXPECT_TRUE( VALUE ) JC_ASSERT_TEST_BOOLEAN( TRUE, VALUE, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_FALSE( VALUE ) JC_ASSERT_TEST_BOOLEAN( FALSE, VALUE, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_EQ( A, B ) JC_ASSERT_TEST_EQ( A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_NE( A, B ) JC_ASSERT_TEST_OP( NE, A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_LT( A, B ) JC_ASSERT_TEST_OP( LT, A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_GT( A, B ) JC_ASSERT_TEST_OP( GT, A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_LE( A, B ) JC_ASSERT_TEST_OP( LE, A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_GE( A, B ) JC_ASSERT_TEST_OP( GE, A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_STREQ( A, B ) JC_ASSERT_TEST_OP( STREQ, A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_STRNE( A, B ) JC_ASSERT_TEST_OP( STRNE, A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_NEAR( A, B, EPS ) JC_ASSERT_TEST_3OP( NEAR, A, B, EPS, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_DEATH(S, RE) JC_ASSERT_TEST_DEATH_OP(S, RE, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_ARRAY_EQ( A, B ) JC_ASSERT_TEST_OP( ARRAY_EQ, A, B, JC_TEST_NON_FATAL_FAILURE ) -#define EXPECT_ARRAY_EQ_LEN( A, B, LEN )JC_ASSERT_TEST_3OP( ARRAY_EQ_LEN, A, B, LEN, JC_TEST_NON_FATAL_FAILURE ) - -#define SCOPED_TRACE(_MSG) // nop - -template -struct jc_test_value_iterator { - virtual ~jc_test_value_iterator(); - virtual const T* Get() const = 0; - virtual void Advance() = 0; - virtual bool Empty() const = 0; // return false when out of values - virtual void Rewind() = 0; -}; -template jc_test_value_iterator::~jc_test_value_iterator() {} // separate line to silence warning - -template -struct jc_test_array_iterator : public jc_test_value_iterator { - const T *begin, *cursor, *end; - jc_test_array_iterator(const T* _begin, const T* _end) : begin(_begin), cursor(_begin), end(_end) {} - const T* Get() const{ return cursor; } - void Advance() { ++cursor; } - bool Empty() const { return cursor == end; } - void Rewind() { cursor = begin; } -}; +#define JC_TEST_C_JOIN2(_A, _B) _A##_B +#define JC_TEST_C_JOIN(_A, _B) JC_TEST_C_JOIN2(_A, _B) +#define JC_TEST_C_NAME3(_A, _B, _C) JC_TEST_C_JOIN(JC_TEST_C_JOIN(_A, _B), _C) +#define JC_TEST_C_TEST_NAME(_SUITE, _NAME) JC_TEST_C_NAME3(jc_test_body_##_SUITE, _, _NAME) +#define JC_TEST_C_ENTRY_NAME(_SUITE, _NAME) JC_TEST_C_NAME3(jc_test_entry_##_SUITE, _, _NAME) +#define JC_TEST_C_REGISTER_NAME(_SUITE, _NAME) JC_TEST_C_NAME3(jc_test_register_##_SUITE, _, _NAME) + +#define TEST(_SUITE, _NAME) \ + static void JC_TEST_C_TEST_NAME(_SUITE, _NAME)(void); \ + static jc_test_entry JC_TEST_C_ENTRY_NAME(_SUITE, _NAME) = { \ + NULL, #_SUITE, #_NAME, __FILE__, __LINE__, \ + JC_TEST_C_TEST_NAME(_SUITE, _NAME), 0, 0, 0 \ + }; \ + JC_TEST_C_INITIALIZER(JC_TEST_C_REGISTER_NAME(_SUITE, _NAME)) { \ + jc_test_register(&JC_TEST_C_ENTRY_NAME(_SUITE, _NAME)); \ + } \ + static void JC_TEST_C_TEST_NAME(_SUITE, _NAME)(void) + +#define JC_TEST_C_FIXTURE_SETUP_NAME(_TYPE) JC_TEST_C_JOIN(jc_test_fixture_setup_, _TYPE) +#define JC_TEST_C_FIXTURE_TEARDOWN_NAME(_TYPE) JC_TEST_C_JOIN(jc_test_fixture_teardown_, _TYPE) +#define JC_TEST_C_FIXTURE_BODY_NAME(_TYPE, _NAME) JC_TEST_C_NAME3(jc_test_fixture_body_##_TYPE, _, _NAME) +#define JC_TEST_C_FIXTURE_RUN_NAME(_TYPE, _NAME) JC_TEST_C_NAME3(jc_test_fixture_run_##_TYPE, _, _NAME) + +#define JC_TEST_FIXTURE_SETUP(_TYPE) \ + static void JC_TEST_C_FIXTURE_SETUP_NAME(_TYPE)(_TYPE* jc_test_fixture) +#define JC_TEST_FIXTURE_TEARDOWN(_TYPE) \ + static void JC_TEST_C_FIXTURE_TEARDOWN_NAME(_TYPE)(_TYPE* jc_test_fixture) + +#define TEST_F(_TYPE, _NAME) \ + static void JC_TEST_C_FIXTURE_SETUP_NAME(_TYPE)(_TYPE*); \ + static void JC_TEST_C_FIXTURE_TEARDOWN_NAME(_TYPE)(_TYPE*); \ + static void JC_TEST_C_FIXTURE_BODY_NAME(_TYPE, _NAME)(_TYPE*); \ + static void JC_TEST_C_FIXTURE_RUN_NAME(_TYPE, _NAME)(void) { \ + _TYPE jc_test_fixture_value; \ + memset(&jc_test_fixture_value, 0, sizeof(jc_test_fixture_value)); \ + JC_TEST_C_FIXTURE_SETUP_NAME(_TYPE)(&jc_test_fixture_value); \ + if (!jc_test_current_fatal() && !jc_test_current_skipped()) \ + JC_TEST_C_FIXTURE_BODY_NAME(_TYPE, _NAME)(&jc_test_fixture_value); \ + jc_test_clear_fatal(); \ + JC_TEST_C_FIXTURE_TEARDOWN_NAME(_TYPE)(&jc_test_fixture_value); \ + } \ + static jc_test_entry JC_TEST_C_ENTRY_NAME(_TYPE, _NAME) = { \ + NULL, #_TYPE, #_NAME, __FILE__, __LINE__, \ + JC_TEST_C_FIXTURE_RUN_NAME(_TYPE, _NAME), 0, 0, 0 \ + }; \ + JC_TEST_C_INITIALIZER(JC_TEST_C_REGISTER_NAME(_TYPE, _NAME)) { \ + jc_test_register(&JC_TEST_C_ENTRY_NAME(_TYPE, _NAME)); \ + } \ + static void JC_TEST_C_FIXTURE_BODY_NAME(_TYPE, _NAME)(_TYPE* jc_test_fixture) -template jc_test_array_iterator* jc_test_values_in(const T* begin, const T* end) { - return new jc_test_array_iterator(begin, end); -} -template jc_test_array_iterator* jc_test_values_in(const T (&arr)[N] ) { - return jc_test_values_in(arr, arr+N); -} - -template const ParamType* jc_test_params_class::param = 0; - -template -struct jc_test_fixture_with_param : public jc_test_fixture { - void SetParam() { JC_TEST_CAST(jc_test_params_class*, jc_test_get_state()->current_test)->SetParam(param); } - void Instantiate(); - const ParamType* param; -}; - -template -void jc_test_create_from_prototype(jc_test_fixture_with_param* fixture); - -template -void jc_test_fixture_with_param::Instantiate() { jc_test_create_from_prototype(this); } - - -struct jc_test_factory_base_interface { - virtual ~jc_test_factory_base_interface(); -}; - -template -struct jc_test_factory_interface : public jc_test_factory_base_interface { - virtual jc_test_params_class* New() = 0; - virtual void SetParam(const ParamType* param) = 0; -}; - -template -struct jc_test_factory : public jc_test_factory_interface { - jc_test_params_class* New() { return new T(); } - void SetParam(const typename T::param_t* param) { T::SetParam(param); } -}; - -#define JC_TEST_FIXTURE_TYPE_CLASS 0 -#define JC_TEST_FIXTURE_TYPE_PARAMS_CLASS 1 -#define JC_TEST_FIXTURE_TYPE_TYPED_CLASS 2 - -extern jc_test_fixture* jc_test_find_fixture(const char* name, unsigned int fixture_type); -extern jc_test_fixture* jc_test_alloc_fixture(const char* name, unsigned int fixture_type); -extern jc_test_fixture* jc_test_create_fixture(jc_test_fixture* fixture, const char* name, unsigned int fixture_type); -extern jc_test_entry* jc_test_add_test_to_fixture(jc_test_fixture* fixture, const char* test_name, jc_test_base_class* instance, jc_test_factory_base_interface* factory); -extern void jc_test_memcpy(void* dst, const void* src, size_t size); - -extern int jc_test_keep_test(jc_test_state* state, const char* name); // checks if the name is tagged for skipping - -extern int jc_test_register_class_test(const char* fixture_name, const char* test_name, - jc_test_void_staticfunc class_setup, jc_test_void_staticfunc class_teardown, - jc_test_base_class* instance, unsigned int fixture_type); -template -int jc_test_register_param_class_test(const char* fixture_name, const char* test_name, - jc_test_void_staticfunc class_setup, jc_test_void_staticfunc class_teardown, - jc_test_factory_interface* factory) { - jc_test_fixture* fixture = jc_test_find_fixture(fixture_name, JC_TEST_FIXTURE_TYPE_PARAMS_CLASS); - if (!fixture) { - fixture = jc_test_alloc_fixture(fixture_name, JC_TEST_FIXTURE_TYPE_PARAMS_CLASS); - fixture->fixture_setup = class_setup; - fixture->fixture_teardown = class_teardown; - } - jc_test_add_test_to_fixture(fixture, test_name, 0, factory); - return 0; -} - -struct jc_test_type0 {}; - -template -struct jc_test_type1 { - typedef T1 head; typedef jc_test_type0 tail; -}; -template -struct jc_test_type2 { - typedef T2 head; typedef jc_test_type1 tail; -}; -template -struct jc_test_type3 { - typedef T3 head; typedef jc_test_type2 tail; -}; -template -struct jc_test_type4 { - typedef T4 head; typedef jc_test_type3 tail; -}; - -template -struct jc_test_register_typed_class_test { - static int register_test(const char* fixture_name, const char* test_name, unsigned int index) { - typedef typename TypeList::head TypeParam; - typedef typename BaseClassSelector::template bind::type TestClass; - - jc_test_fixture* fixture = jc_test_find_fixture(fixture_name, JC_TEST_FIXTURE_TYPE_TYPED_CLASS); - if (!fixture) { - fixture = jc_test_alloc_fixture(fixture_name, JC_TEST_FIXTURE_TYPE_CLASS); - fixture->fixture_setup = TestClass::SetUpTestCase; - fixture->fixture_teardown = TestClass::TearDownTestCase; - } - fixture->index = index; - jc_test_add_test_to_fixture(fixture, test_name, new TestClass, 0); - return jc_test_register_typed_class_test:: - register_test(fixture_name, test_name, index+1); - } -}; - -template -struct jc_test_register_typed_class_test { - static int register_test(const char*, const char*, unsigned int) { - return 0; - } -}; - -template -jc_test_fixture* jc_test_alloc_fixture_with_param(const char* name, unsigned int type) { - return jc_test_create_fixture(new jc_test_fixture_with_param, name, type); -} - -template -void jc_test_create_from_prototype(jc_test_fixture_with_param* fixture) { - - jc_test_fixture* prototype_fixture = jc_test_find_fixture(fixture->prototype, JC_TEST_FIXTURE_TYPE_PARAMS_CLASS); - if (!prototype_fixture) { - JC_TEST_LOGF(0, 0, 0, JC_TEST_EVENT_GENERIC, "Couldn't find fixture of name %s\n", fixture->prototype); - JC_TEST_ASSERT_FN(prototype_fixture != 0); - } - JC_TEST_ASSERT_FN(prototype_fixture->type == JC_TEST_FIXTURE_TYPE_PARAMS_CLASS); - - fixture->fixture_setup = prototype_fixture->fixture_setup; - fixture->fixture_teardown = prototype_fixture->fixture_teardown; - - fixture->num_tests = prototype_fixture->num_tests; - jc_test_entry* prototype_test = prototype_fixture->tests; - jc_test_entry* first = 0; - jc_test_entry* prev = 0; - while (prototype_test) { - jc_test_entry* test = new jc_test_entry; - test->next = 0; - test->name = prototype_test->name; - test->factory = 0; - test->fail = 0; - test->skipped = 0; - - jc_test_factory_interface* factory = JC_TEST_CAST(jc_test_factory_interface*, prototype_test->factory); - factory->SetParam(fixture->param); - test->instance = factory->New(); - - if (!first) { - first = test; - prev = test; - } else { - prev->next = test; - prev = test; - } - prototype_test = prototype_test->next; - } - fixture->tests = first; -} - -template -int jc_test_register_param_tests(const char* prototype_fixture_name, const char* fixture_name, jc_test_value_iterator* values) -{ - unsigned int index = 0; - jc_test_fixture* first_fixture = 0; - while (!values->Empty()) { - - // Allocate a new fixture, and create the test class - jc_test_fixture_with_param* fixture = JC_TEST_CAST(jc_test_fixture_with_param*, - jc_test_alloc_fixture_with_param(fixture_name, JC_TEST_FIXTURE_TYPE_CLASS) ); - - fixture->first = first_fixture == 0 ? 1 : 0; - if (!first_fixture) { - first_fixture = fixture; // A silly trick to make the first fixture accumulate all the timings from this batch - } - fixture->parent = first_fixture; - fixture->index = index++; - fixture->param = values->Get(); - fixture->prototype = prototype_fixture_name; - - values->Advance(); - - fixture->last = values->Empty() ? 1 : 0; - } - - delete values; - return 0; -} - -#define JC_TEST_MAKE_NAME2(X,Y) X ## _ ## Y -#define JC_TEST_MAKE_NAME3(X,Y,Z) X ## _ ## Y ## _ ## Z -#define JC_TEST_MAKE_CLASS_NAME(X, Y) JC_TEST_MAKE_NAME3(X, Y, _TestCase) -#define JC_TEST_MAKE_FUNCTION_NAME(X, Y) JC_TEST_MAKE_NAME2(X, Y) -#define JC_TEST_MAKE_UNIQUE_NAME(X, Y, LINE) JC_TEST_MAKE_NAME3(X, Y, LINE) - -#define TEST3(testfixture,testfn,testname) \ -class JC_TEST_MAKE_CLASS_NAME(testfixture,testfn) : public jc_test_base_class { \ - virtual void TestBody() JC_OVERRIDE; \ -}; \ -static int JC_TEST_MAKE_UNIQUE_NAME(testfixture,testfn,__LINE__) JC_TEST_UNUSED = jc_test_register_class_test( \ - testname, #testfn, jc_test_base_class::SetUpTestCase, jc_test_base_class::TearDownTestCase, \ - new JC_TEST_MAKE_CLASS_NAME(testfixture,testfn), JC_TEST_FIXTURE_TYPE_CLASS); \ -void JC_TEST_MAKE_CLASS_NAME(testfixture,testfn)::TestBody() - -#define TEST(testfixture,testfn) TEST3(testfixture,testfn,#testfixture) - -#define TEST_F(testfixture,testfn) \ - class JC_TEST_MAKE_CLASS_NAME(testfixture,testfn) : public testfixture { \ - virtual void TestBody() JC_OVERRIDE; \ - }; \ - static int JC_TEST_MAKE_UNIQUE_NAME(testfixture,testfn,__LINE__) JC_TEST_UNUSED = jc_test_register_class_test( \ - #testfixture, #testfn, testfixture::SetUpTestCase, testfixture::TearDownTestCase, \ - new JC_TEST_MAKE_CLASS_NAME(testfixture,testfn), JC_TEST_FIXTURE_TYPE_CLASS); \ - void JC_TEST_MAKE_CLASS_NAME(testfixture,testfn)::TestBody() - -#define TEST_P(testfixture,testfn) \ - class JC_TEST_MAKE_CLASS_NAME(testfixture,testfn) : public testfixture { \ - virtual void TestBody() JC_OVERRIDE; \ - }; \ - static int JC_TEST_MAKE_UNIQUE_NAME(testfixture,testfn,__LINE__) JC_TEST_UNUSED = jc_test_register_param_class_test( \ - #testfixture, #testfn, testfixture::SetUpTestCase, testfixture::TearDownTestCase, \ - new jc_test_factory()); \ - void JC_TEST_MAKE_CLASS_NAME(testfixture,testfn)::TestBody() - -#define INSTANTIATE_TEST_CASE_P(prefix,testfixture,testvalues) \ - static int JC_TEST_MAKE_UNIQUE_NAME(prefix,testfixture,__LINE__) JC_TEST_UNUSED = \ - jc_test_register_param_tests(#testfixture, #prefix "/" #testfixture, testvalues) - - -template struct jc_test_typed_list { - typedef T type; -}; - -template