• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 "include/histogram-inl.h"
17 #include "include/runtime.h"
18 #include <gtest/gtest.h>
19 
20 namespace panda::test {
21 
22 class HistogramTest : public testing::Test {
23 public:
HistogramTest()24     HistogramTest()
25     {
26         RuntimeOptions options;
27         options.SetShouldLoadBootPandaFiles(false);
28         options.SetShouldInitializeIntrinsics(false);
29         Runtime::Create(options);
30         thread_ = panda::MTManagedThread::GetCurrent();
31         thread_->ManagedCodeBegin();
32     }
33 
~HistogramTest()34     ~HistogramTest() override
35     {
36         thread_->ManagedCodeEnd();
37         Runtime::Destroy();
38     }
39 
40     template <class Value>
CompareTwoHistogram(const Histogram<Value> & lhs,const Histogram<Value> & rhs)41     void CompareTwoHistogram(const Histogram<Value> &lhs, const Histogram<Value> &rhs)
42     {
43         ASSERT_EQ(lhs.GetSum(), rhs.GetSum());
44         ASSERT_EQ(lhs.GetMin(), rhs.GetMin());
45         ASSERT_EQ(lhs.GetMax(), rhs.GetMax());
46         ASSERT_EQ(lhs.GetAvg(), rhs.GetAvg());
47         ASSERT_EQ(lhs.GetCount(), rhs.GetCount());
48     }
49 
50     struct IntWrapper {
51         int element;
IntWrapperpanda::test::HistogramTest::IntWrapper52         IntWrapper(int new_element) : element(new_element) {}
IntWrapperpanda::test::HistogramTest::IntWrapper53         IntWrapper(const IntWrapper &new_wrapper) : element(new_wrapper.element) {}
operator =panda::test::HistogramTest::IntWrapper54         IntWrapper &operator=(const IntWrapper &new_wrapper)
55         {
56             element = new_wrapper.element;
57             return *this;
58         }
operator <panda::test::HistogramTest::IntWrapper59         bool operator<(const IntWrapper &other_wrapper) const
60         {
61             return element < other_wrapper.element;
62         }
operator ==panda::test::HistogramTest::IntWrapper63         bool operator==(const IntWrapper &other_wrapper) const
64         {
65             return element == other_wrapper.element;
66         }
operator /panda::test::HistogramTest::IntWrapper67         double operator/(double divider) const
68         {
69             return element / divider;
70         }
operator +panda::test::HistogramTest::IntWrapper71         const IntWrapper operator+(const IntWrapper &other_wrapper) const
72         {
73             return IntWrapper(element + other_wrapper.element);
74         }
operator +=panda::test::HistogramTest::IntWrapper75         void operator+=(const IntWrapper &other_wrapper)
76         {
77             element += other_wrapper.element;
78         }
operator *panda::test::HistogramTest::IntWrapper79         const IntWrapper operator*(const IntWrapper &other_wrapper) const
80         {
81             return IntWrapper(element * other_wrapper.element);
82         }
83 
operator <<panda::test::HistogramTest::IntWrapper84         std::ostream &operator<<(std::ostream &os)
85         {
86             return os << element;
87         }
88     };
89 
90 protected:
91     panda::MTManagedThread *thread_;
92 };
93 
TEST_F(HistogramTest,SimpleIntTest)94 TEST_F(HistogramTest, SimpleIntTest)
95 {
96     std::vector<int> simple_vector = {1, 1515, -12, 130, -1, 124, 0};
97     Histogram<int> hist;
98     for (auto element : simple_vector) {
99         hist.AddValue(element);
100     }
101     CompareTwoHistogram(hist, Histogram<int>(simple_vector.begin(), simple_vector.end()));
102     ASSERT_EQ(hist.GetSum(), 1'757);
103     ASSERT_EQ(hist.GetMin(), -12);
104     ASSERT_EQ(hist.GetMax(), 1515);
105     ASSERT_EQ(hist.GetAvg(), 251);
106     ASSERT_EQ(hist.GetDispersion(), 269520);
107     ASSERT_EQ(hist.GetCount(), 7);
108 }
109 
TEST_F(HistogramTest,IntWrapperTest)110 TEST_F(HistogramTest, IntWrapperTest)
111 {
112     Histogram<IntWrapper> hist;
113     std::vector<int> simple_vector = {1, 1515, -12, 129, 0, 124, 0};
114     for (auto element : simple_vector) {
115         hist.AddValue(IntWrapper(element));
116     }
117     ASSERT_EQ(hist.GetSum(), IntWrapper(1'757));
118     ASSERT_EQ(hist.GetMin(), IntWrapper(-12));
119     ASSERT_EQ(hist.GetMax(), IntWrapper(1515));
120     ASSERT_EQ(hist.GetAvg(), 251);
121     ASSERT_EQ(hist.GetCount(), 7);
122 }
123 
TEST_F(HistogramTest,CompareTwoDifferentTest)124 TEST_F(HistogramTest, CompareTwoDifferentTest)
125 {
126     std::vector<int> simple_vector_first = {1, 1515, -12, 129, 0, 124, 0};
127     std::vector<int> simple_vector_second = {1, 1515, -12, 130, 3, 120, 0};
128     Histogram<int> hist_first(simple_vector_first.begin(), simple_vector_first.end());
129     Histogram<int> hist_second(simple_vector_second.begin(), simple_vector_second.end());
130     CompareTwoHistogram(hist_first, hist_second);
131 }
132 
TEST_F(HistogramTest,CompareDifferentTypeTest)133 TEST_F(HistogramTest, CompareDifferentTypeTest)
134 {
135     std::unordered_set<int> simple_set_first = {1, 1515, -12, 130, -1, 124, 0};
136     PandaSet<int> panda_set_first = {1, 1515, -12, 129, 2, 122, 0};
137 
138     std::vector<int> simple_vector_second = {1, 1515, -12, 129, 0, 124, 0};
139     PandaVector<int> panda_vector_first = {5, 1515, -12, 128, -3, 124, 0};
140 
141     Histogram<int> hist_first(simple_set_first.begin(), simple_set_first.end());
142     Histogram<int> hist_second(panda_set_first.begin(), panda_set_first.end());
143     Histogram<int> hist_third(simple_vector_second.begin(), simple_vector_second.end());
144     Histogram<int> hist_fourth(panda_vector_first.begin(), panda_vector_first.end());
145 
146     CompareTwoHistogram(hist_first, hist_second);
147     CompareTwoHistogram(hist_first, hist_third);
148     CompareTwoHistogram(hist_first, hist_fourth);
149     CompareTwoHistogram(hist_second, hist_third);
150     CompareTwoHistogram(hist_second, hist_fourth);
151     CompareTwoHistogram(hist_third, hist_fourth);
152 }
153 
TEST_F(HistogramTest,CheckGetTopDumpTest)154 TEST_F(HistogramTest, CheckGetTopDumpTest)
155 {
156     std::vector<int> simple_vector = {1, 1, 0, 12, 0, 1, 12};
157     Histogram<int> hist(simple_vector.begin(), simple_vector.end());
158     ASSERT_EQ(hist.GetTopDump(), "0:2,1:3,12:2");
159     ASSERT_EQ(hist.GetTopDump(2), "0:2,1:3");
160     ASSERT_EQ(hist.GetTopDump(1), "0:2");
161     ASSERT_EQ(hist.GetTopDump(0), "");
162 }
163 
164 }  // namespace panda::test
165