1 /*
2 * Copyright (c) 2021 Golioth, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <time.h>
9
ZTEST(libc_time,test_time_passing)10 ZTEST(libc_time, test_time_passing)
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
ZTEST(libc_time,test_time_param)44 ZTEST(libc_time, test_time_param)
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 ZTEST_SUITE(libc_time, NULL, NULL, NULL, NULL, NULL);
64