From a6c8b4efa59628a755755c510aa9ee6db2728fb9 Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 8 Nov 2019 14:35:35 +0100 Subject: test: merge test_helpers.c and index.c No need to keep them separate. Originally I thought index.c was only going to contain the list of tests, but that didn't happen. --- player/main.c | 2 +- test/chmap.c | 3 +- test/gl_video.c | 3 +- test/index.c | 57 ------------------------------------ test/index.h | 32 --------------------- test/json.c | 3 +- test/linked_list.c | 3 +- test/test_helpers.c | 29 ------------------- test/test_helpers.h | 24 ---------------- test/tests.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/tests.h | 51 ++++++++++++++++++++++++++++++++ wscript_build.py | 3 +- 12 files changed, 140 insertions(+), 153 deletions(-) delete mode 100644 test/index.c delete mode 100644 test/index.h delete mode 100644 test/test_helpers.c delete mode 100644 test/test_helpers.h create mode 100644 test/tests.c create mode 100644 test/tests.h diff --git a/player/main.c b/player/main.c index d2fb22668f..b6c9e1085d 100644 --- a/player/main.c +++ b/player/main.c @@ -56,7 +56,7 @@ #include "demux/demux.h" #include "misc/thread_tools.h" #include "sub/osd.h" -#include "test/index.h" +#include "test/tests.h" #include "video/out/vo.h" #include "core.h" diff --git a/test/chmap.c b/test/chmap.c index a489a7da61..91b1cf797f 100644 --- a/test/chmap.c +++ b/test/chmap.c @@ -1,7 +1,6 @@ #include "audio/chmap.h" #include "audio/chmap_sel.h" -#include "index.h" -#include "test_helpers.h" +#include "tests.h" #define LAYOUTS(...) (char*[]){__VA_ARGS__, NULL} diff --git a/test/gl_video.c b/test/gl_video.c index eb2288d8b4..556a81ac92 100644 --- a/test/gl_video.c +++ b/test/gl_video.c @@ -1,5 +1,4 @@ -#include "index.h" -#include "test_helpers.h" +#include "tests.h" #include "video/out/gpu/video.h" static void run(struct test_ctx *ctx) diff --git a/test/index.c b/test/index.c deleted file mode 100644 index 4804cead2f..0000000000 --- a/test/index.c +++ /dev/null @@ -1,57 +0,0 @@ -#include "index.h" -#include "player/core.h" - -static const struct unittest *unittests[] = { - &test_chmap, - &test_gl_video, - &test_json, - &test_linked_list, - NULL -}; - -bool run_tests(struct MPContext *mpctx) -{ - char *sel = mpctx->opts->test_mode; - assert(sel && sel[0]); - - if (strcmp(sel, "help") == 0) { - MP_INFO(mpctx, "Available tests:\n"); - for (int n = 0; unittests[n]; n++) - MP_INFO(mpctx, " %s\n", unittests[n]->name); - MP_INFO(mpctx, " all-simple\n"); - return true; - } - - struct test_ctx ctx = { - .global = mpctx->global, - .log = mpctx->log, - }; - - int num_run = 0; - - for (int n = 0; unittests[n]; n++) { - const struct unittest *t = unittests[n]; - - // Exactly 1 entrypoint please. - assert(MP_IS_POWER_OF_2( - (t->run ? (1 << 1) : 0))); - - bool run = false; - run |= strcmp(sel, "all-simple") == 0 && !t->is_complex; - run |= strcmp(sel, t->name); - - if (run) { - if (t->run) - t->run(&ctx); - num_run++; - } - } - - MP_INFO(mpctx, "%d unittests successfully run.\n", num_run); - - return num_run > 0; // still error if none -} - -#ifdef NDEBUG -static_assert(false, "don't define NDEBUG for tests"); -#endif diff --git a/test/index.h b/test/index.h deleted file mode 100644 index 1564fe311a..0000000000 --- a/test/index.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include - -struct MPContext; - -bool run_tests(struct MPContext *mpctx); - -struct test_ctx { - struct mpv_global *global; - struct mp_log *log; -}; - -struct unittest { - // This is used to select the test on command line with --unittest=. - const char *name; - - // Cannot run without additional arguments supplied. - bool is_complex; - - // Entrypoints. There are various for various purposes. Only 1 of them must - // be set. - - // Entrypoint for tests which have a simple dependency on the mpv core. The - // core is sufficiently initialized at this point. - void (*run)(struct test_ctx *ctx); -}; - -extern const struct unittest test_chmap; -extern const struct unittest test_gl_video; -extern const struct unittest test_json; -extern const struct unittest test_linked_list; diff --git a/test/json.c b/test/json.c index 9997cdc892..e95f88c972 100644 --- a/test/json.c +++ b/test/json.c @@ -1,8 +1,7 @@ #include "common/common.h" -#include "index.h" #include "misc/json.h" #include "misc/node.h" -#include "test_helpers.h" +#include "tests.h" struct entry { const char *src; diff --git a/test/linked_list.c b/test/linked_list.c index 8daa854e9c..c9fbf9639e 100644 --- a/test/linked_list.c +++ b/test/linked_list.c @@ -1,7 +1,6 @@ #include "common/common.h" -#include "index.h" #include "misc/linked_list.h" -#include "test_helpers.h" +#include "tests.h" struct list_item { int v; diff --git a/test/test_helpers.c b/test/test_helpers.c deleted file mode 100644 index 187ff73c61..0000000000 --- a/test/test_helpers.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -#include "test_helpers.h" - -void assert_int_equal_impl(const char *file, int line, int64_t a, int64_t b) -{ - if (a != b) { - printf("%s:%d: %"PRId64" != %"PRId64"\n", file, line, a, b); - abort(); - } -} - -void assert_string_equal_impl(const char *file, int line, - const char *a, const char *b) -{ - if (strcmp(a, b) != 0) { - printf("%s:%d: '%s' != '%s'\n", file, line, a, b); - abort(); - } -} - -void assert_float_equal_impl(const char *file, int line, - double a, double b, double tolerance) -{ - if (fabs(a - b) > tolerance) { - printf("%s:%d: %f != %f\n", file, line, a, b); - abort(); - } -} diff --git a/test/test_helpers.h b/test/test_helpers.h deleted file mode 100644 index 6ca3cd99b3..0000000000 --- a/test/test_helpers.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef MP_TESTS_H -#define MP_TESTS_H - -#include -#include - -#include "common/common.h" - -#define assert_true(x) assert(x) -#define assert_false(x) assert(!(x)) -#define assert_int_equal(a, b) \ - assert_int_equal_impl(__FILE__, __LINE__, (a), (b)) -#define assert_string_equal(a, b) \ - assert_string_equal_impl(__FILE__, __LINE__, (a), (b)) -#define assert_float_equal(a, b, tolerance) \ - assert_float_equal_impl(__FILE__, __LINE__, (a), (b), (tolerance)) - -void assert_int_equal_impl(const char *file, int line, int64_t a, int64_t b); -void assert_string_equal_impl(const char *file, int line, - const char *a, const char *b); -void assert_float_equal_impl(const char *file, int line, - double a, double b, double tolerance); - -#endif diff --git a/test/tests.c b/test/tests.c new file mode 100644 index 0000000000..68245d2df1 --- /dev/null +++ b/test/tests.c @@ -0,0 +1,83 @@ +#include "player/core.h" +#include "tests.h" + +static const struct unittest *unittests[] = { + &test_chmap, + &test_gl_video, + &test_json, + &test_linked_list, + NULL +}; + +bool run_tests(struct MPContext *mpctx) +{ + char *sel = mpctx->opts->test_mode; + assert(sel && sel[0]); + + if (strcmp(sel, "help") == 0) { + MP_INFO(mpctx, "Available tests:\n"); + for (int n = 0; unittests[n]; n++) + MP_INFO(mpctx, " %s\n", unittests[n]->name); + MP_INFO(mpctx, " all-simple\n"); + return true; + } + + struct test_ctx ctx = { + .global = mpctx->global, + .log = mpctx->log, + }; + + int num_run = 0; + + for (int n = 0; unittests[n]; n++) { + const struct unittest *t = unittests[n]; + + // Exactly 1 entrypoint please. + assert(MP_IS_POWER_OF_2( + (t->run ? (1 << 1) : 0))); + + bool run = false; + run |= strcmp(sel, "all-simple") == 0 && !t->is_complex; + run |= strcmp(sel, t->name); + + if (run) { + if (t->run) + t->run(&ctx); + num_run++; + } + } + + MP_INFO(mpctx, "%d unittests successfully run.\n", num_run); + + return num_run > 0; // still error if none +} + +#ifdef NDEBUG +static_assert(false, "don't define NDEBUG for tests"); +#endif + +void assert_int_equal_impl(const char *file, int line, int64_t a, int64_t b) +{ + if (a != b) { + printf("%s:%d: %"PRId64" != %"PRId64"\n", file, line, a, b); + abort(); + } +} + +void assert_string_equal_impl(const char *file, int line, + const char *a, const char *b) +{ + if (strcmp(a, b) != 0) { + printf("%s:%d: '%s' != '%s'\n", file, line, a, b); + abort(); + } +} + +void assert_float_equal_impl(const char *file, int line, + double a, double b, double tolerance) +{ + if (fabs(a - b) > tolerance) { + printf("%s:%d: %f != %f\n", file, line, a, b); + abort(); + } +} diff --git a/test/tests.h b/test/tests.h new file mode 100644 index 0000000000..c19f90f75d --- /dev/null +++ b/test/tests.h @@ -0,0 +1,51 @@ +#pragma once + +#include +#include +#include + +#include "common/common.h" + +struct MPContext; + +bool run_tests(struct MPContext *mpctx); + +struct test_ctx { + struct mpv_global *global; + struct mp_log *log; +}; + +struct unittest { + // This is used to select the test on command line with --unittest=. + const char *name; + + // Cannot run without additional arguments supplied. + bool is_complex; + + // Entrypoints. There are various for various purposes. Only 1 of them must + // be set. + + // Entrypoint for tests which have a simple dependency on the mpv core. The + // core is sufficiently initialized at this point. + void (*run)(struct test_ctx *ctx); +}; + +extern const struct unittest test_chmap; +extern const struct unittest test_gl_video; +extern const struct unittest test_json; +extern const struct unittest test_linked_list; + +#define assert_true(x) assert(x) +#define assert_false(x) assert(!(x)) +#define assert_int_equal(a, b) \ + assert_int_equal_impl(__FILE__, __LINE__, (a), (b)) +#define assert_string_equal(a, b) \ + assert_string_equal_impl(__FILE__, __LINE__, (a), (b)) +#define assert_float_equal(a, b, tolerance) \ + assert_float_equal_impl(__FILE__, __LINE__, (a), (b), (tolerance)) + +void assert_int_equal_impl(const char *file, int line, int64_t a, int64_t b); +void assert_string_equal_impl(const char *file, int line, + const char *a, const char *b); +void assert_float_equal_impl(const char *file, int line, + double a, double b, double tolerance); diff --git a/wscript_build.py b/wscript_build.py index 547d0968fe..ff0b0b746f 100644 --- a/wscript_build.py +++ b/wscript_build.py @@ -397,10 +397,9 @@ def build(ctx): ## Tests ( "test/chmap.c", "tests" ), ( "test/gl_video.c", "tests" ), - ( "test/index.c", "tests" ), ( "test/json.c", "tests" ), ( "test/linked_list.c", "tests" ), - ( "test/test_helpers.c", "tests" ), + ( "test/tests.c", "tests" ), ## Video ( "video/csputils.c" ), -- cgit v1.2.3