diff --git a/tests/unit/time_units/CMakeLists.txt b/tests/unit/time_units/CMakeLists.txt new file mode 100644 index 00000000000..78a128c72f1 --- /dev/null +++ b/tests/unit/time_units/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright 2022 Meta +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(time_units) + +FILE(GLOB app_sources *.c) +target_sources(testbinary PRIVATE ${app_sources}) diff --git a/tests/unit/time_units/main.c b/tests/unit/time_units/main.c new file mode 100644 index 00000000000..83e480cf8cf --- /dev/null +++ b/tests/unit/time_units/main.c @@ -0,0 +1,9 @@ +/* + * Copyright 2022 Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include + +ZTEST_SUITE(time_units, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/unit/time_units/overflow.c b/tests/unit/time_units/overflow.c new file mode 100644 index 00000000000..bc2deebc503 --- /dev/null +++ b/tests/unit/time_units/overflow.c @@ -0,0 +1,56 @@ +/* + * Copyright 2022 Meta + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +#include +#include + +/** + * @brief Test @ref z_tmcvt for robustness against intermediate value overflow. + * + * With input + * ``` + * [t0, t1, t2] = [ + * UINT64_MAX / to_hz - 1, + * UINT64_MAX / to_hz, + * UINT64_MAX / to_hz + 1, + * ] + * ``` + * + * passed through @ref z_tmcvt, we expect a linear sequence: + * ``` + * [ + * 562949953369140, + * 562949953399658, + * 562949953430175, + * ] + * ``` + * + * If an overflow occurs, we see something like the following: + * ``` + * [ + * 562949953369140, + * 562949953399658, + * 8863, + * ] + * ``` + */ +ZTEST(time_units, test_z_tmcvt_for_overflow) +{ + const uint32_t from_hz = 32768UL; + const uint32_t to_hz = 1000000000UL; + + zassert_equal(562949953369140ULL, + z_tmcvt(UINT64_MAX / to_hz - 1, from_hz, to_hz, true, false, false, false)); + zassert_equal(562949953399658ULL, + z_tmcvt(UINT64_MAX / to_hz, from_hz, to_hz, true, false, false, false)); + zassert_equal(562949953430175ULL, + z_tmcvt(UINT64_MAX / to_hz + 1, from_hz, to_hz, true, false, false, false)); +} diff --git a/tests/unit/time_units/prj.conf b/tests/unit/time_units/prj.conf new file mode 100644 index 00000000000..9228251051e --- /dev/null +++ b/tests/unit/time_units/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y diff --git a/tests/unit/time_units/testcase.yaml b/tests/unit/time_units/testcase.yaml new file mode 100644 index 00000000000..4624ce7143b --- /dev/null +++ b/tests/unit/time_units/testcase.yaml @@ -0,0 +1,5 @@ +common: + tags: time_units + type: unit +tests: + utilities.time_units.z_tmcvt: {}