Skip to content

Commit af8cb09

Browse files
yuwatabluca
authored andcommitted
test-time-util: fix truncation of usec to sec
Also - use ASSERT_XYZ() macros, - log tzname[] on failure. (cherry picked from commit 3f1d499964abb6a4c0141d7ea8f852829880adff) (cherry picked from commit 11d7050) (cherry picked from commit 1d4bde5) (cherry picked from commit b07b4ce)
1 parent 33b0102 commit af8cb09

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/test/test-time-util.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -393,27 +393,31 @@ TEST(format_timestamp) {
393393
static void test_format_timestamp_impl(usec_t x) {
394394
bool success, override;
395395
const char *xx, *yy;
396-
usec_t y;
396+
usec_t y, x_sec, y_sec;
397397

398398
xx = FORMAT_TIMESTAMP(x);
399399
assert_se(xx);
400400
assert_se(parse_timestamp(xx, &y) >= 0);
401401
yy = FORMAT_TIMESTAMP(y);
402402
assert_se(yy);
403403

404-
success = (x / USEC_PER_SEC == y / USEC_PER_SEC) && streq(xx, yy);
404+
x_sec = x / USEC_PER_SEC;
405+
y_sec = y / USEC_PER_SEC;
406+
success = (x_sec == y_sec) && streq(xx, yy);
405407
/* Workaround for https://github.com/systemd/systemd/issues/28472
406408
* and https://github.com/systemd/systemd/pull/35471. */
407409
override = !success &&
408410
(STRPTR_IN_SET(tzname[0], "CAT", "EAT", "WET") ||
409411
STRPTR_IN_SET(tzname[1], "CAT", "EAT", "WET")) &&
410-
DIV_ROUND_UP(x > y ? x - y : y - x, USEC_PER_SEC) == 3600; /* 1 hour, ignore fractional second */
412+
(x_sec > y_sec ? x_sec - y_sec : y_sec - x_sec) == 3600; /* 1 hour, ignore fractional second */
411413
log_full(success ? LOG_DEBUG : override ? LOG_WARNING : LOG_ERR,
412414
"@" USEC_FMT " → %s → @" USEC_FMT " → %s%s",
413415
x, xx, y, yy,
414416
override ? ", ignoring." : "");
415417
if (!override) {
416-
assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
418+
if (!success)
419+
log_warning("tzname[0]=\"%s\", tzname[1]=\"%s\"", tzname[0], tzname[1]);
420+
assert_se(x_sec == y_sec);
417421
assert_se(streq(xx, yy));
418422
}
419423
}

0 commit comments

Comments
 (0)