diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 4c9729026beed1afecbdb04b953e7d19f9892098..d196458d4064432f734ca0a587461f2410bafbc0 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -82,7 +82,7 @@ create_unit_test(PREFIX rope_avl ) create_unit_test(PREFIX rope_stress SOURCES rope_stress.c - LIBRARIES salad + LIBRARIES salad unit ) create_unit_test(PREFIX rope SOURCES rope.c diff --git a/test/unit/unit.c b/test/unit/unit.c index d12b66f915985d6a723d7bb536b20c2cdb0803c7..38326793d922caa9361adbd27500311a1362e672 100644 --- a/test/unit/unit.c +++ b/test/unit/unit.c @@ -1,5 +1,6 @@ #include "unit.h" +#include <stdbool.h> #include <stdio.h> #include <stdarg.h> @@ -19,13 +20,16 @@ _space(FILE *stream) } void -plan(int count) +_plan(int count, bool tap) { ++level; plan_test[level] = count; tests_done[level] = 0; tests_failed[level] = 0; + if (tap && level == 0) + printf("TAP version 13\n"); + _space(stdout); printf("%d..%d\n", 1, plan_test[level]); } diff --git a/test/unit/unit.h b/test/unit/unit.h index bd1c194ee90d4706f06d5f09c6d7a6384b2da6b4..77d0847b56b9f6935c9d438096d3318c126223e6 100644 --- a/test/unit/unit.h +++ b/test/unit/unit.h @@ -30,6 +30,7 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> /* exit() */ @@ -66,16 +67,8 @@ extern "C" { return check_plan(); // print resume } @endcode - - */ -#if !UNIT_TAP_COMPATIBLE - -#define header() printf("\t*** %s ***\n", __func__) -#define footer() printf("\t*** %s: done ***\n", __func__) - - /* private function, use ok(...) instead */ int _ok(int condition, const char *fmt, ...); @@ -89,15 +82,14 @@ Before anything else, you need a testing plan. This basically declares how many tests your program is going to run to protect against premature failure. */ -void plan(int count); +void +_plan(int count, bool tap); /** @brief check if plan is reached and print report */ int check_plan(void); -#endif - #define ok(condition, fmt, args...) { \ int res = _ok(condition, fmt, ##args); \ if (!res) { \ @@ -136,10 +128,8 @@ int check_plan(void); #if UNIT_TAP_COMPATIBLE -#include <stdarg.h> /* va_start(), va_end() */ - #define header() \ - do { \ + do { \ _space(stdout); \ printf("# *** %s ***\n", __func__); \ } while (0) @@ -150,95 +140,15 @@ int check_plan(void); printf("# *** %s: done ***\n", __func__); \ } while (0) -enum { MAX_LEVELS = 10 }; - -static int tests_done[MAX_LEVELS]; -static int tests_failed[MAX_LEVELS]; -static int plan_test[MAX_LEVELS]; +#define plan(count) _plan(count, true) -static int level = -1; - -/** - * private function, use note(...) or diag(...) instead - */ -static inline void -_space(FILE *stream) -{ - for (int i = 0; i < level; i++) { - fprintf(stream, " "); - } -} +#else /* !UNIT_TAP_COMPATIBLE */ -/** - * private function, use ok(...) instead - */ -static inline int -_ok(int condition, const char *fmt, ...) -{ - va_list ap; - - _space(stdout); - printf("%s %d - ", condition ? "ok" : "not ok", ++tests_done[level]); - if (!condition) - tests_failed[level]++; - va_start(ap, fmt); - vprintf(fmt, ap); - printf("\n"); - va_end(ap); - return condition; -} - -/** - * @brief set and print plan - * @param count - * Before anything else, you need a testing plan. This basically declares - * how many tests your program is going to run to protect against premature - * failure. - */ -static inline void -plan(int count) -{ - ++level; - plan_test[level] = count; - tests_done[level] = 0; - tests_failed[level] = 0; - - if (level == 0) - printf("TAP version 13\n"); - - _space(stdout); - printf("%d..%d\n", 1, plan_test[level]); -} - -/** - * @brief check if plan is reached and print report - */ -static inline int -check_plan(void) -{ - int r = 0; - if (tests_done[level] != plan_test[level]) { - _space(stderr); - fprintf(stderr, - "# Looks like you planned %d tests but ran %d.\n", - plan_test[level], tests_done[level]); - r = -1; - } - - if (tests_failed[level]) { - _space(stderr); - fprintf(stderr, "# Looks like you failed %d test of %d run.\n", - tests_failed[level], tests_done[level]); - r = tests_failed[level]; - } - --level; - if (level >= 0) { - is(r, 0, "subtests"); - } - return r; -} +#define header() printf("\t*** %s ***\n", __func__) +#define footer() printf("\t*** %s: done ***\n", __func__) +#define plan(count) _plan(count, false) -#endif +#endif /* !UNIT_TAP_COMPATIBLE */ #if defined(__cplusplus) }