1 /**
2 * Copyright (c) 2021-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 "libpandabase/utils/utils.h"
17 #include "include/histogram-inl.h"
18 #include "include/runtime.h"
19 #include <gtest/gtest.h>
20
21 namespace ark::test {
22
23 class HistogramTest : public testing::Test {
24 public:
HistogramTest()25 HistogramTest()
26 {
27 RuntimeOptions options;
28 options.SetShouldLoadBootPandaFiles(false);
29 options.SetShouldInitializeIntrinsics(false);
30 Runtime::Create(options);
31 thread_ = ark::MTManagedThread::GetCurrent();
32 thread_->ManagedCodeBegin();
33 }
34
~HistogramTest()35 ~HistogramTest() override
36 {
37 thread_->ManagedCodeEnd();
38 Runtime::Destroy();
39 }
40
41 NO_COPY_SEMANTIC(HistogramTest);
42 NO_MOVE_SEMANTIC(HistogramTest);
43
44 template <class Value>
CompareTwoHistogram(const Histogram<Value> & lhs,const Histogram<Value> & rhs)45 void CompareTwoHistogram(const Histogram<Value> &lhs, const Histogram<Value> &rhs)
46 {
47 ASSERT_EQ(lhs.GetSum(), rhs.GetSum());
48 ASSERT_EQ(lhs.GetMin(), rhs.GetMin());
49 ASSERT_EQ(lhs.GetMax(), rhs.GetMax());
50 ASSERT_EQ(lhs.GetAvg(), rhs.GetAvg());
51 ASSERT_EQ(lhs.GetCount(), rhs.GetCount());
52 }
53
54 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
55 struct IntWrapper {
56 IntWrapper() = default;
57 ~IntWrapper() = default;
IntWrapperark::test::HistogramTest::IntWrapper58 explicit IntWrapper(int newElement) : element_(newElement) {}
59 IntWrapper(const IntWrapper &newWrapper) = default;
60 IntWrapper &operator=(const IntWrapper &newWrapper) = default;
operator <ark::test::HistogramTest::IntWrapper61 bool operator<(const IntWrapper &otherWrapper) const
62 {
63 return element_ < otherWrapper.element_;
64 }
operator ==ark::test::HistogramTest::IntWrapper65 bool operator==(const IntWrapper &otherWrapper) const
66 {
67 return element_ == otherWrapper.element_;
68 }
operator /ark::test::HistogramTest::IntWrapper69 double operator/(double divider) const
70 {
71 return element_ / divider;
72 }
operator +ark::test::HistogramTest::IntWrapper73 IntWrapper operator+(const IntWrapper &otherWrapper) const
74 {
75 return IntWrapper(element_ + otherWrapper.element_);
76 }
operator +=ark::test::HistogramTest::IntWrapper77 void operator+=(const IntWrapper &otherWrapper)
78 {
79 element_ += otherWrapper.element_;
80 }
operator *ark::test::HistogramTest::IntWrapper81 IntWrapper operator*(const IntWrapper &otherWrapper) const
82 {
83 return IntWrapper(element_ * otherWrapper.element_);
84 }
85
operator <<ark::test::HistogramTest::IntWrapper86 std::ostream &operator<<(std::ostream &os)
87 {
88 return os << element_;
89 }
90
91 private:
92 int element_ {};
93 };
94
95 private:
96 ark::MTManagedThread *thread_;
97 };
98
99 // NOLINTBEGIN(readability-magic-numbers)
100
TEST_F(HistogramTest,SimpleIntTest)101 TEST_F(HistogramTest, SimpleIntTest)
102 {
103 std::vector<int> simpleVector = {1_I, 1515_I, -12_I, 130_I, -1_I, 124_I, 0_I};
104 Histogram<int> hist;
105 for (auto element : simpleVector) {
106 hist.AddValue(element);
107 }
108 CompareTwoHistogram(hist, Histogram<int>(simpleVector.begin(), simpleVector.end()));
109 ASSERT_EQ(hist.GetSum(), 1757_I);
110 ASSERT_EQ(hist.GetMin(), -12_I);
111 ASSERT_EQ(hist.GetMax(), 1515_I);
112 ASSERT_EQ(hist.GetAvg(), 251_I);
113 ASSERT_EQ(hist.GetDispersion(), 269520_I);
114 ASSERT_EQ(hist.GetCount(), 7_I);
115 }
116
TEST_F(HistogramTest,IntWrapperTest)117 TEST_F(HistogramTest, IntWrapperTest)
118 {
119 Histogram<IntWrapper> hist;
120 std::vector<int> simpleVector = {1_I, 1515_I, -12_I, 129_I, 0_I, 124_I, 0_I};
121 for (auto element : simpleVector) {
122 hist.AddValue(IntWrapper(element));
123 }
124 ASSERT_EQ(hist.GetSum(), IntWrapper(1757_I));
125 ASSERT_EQ(hist.GetMin(), IntWrapper(-12_I));
126 ASSERT_EQ(hist.GetMax(), IntWrapper(1515_I));
127 ASSERT_EQ(hist.GetAvg(), 251_I);
128 ASSERT_EQ(hist.GetCount(), 7_I);
129 }
130
TEST_F(HistogramTest,CompareTwoDifferentTest)131 TEST_F(HistogramTest, CompareTwoDifferentTest)
132 {
133 std::vector<int> simpleVectorFirst = {1_I, 1515_I, -12_I, 129_I, 0_I, 124_I, 0_I};
134 std::vector<int> simpleVectorSecond = {1_I, 1515_I, -12_I, 130_I, 3_I, 120_I, 0_I};
135 Histogram<int> histFirst(simpleVectorFirst.begin(), simpleVectorFirst.end());
136 Histogram<int> histSecond(simpleVectorSecond.begin(), simpleVectorSecond.end());
137 CompareTwoHistogram(histFirst, histSecond);
138 }
139
TEST_F(HistogramTest,CompareDifferentTypeTest)140 TEST_F(HistogramTest, CompareDifferentTypeTest)
141 {
142 std::unordered_set<int> simpleSetFirst = {1_I, 1515_I, -12_I, 130_I, -1_I, 124_I, 0_I};
143 PandaSet<int> pandaSetFirst = {1_I, 1515_I, -12_I, 129_I, 2_I, 122_I, 0_I};
144
145 std::vector<int> simpleVectorSecond = {1_I, 1515_I, -12_I, 129_I, 0_I, 124_I, 0_I};
146 PandaVector<int> pandaVectorFirst = {5_I, 1515_I, -12_I, 128_I, -3_I, 124_I, 0_I};
147
148 Histogram<int> histFirst(simpleSetFirst.begin(), simpleSetFirst.end());
149 Histogram<int> histSecond(pandaSetFirst.begin(), pandaSetFirst.end());
150 Histogram<int> histThird(simpleVectorSecond.begin(), simpleVectorSecond.end());
151 Histogram<int> histFourth(pandaVectorFirst.begin(), pandaVectorFirst.end());
152
153 CompareTwoHistogram(histFirst, histSecond);
154 CompareTwoHistogram(histFirst, histThird);
155 CompareTwoHistogram(histFirst, histFourth);
156 CompareTwoHistogram(histSecond, histThird);
157 CompareTwoHistogram(histSecond, histFourth);
158 CompareTwoHistogram(histThird, histFourth);
159 }
160
TEST_F(HistogramTest,CheckGetTopDumpTest)161 TEST_F(HistogramTest, CheckGetTopDumpTest)
162 {
163 std::vector<int> simpleVector = {1_I, 1_I, 0_I, 12_I, 0_I, 1_I, 12_I};
164 Histogram<int> hist(simpleVector.begin(), simpleVector.end());
165 ASSERT_EQ(hist.GetTopDump(), "0:2,1:3,12:2");
166 ASSERT_EQ(hist.GetTopDump(2U), "0:2,1:3");
167 ASSERT_EQ(hist.GetTopDump(1U), "0:2");
168 ASSERT_EQ(hist.GetTopDump(0U), "");
169 }
170
171 // NOLINTEND(readability-magic-numbers)
172
173 } // namespace ark::test
174