1 /*
2  * Copyright (C) 2024 HiHope Open Source Organization.
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 <csignal>
17 #include <cstdlib>
18 #include <cstdio>
19 #include <ctime>
20 #include <unistd.h>
21 #include <gtest/gtest.h>
22 
23 using namespace testing::ext;
24 using namespace std;
25 
26 class ClockGetresApiTest : public testing::Test {
27 public:
28     static void SetUpTestCase();
29     static void TearDownTestCase();
30     void SetUp();
31     void TearDown();
32 private:
33 };
SetUp()34 void ClockGetresApiTest::SetUp()
35 {
36 }
TearDown()37 void ClockGetresApiTest::TearDown()
38 {
39 }
SetUpTestCase()40 void ClockGetresApiTest::SetUpTestCase()
41 {
42 }
TearDownTestCase()43 void ClockGetresApiTest::TearDownTestCase()
44 {
45 }
46 
47 /*
48  * @tc.number : SUB_KERNEL_SYSCALL_CLOCK_GETRES_0100
49  * @tc.name   : ClockGetresInvalidClockId_0001
50  * @tc.desc   : Test clock_getres with an invalid clock_id.
51  * @tc.size   : MediumTest
52  * @tc.type   : Function
53  * @tc.level  : Level 2
54  */
HWTEST_F(ClockGetresApiTest, ClockGetresInvalidClockId_0001, Function | MediumTest | Level2)55 HWTEST_F(ClockGetresApiTest, ClockGetresInvalidClockId_0001, Function | MediumTest | Level2)
56 {
57     struct timespec res;
58     clockid_t invalidClockId = -1;
59     int ret = clock_getres(invalidClockId, &res);
60 
61     EXPECT_EQ(ret, -1);
62     EXPECT_EQ(errno, EINVAL);
63 }
64 
65 /*
66  * @tc.number : SUB_KERNEL_SYSCALL_CLOCK_GETRES_0200
67  * @tc.name   : ClockGetresSuccess_0002
68  * @tc.desc   : Test clock_getres successfully gets the resolution of a valid clock.
69  * @tc.size   : MediumTest
70  * @tc.type   : Function
71  * @tc.level  : Level 1
72  */
HWTEST_F(ClockGetresApiTest, ClockGetresSuccess_0002, Function | MediumTest | Level1)73 HWTEST_F(ClockGetresApiTest, ClockGetresSuccess_0002, Function | MediumTest | Level1)
74 {
75     struct timespec res = { 0 };
76     clockid_t validClockId = CLOCK_REALTIME;
77 
78     int ret = clock_getres(validClockId, &res);
79     EXPECT_EQ(ret, 0);
80 
81     EXPECT_GE(res.tv_sec, 0);
82     EXPECT_GE(res.tv_nsec, 0);
83 }