diff --git a/test/meson.build b/test/meson.build index d5d567caa16b3..19fa2dcf005e2 100644 --- a/test/meson.build +++ b/test/meson.build @@ -25,6 +25,8 @@ test_utils_files = [ 'options/path.c', 'osdep/io.c', 'osdep/subprocess.c', + 'osdep/timer.c', + timer_source, path_source, subprocess_source, 'ta/ta.c', @@ -34,13 +36,10 @@ test_utils_files = [ test_utils_deps = [libavutil, libm] -# The zimg code requires using threads. On windows, threads -# also requires timer code so this is added. +# The zimg code requires using threads. if features['win32-internal-pthreads'] test_utils_args += '-DWIN32_TESTS' - test_utils_files += ['osdep/timer.c', - 'osdep/timer-win2.c', - 'osdep/win32/pthread.c', + test_utils_files += ['osdep/win32/pthread.c', 'osdep/windows_utils.c'] else test_utils_deps += pthreads @@ -98,6 +97,9 @@ test('json', json) linked_list = executable('linked-list', files('linked_list.c'), include_directories: incdir) test('linked-list', linked_list) +timer = executable('timer', files('timer.c'), include_directories: incdir, link_with: test_utils) +test('timer', timer) + paths_objects = libmpv.extract_objects('options/path.c', path_source) paths = executable('paths', 'paths.c', include_directories: incdir, objects: paths_objects, link_with: test_utils) diff --git a/test/test_utils.c b/test/test_utils.c index 4bd97f30f8def..ac59c17e8f7f9 100644 --- a/test/test_utils.c +++ b/test/test_utils.c @@ -106,10 +106,3 @@ int mp_msg_find_level(const char *s) {return 0;}; int mp_msg_level(struct mp_log *log) {return 0;}; void mp_write_console_ansi(void) {}; void mp_set_avdict(AVDictionary **dict, char **kv) {}; - -#ifndef WIN32_TESTS -void mp_rel_time_to_timespec(void) {}; -void mp_time_ns(void) {}; -void mp_time_ns_add(void) {}; -void mp_time_ns_to_realtime(void) {}; -#endif diff --git a/test/test_utils.h b/test/test_utils.h index ac1dfb5d1d4b0..66615d3710857 100644 --- a/test/test_utils.h +++ b/test/test_utils.h @@ -54,13 +54,3 @@ int mp_msg_level(struct mp_log *log); void mp_write_console_ansi(void); typedef struct AVDictionary AVDictionary; void mp_set_avdict(AVDictionary **dict, char **kv); - -// Windows additionally requires timer related code so it will actually -// import the real versions of these functions and use them. On other -// platforms, these can just be stubs for simplicity. -#ifndef WIN32_TESTS -void mp_rel_time_to_timespec(void); -void mp_time_ns(void); -void mp_time_ns_add(void); -void mp_time_ns_to_realtime(void); -#endif diff --git a/test/timer.c b/test/timer.c new file mode 100644 index 0000000000000..6fe7770c2e454 --- /dev/null +++ b/test/timer.c @@ -0,0 +1,54 @@ +#include "common/common.h" +#include "osdep/timer.h" +#include "test_utils.h" + +#include +#include +#include + +int main(void) +{ + mp_time_init(); + + /* timekeeping */ + { + int64_t now = mp_time_ns(); + assert_true(now > 0); + + mp_sleep_ns(MP_TIME_MS_TO_NS(10)); + + int64_t now2 = mp_time_ns(); + assert_true(now2 > now); + + mp_sleep_ns(MP_TIME_MS_TO_NS(10)); + + double now3 = mp_time_sec(); + assert_true(now3 > MP_TIME_NS_TO_S(now2)); + } + + /* arithmetic */ + { + const int64_t test = 123456; + assert_int_equal(mp_time_ns_add(test, 1.0), test + MP_TIME_S_TO_NS(1)); + assert_int_equal(mp_time_ns_add(test, DBL_MAX), INT64_MAX); + assert_int_equal(mp_time_ns_add(test, -1e13), 1); + + const int64_t test2 = INT64_MAX - MP_TIME_S_TO_NS(20); + assert_int_equal(mp_time_ns_add(test2, 20.44), INT64_MAX); + } + + /* conversion */ + { + struct timeval tv; + struct timespec ts; + gettimeofday(&tv, NULL); + ts = mp_time_ns_to_realtime(mp_time_ns()); + assert_true(llabs(tv.tv_sec - ts.tv_sec) <= 1); + + gettimeofday(&tv, NULL); + ts = mp_rel_time_to_timespec(0.0); + assert_true(llabs(tv.tv_sec - ts.tv_sec) <= 1); + } + + return 0; +}