Skip to content

Commit f7a2080

Browse files
committed
tests: lib: timespec_util: elaborate timespec to k_timeout_t conversion
Elaborate on testing for functions that convert between timespec and k_timeout_t. We explicitly add tests to verify functionality in the regions of interest, verify the semantic equivalence of specific time points. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent f35fe0e commit f7a2080

File tree

4 files changed

+306
-92
lines changed

4 files changed

+306
-92
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_PICOLIBC=y
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_PICOLIBC=y

tests/lib/timespec_util/src/main.c

Lines changed: 232 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
#include <zephyr/sys/util.h>
1414

1515
BUILD_ASSERT(sizeof(time_t) == sizeof(int64_t), "time_t must be 64-bit");
16-
BUILD_ASSERT(sizeof(((struct timespec *)0)->tv_sec) == sizeof(int64_t), "tv_sec must be 64-bit");
17-
18-
/* need NSEC_PER_SEC to be signed for the purposes of this testsuite */
19-
#undef NSEC_PER_SEC
20-
#define NSEC_PER_SEC 1000000000L
2116

2217
#undef CORRECTABLE
2318
#define CORRECTABLE true
2419

2520
#undef UNCORRECTABLE
2621
#define UNCORRECTABLE false
2722

23+
/* Initialize a struct timespec object from a tick count with additional nanoseconds */
24+
#define K_TICKS_TO_TIMESPEC_PLUS_NSECS(ticks, ns) \
25+
K_TIMESPEC(K_TICKS_TO_SECS(ticks) + \
26+
(K_TICKS_TO_NSECS(ticks) + (uint64_t)(ns)) / NSEC_PER_SEC, \
27+
(K_TICKS_TO_NSECS(ticks) + (uint64_t)(ns)) % NSEC_PER_SEC)
28+
2829
/*
2930
* test spec for simple timespec validation
3031
*
@@ -100,16 +101,15 @@ static const struct ts_test_spec ts_tests[] = {
100101
DECL_INVALID_TS_TEST(INT64_MIN, NSEC_PER_SEC, INT64_MIN + 1, 0, CORRECTABLE),
101102
DECL_INVALID_TS_TEST(INT64_MAX, -1, INT64_MAX - 1, NSEC_PER_SEC - 1, CORRECTABLE),
102103
DECL_INVALID_TS_TEST(0, LONG_MIN, LONG_MAX / NSEC_PER_SEC, 145224192, CORRECTABLE),
103-
DECL_INVALID_TS_TEST(0, LONG_MAX, LONG_MAX / NSEC_PER_SEC, LONG_MAX % NSEC_PER_SEC,
104-
CORRECTABLE),
104+
DECL_INVALID_TS_TEST(0, LONG_MAX, LONG_MAX / NSEC_PER_SEC, LONG_MAX % NSEC_PER_SEC, CORRECTABLE),
105105

106106
/* Uncorrectable, invalid cases */
107-
DECL_INVALID_TS_TEST(INT64_MIN + 2, -2 * NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
108-
DECL_INVALID_TS_TEST(INT64_MIN + 1, -NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
109-
DECL_INVALID_TS_TEST(INT64_MIN + 1, -NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
107+
DECL_INVALID_TS_TEST(INT64_MIN + 2, -2 * (int64_t)NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
108+
DECL_INVALID_TS_TEST(INT64_MIN + 1, -(int64_t)NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
109+
DECL_INVALID_TS_TEST(INT64_MIN + 1, -(int64_t)NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
110110
DECL_INVALID_TS_TEST(INT64_MIN, -1, 0, 0, UNCORRECTABLE),
111-
DECL_INVALID_TS_TEST(INT64_MAX, NSEC_PER_SEC, 0, 0, UNCORRECTABLE),
112-
DECL_INVALID_TS_TEST(INT64_MAX - 1, 2 * NSEC_PER_SEC, 0, 0, UNCORRECTABLE),
111+
DECL_INVALID_TS_TEST(INT64_MAX, (int64_t)NSEC_PER_SEC, 0, 0, UNCORRECTABLE),
112+
DECL_INVALID_TS_TEST(INT64_MAX - 1, 2 * (int64_t)NSEC_PER_SEC, 0, 0, UNCORRECTABLE),
113113
};
114114

115115
ZTEST(timeutil_api, test_timespec_is_valid)
@@ -133,17 +133,23 @@ ZTEST(timeutil_api, test_timespec_normalize)
133133
const struct ts_test_spec *const tspec = &ts_tests[i];
134134
struct timespec norm = tspec->invalid_ts;
135135

136+
TC_PRINT("%zu: timespec_normalize({%lld, %lld})\n", i,
137+
(long long)tspec->invalid_ts.tv_sec, (long long)tspec->invalid_ts.tv_nsec);
138+
136139
overflow = !timespec_normalize(&norm);
137140
zexpect_not_equal(tspec->expect_valid || tspec->correctable, overflow,
138-
"%d: timespec_normalize({%ld, %ld}) %s, unexpectedly", i,
139-
(long)tspec->invalid_ts.tv_sec, (long)tspec->invalid_ts.tv_nsec,
141+
"%d: timespec_normalize({%lld, %lld}) %s, unexpectedly", i,
142+
(long long)tspec->invalid_ts.tv_sec,
143+
(long long)tspec->invalid_ts.tv_nsec,
140144
tspec->correctable ? "failed" : "succeeded");
141145

142146
if (!tspec->expect_valid && tspec->correctable) {
143147
different = !timespec_equal(&tspec->invalid_ts, &norm);
144-
zexpect_true(different, "%d: {%ld, %ld} and {%ld, %ld} are unexpectedly %s",
145-
i, tspec->invalid_ts.tv_sec, tspec->invalid_ts.tv_nsec,
146-
norm.tv_sec, tspec->valid_ts.tv_sec,
148+
zexpect_true(different,
149+
"%d: {%lld, %lld} and {%lld, %lld} are unexpectedly %s", i,
150+
(long long)tspec->invalid_ts.tv_sec,
151+
(long long)tspec->invalid_ts.tv_nsec, (long long)norm.tv_sec,
152+
(long long)tspec->valid_ts.tv_sec,
147153
(tspec->expect_valid || tspec->correctable) ? "different"
148154
: "equal");
149155
}
@@ -268,43 +274,147 @@ ZTEST(timeutil_api, test_timespec_equal)
268274
zexpect_false(timespec_equal(&a, &b));
269275
}
270276

271-
#define NS_PER_TICK (NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
277+
ZTEST(timeutil_api, test_K_TICKS_TO_SECS)
278+
{
279+
zexpect_equal(K_TICKS_TO_SECS(0), 0);
280+
zexpect_equal(K_TICKS_TO_SECS(CONFIG_SYS_CLOCK_TICKS_PER_SEC), 1);
281+
zexpect_equal(K_TICKS_TO_SECS(2 * CONFIG_SYS_CLOCK_TICKS_PER_SEC), 2);
282+
zexpect_equal(K_TICKS_TO_SECS(K_TICK_MAX), K_TS_MAX.tv_sec);
283+
zexpect_equal(K_TICKS_TO_SECS(K_TICKS_FOREVER), INT64_MAX);
284+
285+
#if defined(CONFIG_TIMEOUT_64BIT) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 100)
286+
zexpect_equal(K_TS_MAX.tv_sec, 92233720368547758LL);
287+
#endif
288+
289+
#if (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 32768)
290+
#if defined(CONFIG_TIMEOUT_64BIT)
291+
zexpect_equal(K_TS_MAX.tv_sec, 281474976710655LL);
292+
#else
293+
zexpect_equal(K_TS_MAX.tv_sec, 131071);
294+
#endif
295+
#endif
296+
}
272297

273-
/* 0 := lower limit, 2 := upper limit */
274-
static const struct timespec k_timeout_limits[] = {
275-
/* K_NO_WAIT + 1 tick */
276-
{
277-
.tv_sec = 0,
278-
.tv_nsec = NS_PER_TICK,
279-
},
280-
/* K_FOREVER - 1 tick */
281-
{
282-
.tv_sec = (time_t)CLAMP((NS_PER_TICK * (uint64_t)K_TICK_MAX) / NSEC_PER_SEC, 0,
283-
INT64_MAX),
284-
.tv_nsec = (long)CLAMP((NS_PER_TICK * (uint64_t)K_TICK_MAX) % NSEC_PER_SEC, 0,
285-
NSEC_PER_SEC - 1),
286-
},
287-
};
298+
ZTEST(timeutil_api, test_K_TICKS_TO_NSECS)
299+
{
300+
zexpect_equal(K_TICKS_TO_NSECS(0), 0);
301+
zexpect_equal(K_TICKS_TO_NSECS(1) % NSEC_PER_SEC,
302+
(NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC) % NSEC_PER_SEC);
303+
zexpect_equal(K_TICKS_TO_NSECS(2) % NSEC_PER_SEC,
304+
(2 * NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC) % NSEC_PER_SEC);
305+
zexpect_equal(K_TICKS_TO_NSECS(K_TICK_MAX), K_TS_MAX.tv_nsec);
306+
zexpect_equal(K_TICKS_TO_NSECS(K_TICKS_FOREVER), NSEC_PER_SEC - 1);
307+
308+
#if defined(CONFIG_TIMEOUT_64BIT) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 100)
309+
zexpect_equal(K_TS_MAX.tv_nsec, 70000000L);
310+
#endif
311+
312+
#if (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 32768)
313+
#if defined(CONFIG_TIMEOUT_64BIT)
314+
zexpect_equal(K_TS_MAX.tv_nsec, 999969482L);
315+
#else
316+
zexpect_equal(K_TS_MAX.tv_nsec, 999938964L);
317+
#endif
318+
#endif
319+
}
320+
321+
/* non-saturating */
322+
#define DECL_TOSPEC_TEST(to, ts, sat, neg, round) \
323+
{ \
324+
.timeout = (to), \
325+
.tspec = (ts), \
326+
.saturation = (sat), \
327+
.negative = (neg), \
328+
.roundup = (round), \
329+
}
330+
/* negative timespecs rounded up to K_NO_WAIT */
331+
#define DECL_TOSPEC_NEGATIVE_TEST(ts) DECL_TOSPEC_TEST(K_NO_WAIT, (ts), 0, true, false)
332+
/* zero-valued timeout */
333+
#define DECL_TOSPEC_ZERO_TEST(to) DECL_TOSPEC_TEST((to), K_TIMESPEC(0, 0), 0, false, false)
334+
/* round up toward K_TICK_MIN */
335+
#define DECL_NSAT_TOSPEC_TEST(ts) DECL_TOSPEC_TEST(K_TICKS(K_TICK_MIN), (ts), -1, false, false)
336+
/* round up toward next tick boundary */
337+
#define DECL_ROUND_TOSPEC_TEST(to, ts) DECL_TOSPEC_TEST((to), (ts), 0, false, true)
338+
/* round down toward K_TICK_MAX */
339+
#define DECL_PSAT_TOSPEC_TEST(ts) DECL_TOSPEC_TEST(K_TICKS(K_TICK_MAX), (ts), 1, false, false)
288340

289341
static const struct tospec {
290342
k_timeout_t timeout;
291343
struct timespec tspec;
292344
int saturation;
345+
bool negative;
346+
bool roundup;
293347
} tospecs[] = {
294-
{K_NO_WAIT, {INT64_MIN, 0}, -1},
295-
{K_NO_WAIT, {-1, 0}, -1},
296-
{K_NO_WAIT, {-1, NSEC_PER_SEC - 1}, -1},
297-
{K_NO_WAIT, {0, 0}, 0},
298-
{K_NSEC(0), {0, 0}, 0},
299-
{K_NSEC(2000000000), {2, 0}, 0},
300-
{K_USEC(0), {0, 0}, 0},
301-
{K_USEC(2000000), {2, 0}, 0},
302-
{K_MSEC(100), {0, 100000000}, 0},
303-
{K_MSEC(2000), {2, 0}, 0},
304-
{K_SECONDS(0), {0, 0}, 0},
305-
{K_SECONDS(1), {1, 0}, 0},
306-
{K_SECONDS(100), {100, 0}, 0},
307-
{K_FOREVER, {INT64_MAX, NSEC_PER_SEC - 1}, 0},
348+
/* negative timespecs should round-up to K_NO_WAIT */
349+
DECL_TOSPEC_NEGATIVE_TEST(K_TIMESPEC(INT64_MIN, 0)),
350+
DECL_TOSPEC_NEGATIVE_TEST(K_TIMESPEC(-1, 0)),
351+
DECL_TOSPEC_NEGATIVE_TEST(K_TIMESPEC(-1, NSEC_PER_SEC - 1)),
352+
353+
/* zero-valued timeouts are equivalent to K_NO_WAIT */
354+
DECL_TOSPEC_ZERO_TEST(K_NSEC(0)),
355+
DECL_TOSPEC_ZERO_TEST(K_USEC(0)),
356+
DECL_TOSPEC_ZERO_TEST(K_MSEC(0)),
357+
DECL_TOSPEC_ZERO_TEST(K_SECONDS(0)),
358+
359+
/* round up to K_TICK_MIN */
360+
DECL_NSAT_TOSPEC_TEST(K_TIMESPEC(0, 1)),
361+
DECL_NSAT_TOSPEC_TEST(K_TIMESPEC(0, 2)),
362+
#if CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1
363+
DECL_NSAT_TOSPEC_TEST(K_TIMESPEC(0, K_TICKS_TO_NSECS(K_TICK_MIN))),
364+
#endif
365+
366+
#if CONFIG_SYS_CLOCK_TICKS_PER_SEC < MHZ(1)
367+
DECL_NSAT_TOSPEC_TEST(K_TIMESPEC(0, NSEC_PER_USEC)),
368+
#endif
369+
#if CONFIG_SYS_CLOCK_TICKS_PER_SEC < KHZ(1)
370+
DECL_NSAT_TOSPEC_TEST(K_TIMESPEC(0, NSEC_PER_MSEC)),
371+
#endif
372+
373+
/* round to next tick boundary (low-end) */
374+
#if CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1
375+
DECL_ROUND_TOSPEC_TEST(K_TICKS(2), K_TICKS_TO_TIMESPEC_PLUS_NSECS(1, 1)),
376+
DECL_ROUND_TOSPEC_TEST(K_TICKS(2),
377+
K_TICKS_TO_TIMESPEC_PLUS_NSECS(1, K_TICKS_TO_NSECS(1) / 2)),
378+
DECL_ROUND_TOSPEC_TEST(K_TICKS(2),
379+
K_TICKS_TO_TIMESPEC_PLUS_NSECS(1, K_TICKS_TO_NSECS(1) - 1)),
380+
#endif
381+
382+
/* exact conversions for large timeouts */
383+
#ifdef CONFIG_TIMEOUT_64BIT
384+
DECL_TOSPEC_TEST(K_NSEC(2000000000), K_TIMESPEC(2, 0), 0, false, false),
385+
#endif
386+
DECL_TOSPEC_TEST(K_USEC(2000000), K_TIMESPEC(2, 0), 0, false, false),
387+
DECL_TOSPEC_TEST(K_MSEC(2000), K_TIMESPEC(2, 0), 0, false, false),
388+
389+
DECL_TOSPEC_TEST(K_SECONDS(1),
390+
K_TIMESPEC(1, K_TICKS_TO_NSECS(CONFIG_SYS_CLOCK_TICKS_PER_SEC)), 0, false,
391+
false),
392+
DECL_TOSPEC_TEST(K_SECONDS(2),
393+
K_TIMESPEC(2, K_TICKS_TO_NSECS(2 * CONFIG_SYS_CLOCK_TICKS_PER_SEC)), 0,
394+
false, false),
395+
DECL_TOSPEC_TEST(K_SECONDS(100),
396+
K_TIMESPEC(100, K_TICKS_TO_NSECS(100 * CONFIG_SYS_CLOCK_TICKS_PER_SEC)), 0,
397+
false, false),
398+
399+
DECL_TOSPEC_TEST(K_TICKS(1000), K_TICKS_TO_TIMESPEC(1000), 0, false, false),
400+
401+
/* round to next tick boundary (high-end) */
402+
#if CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1
403+
DECL_ROUND_TOSPEC_TEST(K_TICKS(1000), K_TICKS_TO_TIMESPEC_PLUS_NSECS(999, 1)),
404+
DECL_ROUND_TOSPEC_TEST(K_TICKS(1000),
405+
K_TICKS_TO_TIMESPEC_PLUS_NSECS(999, K_TICKS_TO_NSECS(1) / 2)),
406+
DECL_ROUND_TOSPEC_TEST(K_TICKS(1000),
407+
K_TICKS_TO_TIMESPEC_PLUS_NSECS(999, K_TICKS_TO_NSECS(1) - 1)),
408+
#endif
409+
410+
/* round down toward K_TICK_MAX */
411+
DECL_PSAT_TOSPEC_TEST(K_TICKS_TO_TIMESPEC(K_TICK_MAX)),
412+
#if defined(CONFIG_TIMEOUT_64BIT) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1)
413+
DECL_PSAT_TOSPEC_TEST(K_TICKS_TO_TIMESPEC((uint64_t)K_TICK_MAX + 1)),
414+
#endif
415+
416+
/* K_FOREVER <=> K_TS_FOREVER */
417+
DECL_TOSPEC_TEST(K_FOREVER, K_TIMESPEC(INT64_MAX, NSEC_PER_SEC - 1), 0, false, false),
308418
};
309419

310420
ZTEST(timeutil_api, test_timespec_from_timeout)
@@ -315,11 +425,19 @@ ZTEST(timeutil_api, test_timespec_from_timeout)
315425
const struct tospec *const tspec = &tospecs[i];
316426
struct timespec actual;
317427

318-
if (tspec->saturation != 0) {
319-
/* saturation cases are only checked in test_timespec_to_timeout */
428+
/*
429+
* In this test we only check exact conversions, so skip negative timespecs that
430+
* saturate up to K_NO_WAIT and skip values under K_TS_MIN and over K_TS_MAX. Also,
431+
* skip "normal" conversions that just round up to the next tick boundary.
432+
*/
433+
if (tspec->negative || (tspec->saturation != 0) || tspec->roundup) {
320434
continue;
321435
}
322436

437+
TC_PRINT("%zu: ticks: {%lld}, timespec: {%lld, %lld}\n", i,
438+
(long long)tspec->timeout.ticks, (long long)tspec->tspec.tv_sec,
439+
(long long)tspec->tspec.tv_nsec);
440+
323441
timespec_from_timeout(tspec->timeout, &actual);
324442
zexpect_true(timespec_equal(&actual, &tspec->tspec),
325443
"%d: {%ld, %ld} and {%ld, %ld} are unexpectedly different", i,
@@ -336,48 +454,86 @@ ZTEST(timeutil_api, test_timespec_to_timeout)
336454
const struct tospec *const tspec = &tospecs[i];
337455
k_timeout_t actual;
338456

457+
TC_PRINT("%zu: ticks: {%lld}, timespec: {%lld, %lld}\n", i,
458+
(long long)tspec->timeout.ticks, (long long)tspec->tspec.tv_sec,
459+
(long long)tspec->tspec.tv_nsec);
460+
461+
actual = timespec_to_timeout(&tspec->tspec);
339462
if (tspec->saturation == 0) {
340-
/* no saturation / exact match */
341-
actual = timespec_to_timeout(&tspec->tspec);
463+
/* exact match or rounding up */
464+
if (!tspec->negative &&
465+
(timespec_compare(&tspec->tspec, &K_TS_NO_WAIT) != 0) &&
466+
(timespec_compare(&tspec->tspec, &K_TS_FOREVER) != 0)) {
467+
__ASSERT(timespec_compare(&tspec->tspec, &K_TS_MIN) >= 0,
468+
"%zu: timespec: {%lld, %lld} is not greater than K_TS_MIN",
469+
i, (long long)tspec->tspec.tv_sec,
470+
(long long)tspec->tspec.tv_nsec);
471+
__ASSERT(timespec_compare(&tspec->tspec, &K_TS_MAX) <= 0,
472+
"%zu: timespec: {%lld, %lld} is not less than K_TS_MAX", i,
473+
(long long)tspec->tspec.tv_sec,
474+
(long long)tspec->tspec.tv_nsec);
475+
}
342476
zexpect_equal(actual.ticks, tspec->timeout.ticks,
343477
"%d: {%" PRId64 "} and {%" PRId64
344478
"} are unexpectedly different",
345479
i, (int64_t)actual.ticks, (int64_t)tspec->timeout.ticks);
346-
continue;
347-
}
348-
349-
if ((tspec->saturation < 0) ||
350-
(timespec_compare(&tspec->tspec, &k_timeout_limits[0]) < 0)) {
351-
/* K_NO_WAIT saturation */
352-
actual = timespec_to_timeout(&tspec->tspec);
353-
zexpect_equal(actual.ticks, K_NO_WAIT.ticks,
480+
} else if (tspec->saturation < 0) {
481+
/* K_TICK_MIN saturation */
482+
__ASSERT(timespec_compare(&tspec->tspec, &K_TS_MIN) <= 0,
483+
"timespec: {%lld, %lld} is not less than or equal to K_TS_MIN "
484+
"{%lld, %lld}",
485+
(long long)tspec->tspec.tv_sec, (long long)tspec->tspec.tv_nsec,
486+
(long long)K_TS_MIN.tv_sec, (long long)K_TS_MIN.tv_nsec);
487+
zexpect_equal(actual.ticks, K_TICK_MIN,
354488
"%d: {%" PRId64 "} and {%" PRId64
355489
"} are unexpectedly different",
356-
i, (int64_t)actual.ticks, (int64_t)K_NO_WAIT.ticks);
357-
continue;
358-
}
359-
360-
if ((tspec->saturation > 0) ||
361-
(timespec_compare(&tspec->tspec, &k_timeout_limits[1]) > 0)) {
362-
/* K_FOREVER saturation */
363-
actual = timespec_to_timeout(&tspec->tspec);
364-
zexpect_equal(actual.ticks, K_TICKS_FOREVER,
490+
i, (int64_t)actual.ticks, (int64_t)K_TICK_MIN);
491+
} else if (tspec->saturation > 0) {
492+
/* K_TICK_MAX saturation */
493+
__ASSERT(timespec_compare(&tspec->tspec, &K_TS_MAX) >= 0,
494+
"timespec: {%lld, %lld} is not greater than or equal to K_TS_MAX "
495+
"{%lld, %lld}",
496+
(long long)tspec->tspec.tv_sec, (long long)tspec->tspec.tv_nsec,
497+
(long long)K_TS_MAX.tv_sec, (long long)K_TS_MAX.tv_nsec);
498+
zexpect_equal(actual.ticks, K_TICK_MAX,
365499
"%d: {%" PRId64 "} and {%" PRId64
366500
"} are unexpectedly different",
367-
i, (int64_t)actual.ticks, (int64_t)K_TICKS_FOREVER);
368-
continue;
501+
i, (int64_t)actual.ticks, (int64_t)K_TICK_MAX);
369502
}
370503
}
504+
505+
#if defined(CONFIG_TIMEOUT_64BIT) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 100)
506+
{
507+
k_timeout_t to = K_TICKS(K_TICK_MAX);
508+
/* K_TS_MAX corresponding K_TICK_MAX with a tick rate of 100 Hz */
509+
struct timespec ts = K_TIMESPEC(92233720368547758LL, 70000000L);
510+
511+
zexpect_true(K_TIMEOUT_EQ(timespec_to_timeout(&ts), to),
512+
"timespec_to_timeout(%lld, %lld) != %lld", (long long)ts.tv_sec,
513+
(long long)ts.tv_nsec, (long long)to.ticks);
514+
515+
TC_PRINT("timespec_to_timeout():\nts: {%lld, %lld} => to: {%lld}\n",
516+
(long long)ts.tv_sec, (long long)ts.tv_nsec, (long long)to.ticks);
517+
}
518+
#endif
371519
}
372520

373521
static void *setup(void)
374522
{
375-
printk("CONFIG_TIMEOUT_64BIT=%c\n", CONFIG_TIMEOUT_64BIT ? 'y' : 'n');
376-
printk("K_TICK_MAX: %lld\n", (long long)K_TICK_MAX);
377-
printk("minimum timeout: {%lld, %lld}\n", (long long)k_timeout_limits[0].tv_sec,
378-
(long long)k_timeout_limits[0].tv_nsec);
379-
printk("maximum timeout: {%lld, %lld}\n", (long long)k_timeout_limits[1].tv_sec,
380-
(long long)k_timeout_limits[1].tv_nsec);
523+
TC_PRINT("CONFIG_SYS_CLOCK_TICKS_PER_SEC=%d\n", CONFIG_SYS_CLOCK_TICKS_PER_SEC);
524+
TC_PRINT("CONFIG_TIMEOUT_64BIT=%c\n", IS_ENABLED(CONFIG_TIMEOUT_64BIT) ? 'y' : 'n');
525+
TC_PRINT("K_TICK_MIN: %lld\n", (long long)K_TICK_MIN);
526+
TC_PRINT("K_TICK_MAX: %lld\n", (long long)K_TICK_MAX);
527+
TC_PRINT("K_TS_MIN: {%lld, %lld}\n", (long long)K_TS_MIN.tv_sec,
528+
(long long)K_TS_MIN.tv_nsec);
529+
TC_PRINT("K_TS_MAX: {%lld, %lld}\n", (long long)K_TS_MAX.tv_sec,
530+
(long long)K_TS_MAX.tv_nsec);
531+
TC_PRINT("INT64_MIN: %lld\n", (long long)INT64_MIN);
532+
TC_PRINT("INT64_MAX: %lld\n", (long long)INT64_MAX);
533+
PRINT_LINE;
534+
535+
/* check numerical values corresponding to K_TICK_MAX */
536+
zassert_equal(K_TICK_MAX, IS_ENABLED(CONFIG_TIMEOUT_64BIT) ? INT64_MAX : UINT32_MAX - 1);
381537

382538
return NULL;
383539
}

0 commit comments

Comments
 (0)