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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/* Test timespec functions.
Copyright 2015-2022 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Paul Eggert. */
#include <config.h>
#include "timespec.h"
#include "intprops.h"
#include "macros.h"
#include <stdbool.h>
#include <limits.h>
static struct { int s; int ns; } const prototype[] =
{
{ INT_MIN, 0 },
{ INT_MIN, 1 },
{ INT_MIN, TIMESPEC_HZ - 1 },
{ INT_MIN + 1, 0 },
{ INT_MIN + 1, 1 },
{ INT_MIN + 1, TIMESPEC_HZ - 1 },
{ -1, 0 },
{ -1, 1 },
{ -1, TIMESPEC_HZ - 1 },
{ 0, 0 },
{ 0, 1 },
{ 0, TIMESPEC_HZ - 1 },
{ 1, 0 },
{ 1, 1 },
{ 1, TIMESPEC_HZ - 1 },
{ 1234567890, 0 },
{ 1234567890, 1 },
{ 1234567890, TIMESPEC_HZ - 1 },
{ INT_MAX - 1, 0 },
{ INT_MAX - 1, 1 },
{ INT_MAX - 1, TIMESPEC_HZ - 1 },
{ INT_MAX, 0 },
{ INT_MAX, 1 },
{ INT_MAX, TIMESPEC_HZ - 1 },
{ INT_MAX, 2 * TIMESPEC_HZ }
};
enum { nprototypes = sizeof prototype / sizeof *prototype };
static bool
valid (struct timespec a)
{
return 0 <= a.tv_nsec && a.tv_nsec < TIMESPEC_HZ;
}
static int
sign (int i)
{
return i < 0 ? -1 : 0 < i;
}
static int
cmp (struct timespec a, struct timespec b)
{
return sign (timespec_cmp (a, b));
}
static bool
eq (struct timespec a, struct timespec b)
{
return timespec_cmp (a, b) == 0;
}
static bool
extremal (struct timespec a)
{
return ((a.tv_sec == TYPE_MINIMUM (time_t) && a.tv_nsec == 0)
|| (a.tv_sec == TYPE_MAXIMUM (time_t)
&& a.tv_nsec == TIMESPEC_HZ - 1));
}
int
main (void)
{
int i, j, k;
struct timespec test[nprototypes + 1];
int ntests;
int computed_hz = 1;
struct timespec prevroundtrip;
test[0] = make_timespec (TYPE_MINIMUM (time_t), -1);
ntests = 1;
for (i = 0; i < nprototypes; i++)
{
int s = prototype[i].s;
if (TYPE_SIGNED (time_t) || 0 <= s)
{
time_t t = (s <= INT_MIN + 1 ? s - INT_MIN + TYPE_MINIMUM (time_t)
: INT_MAX - 1 <= s ? s - INT_MAX + TYPE_MAXIMUM (time_t)
: s);
test[ntests++] = make_timespec (t, prototype[i].ns);
}
}
for (i = 0; i < LOG10_TIMESPEC_HZ; i++)
computed_hz *= 10;
ASSERT (computed_hz == TIMESPEC_HZ);
for (i = 0; i < ntests; i++)
{
struct timespec a = test[i];
struct timespec roundtrip = dtotimespec (timespectod (a));
if (i != 0)
ASSERT (cmp (prevroundtrip, roundtrip) <= 0);
prevroundtrip = roundtrip;
ASSERT (sign (timespec_sign (a)) == cmp (a, make_timespec (0, 0)));
if (valid (a))
for (j = 0; j < ntests; j++)
{
struct timespec b = test[j];
if (valid (b))
{
struct timespec sum = timespec_add (a, b);
struct timespec diff = timespec_sub (a, b);
struct timespec rdiff = timespec_sub (b, a);
ASSERT (cmp (a, b) == sign (i - j));
ASSERT (eq (sum, timespec_add (b, a)));
if (! extremal (sum))
{
ASSERT (eq (a, timespec_sub (sum, b)));
ASSERT (eq (b, timespec_sub (sum, a)));
for (k = 0; k < ntests; k++)
{
struct timespec c = test[k];
if (valid (c))
{
struct timespec sumbc = timespec_add (b, c);
if (! extremal (sumbc))
ASSERT (eq (timespec_add (a, sumbc),
timespec_add (sum, c)));
}
}
}
if (! extremal (diff))
ASSERT (eq (a, timespec_add (diff, b)));
if (! extremal (rdiff))
ASSERT (eq (b, timespec_add (rdiff, a)));
}
}
}
return 0;
}
|