1 /* Support code for timespec checks.
2 Copyright (C) 2019-2021 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>
26 test_timespec_before_impl (const char *file
, int line
,
27 const struct timespec left
,
28 const struct timespec right
)
30 if (left
.tv_sec
> right
.tv_sec
31 || (left
.tv_sec
== right
.tv_sec
32 && left
.tv_nsec
> right
.tv_nsec
)) {
33 support_record_failure ();
34 const struct timespec diff
= timespec_sub (left
, right
);
35 printf ("%s:%d: %jd.%09jds not before %jd.%09jds "
36 "(difference %jd.%09jds)\n",
38 (intmax_t) left
.tv_sec
, (intmax_t) left
.tv_nsec
,
39 (intmax_t) right
.tv_sec
, (intmax_t) right
.tv_nsec
,
40 (intmax_t) diff
.tv_sec
, (intmax_t) diff
.tv_nsec
);
45 test_timespec_equal_or_after_impl (const char *file
, int line
,
46 const struct timespec left
,
47 const struct timespec right
)
49 if (left
.tv_sec
< right
.tv_sec
50 || (left
.tv_sec
== right
.tv_sec
51 && left
.tv_nsec
< right
.tv_nsec
)) {
52 support_record_failure ();
53 const struct timespec diff
= timespec_sub (right
, left
);
54 printf ("%s:%d: %jd.%09jds not after %jd.%09jds "
55 "(difference %jd.%09jds)\n",
57 (intmax_t) left
.tv_sec
, (intmax_t) left
.tv_nsec
,
58 (intmax_t) right
.tv_sec
, (intmax_t) right
.tv_nsec
,
59 (intmax_t) diff
.tv_sec
, (intmax_t) diff
.tv_nsec
);
63 /* Convert TIME to nanoseconds stored in a time_t.
64 Returns time_t maximum or minimum if the conversion overflows
65 or underflows, respectively. */
67 support_timespec_ns (struct timespec time
)
70 if (INT_MULTIPLY_WRAPV(time
.tv_sec
, TIMESPEC_HZ
, &time_ns
))
71 return time
.tv_sec
< 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
72 if (INT_ADD_WRAPV(time_ns
, time
.tv_nsec
, &time_ns
))
73 return time
.tv_nsec
< 0 ? TYPE_MINIMUM(time_t) : TYPE_MAXIMUM(time_t);
77 /* Returns time normalized timespec with .tv_nsec < TIMESPEC_HZ
78 and the whole seconds added to .tv_sec. If an overflow or
79 underflow occurs the values are clamped to its maximum or
80 minimum respectively. */
82 support_timespec_normalize (struct timespec time
)
85 if (INT_ADD_WRAPV (time
.tv_sec
, (time
.tv_nsec
/ TIMESPEC_HZ
), &norm
.tv_sec
))
87 norm
.tv_sec
= (time
.tv_nsec
< 0) ? TYPE_MINIMUM (time_t): TYPE_MAXIMUM (time_t);
88 norm
.tv_nsec
= (time
.tv_nsec
< 0) ? -1 * (TIMESPEC_HZ
- 1) : TIMESPEC_HZ
- 1;
91 norm
.tv_nsec
= time
.tv_nsec
% TIMESPEC_HZ
;
95 /* Returns TRUE if the observed time is within the given percentage
96 bounds of the expected time, and FALSE otherwise.
99 support_timespec_check_in_range(expected, observed, 0.5, 1.2);
103 0.5 of expected <= observed <= 1.2 of expected
105 In other words it will check if observed time is within 50% to
106 120% of the expected time. */
108 support_timespec_check_in_range (struct timespec expected
, struct timespec observed
,
109 double lower_bound
, double upper_bound
)
111 assert (upper_bound
>= lower_bound
);
112 time_t expected_norm
, observed_norm
;
113 expected_norm
= support_timespec_ns (expected
);
114 /* Don't divide by zero */
115 assert(expected_norm
!= 0);
116 observed_norm
= support_timespec_ns (observed
);
117 double ratio
= (double)observed_norm
/ expected_norm
;
118 return (lower_bound
<= ratio
&& ratio
<= upper_bound
);