History

test: split libmpv_test into separate files

The big libmpv test file is really actually three different tests shoved together into one. They do all use fairly similar underlying structures, so put the shared stuff into an inline header. There's a little bit of duplication with regards to the main function, but it's cleaner to separate out the tests.

Dudemanguy committed 1 year ago

Commit 25067e81 parent d980e955 files · +241 −140Browse files

Unified · Split

5 files
  1. test/libmpv_common.h +90 −0
  2. test/libmpv_test_file_loading.c +64 −0
  3. test/libmpv_test_lavfi_complex.c +70 −0
  4. test/libmpv_test_options.c +4 −136
  5. test/meson.build +13 −4
test/libmpv_common.h+90−0
@@ -0,0 +1,90 @@
1+/*
2+ * This file is part of mpv.
3+ *
4+ * mpv is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * mpv is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+#include <inttypes.h>
19+#include <libmpv/client.h>
20+#include <stdarg.h>
21+#include <stdbool.h>
22+#include <stdio.h>
23+#include <stdlib.h>
24+#include <string.h>
25+
26+// Stolen from osdep/compiler.h
27+#ifdef __GNUC__
28+#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format(printf, a1, a2)))
29+#define MP_NORETURN __attribute__((noreturn))
30+#else
31+#define PRINTF_ATTRIBUTE(a1, a2)
32+#define MP_NORETURN
33+#endif
34+
35+// Broken crap with __USE_MINGW_ANSI_STDIO
36+#if defined(__MINGW32__) && defined(__GNUC__) && !defined(__clang__)
37+#undef PRINTF_ATTRIBUTE
38+#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (gnu_printf, a1, a2)))
39+#endif
40+
41+// Global handle.
42+static mpv_handle *ctx;
43+MP_NORETURN PRINTF_ATTRIBUTE(1, 2)
44+
45+static inline void fail(const char *fmt, ...)
46+{
47+ if (fmt) {
48+ va_list va;
49+ va_start(va, fmt);
50+ vfprintf(stderr, fmt, va);
51+ va_end(va);
52+ }
53+ exit(1);
54+}
55+
56+static inline void exit_cleanup(void)
57+{
58+ mpv_destroy(ctx);
59+}
60+
61+static inline mpv_event *wrap_wait_event(void)
62+{
63+ while (1) {
64+ mpv_event *ev = mpv_wait_event(ctx, 1);
65+ if (ev->event_id == MPV_EVENT_NONE)
66+ continue;
67+
68+ if (ev->event_id == MPV_EVENT_LOG_MESSAGE) {
69+ mpv_event_log_message *msg = (mpv_event_log_message*)ev->data;
70+ printf("[%s:%s] %s", msg->prefix, msg->level, msg->text);
71+ if (msg->log_level <= MPV_LOG_LEVEL_ERROR)
72+ fail("error was logged");
73+ } else {
74+ return ev;
75+ }
76+ }
77+}
78+
79+static inline void check_api_error(int status)
80+{
81+ if (status < 0)
82+ fail("mpv API error: %s\n", mpv_error_string(status));
83+}
84+
85+static inline void initialize(void)
86+{
87+ check_api_error(mpv_set_option_string(ctx, "vo", "null"));
88+ check_api_error(mpv_request_log_messages(ctx, "debug"));
89+ check_api_error(mpv_initialize(ctx));
90+}
test/libmpv_test_file_loading.c+64−0
@@ -0,0 +1,64 @@
1+/*
2+ * This file is part of mpv.
3+ *
4+ * mpv is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * mpv is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+#include "libmpv_common.h"
19+
20+static void test_file_loading(char *file)
21+{
22+ const char *cmd[] = {"loadfile", file, NULL};
23+ check_api_error(mpv_command(ctx, cmd));
24+ bool loaded = false;
25+ bool finished = false;
26+ while (!finished) {
27+ mpv_event *event = wrap_wait_event();
28+ switch (event->event_id) {
29+ case MPV_EVENT_FILE_LOADED:
30+ // make sure it loads before exiting
31+ loaded = true;
32+ break;
33+ case MPV_EVENT_END_FILE:
34+ if (loaded)
35+ finished = true;
36+ break;
37+ }
38+ }
39+ if (!finished)
40+ fail("Unable to load test file!\n");
41+}
42+
43+int main(int argc, char *argv[])
44+{
45+ if (argc != 2)
46+ return 1;
47+ atexit(exit_cleanup);
48+
49+ ctx = mpv_create();
50+ if (!ctx)
51+ return 1;
52+
53+ initialize();
54+
55+ const char *fmt = "================ TEST: %s ================\n";
56+ printf(fmt, "test_file_loading");
57+ test_file_loading(argv[1]);
58+ printf("================ SHUTDOWN ================\n");
59+
60+ mpv_command_string(ctx, "quit");
61+ while (wrap_wait_event()->event_id != MPV_EVENT_SHUTDOWN) {}
62+
63+ return 0;
64+}
test/libmpv_test_lavfi_complex.c+70−0
@@ -0,0 +1,70 @@
1+/*
2+ * This file is part of mpv.
3+ *
4+ * mpv is free software; you can redistribute it and/or
5+ * modify it under the terms of the GNU Lesser General Public
6+ * License as published by the Free Software Foundation; either
7+ * version 2.1 of the License, or (at your option) any later version.
8+ *
9+ * mpv is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU Lesser General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Lesser General Public
15+ * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16+ */
17+
18+#include "libmpv_common.h"
19+
20+static void test_lavfi_complex(char *file)
21+{
22+ const char *cmd[] = {"loadfile", file, NULL};
23+ check_api_error(mpv_command(ctx, cmd));
24+ bool finished = false;
25+ bool loaded = false;
26+ while (!finished) {
27+ mpv_event *event = wrap_wait_event();
28+ switch (event->event_id) {
29+ case MPV_EVENT_FILE_LOADED:
30+ // Add file as external and toggle lavfi-complex on.
31+ if (!loaded) {
32+ check_api_error(mpv_set_property_string(ctx, "external-files", file));
33+ const char *add_cmd[] = {"video-add", file, "auto", NULL};
34+ check_api_error(mpv_command(ctx, add_cmd));
35+ check_api_error(mpv_set_property_string(ctx, "lavfi-complex", "[vid1] [vid2] vstack [vo]"));
36+ }
37+ loaded = true;
38+ break;
39+ case MPV_EVENT_END_FILE:
40+ if (loaded)
41+ finished = true;
42+ break;
43+ }
44+ }
45+ if (!finished)
46+ fail("Lavfi complex failed!\n");
47+}
48+
49+int main(int argc, char *argv[])
50+{
51+ if (argc != 2)
52+ return 1;
53+ atexit(exit_cleanup);
54+
55+ ctx = mpv_create();
56+ if (!ctx)
57+ return 1;
58+
59+ initialize();
60+
61+ const char *fmt = "================ TEST: %s ================\n";
62+ printf(fmt, "test_lavfi_complex");
63+ test_lavfi_complex(argv[1]);
64+ printf("================ SHUTDOWN ================\n");
65+
66+ mpv_command_string(ctx, "quit");
67+ while (wrap_wait_event()->event_id != MPV_EVENT_SHUTDOWN) {}
68+
69+ return 0;
70+}
test/libmpv_test_options.crenamed from test/libmpv_test.c+4−136
@@ -15,27 +15,7 @@ · 14 unmodified lines
1515 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
1616 */
1717
18−#include <inttypes.h>
19−#include <libmpv/client.h>
20−#include <stdarg.h>
21−#include <stdio.h>
22−#include <stdlib.h>
23−#include <string.h>
24−
25−// Stolen from osdep/compiler.h
26−#ifdef __GNUC__
27−#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format(printf, a1, a2)))
28−#define MP_NORETURN __attribute__((noreturn))
29−#else
30−#define PRINTF_ATTRIBUTE(a1, a2)
31−#define MP_NORETURN
32−#endif
33−
34−// Broken crap with __USE_MINGW_ANSI_STDIO
35−#if defined(__MINGW32__) && defined(__GNUC__) && !defined(__clang__)
36−#undef PRINTF_ATTRIBUTE
37−#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (gnu_printf, a1, a2)))
38−#endif
18+#include "libmpv_common.h"
3919
4020 // Dummy values for test_options_and_properties
4121 static const char *str = "string";
@@ -43,54 +23,6 @@ static int flag = 1; · 1 unmodified line
4323 static int64_t int_ = 20;
4424 static double double_ = 1.5;
4525
46−// Global handle.
47−static mpv_handle *ctx;
48−
49−MP_NORETURN PRINTF_ATTRIBUTE(1, 2)
50−static void fail(const char *fmt, ...)
51−{
52− if (fmt) {
53− va_list va;
54− va_start(va, fmt);
55− vfprintf(stderr, fmt, va);
56− va_end(va);
57− }
58− exit(1);
59−}
60−
61−
62−static void exit_cleanup(void)
63−{
64− if (ctx)
65− mpv_destroy(ctx);
66−}
67−
68−static mpv_event *wrap_wait_event(void)
69−{
70− while (1) {
71− mpv_event *ev = mpv_wait_event(ctx, 1);
72−
73− if (ev->event_id == MPV_EVENT_NONE) {
74− continue;
75− } else if (ev->event_id == MPV_EVENT_LOG_MESSAGE) {
76− mpv_event_log_message *msg = (mpv_event_log_message*)ev->data;
77− printf("[%s:%s] %s", msg->prefix, msg->level, msg->text);
78− if (msg->log_level <= MPV_LOG_LEVEL_ERROR)
79− fail("error was logged");
80− } else {
81− return ev;
82− }
83− }
84−}
85−
86−static void check_api_error(int status)
87−{
88− if (status < 0)
89− fail("mpv API error: %s\n", mpv_error_string(status));
90−}
91−
92−/****/
93−
9426 static void check_double(const char *property, double expect)
9527 {
9628 double result_double;
@@ -169,60 +101,6 @@ static void set_options_and_properties(const char *options[], const char *proper · 72 unmodified lines
169101 }
170102 }
171103
172−/****/
173−
174−static void test_file_loading(char *file)
175−{
176− const char *cmd[] = {"loadfile", file, NULL};
177− check_api_error(mpv_command(ctx, cmd));
178− int loaded = 0;
179− int finished = 0;
180− while (!finished) {
181− mpv_event *event = wrap_wait_event();
182− switch (event->event_id) {
183− case MPV_EVENT_FILE_LOADED:
184− // make sure it loads before exiting
185− loaded = 1;
186− break;
187− case MPV_EVENT_END_FILE:
188− if (loaded)
189− finished = 1;
190− break;
191− }
192− }
193− if (!finished)
194− fail("Unable to load test file!\n");
195−}
196−
197−static void test_lavfi_complex(char *file)
198−{
199− const char *cmd[] = {"loadfile", file, NULL};
200− check_api_error(mpv_command(ctx, cmd));
201− int finished = 0;
202− int loaded = 0;
203− while (!finished) {
204− mpv_event *event = wrap_wait_event();
205− switch (event->event_id) {
206− case MPV_EVENT_FILE_LOADED:
207− // Add file as external and toggle lavfi-complex on.
208− if (!loaded) {
209− check_api_error(mpv_set_property_string(ctx, "external-files", file));
210− const char *add_cmd[] = {"video-add", file, "auto", NULL};
211− check_api_error(mpv_command(ctx, add_cmd));
212− check_api_error(mpv_set_property_string(ctx, "lavfi-complex", "[vid1] [vid2] vstack [vo]"));
213− }
214− loaded = 1;
215− break;
216− case MPV_EVENT_END_FILE:
217− if (loaded)
218− finished = 1;
219− break;
220− }
221− }
222− if (!finished)
223− fail("Lavfi complex failed!\n");
224−}
225−
226104 // Ensure that setting options/properties work correctly and
227105 // have the expected values.
228106 static void test_options_and_properties(void)
@@ -255,8 +133,6 @@ static void test_options_and_properties(void) · 26 unmodified lines
255133
256134 set_options_and_properties(options, properties, formats);
257135
258− check_api_error(mpv_initialize(ctx));
259−
260136 check_results(options, formats);
261137 check_results(properties, formats);
262138
@@ -273,7 +149,7 @@ static void test_options_and_properties(void) · 10 unmodified lines
273149
274150 int main(int argc, char *argv[])
275151 {
276− if (argc != 2)
152+ if (argc != 1)
277153 return 1;
278154 atexit(exit_cleanup);
279155
@@ -281,21 +157,13 @@ int main(int argc, char *argv[]) · 1 unmodified line
281157 if (!ctx)
282158 return 1;
283159
284− check_api_error(mpv_set_option_string(ctx, "vo", "null"));
285− // load osc too to see if it works
286− check_api_error(mpv_set_option_string(ctx, "osc", "yes"));
287− check_api_error(mpv_request_log_messages(ctx, "debug"));
160+ initialize();
288161
289162 const char *fmt = "================ TEST: %s ================\n";
290−
291163 printf(fmt, "test_options_and_properties");
292164 test_options_and_properties();
293− printf(fmt, "test_file_loading");
294− test_file_loading(argv[1]);
295− printf(fmt, "test_lavfi_complex");
296− test_lavfi_complex(argv[1]);
297−
298165 printf("================ SHUTDOWN ================\n");
166+
299167 mpv_command_string(ctx, "quit");
300168 while (wrap_wait_event()->event_id != MPV_EVENT_SHUTDOWN) {}
301169
test/meson.build+13−4
@@ -116,14 +116,23 @@ paths = executable('paths', 'paths.c', include_directories: incdir, · 115 unmodified lines
116116 test('paths', paths)
117117
118118 if get_option('libmpv')
119− exe = executable('libmpv-test', 'libmpv_test.c',
120− include_directories: incdir, link_with: libmpv)
121119 file = join_paths(source_root, 'etc', 'mpv-icon-8bit-16x16.png')
122− test('libmpv', exe, args: file, timeout: 60, should_fail: not features['lua'], suite: 'libmpv')
120+
121+ exe = executable('libmpv-test-file-loading', 'libmpv_test_file_loading.c',
122+ include_directories: incdir, link_with: libmpv)
123+ test('libmpv-test-file-loading', exe, args: file, suite: 'libmpv')
124+
125+ exe = executable('libmpv-test-lavfi-complex', 'libmpv_test_lavfi_complex.c',
126+ include_directories: incdir, link_with: libmpv)
127+ test('libmpv-test-lavfi-complex', exe, args: file, suite: 'libmpv')
128+
129+ exe = executable('libmpv-test-options', 'libmpv_test_options.c',
130+ include_directories: incdir, link_with: libmpv)
131+ test('libmpv-test-options', exe, suite: 'libmpv')
123132
124133 exe = executable('libmpv-encode', 'libmpv_encode.c',
125134 include_directories: incdir, link_with: libmpv)
126− test('libmpv-encode', exe, timeout: 30, suite: 'libmpv')
135+ test('libmpv-encode', exe, suite: 'libmpv')
127136
128137 mpvlib = libmpv
129138 shared = get_option('default_library') == 'shared'

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t