Fix time/tst-cpuclock1 intermitent failures
[glibc.git] / support / tst-timespec.c
blob71423555a922357412940ba22a26b29e0882a2a3
1 /* Test for support_timespec_check_in_range function.
2 Copyright (C) 2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <support/timespec.h>
20 #include <support/check.h>
21 #include <limits.h>
23 #define TIMESPEC_HZ 1000000000
25 struct timespec_ns_test_case
27 struct timespec time;
28 long time_ns;
31 struct timespec_norm_test_case
33 struct timespec time;
34 struct timespec norm;
37 struct timespec_test_case
39 struct timespec expected;
40 struct timespec observed;
41 double upper_bound;
42 double lower_bound;
43 int result;
46 /* Test cases for timespec_ns */
47 struct timespec_ns_test_case ns_cases[] = {
48 {.time = {.tv_sec = 0, .tv_nsec = 0},
49 .time_ns = 0,
51 {.time = {.tv_sec = 0, .tv_nsec = 1},
52 .time_ns = 1,
54 {.time = {.tv_sec = 1, .tv_nsec = 0},
55 .time_ns = TIMESPEC_HZ,
57 {.time = {.tv_sec = 1, .tv_nsec = 1},
58 .time_ns = TIMESPEC_HZ + 1,
60 {.time = {.tv_sec = 0, .tv_nsec = -1},
61 .time_ns = -1,
63 {.time = {.tv_sec = -1, .tv_nsec = 0},
64 .time_ns = -TIMESPEC_HZ,
66 {.time = {.tv_sec = -1, .tv_nsec = -1},
67 .time_ns = -TIMESPEC_HZ - 1,
69 {.time = {.tv_sec = 1, .tv_nsec = -1},
70 .time_ns = TIMESPEC_HZ - 1,
72 {.time = {.tv_sec = -1, .tv_nsec = 1},
73 .time_ns = -TIMESPEC_HZ + 1,
75 /* Overflow bondary by 2 */
76 {.time = {.tv_sec = LONG_MAX / TIMESPEC_HZ, .tv_nsec = LONG_MAX%TIMESPEC_HZ - 1},
77 .time_ns = LONG_MAX - 1,
79 /* Overflow bondary */
80 {.time = {.tv_sec = LONG_MAX / TIMESPEC_HZ, .tv_nsec = LONG_MAX%TIMESPEC_HZ},
81 .time_ns = LONG_MAX,
83 /* Underflow bondary by 1 */
84 {.time = {.tv_sec = LONG_MIN / TIMESPEC_HZ, .tv_nsec = LONG_MIN%TIMESPEC_HZ + 1},
85 .time_ns = LONG_MIN + 1,
87 /* Underflow bondary */
88 {.time = {.tv_sec = LONG_MIN / TIMESPEC_HZ, .tv_nsec = LONG_MIN%TIMESPEC_HZ},
89 .time_ns = LONG_MIN,
91 /* Multiplication overflow */
92 {.time = {.tv_sec = LONG_MAX / TIMESPEC_HZ + 1, .tv_nsec = 1},
93 .time_ns = LONG_MAX,
95 /* Multiplication underflow */
96 {.time = {.tv_sec = LONG_MIN / TIMESPEC_HZ - 1, .tv_nsec = -1},
97 .time_ns = LONG_MIN,
99 /* Sum overflows */
100 {.time = {.tv_sec = LONG_MAX / TIMESPEC_HZ, .tv_nsec = LONG_MAX%TIMESPEC_HZ + 1},
101 .time_ns = LONG_MAX,
103 /* Sum underflow */
104 {.time = {.tv_sec = LONG_MIN / TIMESPEC_HZ, .tv_nsec = LONG_MIN%TIMESPEC_HZ - 1},
105 .time_ns = LONG_MIN,
109 /* Test cases for timespec_norm */
110 struct timespec_norm_test_case norm_cases[] = {
111 /* Positive cases */
112 {.time = {.tv_sec = 0, .tv_nsec = 0},
113 .norm = {.tv_sec = 0, .tv_nsec = 0}
115 {.time = {.tv_sec = 1, .tv_nsec = 0},
116 .norm = {.tv_sec = 1, .tv_nsec = 0}
118 {.time = {.tv_sec = 0, .tv_nsec = 1},
119 .norm = {.tv_sec = 0, .tv_nsec = 1}
121 {.time = {.tv_sec = 0, .tv_nsec = TIMESPEC_HZ},
122 .norm = {.tv_sec = 1, .tv_nsec = 0}
124 {.time = {.tv_sec = 0, .tv_nsec = TIMESPEC_HZ + 1},
125 .norm = {.tv_sec = 1, .tv_nsec = 1}
127 {.time = {.tv_sec = 1, .tv_nsec = TIMESPEC_HZ},
128 .norm = {.tv_sec = 2, .tv_nsec = 0}
130 {.time = {.tv_sec = 1, .tv_nsec = TIMESPEC_HZ + 1},
131 .norm = {.tv_sec = 2, .tv_nsec = 1}
133 /* Negative cases */
134 {.time = {.tv_sec = 0, .tv_nsec = -TIMESPEC_HZ},
135 .norm = {.tv_sec = -1, .tv_nsec = 0}
137 {.time = {.tv_sec = 0, .tv_nsec = -TIMESPEC_HZ - 1},
138 .norm = {.tv_sec = -1, .tv_nsec = -1}
140 {.time = {.tv_sec = -1, .tv_nsec = -TIMESPEC_HZ},
141 .norm = {.tv_sec = -2, .tv_nsec = 0}
143 {.time = {.tv_sec = -1, .tv_nsec = -TIMESPEC_HZ - 1},
144 .norm = {.tv_sec = -2, .tv_nsec = -1}
146 /* Overflow bondary by 2 */
147 {.time = {.tv_sec = LONG_MAX - 2, .tv_nsec = TIMESPEC_HZ + 1},
148 .norm = {.tv_sec = LONG_MAX - 1, 1},
150 /* Overflow bondary by 1 */
151 {.time = {.tv_sec = LONG_MAX - 1, .tv_nsec = TIMESPEC_HZ + 1},
152 .norm = {.tv_sec = LONG_MAX, .tv_nsec = 1},
154 /* Underflow bondary by 2 */
155 {.time = {.tv_sec = LONG_MIN + 2, .tv_nsec = -TIMESPEC_HZ - 1},
156 .norm = {.tv_sec = LONG_MIN + 1, -1},
158 /* Underflow bondary by 1 */
159 {.time = {.tv_sec = LONG_MIN + 1, .tv_nsec = -TIMESPEC_HZ - 1},
160 .norm = {.tv_sec = LONG_MIN, .tv_nsec = -1},
162 /* SUM overflow */
163 {.time = {.tv_sec = LONG_MAX, .tv_nsec = TIMESPEC_HZ},
164 .norm = {.tv_sec = LONG_MAX, .tv_nsec = TIMESPEC_HZ - 1},
166 /* SUM underflow */
167 {.time = {.tv_sec = LONG_MIN, .tv_nsec = -TIMESPEC_HZ},
168 .norm = {.tv_sec = LONG_MIN, .tv_nsec = -1 * (TIMESPEC_HZ - 1)},
172 /* Test cases for timespec_check_in_range */
173 struct timespec_test_case check_cases[] = {
174 /* 0 - In range */
175 {.expected = {.tv_sec = 1, .tv_nsec = 0},
176 .observed = {.tv_sec = 1, .tv_nsec = 0},
177 .upper_bound = 1, .lower_bound = 1, .result = 1,
179 /* 1 - Out of range */
180 {.expected = {.tv_sec = 1, .tv_nsec = 0},
181 .observed = {.tv_sec = 2, .tv_nsec = 0},
182 .upper_bound = 1, .lower_bound = 1, .result = 0,
184 /* 2 - Upper Bound */
185 {.expected = {.tv_sec = 1, .tv_nsec = 0},
186 .observed = {.tv_sec = 2, .tv_nsec = 0},
187 .upper_bound = 2, .lower_bound = 1, .result = 1,
189 /* 3 - Lower Bound */
190 {.expected = {.tv_sec = 1, .tv_nsec = 0},
191 .observed = {.tv_sec = 0, .tv_nsec = 0},
192 .upper_bound = 1, .lower_bound = 0, .result = 1,
194 /* 4 - Out of range by nanosecs */
195 {.expected = {.tv_sec = 1, .tv_nsec = 0},
196 .observed = {.tv_sec = 1, .tv_nsec = 500},
197 .upper_bound = 1, .lower_bound = 1, .result = 0,
199 /* 5 - In range by nanosecs */
200 {.expected = {.tv_sec = 1, .tv_nsec = 0},
201 .observed = {.tv_sec = 1, .tv_nsec = 50000},
202 .upper_bound = 1.3, .lower_bound = 1, .result = 1,
204 /* 6 - Big nanosecs */
205 {.expected = {.tv_sec = 1, .tv_nsec = 0},
206 .observed = {.tv_sec = 0, .tv_nsec = 4000000},
207 .upper_bound = 1, .lower_bound = .001, .result = 1,
209 /* 7 - In range Negative values */
210 {.expected = {.tv_sec = -1, .tv_nsec = 0},
211 .observed = {.tv_sec = -1, .tv_nsec = 0},
212 .upper_bound = 1, .lower_bound = 1, .result = 1,
214 /* 8 - Out of range Negative values */
215 {.expected = {.tv_sec = -1, .tv_nsec = 0},
216 .observed = {.tv_sec = -1, .tv_nsec = 0},
217 .upper_bound = -1, .lower_bound = -1, .result = 0,
219 /* 9 - Negative values with negative nanosecs */
220 {.expected = {.tv_sec = -1, .tv_nsec = 0},
221 .observed = {.tv_sec = -1, .tv_nsec = -2000},
222 .upper_bound = 1, .lower_bound = 1, .result = 0,
224 /* 10 - Strict bounds */
225 {.expected = {.tv_sec = -1, .tv_nsec = 0},
226 .observed = {.tv_sec = -1, .tv_nsec = -20000},
227 .upper_bound = 1.00002, .lower_bound = 1.0000191, .result = 1,
229 /* 11 - Strict bounds with loose upper bound */
230 {.expected = {.tv_sec = 1, .tv_nsec = 20000},
231 .observed = {.tv_sec = 1, .tv_nsec = 30000},
232 .upper_bound = 1.0000100000, .lower_bound = 1.0000099998, .result = 1,
234 /* 12 - Strict bounds with loose lower bound */
235 {.expected = {.tv_sec = 1, .tv_nsec = 20000},
236 .observed = {.tv_sec = 1, .tv_nsec = 30000},
237 .upper_bound = 1.0000099999, .lower_bound = 1.00000999979, .result = 1,
239 /* 13 - Strict bounds highest precision */
240 {.expected = {.tv_sec = 1, .tv_nsec = 20000},
241 .observed = {.tv_sec = 1, .tv_nsec = 30000},
242 .upper_bound = 1.00000999980001, .lower_bound = 1.00000999979999, .result = 1,
244 /* Maximum/Minimum long values */
245 /* 14 */
246 {.expected = {.tv_sec = LONG_MAX, .tv_nsec = TIMESPEC_HZ - 1},
247 .observed = {.tv_sec = LONG_MAX, .tv_nsec = TIMESPEC_HZ - 2},
248 .upper_bound = 1, .lower_bound = .9, .result = 1,
250 /* 15 - support_timespec_ns overflow */
251 {.expected = {.tv_sec = LONG_MAX, .tv_nsec = TIMESPEC_HZ},
252 .observed = {.tv_sec = LONG_MAX, .tv_nsec = TIMESPEC_HZ},
253 .upper_bound = 1, .lower_bound = 1, .result = 1,
255 /* 16 - support_timespec_ns overflow + underflow */
256 {.expected = {.tv_sec = LONG_MAX, .tv_nsec = TIMESPEC_HZ},
257 .observed = {.tv_sec = LONG_MIN, .tv_nsec = -TIMESPEC_HZ},
258 .upper_bound = 1, .lower_bound = 1, .result = 0,
260 /* 17 - support_timespec_ns underflow */
261 {.expected = {.tv_sec = LONG_MIN, .tv_nsec = -TIMESPEC_HZ},
262 .observed = {.tv_sec = LONG_MIN, .tv_nsec = -TIMESPEC_HZ},
263 .upper_bound = 1, .lower_bound = 1, .result = 1,
265 /* 18 - support_timespec_ns underflow + overflow */
266 {.expected = {.tv_sec = LONG_MIN, .tv_nsec = -TIMESPEC_HZ},
267 .observed = {.tv_sec = LONG_MAX, .tv_nsec = TIMESPEC_HZ},
268 .upper_bound = 1, .lower_bound = 1, .result = 0,
270 /* 19 - Biggest division */
271 {.expected = {.tv_sec = LONG_MAX / TIMESPEC_HZ , .tv_nsec = TIMESPEC_HZ - 1},
272 .observed = {.tv_sec = 0, .tv_nsec = 1},
273 .upper_bound = 1, .lower_bound = 1.0842021724855044e-19, .result = 1,
275 /* 20 - Lowest division */
276 {.expected = {.tv_sec = 0, .tv_nsec = 1},
277 .observed = {.tv_sec = LONG_MAX / TIMESPEC_HZ , .tv_nsec = TIMESPEC_HZ - 1},
278 .upper_bound = LONG_MAX, .lower_bound = 1, .result = 1,
282 static int
283 do_test (void)
285 int i = 0;
286 int ntests = sizeof (ns_cases) / sizeof (ns_cases[0]);
288 printf("Testing support_timespec_ns\n");
289 for (i = 0; i < ntests; i++)
291 TEST_COMPARE (support_timespec_ns (ns_cases[i].time),
292 ns_cases[i].time_ns);
295 ntests = sizeof (norm_cases) / sizeof (norm_cases[0]);
296 struct timespec result;
297 printf("Testing support_timespec_normalize\n");
298 for (i = 0; i < ntests; i++)
300 result = support_timespec_normalize (norm_cases[i].time);
301 TEST_COMPARE (norm_cases[i].norm.tv_sec, result.tv_sec);
302 TEST_COMPARE (norm_cases[i].norm.tv_nsec, result.tv_nsec);
305 ntests = sizeof (check_cases) / sizeof (check_cases[0]);
306 printf("Testing support_timespec_check_in_range\n");
307 for (i = 0; i < ntests; i++)
309 /* Its hard to find which test failed with just the TEST_COMPARE report.
310 So here we print every running testcase as well. */
311 printf("Test case %d\n", i);
312 TEST_COMPARE (support_timespec_check_in_range
313 (check_cases[i].expected, check_cases[i].observed,
314 check_cases[i].lower_bound,
315 check_cases[i].upper_bound), check_cases[i].result);
317 return 0;
320 #include <support/test-driver.c>