test: add basic tests for timer code

This commit is contained in:
sfan5 2023-10-18 15:45:47 +02:00
parent af53ce18bd
commit dad4e61b68
4 changed files with 61 additions and 22 deletions

View File

@ -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)

View File

@ -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

View File

@ -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
test/timer.c Normal file
View File

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