25#include <freeradius-devel/util/time.h>
27#define ROUNDS (100000)
37 for (i = 0; i <
ROUNDS; i++) {
45 printf(
"fr_time rate %" PRIu64
"\n", rate);
67 TEST_CASE(
"signed INT64_MIN, seconds resolution");
72 TEST_CHECK(strcmp(buf,
"-9223372036.854775808") == 0);
75 TEST_CASE(
"unsigned INT64_MIN clamps to zero");
96 TEST_MSG(
"\"%s\": got %" PRIi64
", expected %" PRIi64,
113 TEST_CASE(
"plain integers with a resolution hint");
118 TEST_CASE(
"explicit scale suffixes override the hint");
124 TEST_CASE(
"floating point is pre-scaled to nanoseconds");
128 TEST_CASE(
"[hours:]minutes:seconds timestamps");
138 TEST_CASE(
"negative [hours:]minutes:seconds timestamps");
149 TEST_CASE(
"hours component at the int16_t guard boundary");
173 TEST_CASE(
"extreme first-component magnitudes are rejected");
192 char const *expected;
206 tests[i].res, tests[i].is_unsigned);
208 TEST_CHECK(strcmp(buf, tests[i].expected) == 0);
209 TEST_MSG(
"delta %" PRIi64
": got \"%s\", expected \"%s\"",
210 tests[i].delta, buf, tests[i].expected);
217 TEST_CASE(
"each resolution scales to nanoseconds");
223 TEST_CASE(
"hints the function does not handle return 0");
227 TEST_CASE(
"overflow and underflow clamp");
239 memset(&tm, 0,
sizeof(tm));
240 tm.tm_year = 1970 - 1900;
245 TEST_CASE(
"2000-01-01T00:00:00Z == 946684800");
246 memset(&tm, 0,
sizeof(tm));
247 tm.tm_year = 2000 - 1900;
252 TEST_CASE(
"the Y2038 boundary, 2038-01-19T03:14:07Z == INT32_MAX");
253 memset(&tm, 0,
sizeof(tm));
254 tm.tm_year = 2038 - 1900;
262 TEST_CASE(
"a positive gmtoff is removed to give UTC");
263 memset(&tm, 0,
sizeof(tm));
264 tm.tm_year = 2000 - 1900;
278 TEST_CASE(
"a bare unix timestamp scales by the hint");
282 TEST_CASE(
"RFC 3339 in UTC round-trips");
287 TEST_CHECK(strcmp(buf,
"2000-01-01T00:00:00Z") == 0);
290 TEST_CASE(
"RFC 3339 numeric timezone offsets are removed to give UTC");
296 TEST_CASE(
"sub-second parsing and millisecond formatting");
301 TEST_CHECK(strcmp(buf,
"2000-01-01T00:00:00.500Z") == 0);
304 TEST_CASE(
"malformed input is rejected");
313 static int64_t
const delays[8] = {
324 memset(&elapsed, 0,
sizeof(elapsed));
326 TEST_CASE(
"one delay lands in each bin");
327 for (
int i = 0; i < 8; i++) {
330 for (
int i = 0; i < 8; i++) {
335 TEST_CASE(
"start >= end is treated as a zero delay");
#define FR_SBUFF_OUT(_start, _len_or_end)
void fr_time_elapsed_update(fr_time_elapsed_t *elapsed, fr_time_t start, fr_time_t end)
fr_unix_time_t fr_unix_time_from_tm(struct tm *tm)
fr_slen_t fr_time_delta_from_str(fr_time_delta_t *out, char const *in, size_t inlen, fr_time_res_t hint)
Create fr_time_delta_t from a string.
int fr_unix_time_from_str(fr_unix_time_t *date, char const *date_str, fr_time_res_t hint)
Convert string in various formats to a fr_unix_time_t.
int64_t fr_time_scale(int64_t t, fr_time_res_t hint)
Scale an input time to NSEC, clamping it at max / min.
fr_slen_t fr_time_delta_to_str(fr_sbuff_t *out, fr_time_delta_t delta, fr_time_res_t res, bool is_unsigned)
Print fr_time_delta_t to a string with an appropriate suffix.
fr_slen_t fr_unix_time_to_str(fr_sbuff_t *out, fr_unix_time_t time, fr_time_res_t res, bool utc)
Convert unix time to string.
static int64_t fr_time_delta_unwrap(fr_time_delta_t time)
#define fr_time_delta_wrap(_time)
#define fr_time_wrap(_time)
fr_time_res_t
The base resolution for print parse operations.
static uint64_t fr_unix_time_unwrap(fr_unix_time_t time)
uint64_t array[8]
100ns to 100s
#define fr_time_sub(_a, _b)
Subtract one time from another.
A time delta, a difference in time measured in nanoseconds.
static void unix_time_str(void)
Round-trip fr_unix_time_from_str() and fr_unix_time_to_str() in UTC.
static void time_delta_from_str(void)
Parse time deltas from strings: integers, floats, scale suffixes, timestamps and errors.
static void time_delta_to_str_int64_min(void)
Format the most negative delta possible.
static void time_delta_to_str(void)
Format time deltas to strings across sign, fraction and the unsigned flag.
static void unix_time_from_tm(void)
Test fr_unix_time_from_tm against well-known UTC epochs.
static void time_elapsed_update(void)
Test fr_time_elapsed_update sorts delays into the correct histogram bins.
static void time_benchmark(void)
static void time_scale(void)
Test fr_time_scale: per-resolution multipliers, clamping and invalid hints.
static void check_delta_from_str(char const *in, fr_time_res_t hint, int64_t expected)
static void check_delta_from_str_fail(char const *in, fr_time_res_t hint)