tests: lib: time: add test for time() function
Add test cases for time() function. Signed-off-by: Marcin Niestroj <m.niestroj@emb.dev>
This commit is contained in:
parent
064c6ef830
commit
4682cf12bb
4 changed files with 86 additions and 0 deletions
8
tests/lib/time/CMakeLists.txt
Normal file
8
tests/lib/time/CMakeLists.txt
Normal file
|
@ -0,0 +1,8 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.13.1)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(time)
|
||||
|
||||
FILE(GLOB app_sources src/*.c)
|
||||
target_sources(app PRIVATE ${app_sources})
|
2
tests/lib/time/prj.conf
Normal file
2
tests/lib/time/prj.conf
Normal file
|
@ -0,0 +1,2 @@
|
|||
CONFIG_ZTEST=y
|
||||
CONFIG_POSIX_CLOCK=y
|
70
tests/lib/time/src/main.c
Normal file
70
tests/lib/time/src/main.c
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Golioth, Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <ztest.h>
|
||||
#include <time.h>
|
||||
|
||||
static void test_time_passing(void)
|
||||
{
|
||||
time_t time_initial_unaligned;
|
||||
time_t time_initial;
|
||||
time_t time_current;
|
||||
int i;
|
||||
|
||||
time_initial_unaligned = time(NULL);
|
||||
zassert_true(time_initial_unaligned >= 0, "Fail to get time");
|
||||
|
||||
/* Wait until time() will return new value, which should be aligned */
|
||||
for (i = 0; i < 100; i++) {
|
||||
k_sleep(K_MSEC(10));
|
||||
|
||||
if (time(NULL) != time_initial_unaligned) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
time_initial = time(NULL);
|
||||
zassert_equal(time_initial, time_initial_unaligned + 1,
|
||||
"Time (%d) should be one second larger than initially (%d)",
|
||||
time_initial, time_initial_unaligned);
|
||||
|
||||
for (i = 1; i <= 10; i++) {
|
||||
k_sleep(K_SECONDS(1));
|
||||
|
||||
time_current = time(NULL);
|
||||
zassert_equal(time_current, time_initial + i,
|
||||
"Current time (%d) does not match expected time (%d)",
|
||||
(int) time_current, (int) (time_initial + i));
|
||||
}
|
||||
}
|
||||
|
||||
static void test_time_param(void)
|
||||
{
|
||||
time_t time_result;
|
||||
time_t time_param;
|
||||
int i;
|
||||
|
||||
time_result = time(&time_param);
|
||||
|
||||
zassert_equal(time_result, time_param,
|
||||
"time() result does not match param value");
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
k_sleep(K_SECONDS(1));
|
||||
|
||||
zassert_equal(time_result, time_param,
|
||||
"time() result does not match param value");
|
||||
}
|
||||
}
|
||||
|
||||
void test_main(void)
|
||||
{
|
||||
ztest_test_suite(libc_time,
|
||||
ztest_unit_test(test_time_passing),
|
||||
ztest_unit_test(test_time_param)
|
||||
);
|
||||
ztest_run_test_suite(libc_time);
|
||||
}
|
6
tests/lib/time/testcase.yaml
Normal file
6
tests/lib/time/testcase.yaml
Normal file
|
@ -0,0 +1,6 @@
|
|||
tests:
|
||||
libraries.libc.time:
|
||||
tags: libc
|
||||
filter: not CONFIG_ARCH_POSIX or CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME
|
||||
integration_platforms:
|
||||
- mps2_an385
|
Loading…
Add table
Add a link
Reference in a new issue