• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <errno.h>
2 #include <gtest/gtest.h>
3 #include <thread>
4 using namespace testing::ext;
5 
6 class SysClockTest : public testing::Test {
SetUp()7     void SetUp() override {}
TearDown()8     void TearDown() override {}
9 };
10 
11 /**
12  * @tc.name: clock_getres_001
13  * @tc.desc: Ensure that the clock_getres function returns the expected time precision when the CLOCK_REALTIME
14  *           parameter is used. By passing this test, it confirms that the time precision of the system's real-time
15  *           clock (CLOCK_REALTIME) meets the expected requirements.
16  * @tc.type: FUNC
17  **/
18 HWTEST_F(SysClockTest, clock_getres_001, TestSize.Level1)
19 {
20     timespec times;
21     int result = clock_getres(CLOCK_REALTIME, &times);
22     EXPECT_EQ(0, result);
23     EXPECT_EQ(0, times.tv_sec);
24     EXPECT_EQ(1, times.tv_nsec);
25 }
26 
27 /**
28  * @tc.name: clock_getres_002
29  * @tc.desc: Ensure that the clock_getres function returns the expected time precision when the CLOCK_MONOTONIC
30  *           parameter is used.
31  * @tc.type: FUNC
32  **/
33 HWTEST_F(SysClockTest, clock_getres_002, TestSize.Level1)
34 {
35     timespec times;
36     int result = clock_getres(CLOCK_MONOTONIC, &times);
37     EXPECT_EQ(0, result);
38     EXPECT_EQ(0, times.tv_sec);
39     EXPECT_EQ(1, times.tv_nsec);
40 }
41 
42 /**
43  * @tc.name: clock_getres_003
44  * @tc.desc: Ensure that the clock_getres function can successfully execute with the CLOCK_PROCESS_CPUTIME_ID
45  *           parameter without causing any errors or exceptions.
46  * @tc.type: FUNC
47  **/
48 HWTEST_F(SysClockTest, clock_getres_003, TestSize.Level1)
49 {
50     timespec times;
51     int result = clock_getres(CLOCK_PROCESS_CPUTIME_ID, &times);
52     EXPECT_EQ(0, result);
53 }
54 
55 /**
56  * @tc.name: clock_getres_004
57  * @tc.desc: Ensure that the clock_getres function can successfully execute with the CLOCK_THREAD_CPUTIME_ID parameter
58  *           without causing any errors or exceptions.
59  * @tc.type: FUNC
60  **/
61 HWTEST_F(SysClockTest, clock_getres_004, TestSize.Level1)
62 {
63     timespec times;
64     int result = clock_getres(CLOCK_THREAD_CPUTIME_ID, &times);
65     EXPECT_EQ(0, result);
66 }
67 /**
68  * @tc.name: clock_getres_005
69  * @tc.desc: Verify that the resolution of the CLOCK_MONOTONIC_RAW clock is 0 seconds and 1 nanosecond, as expected.
70  * @tc.type: FUNC
71  **/
72 HWTEST_F(SysClockTest, clock_getres_005, TestSize.Level1)
73 {
74     timespec times;
75     int result = clock_getres(CLOCK_MONOTONIC_RAW, &times);
76     EXPECT_EQ(0, result);
77     EXPECT_EQ(0, times.tv_sec);
78     EXPECT_EQ(1, times.tv_nsec);
79 }
80 /**
81  * @tc.name: clock_getres_006
82  * @tc.desc: Verify that the clock_getres function correctly retrieves the resolution of the specified clock and
83  *           that the resolution is as expected (0 seconds and 1 nanosecond in this case).
84  * @tc.type: FUNC
85  **/
86 HWTEST_F(SysClockTest, clock_getres_006, TestSize.Level1)
87 {
88     timespec times;
89     int result = clock_getres(CLOCK_REALTIME_COARSE, &times);
90     EXPECT_EQ(0, result);
91 }
92 /**
93  * @tc.name: clock_getres_007
94  * @tc.desc: Verify that the clock_getres() function can successfully retrieve the resolution of the specified clock
95  *           (CLOCK_MONOTONIC_COARSE in this case) and that the function call itself is successful.
96  * @tc.type: FUNC
97  **/
98 HWTEST_F(SysClockTest, clock_getres_007, TestSize.Level1)
99 {
100     timespec times;
101     int result = clock_getres(CLOCK_MONOTONIC_COARSE, &times);
102     EXPECT_EQ(0, result);
103 }
104 
105 /**
106  * @tc.name: clock_getres_008
107  * @tc.desc: Verify that the clock_getres() function can successfully retrieve the resolution of the
108  *           specified clock (CLOCK_BOOTTIME in this case), and it checks if the retrieved resolution matches the
109  *           expected values of 0 seconds and 1 nanosecond.
110  * @tc.type: FUNC
111  **/
112 HWTEST_F(SysClockTest, clock_getres_008, TestSize.Level1)
113 {
114     timespec times;
115     int result = clock_getres(CLOCK_BOOTTIME, &times);
116     EXPECT_EQ(0, result);
117     EXPECT_EQ(0, times.tv_sec);
118     EXPECT_EQ(1, times.tv_nsec);
119 }
120 /**
121  * @tc.name: clock_getres_009
122  * @tc.desc: Verify that the clock_getres() function can successfully retrieve the resolution of the
123  *           specified clock (CLOCK_REALTIME_ALARM in this case), and it checks if the retrieved resolution matches
124  *           the expected values of 0 seconds and 1 nanosecond.
125  * @tc.type: FUNC
126  **/
127 HWTEST_F(SysClockTest, clock_getres_009, TestSize.Level1)
128 {
129     timespec times;
130     int result = clock_getres(CLOCK_REALTIME_ALARM, &times);
131     EXPECT_EQ(0, result);
132     EXPECT_EQ(0, times.tv_sec);
133     EXPECT_EQ(1, times.tv_nsec);
134 }
135 /**
136  * @tc.name: clock_getres_010
137  * @tc.desc: verifies that the clock_getres() function can successfully retrieve the resolution of the specified
138  *           clock (CLOCK_BOOTTIME_ALARM in this case), and it checks if the retrieved resolution matches the
139  *           expected values of 0 seconds and 1 nanosecond.
140  * @tc.type: FUNC
141  **/
142 HWTEST_F(SysClockTest, clock_getres_010, TestSize.Level1)
143 {
144     timespec times;
145     int result = clock_getres(CLOCK_BOOTTIME_ALARM, &times);
146     EXPECT_EQ(0, result);
147     EXPECT_EQ(0, times.tv_sec);
148     EXPECT_EQ(1, times.tv_nsec);
149 }
150 /**
151  * @tc.name: clock_getres_011
152  * @tc.desc: verifies that the clock_getres() function can successfully retrieve the resolution of the specified
153  *           clock (CLOCK_TAI in this case), and it checks if the retrieved resolution matches the expected
154  *           values of 0 seconds and 1 nanosecond.
155  * @tc.type: FUNC
156  **/
157 HWTEST_F(SysClockTest, clock_getres_011, TestSize.Level1)
158 {
159     timespec times;
160     int result = clock_getres(CLOCK_TAI, &times);
161     EXPECT_EQ(0, result);
162     EXPECT_EQ(0, times.tv_sec);
163     EXPECT_EQ(1, times.tv_nsec);
164 }
165 
166 /**
167  * @tc.name: clock_getres_012
168  * @tc.desc: Confirm that when an invalid clock ID is passed to the clock_getres function, the function handles
169  *           the error correctly by returning -1 and setting errno to EINVAL.
170  * @tc.type: FUNC
171  **/
172 HWTEST_F(SysClockTest, clock_getres_012, TestSize.Level1)
173 {
174     errno = 0;
175     timespec times = { -10, -10 };
176     int result = clock_getres(-1, &times);
177     EXPECT_EQ(-1, result);
178     EXPECT_EQ(EINVAL, errno);
179     EXPECT_EQ(-10, times.tv_sec);
180     EXPECT_EQ(-10, times.tv_nsec);
181 }
182 
183 /**
184  * @tc.name: clock_gettime_001
185  * @tc.desc: Confirm that the clock_gettime function can obtain the current time accurately from the CLOCK_MONOTONIC
186  *           clock ID, and that the provided timestamps are precise and consistent.
187  * @tc.type: FUNC
188  **/
189 HWTEST_F(SysClockTest, clock_gettime_001, TestSize.Level1)
190 {
191     timespec ts1;
192     int result = clock_gettime(CLOCK_MONOTONIC, &ts1);
193     EXPECT_EQ(result, 0);
194 
195     timespec ts2;
196     result = clock_gettime(CLOCK_MONOTONIC, &ts2);
197     EXPECT_EQ(result, 0);
198     auto start = std::chrono::seconds(ts1.tv_sec) + std::chrono::nanoseconds(ts1.tv_nsec);
199     auto end = std::chrono::seconds(ts2.tv_sec) + std::chrono::nanoseconds(ts2.tv_nsec);
200     auto duration = end - start;
201     auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
202     auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count() % 1000000000;
203 
204     EXPECT_EQ(seconds, 0);
205     EXPECT_GT(10'000'000, nanoseconds);
206 }
207 
208 /**
209  * @tc.name: clock_gettime_002
210  * @tc.desc: Confirm that the clock_gettime function can obtain the current time accurately from the CLOCK_REALTIME
211  *           clock ID. The CLOCK_REALTIME clock represents the system's best-guess time since the epoch (typically
212  *           the system's startup time) and is affected by changes in the system time.
213  * @tc.type: FUNC
214  **/
215 HWTEST_F(SysClockTest, clock_gettime_002, TestSize.Level1)
216 {
217     timespec times;
218     int result = clock_gettime(CLOCK_REALTIME, &times);
219     EXPECT_EQ(0, result);
220 }
221 
222 /**
223  * @tc.name: clock_gettime_003
224  * @tc.desc: Confirm that the clock_gettime function can obtain the current time accurately from the CLOCK_MONOTONIC
225  *           clock ID. The CLOCK_MONOTONIC clock represents the monotonic time since some unspecified starting point,
226  *           which is immune to changes in the system time and unaffected by adjustments to the system clock.
227  * @tc.type: FUNC
228  **/
229 HWTEST_F(SysClockTest, clock_gettime_003, TestSize.Level1)
230 {
231     timespec times;
232     int result = clock_gettime(CLOCK_MONOTONIC, &times);
233     EXPECT_EQ(0, result);
234 }
235 
236 /**
237  * @tc.name: clock_gettime_004
238  * @tc.desc: Confirm that the clock_gettime function can obtain the process CPU time accurately from the
239  *           CLOCK_PROCESS_CPUTIME_ID clock ID. This clock represents the CPU time used by the calling process, and
240  *           each process has its own instance of this clock.
241  * @tc.type: FUNC
242  **/
243 HWTEST_F(SysClockTest, clock_gettime_004, TestSize.Level1)
244 {
245     timespec times;
246     int result = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &times);
247     EXPECT_EQ(0, result);
248 }
249 
250 /**
251  * @tc.name: clock_gettime_005
252  * @tc.desc: Confirm that the clock_gettime function can obtain the thread CPU time accurately from the
253  *           CLOCK_THREAD_CPUTIME_ID clock ID. This clock represents the CPU time used by the calling thread, and each
254  *           thread has its own instance of this clock.
255  * @tc.type: FUNC
256  **/
257 HWTEST_F(SysClockTest, clock_gettime_005, TestSize.Level1)
258 {
259     timespec times;
260     int result = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &times);
261     EXPECT_EQ(0, result);
262 }
263 
264 /**
265  * @tc.name: clock_gettime_006
266  * @tc.desc: Confirm that the clock_gettime function can obtain the boot time accurately from the CLOCK_BOOTTIME
267  *           clock ID. This clock represents the system-wide monotonic boot time, including time spent in sleep, and
268  *           is immune to changes in the system time.
269  * @tc.type: FUNC
270  **/
271 HWTEST_F(SysClockTest, clock_gettime_006, TestSize.Level1)
272 {
273     timespec times;
274     int result = clock_gettime(CLOCK_BOOTTIME, &times);
275     EXPECT_EQ(0, result);
276 }
277 
278 /**
279  * @tc.name: clock_gettime_007
280  * @tc.desc: Aim to validate the error handling mechanism of clock_gettime when an invalid clock ID is encountered,
281  *           ensuring the function behaves as expected in such scenarios.
282  * @tc.type: FUNC
283  **/
284 HWTEST_F(SysClockTest, clock_gettime_007, TestSize.Level1)
285 {
286     errno = 0;
287     timespec times;
288     int result = clock_gettime(-1, &times);
289     EXPECT_EQ(-1, result);
290     EXPECT_EQ(EINVAL, errno);
291 }
292 
293 /**
294  * @tc.name: clock_settime_001
295  * @tc.desc: Aim to validate the error handling mechanism of the clock_settime function when an invalid clock ID is
296  *           encountered, ensuring the function behaves as expected in such scenarios.
297  * @tc.type: FUNC
298  **/
299 HWTEST_F(SysClockTest, clock_settime_001, TestSize.Level1)
300 {
301     errno = 0;
302     timespec times;
303     int result = clock_settime(-1, &times);
304     EXPECT_EQ(-1, result);
305     EXPECT_EQ(EINVAL, errno);
306 }
307 
308 /**
309  * @tc.name: clock_nanosleep_001
310  * @tc.desc: Aim to assess the accuracy and reliability of the clock_nanosleep function by measuring the elapsed
311  *           time between two steady clock readings and confirming that it matches the expected sleep duration.
312  * @tc.type: FUNC
313  **/
314 HWTEST_F(SysClockTest, clock_nanosleep_001, TestSize.Level1)
315 {
316     auto time0 = std::chrono::steady_clock::now();
317     const timespec req = { .tv_nsec = 5000000 };
318     int result = clock_nanosleep(CLOCK_MONOTONIC, 0, &req, nullptr);
319     EXPECT_EQ(0, result);
320     auto time1 = std::chrono::steady_clock::now();
321     EXPECT_GE(std::chrono::duration_cast<std::chrono::nanoseconds>(time1 - time0).count(), 5000000);
322 }
323 
324 /**
325  * @tc.name: clock_nanosleep_002
326  * @tc.desc: Validate the error handling mechanism of the clock_nanosleep function when an invalid clock ID is
327  *           encountered, ensuring the function behaves as expected in such scenarios.
328  * @tc.type: FUNC
329  **/
330 HWTEST_F(SysClockTest, clock_nanosleep_002, TestSize.Level1)
331 {
332     timespec req;
333     timespec rem;
334     int err = clock_nanosleep(-1, 0, &req, &rem);
335     EXPECT_EQ(EINVAL, err);
336 }
337 
338 /**
339  * @tc.name: clock_nanosleep_003
340  * @tc.desc: Validate the error handling mechanism of the clock_nanosleep function when an invalid flag is encountered,
341  *           ensuring the function behaves as expected in such scenarios.
342  * @tc.type: FUNC
343  **/
344 HWTEST_F(SysClockTest, clock_nanosleep_003, TestSize.Level1)
345 {
346     timespec req;
347     req.tv_sec = 1;
348     req.tv_nsec = 0;
349     int result = clock_nanosleep(CLOCK_THREAD_CPUTIME_ID, 0, &req, nullptr);
350     EXPECT_EQ(EINVAL, result);
351 }
352 
353 /**
354  * @tc.name: clock_getcpuclockid_001
355  * @tc.desc: Verify that clock_getcpuclockid can successfully retrieve the CPU clock ID for the current process and
356  *           subsequently use it to obtain the current time using clock_gettime.
357  * @tc.type: FUNC
358  **/
359 HWTEST_F(SysClockTest, clock_getcpuclockid_001, TestSize.Level1)
360 {
361     clockid_t id;
362     int result1 = clock_getcpuclockid(getpid(), &id);
363     EXPECT_EQ(0, result1);
364     timespec times;
365     int result2 = clock_gettime(id, &times);
366     EXPECT_EQ(0, result2);
367 }
368 
369 /**
370  * @tc.name: clock_getcpuclockid_002
371  * @tc.desc: Verify that clock_getcpuclockid can successfully retrieve the CPU clock ID for the parent process of the
372  *           current process and subsequently use it to obtain the current time using clock_gettime.
373  * @tc.type: FUNC
374  **/
375 HWTEST_F(SysClockTest, clock_getcpuclockid_002, TestSize.Level1)
376 {
377     clockid_t id;
378     int result = clock_getcpuclockid(getppid(), &id);
379     EXPECT_EQ(0, result);
380     timespec times;
381     int result2 = clock_gettime(id, &times);
382     EXPECT_EQ(0, result2);
383 }
384