-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.c
120 lines (92 loc) · 1.72 KB
/
time_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#define _GNU_SOURCE
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 700
#endif
#include "sc_time.h"
#include <assert.h>
void test1(void)
{
assert(sc_time_ns() != 0);
assert(sc_time_ms() != 0);
for (int i = 0; i < 100000; i++) {
uint64_t t1 = sc_time_mono_ms();
uint64_t t2 = sc_time_mono_ms();
assert(t2 >= t1);
}
for (int i = 0; i < 100000; i++) {
uint64_t t1 = sc_time_mono_ns();
uint64_t t2 = sc_time_mono_ns();
assert(t2 >= t1);
}
}
void test2(void)
{
uint64_t t1, t2;
t1 = sc_time_mono_ms();
sc_time_sleep(1000);
t2 = sc_time_mono_ms();
assert(t2 > t1);
t1 = sc_time_mono_ns();
sc_time_sleep(1000);
t2 = sc_time_mono_ns();
assert(t2 > t1);
}
#ifdef SC_HAVE_WRAP
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
void sig_handler(int signum)
{
printf("signal : %d", signum);
}
void test3(void)
{
uint64_t t1, t2;
signal(SIGALRM, sig_handler);
printf("%d \n", alarm(2));
t1 = sc_time_mono_ms();
sc_time_sleep(4000);
t2 = sc_time_mono_ms();
assert(t2 > t1);
printf("%d \n", alarm(2));
t1 = sc_time_mono_ns();
sc_time_sleep(4000);
t2 = sc_time_mono_ns();
assert(t2 > t1);
}
bool fail_nanosleep = false;
int __real_nanosleep(const struct timespec *__requested_time,
struct timespec *__remaining);
int __wrap_nanosleep(const struct timespec *__requested_time,
struct timespec *__remaining)
{
if (fail_nanosleep) {
errno = ERANGE;
return -1;
}
return __real_nanosleep(__requested_time, __remaining);
}
void test4(void)
{
fail_nanosleep = true;
assert(sc_time_sleep(100) != 0);
fail_nanosleep = false;
}
#else
void test3(void)
{
}
void test4(void)
{
}
#endif
int main(void)
{
test1();
test2();
test3();
test4();
return 0;
}