diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-07-01 12:10:52 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-07-09 11:03:35 +0200 |
commit | ae7c644c22f1bf8b4990ced7f3533d1b33547442 (patch) | |
tree | d8034d929a28743feecf947a9ed0e86a0d81a791 /src/test/test-time-util.c | |
parent | test-ether-addr-util: add a simple test that HW_ADDR_TO_STR works with nesting (diff) | |
download | systemd-ae7c644c22f1bf8b4990ced7f3533d1b33547442.tar.xz systemd-ae7c644c22f1bf8b4990ced7f3533d1b33547442.zip |
basic/time-util: add FORMAT_TIMESTAMP
This uses the same idea of an anonyous buffer as ETHER_ADDR_TO_STR().
Diffstat (limited to 'src/test/test-time-util.c')
-rw-r--r-- | src/test/test-time-util.c | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index 6f4675aaf4..bc23affc81 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -335,8 +335,7 @@ static void test_format_timestamp(void) { char buf[MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESPAN_MAX)]; usec_t x, y; - random_bytes(&x, sizeof(x)); - x = x % (2147483600 * USEC_PER_SEC) + 1; + x = random_u64_range(2147483600 * USEC_PER_SEC) + 1; assert_se(format_timestamp(buf, sizeof(buf), x)); log_debug("%s", buf); @@ -370,6 +369,26 @@ static void test_format_timestamp(void) { } } +static void test_FORMAT_TIMESTAMP(void) { + log_info("/* %s */", __func__); + + for (unsigned i = 0; i < 100; i++) { + _cleanup_free_ char *buf; + usec_t x, y; + + x = random_u64_range(2147483600 * USEC_PER_SEC) + 1; + + /* strbuf() is to test the macro in an argument to a function call. */ + assert_se(buf = strdup(FORMAT_TIMESTAMP(x))); + log_debug("%s", buf); + assert_se(parse_timestamp(buf, &y) >= 0); + assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC); + + char *t = FORMAT_TIMESTAMP(x); + assert_se(streq(t, buf)); + } +} + static void test_format_timestamp_relative(void) { log_info("/* %s */", __func__); @@ -624,6 +643,7 @@ int main(int argc, char *argv[]) { test_usec_sub_signed(); test_usec_sub_unsigned(); test_format_timestamp(); + test_FORMAT_TIMESTAMP(); test_format_timestamp_relative(); test_format_timestamp_utc(); test_deserialize_dual_timestamp(); |