diff options
Diffstat (limited to 'src/test/test-time.c')
-rw-r--r-- | src/test/test-time.c | 142 |
1 files changed, 131 insertions, 11 deletions
diff --git a/src/test/test-time.c b/src/test/test-time.c index 911282bf0c..b7a06c7b19 100644 --- a/src/test/test-time.c +++ b/src/test/test-time.c @@ -61,6 +61,19 @@ static void test_parse_sec(void) { assert_se(parse_sec(".3 infinity", &u) < 0); } +static void test_parse_sec_fix_0(void) { + usec_t u; + + assert_se(parse_sec_fix_0("5s", &u) >= 0); + assert_se(u == 5 * USEC_PER_SEC); + assert_se(parse_sec_fix_0("0s", &u) >= 0); + assert_se(u == 0 * USEC_PER_SEC); + assert_se(parse_sec_fix_0("0", &u) >= 0); + assert_se(u == USEC_INFINITY); + assert_se(parse_sec_fix_0(" 0", &u) >= 0); + assert_se(u == USEC_INFINITY); +} + static void test_parse_time(void) { usec_t u; @@ -195,16 +208,37 @@ static void test_usec_add(void) { assert_se(usec_add(USEC_INFINITY, 2) == USEC_INFINITY); } -static void test_usec_sub(void) { - assert_se(usec_sub(0, 0) == 0); - assert_se(usec_sub(4, 1) == 3); - assert_se(usec_sub(4, 4) == 0); - assert_se(usec_sub(4, 5) == 0); - assert_se(usec_sub(USEC_INFINITY-3, -3) == USEC_INFINITY); - assert_se(usec_sub(USEC_INFINITY-3, -3) == USEC_INFINITY); - assert_se(usec_sub(USEC_INFINITY-3, -4) == USEC_INFINITY); - assert_se(usec_sub(USEC_INFINITY-3, -5) == USEC_INFINITY); - assert_se(usec_sub(USEC_INFINITY, 5) == USEC_INFINITY); +static void test_usec_sub_unsigned(void) { + assert_se(usec_sub_unsigned(0, 0) == 0); + assert_se(usec_sub_unsigned(0, 2) == 0); + assert_se(usec_sub_unsigned(0, USEC_INFINITY) == 0); + assert_se(usec_sub_unsigned(1, 0) == 1); + assert_se(usec_sub_unsigned(1, 1) == 0); + assert_se(usec_sub_unsigned(1, 2) == 0); + assert_se(usec_sub_unsigned(1, 3) == 0); + assert_se(usec_sub_unsigned(1, USEC_INFINITY) == 0); + assert_se(usec_sub_unsigned(USEC_INFINITY-1, 0) == USEC_INFINITY-1); + assert_se(usec_sub_unsigned(USEC_INFINITY-1, 1) == USEC_INFINITY-2); + assert_se(usec_sub_unsigned(USEC_INFINITY-1, 2) == USEC_INFINITY-3); + assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-2) == 1); + assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY-1) == 0); + assert_se(usec_sub_unsigned(USEC_INFINITY-1, USEC_INFINITY) == 0); + assert_se(usec_sub_unsigned(USEC_INFINITY, 0) == USEC_INFINITY); + assert_se(usec_sub_unsigned(USEC_INFINITY, 1) == USEC_INFINITY); + assert_se(usec_sub_unsigned(USEC_INFINITY, 2) == USEC_INFINITY); + assert_se(usec_sub_unsigned(USEC_INFINITY, USEC_INFINITY) == USEC_INFINITY); +} + +static void test_usec_sub_signed(void) { + assert_se(usec_sub_signed(0, 0) == 0); + assert_se(usec_sub_signed(4, 1) == 3); + assert_se(usec_sub_signed(4, 4) == 0); + assert_se(usec_sub_signed(4, 5) == 0); + assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY); + assert_se(usec_sub_signed(USEC_INFINITY-3, -3) == USEC_INFINITY); + assert_se(usec_sub_signed(USEC_INFINITY-3, -4) == USEC_INFINITY); + assert_se(usec_sub_signed(USEC_INFINITY-3, -5) == USEC_INFINITY); + assert_se(usec_sub_signed(USEC_INFINITY, 5) == USEC_INFINITY); } static void test_format_timestamp(void) { @@ -273,10 +307,93 @@ static void test_format_timestamp_utc(void) { test_format_timestamp_utc_one(USEC_INFINITY, NULL); } +static void test_dual_timestamp_deserialize(void) { + int r; + dual_timestamp t; + + r = dual_timestamp_deserialize("1234 5678", &t); + assert_se(r == 0); + assert_se(t.realtime == 1234); + assert_se(t.monotonic == 5678); + + r = dual_timestamp_deserialize("1234x 5678", &t); + assert_se(r == -EINVAL); + + r = dual_timestamp_deserialize("1234 5678y", &t); + assert_se(r == -EINVAL); + + r = dual_timestamp_deserialize("-1234 5678", &t); + assert_se(r == -EINVAL); + + r = dual_timestamp_deserialize("1234 -5678", &t); + assert_se(r == -EINVAL); + + /* Check that output wasn't modified. */ + assert_se(t.realtime == 1234); + assert_se(t.monotonic == 5678); + + r = dual_timestamp_deserialize("+123 567", &t); + assert_se(r == 0); + assert_se(t.realtime == 123); + assert_se(t.monotonic == 567); + + /* Check that we get "infinity" on overflow. */ + r = dual_timestamp_deserialize("18446744073709551617 0", &t); + assert_se(r == 0); + assert_se(t.realtime == USEC_INFINITY); + assert_se(t.monotonic == 0); +} + +static void assert_similar(usec_t a, usec_t b) { + usec_t d; + + if (a > b) + d = a - b; + else + d = b - a; + + assert(d < 10*USEC_PER_SEC); +} + +static void test_usec_shift_clock(void) { + usec_t rt, mn, bt; + + rt = now(CLOCK_REALTIME); + mn = now(CLOCK_MONOTONIC); + bt = now(clock_boottime_or_monotonic()); + + assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY); + + assert_similar(usec_shift_clock(rt + USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_MONOTONIC), mn + USEC_PER_HOUR); + assert_similar(usec_shift_clock(rt + 2*USEC_PER_HOUR, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt + 2*USEC_PER_HOUR); + assert_se(usec_shift_clock(rt + 3*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_REALTIME_ALARM) == rt + 3*USEC_PER_HOUR); + + assert_similar(usec_shift_clock(mn + 4*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_REALTIME_ALARM), rt + 4*USEC_PER_HOUR); + assert_similar(usec_shift_clock(mn + 5*USEC_PER_HOUR, CLOCK_MONOTONIC, clock_boottime_or_monotonic()), bt + 5*USEC_PER_HOUR); + assert_se(usec_shift_clock(mn + 6*USEC_PER_HOUR, CLOCK_MONOTONIC, CLOCK_MONOTONIC) == mn + 6*USEC_PER_HOUR); + + assert_similar(usec_shift_clock(bt + 7*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_MONOTONIC), mn + 7*USEC_PER_HOUR); + assert_similar(usec_shift_clock(bt + 8*USEC_PER_HOUR, clock_boottime_or_monotonic(), CLOCK_REALTIME_ALARM), rt + 8*USEC_PER_HOUR); + assert_se(usec_shift_clock(bt + 9*USEC_PER_HOUR, clock_boottime_or_monotonic(), clock_boottime_or_monotonic()) == bt + 9*USEC_PER_HOUR); + + if (mn > USEC_PER_MINUTE) { + assert_similar(usec_shift_clock(rt - 30 * USEC_PER_SEC, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC), mn - 30 * USEC_PER_SEC); + assert_similar(usec_shift_clock(rt - 50 * USEC_PER_SEC, CLOCK_REALTIME, clock_boottime_or_monotonic()), bt - 50 * USEC_PER_SEC); + } +} + int main(int argc, char *argv[]) { uintmax_t x; + log_info("realtime=" USEC_FMT "\n" + "monotonic=" USEC_FMT "\n" + "boottime=" USEC_FMT "\n", + now(CLOCK_REALTIME), + now(CLOCK_MONOTONIC), + now(clock_boottime_or_monotonic())); + test_parse_sec(); + test_parse_sec_fix_0(); test_parse_time(); test_parse_nsec(); test_format_timespan(1); @@ -285,9 +402,12 @@ int main(int argc, char *argv[]) { test_timezone_is_valid(); test_get_timezones(); test_usec_add(); - test_usec_sub(); + test_usec_sub_signed(); + test_usec_sub_unsigned(); test_format_timestamp(); test_format_timestamp_utc(); + test_dual_timestamp_deserialize(); + test_usec_shift_clock(); /* Ensure time_t is signed */ assert_cc((time_t) -1 < (time_t) 1); |