| 1 | /* Support code for timespec checks. |
| 2 | Copyright (C) 2019-2020 Free Software Foundation, Inc. |
| 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 |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <support/timespec.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <assert.h> |
| 23 | #include <intprops.h> |
| 24 | |
| 25 | void |
| 26 | test_timespec_before_impl (const char *file, int line, |
| 27 | const struct timespec left, |
| 28 | const struct timespec right) |
| 29 | { |
| 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" , |
| 37 | file, line, |
| 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); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | test_timespec_equal_or_after_impl (const char *file, int line, |
| 46 | const struct timespec left, |
| 47 | const struct timespec right) |
| 48 | { |
| 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" , |
| 56 | file, line, |
| 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); |
| 60 | } |
| 61 | } |
| 62 | |
| 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. */ |
| 66 | time_t |
| 67 | support_timespec_ns (struct timespec time) |
| 68 | { |
| 69 | time_t time_ns; |
| 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); |
| 74 | return time_ns; |
| 75 | } |
| 76 | |
| 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. */ |
| 81 | struct timespec |
| 82 | support_timespec_normalize (struct timespec time) |
| 83 | { |
| 84 | struct timespec norm; |
| 85 | if (INT_ADD_WRAPV (time.tv_sec, (time.tv_nsec / TIMESPEC_HZ), &norm.tv_sec)) |
| 86 | { |
| 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; |
| 89 | return norm; |
| 90 | } |
| 91 | norm.tv_nsec = time.tv_nsec % TIMESPEC_HZ; |
| 92 | return norm; |
| 93 | } |
| 94 | |
| 95 | /* Returns TRUE if the observed time is within the given percentage |
| 96 | bounds of the expected time, and FALSE otherwise. |
| 97 | For example the call |
| 98 | |
| 99 | support_timespec_check_in_range(expected, observed, 0.5, 1.2); |
| 100 | |
| 101 | will check if |
| 102 | |
| 103 | 0.5 of expected <= observed <= 1.2 of expected |
| 104 | |
| 105 | In other words it will check if observed time is within 50% to |
| 106 | 120% of the expected time. */ |
| 107 | int |
| 108 | support_timespec_check_in_range (struct timespec expected, struct timespec observed, |
| 109 | double lower_bound, double upper_bound) |
| 110 | { |
| 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); |
| 119 | } |
| 120 | |