Skip to content

Commit 4682cf1

Browse files
mniestrojnashif
authored andcommitted
tests: lib: time: add test for time() function
Add test cases for time() function. Signed-off-by: Marcin Niestroj <[email protected]>
1 parent 064c6ef commit 4682cf1

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

tests/lib/time/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(time)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})

tests/lib/time/prj.conf

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_POSIX_CLOCK=y

tests/lib/time/src/main.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021 Golioth, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <ztest.h>
8+
#include <time.h>
9+
10+
static void test_time_passing(void)
11+
{
12+
time_t time_initial_unaligned;
13+
time_t time_initial;
14+
time_t time_current;
15+
int i;
16+
17+
time_initial_unaligned = time(NULL);
18+
zassert_true(time_initial_unaligned >= 0, "Fail to get time");
19+
20+
/* Wait until time() will return new value, which should be aligned */
21+
for (i = 0; i < 100; i++) {
22+
k_sleep(K_MSEC(10));
23+
24+
if (time(NULL) != time_initial_unaligned) {
25+
break;
26+
}
27+
}
28+
29+
time_initial = time(NULL);
30+
zassert_equal(time_initial, time_initial_unaligned + 1,
31+
"Time (%d) should be one second larger than initially (%d)",
32+
time_initial, time_initial_unaligned);
33+
34+
for (i = 1; i <= 10; i++) {
35+
k_sleep(K_SECONDS(1));
36+
37+
time_current = time(NULL);
38+
zassert_equal(time_current, time_initial + i,
39+
"Current time (%d) does not match expected time (%d)",
40+
(int) time_current, (int) (time_initial + i));
41+
}
42+
}
43+
44+
static void test_time_param(void)
45+
{
46+
time_t time_result;
47+
time_t time_param;
48+
int i;
49+
50+
time_result = time(&time_param);
51+
52+
zassert_equal(time_result, time_param,
53+
"time() result does not match param value");
54+
55+
for (i = 0; i < 10; i++) {
56+
k_sleep(K_SECONDS(1));
57+
58+
zassert_equal(time_result, time_param,
59+
"time() result does not match param value");
60+
}
61+
}
62+
63+
void test_main(void)
64+
{
65+
ztest_test_suite(libc_time,
66+
ztest_unit_test(test_time_passing),
67+
ztest_unit_test(test_time_param)
68+
);
69+
ztest_run_test_suite(libc_time);
70+
}

tests/lib/time/testcase.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tests:
2+
libraries.libc.time:
3+
tags: libc
4+
filter: not CONFIG_ARCH_POSIX or CONFIG_NATIVE_POSIX_SLOWDOWN_TO_REAL_TIME
5+
integration_platforms:
6+
- mps2_an385

0 commit comments

Comments
 (0)