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
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
int
libsimple_timespec2timeval(struct timeval *restrict tv, const struct timespec *restrict ts)
{
tv->tv_sec = ts->tv_sec;
tv->tv_usec = ts->tv_nsec / 1000L;
if ((ts->tv_nsec % 1000L) >= 500L) {
if (++(tv->tv_usec) == 1000000L) {
if (LIBSIMPLE_SINCR_OVERFLOW(&tv->tv_sec, TIME_MAX)) {
tv->tv_sec = TIME_MAX;
tv->tv_usec = 999999L;
errno = EOVERFLOW;
return -1;
}
tv->tv_usec = 0;
}
}
return 0;
}
#else
#include "test.h"
int
main(void)
{
struct timeval tv;
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){0, 0}));
assert(tv.tv_sec == 0);
assert(tv.tv_usec == 0);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 0}));
assert(tv.tv_sec == 100);
assert(tv.tv_usec == 0);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 100000000L}));
assert(tv.tv_sec == 100);
assert(tv.tv_usec == 100000L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 100000400L}));
assert(tv.tv_sec == 100);
assert(tv.tv_usec == 100000L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 100000500L}));
assert(tv.tv_sec == 100);
assert(tv.tv_usec == 100001L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){100, 999999999L}));
assert(tv.tv_sec == 101);
assert(tv.tv_usec == 0L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 0}));
assert(tv.tv_sec == -100);
assert(tv.tv_usec == 0);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 100000000L}));
assert(tv.tv_sec == -100);
assert(tv.tv_usec == 100000L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 100000400L}));
assert(tv.tv_sec == -100);
assert(tv.tv_usec == 100000L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 100000500L}));
assert(tv.tv_sec == -100);
assert(tv.tv_usec == 100001L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){-100, 999999999L}));
assert(tv.tv_sec == -99);
assert(tv.tv_usec == 0L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MAX, 100000500L}));
assert(tv.tv_sec == TIME_MAX);
assert(tv.tv_usec == 100001L);
assert(libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MAX, 999999500L}) == -1 && errno == EOVERFLOW);
assert(tv.tv_sec == TIME_MAX);
assert(tv.tv_usec == 999999L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MIN, 100000500L}));
assert(tv.tv_sec == TIME_MIN);
assert(tv.tv_usec == 100001L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MIN, 0L}));
assert(tv.tv_sec == TIME_MIN);
assert(tv.tv_usec == 0L);
assert(!libsimple_timespec2timeval(&tv, &(struct timespec){TIME_MIN, 500L}));
assert(tv.tv_sec == TIME_MIN);
assert(tv.tv_usec == 1L);
return 0;
}
#endif
|