1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hc_time_test.h"
17
18 // math.h must be included before hctest.h -> unity.h -> unity_internals.h
19 // otherwise isinf and isnan macro will be redefined
20 #include <math.h>
21
22 #include <hctest.h>
23 #include <securec.h>
24 #include <stdlib.h>
25 #include <sys/time.h>
26 #include <time.h>
27 #include <unistd.h>
28
29 #include "print_log.h"
30 #include "test_timer.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 enum {
37 SLEEP_SECONDS_MIN = 3,
38 SLEEP_SECONDS_MAX = 5,
39 TEST_TIMES = 5,
40 };
41
42 #define MILLISECONDS_DEVIATION 1.e2
43
TestTime(void)44 static void TestTime(void)
45 {
46 struct timespec start, end;
47 int res, sleepSeconds = rand() % (SLEEP_SECONDS_MAX - SLEEP_SECONDS_MIN) + SLEEP_SECONDS_MIN;
48 res = memset_s(&start, sizeof(start), 0, sizeof(start));
49 TEST_ASSERT_EQUAL(0, res);
50 res = memset_s(&end, sizeof(end), 0, sizeof(end));
51 TEST_ASSERT_EQUAL(0, res);
52
53 LOGI("test correctness");
54
55 RUN_AND_PRINT_ELAPSED_TIME(res, clock_gettime(CLOCK_MONOTONIC, &start));
56 TEST_ASSERT_EQUAL(0, res);
57
58 LOGI("begin sleeping for %d seconds", sleepSeconds);
59 sleep(sleepSeconds);
60
61 RUN_AND_PRINT_ELAPSED_TIME(res, clock_gettime(CLOCK_MONOTONIC, &end));
62 TEST_ASSERT_EQUAL(0, res);
63
64 TEST_ASSERT_LESS_THAN(MILLISECONDS_DEVIATION, fabs(CalcTimeSpecDiff(start, end) - sleepSeconds * 1000.0f));
65
66 LOGI("test precision");
67
68 res = memset_s(&start, sizeof(start), 0, sizeof(start));
69 TEST_ASSERT_EQUAL(0, res);
70 res = memset_s(&end, sizeof(end), 0, sizeof(end));
71 TEST_ASSERT_EQUAL(0, res);
72
73 RUN_AND_PRINT_ELAPSED_TIME(res, clock_gettime(CLOCK_MONOTONIC, &start));
74 TEST_ASSERT_EQUAL(0, res);
75
76 LOGI("in testing precision");
77
78 RUN_AND_PRINT_ELAPSED_TIME(res, clock_gettime(CLOCK_MONOTONIC, &end));
79 TEST_ASSERT_EQUAL(0, res);
80
81 TEST_ASSERT_LESS_THAN(MILLISECONDS_DEVIATION, fabs(CalcTimeSpecDiff(start, end)));
82 }
83
TestPrintTime(void)84 static void TestPrintTime(void)
85 {
86 for (int i = 0; i < TEST_TIMES; ++i) {
87 struct timespec start;
88 int res, sleepSeconds;
89 res = memset_s(&start, sizeof(start), 0, sizeof(start));
90 TEST_ASSERT_EQUAL(0, res);
91
92 sleepSeconds = rand() % (SLEEP_SECONDS_MAX - SLEEP_SECONDS_MIN) + SLEEP_SECONDS_MIN;
93 res = clock_gettime(CLOCK_MONOTONIC, &start);
94 TEST_ASSERT_EQUAL(0, res);
95
96 double rawTime = (start.tv_nsec / 1000.0 / 1000.0 / 1000.0) + start.tv_sec;
97 time_t timeTime = (time_t)rawTime;
98 struct tm *pts = gmtime(&timeTime);
99 TEST_ASSERT_NOT_NULL(pts);
100 LOGI("time = %d-%d-%d %d:%d:%d, %d day of week, %d days in year, isdst = %d",
101 pts->tm_year,
102 pts->tm_mon + 1,
103 pts->tm_mday,
104 pts->tm_hour,
105 pts->tm_min,
106 pts->tm_sec,
107 pts->tm_wday + 1,
108 pts->tm_yday + 1,
109 pts->tm_isdst);
110 LOGI("begin sleeping for %d seconds", sleepSeconds);
111 sleep(sleepSeconds);
112 }
113 }
114
TestHcTime(void)115 void TestHcTime(void)
116 {
117 for (int i = 0; i < TEST_TIMES; ++i) {
118 TestTime();
119 }
120
121 TestPrintTime();
122 }
123
124 #ifdef __cplusplus
125 }
126 #endif
127