]>
Commit | Line | Data |
---|---|---|
51983996 | 1 | /* Support code for timespec checks. |
2b778ceb | 2 | Copyright (C) 2019-2021 Free Software Foundation, Inc. |
51983996 MC |
3 | This file is part of the GNU C Library. |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
51983996 MC |
18 | |
19 | #include <support/timespec.h> | |
20 | #include <stdio.h> | |
eb669ff5 | 21 | #include <stdint.h> |
04deeaa9 LM |
22 | #include <assert.h> |
23 | #include <intprops.h> | |
51983996 MC |
24 | |
25 | void | |
e9bc2b3b AZ |
26 | test_timespec_before_impl (const char *file, int line, struct timespec left, |
27 | struct timespec right) | |
51983996 MC |
28 | { |
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); | |
eb669ff5 | 34 | printf ("%s:%d: %jd.%09jds not before %jd.%09jds " |
b62bb3bc | 35 | "(difference %jd.%09jds)\n", |
51983996 | 36 | file, line, |
eb669ff5 AZ |
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); | |
51983996 MC |
40 | } |
41 | } | |
42 | ||
43 | void | |
44 | test_timespec_equal_or_after_impl (const char *file, int line, | |
e9bc2b3b AZ |
45 | struct timespec left, |
46 | struct timespec right) | |
51983996 MC |
47 | { |
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); | |
eb669ff5 | 53 | printf ("%s:%d: %jd.%09jds not after %jd.%09jds " |
b62bb3bc | 54 | "(difference %jd.%09jds)\n", |
51983996 | 55 | file, line, |
eb669ff5 AZ |
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); | |
51983996 MC |
59 | } |
60 | } | |
04deeaa9 | 61 | |
f896fc0f L |
62 | /* Convert TIME to nanoseconds stored in a time_t. |
63 | Returns time_t maximum or minimum if the conversion overflows | |
04deeaa9 | 64 | or underflows, respectively. */ |
f896fc0f | 65 | time_t |
04deeaa9 LM |
66 | support_timespec_ns (struct timespec time) |
67 | { | |
f896fc0f | 68 | time_t time_ns; |
04deeaa9 | 69 | if (INT_MULTIPLY_WRAPV(time.tv_sec, TIMESPEC_HZ, &time_ns)) |
f896fc0f | 70 | return time.tv_sec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t); |
04deeaa9 | 71 | if (INT_ADD_WRAPV(time_ns, time.tv_nsec, &time_ns)) |
f896fc0f | 72 | return time.tv_nsec < 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t); |
04deeaa9 LM |
73 | return time_ns; |
74 | } | |
75 | ||
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) | |
82 | { | |
83 | struct timespec norm; | |
84 | if (INT_ADD_WRAPV (time.tv_sec, (time.tv_nsec / TIMESPEC_HZ), &norm.tv_sec)) | |
85 | { | |
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; | |
89 | } | |
90 | norm.tv_nsec = time.tv_nsec % TIMESPEC_HZ; | |
91 | return norm; | |
92 | } | |
93 | ||
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 | |
97 | ||
98 | support_timespec_check_in_range(expected, observed, 0.5, 1.2); | |
99 | ||
100 | will check if | |
101 | ||
102 | 0.5 of expected <= observed <= 1.2 of expected | |
103 | ||
104 | In other words it will check if observed time is within 50% to | |
105 | 120% of the expected time. */ | |
106 | int | |
107 | support_timespec_check_in_range (struct timespec expected, struct timespec observed, | |
108 | double lower_bound, double upper_bound) | |
109 | { | |
110 | assert (upper_bound >= lower_bound); | |
f896fc0f | 111 | time_t expected_norm, observed_norm; |
04deeaa9 LM |
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); | |
118 | } |