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 <climits>
17 #include <iostream>
18 #include <unistd.h>
19 
20 #include "cpu_collector.h"
21 
22 #include <gtest/gtest.h>
23 
24 using namespace testing::ext;
25 using namespace OHOS::HiviewDFX;
26 using namespace OHOS::HiviewDFX::UCollectClient;
27 using namespace OHOS::HiviewDFX::UCollect;
28 
29 namespace {
30     constexpr int CPU_USAGE_MIN_VALUE = 0;
31     constexpr int CPU_USAGE_MAX_VALUE = 1;
32 }
33 
34 class CpuCollectorTest : public testing::Test {
35 public:
SetUp()36     void SetUp() {};
TearDown()37     void TearDown() {};
SetUpTestCase()38     static void SetUpTestCase() {};
TearDownTestCase()39     static void TearDownTestCase() {};
40 };
41 
42 /**
43  * @tc.name: CpuCollectorTest01
44  * @tc.desc: used to test the function of CpuCollector.GetSysCpuUsage;
45  * @tc.type: FUNC
46 */
47 HWTEST_F(CpuCollectorTest, CpuCollectorTest01, TestSize.Level1)
48 {
49     std::shared_ptr<CpuCollector> collector = CpuCollector::Create();
50     auto collectResult = collector->GetSysCpuUsage();
51     ASSERT_TRUE(collectResult.retCode == UcError::SUCCESS);
52     ASSERT_TRUE(collectResult.data >= CPU_USAGE_MIN_VALUE && collectResult.data <= CPU_USAGE_MAX_VALUE);
53 }