tests: test the timepoint API

This tests sys_timepoint_calc(), sys_timepoint_timeout() and
sys_timepoint_expired().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2023-07-10 18:18:50 -04:00 committed by Carles Cufí
commit 3c9249cedc
5 changed files with 53 additions and 21 deletions

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(timer_timepoints)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 BayLibre SAS
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
ZTEST(timepoints, test_timepoint_api)
{
k_timepoint_t timepoint;
k_timeout_t timeout, remaining;
timeout = K_NO_WAIT;
timepoint = sys_timepoint_calc(timeout);
zassert_true(sys_timepoint_expired(timepoint));
remaining = sys_timepoint_timeout(timepoint);
zassert_true(K_TIMEOUT_EQ(remaining, K_NO_WAIT));
timeout = K_FOREVER;
timepoint = sys_timepoint_calc(timeout);
zassert_false(sys_timepoint_expired(timepoint));
remaining = sys_timepoint_timeout(timepoint);
zassert_true(K_TIMEOUT_EQ(remaining, K_FOREVER));
timeout = K_SECONDS(1);
timepoint = sys_timepoint_calc(timeout);
zassert_false(sys_timepoint_expired(timepoint));
remaining = sys_timepoint_timeout(timepoint);
zassert_true(remaining.ticks <= timeout.ticks && remaining.ticks != 0);
k_sleep(K_MSEC(1100));
zassert_true(sys_timepoint_expired(timepoint));
remaining = sys_timepoint_timeout(timepoint);
zassert_true(K_TIMEOUT_EQ(remaining, K_NO_WAIT));
}
ZTEST_SUITE(timepoints, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,5 @@
tests:
kernel.timer.timepoints:
tags:
- kernel
- timer

View file

@ -309,27 +309,6 @@ ZTEST_USER(timer_api_error, test_timer_add_timeout)
ztest_test_pass();
}
extern uint64_t sys_clock_timeout_end_calc(k_timeout_t timeout);
extern void sys_clock_announce(int32_t ticks);
ZTEST(timer_api_error, test_timeout_end_calc)
{
int ret;
k_timeout_t timeout;
timeout = K_MSEC(DURATION);
ret = sys_clock_timeout_end_calc(timeout);
zassert_true(ret, "---timeout end calc---");
timeout.ticks = TEST_TIMEOUT;
ret = sys_clock_timeout_end_calc(timeout);
zassert_true(ret, "timeout end calc error");
timeout = K_FOREVER;
ret = sys_clock_timeout_end_calc(timeout);
zassert_true(ret, "timeout end calc forever");
}
/**
* @brief Tests for the Timer kernel object
* @defgroup kernel_timer_tests Timer