Skip to content

Commit 6f8f9b5

Browse files
Chris Friedtcfriedt
authored andcommitted
tests: time_units: check for overflow in z_tmcvt intermediate
Prior to #41602, due to the ordering of operations (first mul, then div), an intermediate value would overflow, resulting in a time non-linearity. This test ensures that time rolls-over properly. Signed-off-by: Chris Friedt <[email protected]> (cherry picked from commit 74c9c0e)
1 parent afbc932 commit 6f8f9b5

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright 2022 Meta
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
project(time_units)
5+
set(SOURCES main.c overflow.c)
6+
find_package(ZephyrUnittest REQUIRED HINTS $ENV{ZEPHYR_BASE})

tests/unit/time_units/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ztest.h>
8+
9+
extern void test_z_tmcvt_for_overflow(void);
10+
11+
void test_main(void)
12+
{
13+
ztest_test_suite(test_time_units, ztest_unit_test(test_z_tmcvt_for_overflow));
14+
ztest_run_test_suite(test_time_units);
15+
}

tests/unit/time_units/overflow.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <inttypes.h>
8+
#include <limits.h>
9+
#include <stdbool.h>
10+
#include <stdint.h>
11+
12+
#include <ztest.h>
13+
#include <sys/time_units.h>
14+
15+
/**
16+
* @brief Test @ref z_tmcvt for robustness against intermediate value overflow.
17+
*
18+
* With input
19+
* ```
20+
* [t0, t1, t2] = [
21+
* UINT64_MAX / to_hz - 1,
22+
* UINT64_MAX / to_hz,
23+
* UINT64_MAX / to_hz + 1,
24+
* ]
25+
* ```
26+
*
27+
* passed through @ref z_tmcvt, we expect a linear sequence:
28+
* ```
29+
* [
30+
* 562949953369140,
31+
* 562949953399658,
32+
* 562949953430175,
33+
* ]
34+
* ```
35+
*
36+
* If an overflow occurs, we see something like the following:
37+
* ```
38+
* [
39+
* 562949953369140,
40+
* 562949953399658,
41+
* 8863,
42+
* ]
43+
* ```
44+
*/
45+
void test_z_tmcvt_for_overflow(void)
46+
{
47+
const uint32_t from_hz = 32768UL;
48+
const uint32_t to_hz = 1000000000UL;
49+
50+
zassert_equal(562949953369140ULL,
51+
z_tmcvt(UINT64_MAX / to_hz - 1, from_hz, to_hz, true, false, false, false),
52+
NULL);
53+
zassert_equal(562949953399658ULL,
54+
z_tmcvt(UINT64_MAX / to_hz, from_hz, to_hz, true, false, false, false),
55+
NULL);
56+
zassert_equal(562949953430175ULL,
57+
z_tmcvt(UINT64_MAX / to_hz + 1, from_hz, to_hz, true, false, false, false),
58+
NULL);
59+
}

tests/unit/time_units/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
common:
2+
tags: time_units
3+
type: unit
4+
tests:
5+
utilities.time_units.z_tmcvt: {}

0 commit comments

Comments
 (0)