From 5c20665ad9153021f962a458cad11cb64a638f98 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Tue, 4 Jul 2023 13:57:44 +0200 Subject: [PATCH] lib/timeutils: (tests) add test for formatting MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Weißschuh --- lib/timeutils.c | 45 ++++++++++++++++++++++++++++++++++++++++-- tests/ts/lib/timeutils | 6 ++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/timeutils.c b/lib/timeutils.c index a5f0323416..c1ffc547c0 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -687,6 +687,46 @@ static int run_unittest_timestamp(void) return rc; } +static int run_unittest_format(void) +{ + int rc = EXIT_SUCCESS; + const struct timespec ts = { + .tv_sec = 1674180427, + .tv_nsec = 12345, + }; + char buf[FORMAT_TIMESTAMP_MAX]; + static const struct testcase { + int flags; + const char * const expected; + } testcases[] = { + { ISO_DATE, "2023-01-20" }, + { ISO_TIME, "02:07:07" }, + { ISO_TIMEZONE, "+00:00" }, + { ISO_TIMESTAMP_T, "2023-01-20T02:07:07+00:00" }, + { ISO_TIMESTAMP_COMMA_G, "2023-01-20 02:07:07,000012+00:00" }, + { ISO_TIME | ISO_DOTNSEC, "02:07:07.000012345" }, + }; + + setenv("TZ", "GMT", 1); + tzset(); + + for (size_t i = 0; i < ARRAY_SIZE(testcases); i++) { + struct testcase t = testcases[i]; + int r = strtimespec_iso(&ts, t.flags, buf, sizeof(buf)); + if (r) { + fprintf(stderr, "Could not format '%s'\n", t.expected); + rc = EXIT_FAILURE; + } + + if (strcmp(buf, t.expected)) { + fprintf(stderr, "#%02zu %-20s != %-20s\n", i, buf, t.expected); + rc = EXIT_FAILURE; + } + } + + return rc; +} + int main(int argc, char *argv[]) { struct timespec ts = { 0 }; @@ -697,9 +737,10 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } - if (strcmp(argv[1], "--unittest-timestamp") == 0) { + if (strcmp(argv[1], "--unittest-timestamp") == 0) return run_unittest_timestamp(); - } + else if (strcmp(argv[1], "--unittest-format") == 0) + return run_unittest_format(); if (strcmp(argv[1], "--timestamp") == 0) { usec_t usec = 0; diff --git a/tests/ts/lib/timeutils b/tests/ts/lib/timeutils index 13ec68be07..74bb17b8e1 100755 --- a/tests/ts/lib/timeutils +++ b/tests/ts/lib/timeutils @@ -16,9 +16,11 @@ ts_init "$*" ts_check_test_command "$TS_HELPER_TIMEUTILS" ts_init_subtest "timestamp" +"$TS_HELPER_TIMEUTILS" --unittest-timestamp 2> "$TS_ERRLOG" || ts_die "test failed" +ts_finalize_subtest -"$TS_HELPER_TIMEUTILS" --unittest-timestamp 2> "$TS_ERRLOG" - +ts_init_subtest "format" +"$TS_HELPER_TIMEUTILS" --unittest-format 2> "$TS_ERRLOG" || ts_die "test failed" ts_finalize_subtest ts_finalize -- 2.47.3