sc/time/time_test.c

121 lines
1.7 KiB
C
Raw Normal View History

#define _GNU_SOURCE
2021-02-10 21:33:03 +03:00
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
2021-02-10 21:33:03 +03:00
#endif
2020-11-11 01:19:49 +03:00
#include "sc_time.h"
#include <assert.h>
void test1(void)
{
assert(sc_time_ns() != 0);
assert(sc_time_ms() != 0);
2020-11-11 01:19:49 +03:00
for (int i = 0; i < 100000; i++) {
uint64_t t1 = sc_time_mono_ms();
uint64_t t2 = sc_time_mono_ms();
2020-11-11 01:19:49 +03:00
assert(t2 >= t1);
}
2020-11-11 01:19:49 +03:00
for (int i = 0; i < 100000; i++) {
uint64_t t1 = sc_time_mono_ns();
uint64_t t2 = sc_time_mono_ns();
2020-11-11 01:19:49 +03:00
assert(t2 >= t1);
}
2020-11-11 01:19:49 +03:00
}
void test2(void)
{
uint64_t t1, t2;
2020-11-11 01:19:49 +03:00
t1 = sc_time_mono_ms();
sc_time_sleep(1000);
t2 = sc_time_mono_ms();
2020-11-11 01:19:49 +03:00
assert(t2 > t1);
2020-11-11 01:19:49 +03:00
t1 = sc_time_mono_ns();
sc_time_sleep(1000);
t2 = sc_time_mono_ns();
2020-11-11 01:19:49 +03:00
assert(t2 > t1);
2020-11-11 01:19:49 +03:00
}
2021-01-31 05:21:59 +03:00
#ifdef SC_HAVE_WRAP
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
2021-01-31 05:21:59 +03:00
void sig_handler(int signum)
{
printf("signal : %d", signum);
2021-01-31 05:21:59 +03:00
}
void test3(void)
{
uint64_t t1, t2;
2021-01-31 05:21:59 +03:00
signal(SIGALRM, sig_handler);
printf("%d \n", alarm(2));
2021-01-31 05:21:59 +03:00
t1 = sc_time_mono_ms();
sc_time_sleep(4000);
t2 = sc_time_mono_ms();
2021-01-31 05:21:59 +03:00
assert(t2 > t1);
2021-01-31 05:21:59 +03:00
printf("%d \n", alarm(2));
t1 = sc_time_mono_ns();
sc_time_sleep(4000);
t2 = sc_time_mono_ns();
2021-01-31 05:21:59 +03:00
assert(t2 > t1);
2021-01-31 05:21:59 +03:00
}
bool fail_nanosleep = false;
int __real_nanosleep(const struct timespec *__requested_time,
struct timespec *__remaining);
2021-01-31 05:21:59 +03:00
int __wrap_nanosleep(const struct timespec *__requested_time,
struct timespec *__remaining)
2021-01-31 05:21:59 +03:00
{
if (fail_nanosleep) {
errno = ERANGE;
return -1;
}
2021-01-31 05:21:59 +03:00
return __real_nanosleep(__requested_time, __remaining);
2021-01-31 05:21:59 +03:00
}
void test4(void)
{
fail_nanosleep = true;
assert(sc_time_sleep(100) != 0);
fail_nanosleep = false;
2021-01-31 05:21:59 +03:00
}
#else
void test3(void)
{
}
void test4(void)
{
}
#endif
2021-02-07 22:31:04 +03:00
int main()
2020-11-11 01:19:49 +03:00
{
test1();
test2();
test3();
test4();
2021-01-31 05:21:59 +03:00
return 0;
2020-11-11 01:19:49 +03:00
}