Use Linux 6.6 in build-many-glibcs.py
[glibc.git] / support / timespec.c
blobd2ef4315343e7c6d6f05f3074607a3a2bd101605
1 /* Support code for timespec checks.
2 Copyright (C) 2019-2023 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 <stdio.h>
21 #include <stdint.h>
22 #include <assert.h>
23 #include <intprops.h>
25 void
26 test_timespec_before_impl (const char *file, int line, struct timespec left,
27 struct timespec right)
29 if (left.tv_sec > right.tv_sec
30 || (left.tv_sec == right.tv_sec
31 && left.tv_nsec > right.tv_nsec)) {
32 support_record_failure ();
33 const struct timespec diff = timespec_sub (left, right);
34 printf ("%s:%d: %jd.%09jds not before %jd.%09jds "
35 "(difference %jd.%09jds)\n",
36 file, line,
37 (intmax_t) left.tv_sec, (intmax_t) left.tv_nsec,
38 (intmax_t) right.tv_sec, (intmax_t) right.tv_nsec,
39 (intmax_t) diff.tv_sec, (intmax_t) diff.tv_nsec);
43 void
44 test_timespec_equal_or_after_impl (const char *file, int line,
45 struct timespec left,
46 struct timespec right)
48 if (left.tv_sec < right.tv_sec
49 || (left.tv_sec == right.tv_sec
50 && left.tv_nsec < right.tv_nsec)) {
51 support_record_failure ();
52 const struct timespec diff = timespec_sub (right, left);
53 printf ("%s:%d: %jd.%09jds not after %jd.%09jds "
54 "(difference %jd.%09jds)\n",
55 file, line,
56 (intmax_t) left.tv_sec, (intmax_t) left.tv_nsec,
57 (intmax_t) right.tv_sec, (intmax_t) right.tv_nsec,
58 (intmax_t) diff.tv_sec, (intmax_t) diff.tv_nsec);
62 /* Convert TIME to nanoseconds stored in a time_t.
63 Returns time_t maximum or minimum if the conversion overflows
64 or underflows, respectively. */
65 time_t
66 support_timespec_ns (struct timespec time)
68 time_t time_ns;
69 if (INT_MULTIPLY_WRAPV(time.tv_sec, TIMESPEC_HZ, &time_ns))
70 return time.tv_sec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
71 if (INT_ADD_WRAPV(time_ns, time.tv_nsec, &time_ns))
72 return time.tv_nsec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
73 return time_ns;
76 /* Returns time normalized timespec with .tv_nsec < TIMESPEC_HZ
77 and the whole seconds added to .tv_sec. If an overflow or
78 underflow occurs the values are clamped to its maximum or
79 minimum respectively. */
80 struct timespec
81 support_timespec_normalize (struct timespec time)
83 struct timespec norm;
84 if (INT_ADD_WRAPV (time.tv_sec, (time.tv_nsec / TIMESPEC_HZ), &norm.tv_sec))
86 norm.tv_sec = (time.tv_nsec < 0) ? TYPE_MINIMUM (time_t): TYPE_MAXIMUM (time_t);
87 norm.tv_nsec = (time.tv_nsec < 0) ? -1 * (TIMESPEC_HZ - 1) : TIMESPEC_HZ - 1;
88 return norm;
90 norm.tv_nsec = time.tv_nsec % TIMESPEC_HZ;
91 return norm;
94 /* Returns TRUE if the observed time is within the given percentage
95 bounds of the expected time, and FALSE otherwise.
96 For example the call
98 support_timespec_check_in_range(expected, observed, 0.5, 1.2);
100 will check if
102 0.5 of expected <= observed <= 1.2 of expected
104 In other words it will check if observed time is within 50% to
105 120% of the expected time. */
107 support_timespec_check_in_range (struct timespec expected, struct timespec observed,
108 double lower_bound, double upper_bound)
110 assert (upper_bound >= lower_bound);
111 time_t expected_norm, observed_norm;
112 expected_norm = support_timespec_ns (expected);
113 /* Don't divide by zero */
114 assert(expected_norm != 0);
115 observed_norm = support_timespec_ns (observed);
116 double ratio = (double)observed_norm / expected_norm;
117 return (lower_bound <= ratio && ratio <= upper_bound);