Skip to content

Commit 551a7ab

Browse files
aescolarkartben
authored andcommitted
sys: timeutil & test: Multiple fixes
* Fixes in code and test so it works with any expected time_t size. * Fix test_timespec_normalize so it also tests the output of timespec_normalize(), fixing some incorrect expected values accordingly. * Test: Do not force a C library in the integration platform and do not filter by libC as it is not necessary anymore. Signed-off-by: Alberto Escolar Piedras <[email protected]>
1 parent f493688 commit 551a7ab

File tree

5 files changed

+46
-44
lines changed

5 files changed

+46
-44
lines changed

include/zephyr/sys/clock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ typedef struct {
184184
/* Define a timespec */
185185
#define K_TIMESPEC(sec, nsec) \
186186
((struct timespec){ \
187-
.tv_sec = (time_t)(sec), \
187+
.tv_sec = (time_t)CLAMP((int64_t)(sec), SYS_TIME_T_MIN, SYS_TIME_T_MAX), \
188188
.tv_nsec = (long)(nsec), \
189189
})
190190

tests/lib/timespec_util/boards/native_sim.conf

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/lib/timespec_util/boards/native_sim_native_64.conf

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/lib/timespec_util/src/main.c

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <zephyr/sys/timeutil.h>
1313
#include <zephyr/sys/util.h>
1414

15-
BUILD_ASSERT(sizeof(time_t) == sizeof(int64_t), "time_t must be 64-bit");
16-
1715
#undef CORRECTABLE
1816
#define CORRECTABLE true
1917

@@ -80,37 +78,39 @@ static const struct ts_test_spec ts_tests[] = {
8078
DECL_VALID_TS_TEST(-1, 0),
8179
DECL_VALID_TS_TEST(-1, 1),
8280
DECL_VALID_TS_TEST(-1, NSEC_PER_SEC - 1),
83-
DECL_VALID_TS_TEST(INT64_MIN, 0),
84-
DECL_VALID_TS_TEST(INT64_MIN, NSEC_PER_SEC - 1),
85-
DECL_VALID_TS_TEST(INT64_MAX, 0),
86-
DECL_VALID_TS_TEST(INT64_MAX, NSEC_PER_SEC - 1),
81+
DECL_VALID_TS_TEST(SYS_TIME_T_MIN, 0),
82+
DECL_VALID_TS_TEST(SYS_TIME_T_MIN, NSEC_PER_SEC - 1),
83+
DECL_VALID_TS_TEST(SYS_TIME_T_MAX, 0),
84+
DECL_VALID_TS_TEST(SYS_TIME_T_MAX, NSEC_PER_SEC - 1),
8785

8886
/* Correctable, invalid cases */
89-
DECL_INVALID_TS_TEST(0, -2 * NSEC_PER_SEC + 1, -2, 1, CORRECTABLE),
90-
DECL_INVALID_TS_TEST(0, -2 * NSEC_PER_SEC - 1, -3, NSEC_PER_SEC - 1, CORRECTABLE),
91-
DECL_INVALID_TS_TEST(0, -NSEC_PER_SEC - 1, -2, NSEC_PER_SEC - 1, CORRECTABLE),
87+
DECL_INVALID_TS_TEST(0, -2LL * NSEC_PER_SEC + 1, -2, 1, CORRECTABLE),
88+
DECL_INVALID_TS_TEST(0, -2LL * NSEC_PER_SEC - 1, -3, NSEC_PER_SEC - 1, CORRECTABLE),
89+
DECL_INVALID_TS_TEST(0, -1LL * NSEC_PER_SEC - 1, -2, NSEC_PER_SEC - 1, CORRECTABLE),
9290
DECL_INVALID_TS_TEST(0, -1, -1, NSEC_PER_SEC - 1, CORRECTABLE),
9391
DECL_INVALID_TS_TEST(0, NSEC_PER_SEC, 1, 0, CORRECTABLE),
94-
DECL_INVALID_TS_TEST(0, NSEC_PER_SEC + 1, 1, 0, CORRECTABLE),
92+
DECL_INVALID_TS_TEST(0, NSEC_PER_SEC + 1, 1, 1, CORRECTABLE),
9593
DECL_INVALID_TS_TEST(1, -1, 0, NSEC_PER_SEC - 1, CORRECTABLE),
9694
DECL_INVALID_TS_TEST(1, NSEC_PER_SEC, 2, 0, CORRECTABLE),
9795
DECL_INVALID_TS_TEST(-1, -1, -2, NSEC_PER_SEC - 1, CORRECTABLE),
9896
DECL_INVALID_TS_TEST(0, NSEC_PER_SEC, 1, 0, CORRECTABLE),
9997
DECL_INVALID_TS_TEST(1, -1, 0, NSEC_PER_SEC - 1, CORRECTABLE),
10098
DECL_INVALID_TS_TEST(1, NSEC_PER_SEC, 2, 0, CORRECTABLE),
101-
DECL_INVALID_TS_TEST(INT64_MIN, NSEC_PER_SEC, INT64_MIN + 1, 0, CORRECTABLE),
102-
DECL_INVALID_TS_TEST(INT64_MAX, -1, INT64_MAX - 1, NSEC_PER_SEC - 1, CORRECTABLE),
103-
DECL_INVALID_TS_TEST(0, LONG_MIN, LONG_MAX / NSEC_PER_SEC, 145224192, CORRECTABLE),
99+
DECL_INVALID_TS_TEST(SYS_TIME_T_MIN, NSEC_PER_SEC, SYS_TIME_T_MIN + 1, 0, CORRECTABLE),
100+
DECL_INVALID_TS_TEST(SYS_TIME_T_MAX, -1, SYS_TIME_T_MAX - 1, NSEC_PER_SEC - 1, CORRECTABLE),
101+
DECL_INVALID_TS_TEST(0, LONG_MIN, (int64_t)LONG_MIN / NSEC_PER_SEC - 1,
102+
NSEC_PER_SEC + LONG_MIN % (long long)NSEC_PER_SEC, CORRECTABLE),
104103
DECL_INVALID_TS_TEST(0, LONG_MAX, LONG_MAX / NSEC_PER_SEC, LONG_MAX % NSEC_PER_SEC,
105104
CORRECTABLE),
106105

107106
/* Uncorrectable, invalid cases */
108-
DECL_INVALID_TS_TEST(INT64_MIN + 2, -2 * (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),
110-
DECL_INVALID_TS_TEST(INT64_MIN + 1, -(int64_t)NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
111-
DECL_INVALID_TS_TEST(INT64_MIN, -1, 0, 0, UNCORRECTABLE),
112-
DECL_INVALID_TS_TEST(INT64_MAX, (int64_t)NSEC_PER_SEC, 0, 0, UNCORRECTABLE),
113-
DECL_INVALID_TS_TEST(INT64_MAX - 1, 2 * (int64_t)NSEC_PER_SEC, 0, 0, UNCORRECTABLE),
107+
DECL_INVALID_TS_TEST(SYS_TIME_T_MIN + 2, -2 * (int64_t)NSEC_PER_SEC - 1, 0, 0,
108+
UNCORRECTABLE),
109+
DECL_INVALID_TS_TEST(SYS_TIME_T_MIN + 1, -(int64_t)NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
110+
DECL_INVALID_TS_TEST(SYS_TIME_T_MIN + 1, -(int64_t)NSEC_PER_SEC - 1, 0, 0, UNCORRECTABLE),
111+
DECL_INVALID_TS_TEST(SYS_TIME_T_MIN, -1, 0, 0, UNCORRECTABLE),
112+
DECL_INVALID_TS_TEST(SYS_TIME_T_MAX, (int64_t)NSEC_PER_SEC, 0, 0, UNCORRECTABLE),
113+
DECL_INVALID_TS_TEST(SYS_TIME_T_MAX - 1, 2 * (int64_t)NSEC_PER_SEC, 0, 0, UNCORRECTABLE),
114114
};
115115

116116
ZTEST(timeutil_api, test_timespec_is_valid)
@@ -129,7 +129,7 @@ ZTEST(timeutil_api, test_timespec_is_valid)
129129
ZTEST(timeutil_api, test_timespec_normalize)
130130
{
131131
ARRAY_FOR_EACH(ts_tests, i) {
132-
bool different;
132+
bool different, corrected;
133133
bool overflow;
134134
const struct ts_test_spec *const tspec = &ts_tests[i];
135135
struct timespec norm = tspec->invalid_ts;
@@ -146,13 +146,16 @@ ZTEST(timeutil_api, test_timespec_normalize)
146146

147147
if (!tspec->expect_valid && tspec->correctable) {
148148
different = !timespec_equal(&tspec->invalid_ts, &norm);
149-
zexpect_true(different,
150-
"%d: {%lld, %lld} and {%lld, %lld} are unexpectedly %s", i,
149+
corrected = timespec_equal(&tspec->valid_ts, &norm);
150+
zexpect_true(different && corrected,
151+
"%d: {%lld, %lld} is not properly corrected:"
152+
"{%lld, %lld} != {%lld, %lld}", i,
151153
(long long)tspec->invalid_ts.tv_sec,
152-
(long long)tspec->invalid_ts.tv_nsec, (long long)norm.tv_sec,
154+
(long long)tspec->invalid_ts.tv_nsec,
153155
(long long)tspec->valid_ts.tv_sec,
154-
(tspec->expect_valid || tspec->correctable) ? "different"
155-
: "equal");
156+
(long long)tspec->valid_ts.tv_nsec,
157+
(long long)norm.tv_sec,
158+
(long long)norm.tv_nsec);
156159
}
157160
}
158161
}
@@ -173,10 +176,10 @@ ZTEST(timeutil_api, test_timespec_add)
173176
{.a = {-1, 1}, .b = {-1, 1}, .result = {-2, 2}, .expect = false},
174177
{.a = {-1, NSEC_PER_SEC - 1}, .b = {0, 1}, .result = {0, 0}, .expect = false},
175178
/* overflow cases */
176-
{.a = {INT64_MAX, 0}, .b = {1, 0}, .result = {0}, .expect = true},
177-
{.a = {INT64_MIN, 0}, .b = {-1, 0}, .result = {0}, .expect = true},
178-
{.a = {INT64_MAX, NSEC_PER_SEC - 1}, .b = {1, 1}, .result = {0}, .expect = true},
179-
{.a = {INT64_MIN, NSEC_PER_SEC - 1}, .b = {-1, 0}, .result = {0}, .expect = true},
179+
{.a = {SYS_TIME_T_MAX, 0}, .b = {1, 0}, .result = {0}, .expect = true},
180+
{.a = {SYS_TIME_T_MIN, 0}, .b = {-1, 0}, .result = {0}, .expect = true},
181+
{.a = {SYS_TIME_T_MAX, NSEC_PER_SEC - 1}, .b = {1, 1}, .result = {0}, .expect = true},
182+
{.a = {SYS_TIME_T_MIN, NSEC_PER_SEC - 1}, .b = {-1, 0}, .result = {0}, .expect = true},
180183
};
181184

182185
ARRAY_FOR_EACH(tspecs, i) {
@@ -210,9 +213,9 @@ ZTEST(timeutil_api, test_timespec_negate)
210213
{.ts = {0, 0}, .result = {0, 0}, .expect_failure = false},
211214
{.ts = {1, 1}, .result = {-2, NSEC_PER_SEC - 1}, .expect_failure = false},
212215
{.ts = {-1, 1}, .result = {0, NSEC_PER_SEC - 1}, .expect_failure = false},
213-
{.ts = {INT64_MAX, 0}, .result = {INT64_MIN + 1, 0}, .expect_failure = false},
216+
{.ts = {SYS_TIME_T_MAX, 0}, .result = {SYS_TIME_T_MIN + 1, 0}, .expect_failure = false},
214217
/* overflow cases */
215-
{.ts = {INT64_MIN, 0}, .result = {0}, .expect_failure = true},
218+
{.ts = {SYS_TIME_T_MIN, 0}, .result = {0}, .expect_failure = true},
216219
};
217220

218221
ARRAY_FOR_EACH(tspecs, i) {
@@ -280,16 +283,21 @@ ZTEST(timeutil_api, test_K_TICKS_TO_SECS)
280283
zexpect_equal(K_TICKS_TO_SECS(0), 0);
281284
zexpect_equal(K_TICKS_TO_SECS(CONFIG_SYS_CLOCK_TICKS_PER_SEC), 1);
282285
zexpect_equal(K_TICKS_TO_SECS(2 * CONFIG_SYS_CLOCK_TICKS_PER_SEC), 2);
283-
zexpect_equal(K_TICKS_TO_SECS(K_TICK_MAX), K_TIMESPEC_MAX.tv_sec);
284-
zexpect_equal(K_TICKS_TO_SECS(K_TICKS_FOREVER), INT64_MAX);
286+
zexpect_equal(K_TICKS_TO_SECS(K_TICKS_FOREVER), SYS_TIME_T_MAX);
285287

288+
if (SYS_TIME_T_MAX >= 92233720368547758LL) {
289+
/* These checks should only be done if time_t has enough bits to hold K_TS_MAX without overflowing */
290+
zexpect_equal(K_TICKS_TO_SECS(K_TICK_MAX), K_TIMESPEC_MAX.tv_sec);
286291
#if defined(CONFIG_TIMEOUT_64BIT) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 100)
287-
zexpect_equal(K_TIMESPEC_MAX.tv_sec, 92233720368547758LL);
292+
zexpect_equal(K_TIMESPEC_MAX.tv_sec, 92233720368547758LL);
288293
#endif
294+
}
289295

290296
#if (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 32768)
291297
#if defined(CONFIG_TIMEOUT_64BIT)
292-
zexpect_equal(K_TIMESPEC_MAX.tv_sec, 281474976710655LL);
298+
if (SYS_TIME_T_MAX >= 281474976710655LL) {
299+
zexpect_equal(K_TIMESPEC_MAX.tv_sec, 281474976710655LL);
300+
}
293301
#else
294302
zexpect_equal(K_TIMESPEC_MAX.tv_sec, 131071);
295303
#endif
@@ -347,7 +355,7 @@ static const struct tospec {
347355
bool roundup;
348356
} tospecs[] = {
349357
/* negative timespecs should round-up to K_NO_WAIT */
350-
DECL_TOSPEC_NEGATIVE_TEST(K_TIMESPEC(INT64_MIN, 0)),
358+
DECL_TOSPEC_NEGATIVE_TEST(K_TIMESPEC(SYS_TIME_T_MIN, 0)),
351359
DECL_TOSPEC_NEGATIVE_TEST(K_TIMESPEC(-1, 0)),
352360
DECL_TOSPEC_NEGATIVE_TEST(K_TIMESPEC(-1, NSEC_PER_SEC - 1)),
353361

@@ -410,12 +418,9 @@ static const struct tospec {
410418

411419
/* round down toward K_TICK_MAX */
412420
DECL_PSAT_TOSPEC_TEST(K_TICKS_TO_TIMESPEC(K_TICK_MAX)),
413-
#if defined(CONFIG_TIMEOUT_64BIT) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1)
414-
DECL_PSAT_TOSPEC_TEST(K_TICKS_TO_TIMESPEC((uint64_t)K_TICK_MAX + 1)),
415-
#endif
416421

417422
/* K_FOREVER <=> K_TIMESPEC_FOREVER */
418-
DECL_TOSPEC_TEST(K_FOREVER, K_TIMESPEC(INT64_MAX, NSEC_PER_SEC - 1), 0, false, false),
423+
DECL_TOSPEC_TEST(K_FOREVER, K_TIMESPEC(SYS_TIME_T_MAX, NSEC_PER_SEC - 1), 0, false, false),
419424
};
420425

421426
ZTEST(timeutil_api, test_timespec_from_timeout)

tests/lib/timespec_util/testcase.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# FIXME: this should be under tests/unit/timeutil but will not work due to #90029
22
common:
3-
filter: not CONFIG_NATIVE_LIBC
43
tags:
54
- timeutils
65
# 1 tier0 platform per supported architecture

0 commit comments

Comments
 (0)