1 /*
2  * Copyright (c) 2021-2023 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 <gtest/gtest.h>
17 #include "sampling.h"
18 
19 using namespace testing::ext;
20 
21 namespace {
22 
23 class SamplerTest : public ::testing::Test {
24 public:
SetUpTestCase()25     static void SetUpTestCase() {}
TearDownTestCase()26     static void TearDownTestCase() {}
SetUp()27     void SetUp() {}
TearDown()28     void TearDown() {}
29 };
30 
31 /*
32  * @tc.name: Sampler
33  * @tc.desc: test Sampler::InitSampling with normal case.
34  * @tc.type: FUNC
35  */
HWTEST_F(SamplerTest, InitSampling001, TestSize.Level1)36 HWTEST_F(SamplerTest, InitSampling001, TestSize.Level1)
37 {
38     Sampling sampler;
39     sampler.InitSampling(512);
40     EXPECT_EQ(sampler.StartSampling(1024), 1024u);
41 }
42 
43 /*
44  * @tc.name: Sampler
45  * @tc.desc: test Sampler::InitSampling with abnormal case.
46  * @tc.type: FUNC
47  */
HWTEST_F(SamplerTest, InitSampling002, TestSize.Level1)48 HWTEST_F(SamplerTest, InitSampling002, TestSize.Level1)
49 {
50     Sampling sampler;
51     sampler.SetRandomEngineSeed(1);
52     sampler.InitSampling(512);
53     EXPECT_EQ(sampler.StartSampling(511), 512u);
54 }
55 
56 /*
57  * @tc.name: Sampler
58  * @tc.desc: test Sampler::InitSampling with boundary case.
59  * @tc.type: FUNC
60  */
HWTEST_F(SamplerTest, InitSampling003, TestSize.Level1)61 HWTEST_F(SamplerTest, InitSampling003, TestSize.Level1)
62 {
63     Sampling sampler;
64     sampler.InitSampling(1);
65     EXPECT_EQ(sampler.StartSampling(1), 1u);
66     EXPECT_EQ(sampler.StartSampling(5), 5u);
67     EXPECT_EQ(sampler.StartSampling(9), 9u);
68 }
69 
70 /*
71  * @tc.name: Sampler
72  * @tc.desc: test Sampler::Reset with normal case.
73  * @tc.type: FUNC
74  */
HWTEST_F(SamplerTest, Reset, TestSize.Level1)75 HWTEST_F(SamplerTest, Reset, TestSize.Level1)
76 {
77     Sampling sampler;
78     sampler.InitSampling(4096);
79     sampler.Reset();
80     EXPECT_EQ(sampler.GetSampleInterval(), 0);
81 }
82 } // namespace