1 /*
2 * Copyright (c) 2024 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 "res_sched_time_util_test.h"
17
18 #include "res_sched_time_util.h"
19
20 using namespace std;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace ResourceSchedule {
25 namespace {
26 const int64_t MAX_DIFF_MS = 200;
27 const int64_t ONE_MS_TO_US = 1000;
28 const int64_t ONE_S_TO_MS = 1000;
29 }
30
SetUpTestCase()31 void ResSchedTimeUtilTest::SetUpTestCase() {}
32
TearDownTestCase()33 void ResSchedTimeUtilTest::TearDownTestCase() {}
34
SetUp()35 void ResSchedTimeUtilTest::SetUp() {}
36
TearDown()37 void ResSchedTimeUtilTest::TearDown() {}
38
39 /**
40 * @tc.name: ResSchedTimeUtilTest GetNowMillTime_001
41 * @tc.desc: test GetNowMillTime
42 * @tc.type: FUNC
43 * @tc.require: issueIB8Y0E
44 */
45 HWTEST_F(ResSchedTimeUtilTest, GetNowMillTime_001, Function | MediumTest | Level0)
46 {
47 int64_t start = ResCommonUtil::GetNowMillTime();
48 int64_t end = ResCommonUtil::GetNowMillTime();
49 int64_t diff = end - start;
50 EXPECT_TRUE(diff >= 0 && diff <= MAX_DIFF_MS);
51 start = ResCommonUtil::GetNowMillTime(true);
52 sleep(1);
53 end = ResCommonUtil::GetNowMillTime(true);
54 diff = end - start - ONE_S_TO_MS;
55 EXPECT_TRUE(diff >= 0 && diff <= MAX_DIFF_MS);
56 }
57
58 /**
59 * @tc.name: ResSchedTimeUtilTest GetNowMicroTime_001
60 * @tc.desc: test GetNowMicroTime
61 * @tc.type: FUNC
62 * @tc.require: issueIB8Y0E
63 */
64 HWTEST_F(ResSchedTimeUtilTest, GetNowMicroTime_001, Function | MediumTest | Level0)
65 {
66 int64_t start = ResCommonUtil::GetNowMicroTime();
67 int64_t end = ResCommonUtil::GetNowMicroTime();
68 int64_t diff = end - start;
69 EXPECT_TRUE(diff >= 0 && diff <= (MAX_DIFF_MS * ONE_MS_TO_US));
70 start = ResCommonUtil::GetNowMicroTime(true);
71 sleep(1);
72 end = ResCommonUtil::GetNowMicroTime(true);
73 diff = end - start - (ONE_S_TO_MS * ONE_MS_TO_US);
74 EXPECT_TRUE(diff >= 0 && diff <= (MAX_DIFF_MS * ONE_MS_TO_US));
75 }
76
77 /**
78 * @tc.name: ResSchedTimeUtilTest ConvertTimestampToStr_001
79 * @tc.desc: test ConvertTimestampToStr
80 * @tc.type: FUNC
81 * @tc.require: issueIB8Y0E
82 */
83 HWTEST_F(ResSchedTimeUtilTest, ConvertTimestampToStr_001, Function | MediumTest | Level0)
84 {
85 EXPECT_NE(ResCommonUtil::ConvertTimestampToStr(ResCommonUtil::GetCurrentTimestamp()), "");
86 }
87
88 /**
89 * @tc.name: ResSchedTimeUtilTest GetNextMillTimeStamp_001
90 * @tc.desc: test GetNextMillTimeStamp
91 * @tc.type: FUNC
92 * @tc.require: issueIB8Y0E
93 */
94 HWTEST_F(ResSchedTimeUtilTest, GetNextMillTimeStamp_001, Function | MediumTest | Level0)
95 {
96 int64_t currentTime = ResCommonUtil::GetNowMillTime(true);
97 int64_t nextTime = ResCommonUtil::GetNextMillTimeStamp(100, true);
98 int64_t diff = nextTime - currentTime - 100;
99 EXPECT_TRUE(diff >= 0 && diff <= MAX_DIFF_MS);
100 }
101 } // namespace ResourceSchedule
102 } // namespace OHOS
103