From 0551616eab1bbde0c5e633b8666eefc776e1e18b Mon Sep 17 00:00:00 2001 From: Michał Łyszczek Date: Sat, 18 May 2019 16:53:46 +0200 Subject: tst/mtest.h: udpate mtest to v1.1.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michał Łyszczek --- tst/mtest.h | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 116 insertions(+), 16 deletions(-) diff --git a/tst/mtest.h b/tst/mtest.h index 27b6b39..db8c4de 100644 --- a/tst/mtest.h +++ b/tst/mtest.h @@ -1,24 +1,51 @@ /* ========================================================================== Licensed under BSD 2clause license. See LICENSE file for more information Author: Michał Łyszczek + ========================================================================== + __________________________________________________________ + / mtest version v1.1.4 \ + | | + | Simple test framework that uses TAP output format | + \ http://testanything.org / + ---------------------------------------------------------- + \ ,-^-. + \ !oYo! + \ /./=\.\______ + ## )\/\ + ||-----w|| + || || + + Cowth Vader ========================================================================== */ -/* ==== mtest version v0.1.0 ================================================ */ - - /* ========================================================================== - Tests uses simple TAP output format (http://testanything.org) - ========================================================================== */ + _ __ __ ____ _ __ + (_)____ _____ / /__ __ ____/ /___ / __/(_)/ /___ _____ + / // __ \ / ___// // / / // __ // _ \ / /_ / // // _ \ / ___/ + / // / / // /__ / // /_/ // /_/ // __/ / __// // // __/(__ ) + /_//_/ /_/ \___//_/ \__,_/ \__,_/ \___/ /_/ /_//_/ \___//____/ - -/* ==== include files ======================================================= */ + ========================================================================== */ #include -/* ==== public macros ======================================================= */ +/* ========================================================================== + __ __ _ + ____ __ __ / /_ / /(_)_____ + / __ \ / / / // __ \ / // // ___/ + / /_/ // /_/ // /_/ // // // /__ + / .___/ \__,_//_.___//_//_/ \___/ + /_/ + + ____ ___ ____ _ _____ _____ ____ _____ + / __ `__ \ / __ `// ___// ___// __ \ / ___/ + / / / / / // /_/ // /__ / / / /_/ /(__ ) + /_/ /_/ /_/ \__,_/ \___//_/ \____//____/ + + ========================================================================== */ /* ========================================================================== @@ -32,6 +59,8 @@ int mt_test_status; \ int mt_total_tests = 0; \ int mt_total_failed = 0; \ + int mt_total_checks = 0; \ + int mt_checks_failed = 0; \ static void (*mt_prepare_test)(void); \ static void (*mt_cleanup_test)(void) @@ -48,6 +77,8 @@ extern int mt_test_status; \ extern int mt_total_tests; \ extern int mt_total_failed; \ + extern int mt_total_checks; \ + extern int mt_checks_failed; \ static void (*mt_prepare_test)(void); \ static void (*mt_cleanup_test)(void) @@ -57,8 +88,27 @@ ========================================================================== */ -#define mt_run(f) do { \ - curr_test = #f; \ +#define mt_run(f) mt_run_named(f, #f) + + +/* ========================================================================== + macro runs test 'f' with parameter 'p'. + + 'p' can be of any type, as long as it matches prototype of 'f' function. + ========================================================================== */ + + +#define mt_run_param(f, p) mt_run_param_named(f, p, #f) + + +/* ========================================================================== + macro runs test 'f' and instead of printing function name as a test name + it allows to provide custom name 'n' + ========================================================================== */ + + +#define mt_run_named(f, n) do { \ + curr_test = n; \ mt_test_status = 0; \ ++mt_total_tests; \ if (mt_prepare_test) mt_prepare_test(); \ @@ -74,6 +124,31 @@ } while(0) +/* ========================================================================== + macro runs test 'f' with parameter 'p' and instead of printing function + name as a test name, it allows to provide custom name 'n'. + + 'p' can be of any type, as long as it matches prototype of 'f' function. + ========================================================================== */ + + +#define mt_run_param_named(f, p, n) do { \ + curr_test = n; \ + mt_test_status = 0; \ + ++mt_total_tests; \ + if (mt_prepare_test) mt_prepare_test(); \ + f(p); \ + if (mt_cleanup_test) mt_cleanup_test(); \ + if (mt_test_status != 0) \ + { \ + fprintf(stdout, "not ok %d - %s\n", mt_total_tests, curr_test); \ + ++mt_total_failed; \ + } \ + else \ + fprintf(stdout, "ok %d - %s\n", mt_total_tests, curr_test); \ + } while(0) + + /* ========================================================================== simple assert, when expression 'e' is evaluated to false, assert message will be logged, and macro will force function to return @@ -81,11 +156,13 @@ #define mt_assert(e) do { \ + ++mt_total_checks; \ if (!(e)) \ { \ - fprintf(stdout, "# assert [%s:%d] %s, %s\n", \ + fprintf(stderr, "# assert [%s:%d] %s, %s\n", \ __FILE__, __LINE__, curr_test, #e); \ mt_test_status = -1; \ + ++mt_checks_failed; \ return; \ } } while (0) @@ -97,21 +174,33 @@ #define mt_fail(e) do { \ + ++mt_total_checks; \ if (!(e)) \ { \ - fprintf(stdout, "# assert [%s:%d] %s, %s\n", \ + fprintf(stderr, "# assert [%s:%d] %s, %s\n", \ __FILE__, __LINE__, curr_test, #e); \ mt_test_status = -1; \ + ++mt_checks_failed; \ } } while (0) /* ========================================================================== - shortcut macro to test if function exits with success (with return value - set to 0) + Checks if function exits with success (return value == 0). ========================================================================== */ -#define mt_fok(e) mt_fail(e == 0) +#define mt_fok(e) do { \ + int ret; \ + ++mt_total_checks; \ + if ((ret = e) != 0) \ + { \ + fprintf(stderr, "# assert [%s:%d] %s, %s != ok\n", \ + __FILE__, __LINE__, curr_test, #e); \ + fprintf(stderr, "# assert [%s:%d] %s, return: %d, errno: %d\n", \ + __FILE__, __LINE__, curr_test, ret, errno); \ + mt_test_status = -1; \ + ++mt_checks_failed; \ + } } while (0) /* ========================================================================== @@ -124,7 +213,12 @@ errno = 0; \ mt_fail(e == -1); \ mt_fail(errno == errn); \ - } while (0) + if (errno != errn) \ + { \ + fprintf(stderr, "# assert [%s:%d] %s, got errno: %d\n", \ + __FILE__, __LINE__, curr_test, errno); \ + } } while (0) + /* ========================================================================== prints test plan, in format 1... If all tests have @@ -136,4 +230,10 @@ #define mt_return() do { \ fprintf(stdout, "1..%d\n", mt_total_tests); \ + fprintf(stderr, "# total tests.......:%4d\n", mt_total_tests); \ + fprintf(stderr, "# passed tests......:%4d\n", mt_total_tests - mt_total_failed); \ + fprintf(stderr, "# failed tests......:%4d\n", mt_total_failed); \ + fprintf(stderr, "# total checks......:%4d\n", mt_total_checks); \ + fprintf(stderr, "# passed checks.....:%4d\n", mt_total_checks - mt_checks_failed); \ + fprintf(stderr, "# failed checks.....:%4d\n", mt_checks_failed); \ return mt_total_failed > 254 ? 254 : mt_total_failed; } while(0) -- cgit v1.2.3-8-gadcc