Skip to content

Commit

Permalink
test: add basic tests for timer code
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Oct 20, 2023
1 parent af53ce1 commit dad4e61
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
12 changes: 7 additions & 5 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions test/test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 0 additions & 10 deletions test/test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 54 additions & 0 deletions test/timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "common/common.h"
#include "osdep/timer.h"
#include "test_utils.h"

#include <time.h>
#include <sys/time.h>
#include <limits.h>

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;
}

0 comments on commit dad4e61

Please sign in to comment.