diff options
author | wm4 <wm4@nowhere> | 2019-11-08 14:35:35 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2019-11-08 20:34:07 +0100 |
commit | a6c8b4efa59628a755755c510aa9ee6db2728fb9 (patch) | |
tree | 2a5af32fad4748cae5504462ccb855682d950a1d /test/tests.c | |
parent | 98b38b04c9f771562cdf262c6fa49734a318f5ce (diff) | |
download | mpv-a6c8b4efa59628a755755c510aa9ee6db2728fb9.tar.bz2 mpv-a6c8b4efa59628a755755c510aa9ee6db2728fb9.tar.xz |
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.
Diffstat (limited to 'test/tests.c')
-rw-r--r-- | test/tests.c | 83 |
1 files changed, 83 insertions, 0 deletions
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(); + } +} |