diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/chmap.c | 86 | ||||
-rw-r--r-- | test/chmap_sel.c | 136 | ||||
-rw-r--r-- | test/gl_video.c | 28 | ||||
-rw-r--r-- | test/index.c | 51 | ||||
-rw-r--r-- | test/index.h | 29 | ||||
-rw-r--r-- | test/json.c | 22 | ||||
-rw-r--r-- | test/linked_list.c | 17 | ||||
-rw-r--r-- | test/test_helpers.c | 29 | ||||
-rw-r--r-- | test/test_helpers.h | 23 |
9 files changed, 229 insertions, 192 deletions
diff --git a/test/chmap.c b/test/chmap.c index 395b716b74..6484698f4c 100644 --- a/test/chmap.c +++ b/test/chmap.c @@ -1,9 +1,81 @@ -#include "test_helpers.h" #include "audio/chmap.h" +#include "audio/chmap_sel.h" +#include "index.h" +#include "test_helpers.h" + +#define LAYOUTS(...) (char*[]){__VA_ARGS__, NULL} + +static void test_sel(const char *input, const char *expected_selection, + char **layouts) +{ + struct mp_chmap_sel s = {0}; + struct mp_chmap input_map; + struct mp_chmap expected_map; + + assert_true(mp_chmap_from_str(&input_map, bstr0(input))); + assert_true(mp_chmap_from_str(&expected_map, bstr0(expected_selection))); + + for (int n = 0; layouts[n]; n++) { + struct mp_chmap tmp; + assert_true(mp_chmap_from_str(&tmp, bstr0(layouts[n]))); + int count = s.num_chmaps; + mp_chmap_sel_add_map(&s, &tmp); + assert_true(s.num_chmaps > count); // assure validity and max. count + } + + assert_true(mp_chmap_sel_fallback(&s, &input_map)); + // We convert expected_map to a chmap and then back to a string to avoid + // problems with ambiguous layouts. + assert_string_equal(mp_chmap_to_str(&input_map), + mp_chmap_to_str(&expected_map)); +} -static void test_mp_chmap_diff(void **state) { +static void run(void) +{ struct mp_chmap a; struct mp_chmap b; + struct mp_chmap_sel s = {0}; + + test_sel("5.1", "7.1", LAYOUTS("7.1")); + test_sel("7.1", "5.1", LAYOUTS("5.1")); + test_sel("7.1(wide-side)", "7.1", LAYOUTS("7.1")); + test_sel("7.1(wide-side)", "5.1(side)", LAYOUTS("7.1", "5.1(side)")); + test_sel("3.1", "5.1", LAYOUTS("7.1", "5.1", "2.1", "stereo", "mono")); + test_sel("5.1", "7.1(rear)", LAYOUTS("7.1(rear)")); + test_sel("5.1(side)", "5.1", LAYOUTS("5.1", "7.1")); + test_sel("5.1", "7.1(alsa)", LAYOUTS("7.1(alsa)")); + test_sel("mono", "stereo", LAYOUTS("stereo", "5.1")); + test_sel("stereo", "stereo", LAYOUTS("stereo", "5.1")); + test_sel("5.1(side)", "7.1(rear)", LAYOUTS("stereo", "7.1(rear)")); + test_sel("7.1", "fl-fr-lfe-fc-bl-br-flc-frc", + LAYOUTS("fl-fr-lfe-fc-bl-br-flc-frc", "3.0(back)")); + + mp_chmap_set_unknown(&a, 2); + + mp_chmap_from_str(&b, bstr0("5.1")); + + mp_chmap_sel_add_map(&s, &a); + assert_false(mp_chmap_sel_fallback(&s, &b)); + assert_string_equal(mp_chmap_to_str(&b), "5.1"); + + test_sel("quad", "quad(side)", LAYOUTS("quad(side)", "stereo")); + test_sel("quad", "quad(side)", LAYOUTS("quad(side)", "7.0")); + test_sel("quad", "quad(side)", LAYOUTS("7.0", "quad(side)")); + test_sel("quad", "7.1(wide-side)", LAYOUTS("7.1(wide-side)", "stereo")); + test_sel("quad", "7.1(wide-side)", LAYOUTS("stereo", "7.1(wide-side)")); + test_sel("quad", "fl-fr-sl-sr", + LAYOUTS("fl-fr-fc-bl-br", "fl-fr-sl-sr")); + test_sel("quad", "fl-fr-bl-br-na-na-na-na", + LAYOUTS("fl-fr-bl-br-na-na-na-na", "quad(side)", "stereo")); + test_sel("quad", "fl-fr-bl-br-na-na-na-na", + LAYOUTS("stereo", "quad(side)", "fl-fr-bl-br-na-na-na-na")); + test_sel("fl-fr-fc-lfe-sl-sr", "fl-fr-lfe-fc-bl-br-na-na", + LAYOUTS("fl-fr-lfe-fc-bl-br-na-na", "fl-fr-lfe-fc-bl-br-sdl-sdr")); + test_sel("fl-fr-fc-lfe-sl-sr", "fl-fr-lfe-fc-bl-br-na-na", + LAYOUTS("fl-fr-lfe-fc-bl-br-sdl-sdr", "fl-fr-lfe-fc-bl-br-na-na")); + + test_sel("na-fl-fr", "na-fl-fr", LAYOUTS("na-fl-fr-na", "fl-na-fr", "na-fl-fr", + "fl-fr-na-na", "na-na-fl-fr")); mp_chmap_from_str(&a, bstr0("3.1")); mp_chmap_from_str(&b, bstr0("2.1")); @@ -15,9 +87,7 @@ static void test_mp_chmap_diff(void **state) { assert_int_equal(mp_chmap_diffn(&b, &a), 3); } -int main(void) { - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_mp_chmap_diff), - }; - return cmocka_run_group_tests(tests, NULL, NULL); -} +const struct unittest test_chmap = { + .name = "chmap", + .run_simple = run, +}; diff --git a/test/chmap_sel.c b/test/chmap_sel.c deleted file mode 100644 index 38a5254073..0000000000 --- a/test/chmap_sel.c +++ /dev/null @@ -1,136 +0,0 @@ -#include "test_helpers.h" -#include "audio/chmap_sel.h" - -#define LAYOUTS(...) (char*[]){__VA_ARGS__, NULL} - -static void test_sel(const char *input, const char *expected_selection, - char **layouts) -{ - struct mp_chmap_sel s = {0}; - struct mp_chmap input_map; - struct mp_chmap expected_map; - - assert_true(mp_chmap_from_str(&input_map, bstr0(input))); - assert_true(mp_chmap_from_str(&expected_map, bstr0(expected_selection))); - - for (int n = 0; layouts[n]; n++) { - struct mp_chmap tmp; - assert_true(mp_chmap_from_str(&tmp, bstr0(layouts[n]))); - int count = s.num_chmaps; - mp_chmap_sel_add_map(&s, &tmp); - assert_true(s.num_chmaps > count); // assure validity and max. count - } - - assert_true(mp_chmap_sel_fallback(&s, &input_map)); - // We convert expected_map to a chmap and then back to a string to avoid - // problems with ambiguous layouts. - assert_string_equal(mp_chmap_to_str(&input_map), - mp_chmap_to_str(&expected_map)); -} - -static void test_mp_chmap_sel_fallback_upmix(void **state) { - test_sel("5.1", "7.1", LAYOUTS("7.1")); -} - -static void test_mp_chmap_sel_fallback_downmix(void **state) { - test_sel("7.1", "5.1", LAYOUTS("5.1")); -} - -static void test_mp_chmap_sel_fallback_incompatible(void **state) { - test_sel("7.1(wide-side)", "7.1", LAYOUTS("7.1")); -} - -static void test_mp_chmap_sel_fallback_prefer_compatible(void **state) { - test_sel("7.1(wide-side)", "5.1(side)", LAYOUTS("7.1", "5.1(side)")); -} - -static void test_mp_chmap_sel_fallback_prefer_closest_upmix(void **state) { - test_sel("3.1", "5.1", LAYOUTS("7.1", "5.1", "2.1", "stereo", "mono")); -} - -static void test_mp_chmap_sel_fallback_use_replacements(void **state) { - test_sel("5.1", "7.1(rear)", LAYOUTS("7.1(rear)")); -} - -static void test_mp_chmap_sel_fallback_inexact_equivalent(void **state) { - test_sel("5.1(side)", "5.1", LAYOUTS("5.1", "7.1")); -} - -static void test_mp_chmap_sel_fallback_works_on_alsa_chmaps(void **state) { - test_sel("5.1", "7.1(alsa)", LAYOUTS("7.1(alsa)")); -} - -static void test_mp_chmap_sel_fallback_mono_to_stereo(void **state) { - test_sel("mono", "stereo", LAYOUTS("stereo", "5.1")); -} - -static void test_mp_chmap_sel_fallback_stereo_to_stereo(void **state) { - test_sel("stereo", "stereo", LAYOUTS("stereo", "5.1")); -} - -static void test_mp_chmap_sel_fallback_no_downmix(void **state) { - test_sel("5.1(side)", "7.1(rear)", LAYOUTS("stereo", "7.1(rear)")); -} - -static void test_mp_chmap_sel_fallback_minimal_downmix(void **state) { - test_sel("7.1", "fl-fr-lfe-fc-bl-br-flc-frc", - LAYOUTS("fl-fr-lfe-fc-bl-br-flc-frc", "3.0(back)")); -} - -static void test_mp_chmap_sel_fallback_reject_unknown(void **state) { - struct mp_chmap a; - struct mp_chmap b; - struct mp_chmap_sel s = {0}; - - mp_chmap_set_unknown(&a, 2); - - mp_chmap_from_str(&b, bstr0("5.1")); - - mp_chmap_sel_add_map(&s, &a); - assert_false(mp_chmap_sel_fallback(&s, &b)); - assert_string_equal(mp_chmap_to_str(&b), "5.1"); -} - -static void test_mp_chmap_sel_fallback_more_replacements(void **state) { - test_sel("quad", "quad(side)", LAYOUTS("quad(side)", "stereo")); - test_sel("quad", "quad(side)", LAYOUTS("quad(side)", "7.0")); - test_sel("quad", "quad(side)", LAYOUTS("7.0", "quad(side)")); - test_sel("quad", "7.1(wide-side)", LAYOUTS("7.1(wide-side)", "stereo")); - test_sel("quad", "7.1(wide-side)", LAYOUTS("stereo", "7.1(wide-side)")); - test_sel("quad", "fl-fr-sl-sr", - LAYOUTS("fl-fr-fc-bl-br", "fl-fr-sl-sr")); - test_sel("quad", "fl-fr-bl-br-na-na-na-na", - LAYOUTS("fl-fr-bl-br-na-na-na-na", "quad(side)", "stereo")); - test_sel("quad", "fl-fr-bl-br-na-na-na-na", - LAYOUTS("stereo", "quad(side)", "fl-fr-bl-br-na-na-na-na")); - test_sel("fl-fr-fc-lfe-sl-sr", "fl-fr-lfe-fc-bl-br-na-na", - LAYOUTS("fl-fr-lfe-fc-bl-br-na-na", "fl-fr-lfe-fc-bl-br-sdl-sdr")); - test_sel("fl-fr-fc-lfe-sl-sr", "fl-fr-lfe-fc-bl-br-na-na", - LAYOUTS("fl-fr-lfe-fc-bl-br-sdl-sdr", "fl-fr-lfe-fc-bl-br-na-na")); -} - -static void test_mp_chmap_sel_fallback_na_channels(void **state) { - test_sel("na-fl-fr", "na-fl-fr", LAYOUTS("na-fl-fr-na", "fl-na-fr", "na-fl-fr", - "fl-fr-na-na", "na-na-fl-fr")); -} - -int main(void) { - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_mp_chmap_sel_fallback_upmix), - cmocka_unit_test(test_mp_chmap_sel_fallback_downmix), - cmocka_unit_test(test_mp_chmap_sel_fallback_incompatible), - cmocka_unit_test(test_mp_chmap_sel_fallback_prefer_compatible), - cmocka_unit_test(test_mp_chmap_sel_fallback_prefer_closest_upmix), - cmocka_unit_test(test_mp_chmap_sel_fallback_use_replacements), - cmocka_unit_test(test_mp_chmap_sel_fallback_inexact_equivalent), - cmocka_unit_test(test_mp_chmap_sel_fallback_works_on_alsa_chmaps), - cmocka_unit_test(test_mp_chmap_sel_fallback_mono_to_stereo), - cmocka_unit_test(test_mp_chmap_sel_fallback_stereo_to_stereo), - cmocka_unit_test(test_mp_chmap_sel_fallback_no_downmix), - cmocka_unit_test(test_mp_chmap_sel_fallback_minimal_downmix), - cmocka_unit_test(test_mp_chmap_sel_fallback_reject_unknown), - cmocka_unit_test(test_mp_chmap_sel_fallback_more_replacements), - cmocka_unit_test(test_mp_chmap_sel_fallback_na_channels), - }; - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/gl_video.c b/test/gl_video.c index 91271bd2d6..dd866c955c 100644 --- a/test/gl_video.c +++ b/test/gl_video.c @@ -1,42 +1,30 @@ +#include "index.h" #include "test_helpers.h" #include "video/out/gpu/video.h" -static void test_scale_ambient_lux_limits(void **state) { +static void run(void) +{ float x; + x = gl_video_scale_ambient_lux(16.0, 64.0, 2.40, 1.961, 16.0); assert_float_equal(x, 2.40f, FLT_EPSILON); x = gl_video_scale_ambient_lux(16.0, 64.0, 2.40, 1.961, 64.0); assert_float_equal(x, 1.961f, FLT_EPSILON); -} -static void test_scale_ambient_lux_sign(void **state) { - float x; x = gl_video_scale_ambient_lux(16.0, 64.0, 1.961, 2.40, 64.0); assert_float_equal(x, 2.40f, FLT_EPSILON); -} -static void test_scale_ambient_lux_clamping(void **state) { - float x; x = gl_video_scale_ambient_lux(16.0, 64.0, 2.40, 1.961, 0.0); assert_float_equal(x, 2.40f, FLT_EPSILON); -} -static void test_scale_ambient_lux_log10_midpoint(void **state) { - float x; // 32 corresponds to the the midpoint after converting lux to the log10 scale x = gl_video_scale_ambient_lux(16.0, 64.0, 2.40, 1.961, 32.0); float mid_gamma = (2.40 - 1.961) / 2 + 1.961; assert_float_equal(x, mid_gamma, FLT_EPSILON); } -int main(void) { - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_scale_ambient_lux_limits), - cmocka_unit_test(test_scale_ambient_lux_sign), - cmocka_unit_test(test_scale_ambient_lux_clamping), - cmocka_unit_test(test_scale_ambient_lux_log10_midpoint), - }; - return cmocka_run_group_tests(tests, NULL, NULL); -} - +const struct unittest test_gl_video = { + .name = "gl_video", + .run_simple = run, +}; diff --git a/test/index.c b/test/index.c new file mode 100644 index 0000000000..64f3f85344 --- /dev/null +++ b/test/index.c @@ -0,0 +1,51 @@ +#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; + } + + 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_simple ? (1 << 0) : 0) | + (t->run ? (1 << 1) : 0))); + + bool run = false; + run |= strcmp(sel, "all-simple") == 0 && !!t->run_simple; + run |= strcmp(sel, t->name); + + if (run) { + if (t->run_simple) + t->run_simple(); + if (t->run) + t->run(mpctx->global, mpctx->log); + num_run++; + } + } + + MP_INFO(mpctx, "%d unittests successfully run.\n", num_run); + + return num_run > 0; // still error if none +} diff --git a/test/index.h b/test/index.h new file mode 100644 index 0000000000..6060eb699f --- /dev/null +++ b/test/index.h @@ -0,0 +1,29 @@ +#pragma once + +#include <stdbool.h> + +struct mpv_global; +struct mp_log; +struct MPContext; + +bool run_tests(struct MPContext *mpctx); + +struct unittest { + // This is used to select the test on command line with --unittest=<name>. + const char *name; + + // Entrypoints. There are various for various purposes. Only 1 of them must + // be set. + + // Entrypoint for tests which don't depend on the mpv core. + void (*run_simple)(void); + + // Entrypoint for tests which have a simple dependency on the mpv core. The + // core is sufficiently initialized at this point. + void (*run)(struct mpv_global *global, struct mp_log *log); +}; + +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 0a4462bc21..e8499bee39 100644 --- a/test/json.c +++ b/test/json.c @@ -1,8 +1,8 @@ -#include "test_helpers.h" - #include "common/common.h" +#include "index.h" #include "misc/json.h" #include "misc/node.h" +#include "test_helpers.h" struct entry { const char *src; @@ -67,19 +67,20 @@ static const struct entry entries[] = { #define MAX_DEPTH 10 -static void test_json(void **state) +static void run(void) { for (int n = 0; n < MP_ARRAY_SIZE(entries); n++) { const struct entry *e = &entries[n]; - print_message("%d: %s\n", n, e->src); void *tmp = talloc_new(NULL); char *s = talloc_strdup(tmp, e->src); json_skip_whitespace(&s); struct mpv_node res; bool ok = json_parse(tmp, &res, &s, MAX_DEPTH) >= 0; assert_true(ok != e->expect_fail); - if (!ok) + if (!ok) { + talloc_free(tmp); continue; + } char *d = talloc_strdup(tmp, ""); assert_true(json_write(&d, &res) >= 0); assert_string_equal(e->out_txt, d); @@ -88,10 +89,7 @@ static void test_json(void **state) } } -int main(void) { - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_json), - }; - return cmocka_run_group_tests(tests, NULL, NULL); -} - +const struct unittest test_json = { + .name = "json", + .run_simple = run, +}; diff --git a/test/linked_list.c b/test/linked_list.c index a7b5b6b171..c679d8cc28 100644 --- a/test/linked_list.c +++ b/test/linked_list.c @@ -1,7 +1,7 @@ -#include "test_helpers.h" - #include "common/common.h" +#include "index.h" #include "misc/linked_list.h" +#include "test_helpers.h" struct list_item { int v; @@ -56,7 +56,7 @@ static bool do_check_list(struct the_list *lst, int *c, int num_c) return true; } -static void test_linked_list(void **state) +static void run(void) { struct the_list lst = {0}; struct list_item e1 = {1}; @@ -159,10 +159,7 @@ static void test_linked_list(void **state) check_list(3, 4, 5, 2, 1, 6); } -int main(void) { - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_linked_list), - }; - return cmocka_run_group_tests(tests, NULL, NULL); -} - +const struct unittest test_linked_list = { + .name = "linked_list", + .run_simple = run, +}; diff --git a/test/test_helpers.c b/test/test_helpers.c new file mode 100644 index 0000000000..187ff73c61 --- /dev/null +++ b/test/test_helpers.c @@ -0,0 +1,29 @@ +#include <inttypes.h> + +#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 index 8a8669c624..6ca3cd99b3 100644 --- a/test/test_helpers.h +++ b/test/test_helpers.h @@ -1,13 +1,24 @@ #ifndef MP_TESTS_H #define MP_TESTS_H -#include <stdarg.h> -#include <stddef.h> -#include <setjmp.h> -#include <cmocka.h> - -#include <stdio.h> #include <math.h> #include <float.h> +#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 |