Skip to content

Commit

Permalink
tests/kernel/timer/timer_api: Fix static double-conversion goof
Browse files Browse the repository at this point in the history
The test of the absolute timeout feature was a simple whitebox test
that inspected the generated ticks field of different constructors for
identity.  But it wasn't simple enough, because it was doing a
ticks->ms->ticks conversion (at compile time, sigh) on the input data,
which is obviously lossy on platforms where ticks are shorter than
milliseconds by non-integral factors.

Fix to do the conversion in just one direction.

Signed-off-by: Andy Ross <[email protected]>
  • Loading branch information
Andy Ross authored and carlescufi committed Apr 22, 2020
1 parent cc0db43 commit 00e2cce
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions tests/kernel/timer/timer_api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,22 +560,23 @@ void test_timer_remaining(void)
void test_timeout_abs(void)
{
#ifdef CONFIG_TIMEOUT_64BIT
const int expiration = 10000000; /* 10M ticks */
k_timeout_t t = K_TIMEOUT_ABS_TICKS(10000000), t2;
const u64_t exp_ms = 10000000;
u64_t exp_ticks = k_ms_to_ticks_ceil64(exp_ms);
k_timeout_t t = K_TIMEOUT_ABS_TICKS(exp_ticks), t2;

/* Check the other generator macros to make sure they produce
* the same (whiteboxed) converted values
*/
t2 = K_TIMEOUT_ABS_MS(k_ticks_to_ms_ceil64(expiration));
t2 = K_TIMEOUT_ABS_MS(exp_ms);
zassert_true(t2.ticks == t.ticks, NULL);

t2 = K_TIMEOUT_ABS_US(k_ticks_to_us_ceil64(expiration));
t2 = K_TIMEOUT_ABS_US(1000 * exp_ms);
zassert_true(t2.ticks == t.ticks, NULL);

t2 = K_TIMEOUT_ABS_NS(k_ticks_to_ns_ceil64(expiration));
t2 = K_TIMEOUT_ABS_NS(1000 * 1000 * exp_ms);
zassert_true(t2.ticks == t.ticks, NULL);

t2 = K_TIMEOUT_ABS_CYC(k_ticks_to_cyc_ceil64(expiration));
t2 = K_TIMEOUT_ABS_CYC(k_ms_to_cyc_ceil64(exp_ms));
zassert_true(t2.ticks == t.ticks, NULL);

/* Now set the timeout and make sure the expiration time is
Expand All @@ -588,7 +589,7 @@ void test_timeout_abs(void)
k_usleep(1); /* align to tick */
k_timer_start(&remain_timer, t, K_FOREVER);
zassert_true(k_timer_remaining_ticks(&remain_timer)
+ k_uptime_ticks() + 1 == expiration, NULL);
+ k_uptime_ticks() + 1 == exp_ticks, NULL);
k_timer_stop(&remain_timer);
#endif
}
Expand Down

0 comments on commit 00e2cce

Please sign in to comment.