1 //===- llvm/unittest/ADT/StatisticTest.cpp - Statistic unit tests ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/Statistic.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "gtest/gtest.h"
14 using OptionalStatistic
= std::optional
<std::pair
<StringRef
, uint64_t>>;
17 #define DEBUG_TYPE "unittest"
18 STATISTIC(Counter
, "Counts things");
19 STATISTIC(Counter2
, "Counts other things");
20 ALWAYS_ENABLED_STATISTIC(AlwaysCounter
, "Counts things always");
24 extractCounters(const std::vector
<std::pair
<StringRef
, uint64_t>> &Range
,
25 OptionalStatistic
&S1
, OptionalStatistic
&S2
) {
26 for (const auto &S
: Range
) {
27 if (S
.first
== "Counter")
29 if (S
.first
== "Counter2")
35 TEST(StatisticTest
, Count
) {
39 EXPECT_EQ(Counter
, 0ull);
42 Counter
+= (std::numeric_limits
<uint64_t>::max() - 3);
44 EXPECT_EQ(Counter
, std::numeric_limits
<uint64_t>::max() - 1);
46 EXPECT_EQ(Counter
, UINT64_C(0));
50 EXPECT_EQ(AlwaysCounter
, 0ull);
53 EXPECT_EQ(AlwaysCounter
, 2ull);
56 TEST(StatisticTest
, Assign
) {
61 EXPECT_EQ(Counter
, 2u);
63 EXPECT_EQ(Counter
, 0u);
67 EXPECT_EQ(AlwaysCounter
, 2u);
70 TEST(StatisticTest
, API
) {
72 // Reset beforehand to make sure previous tests don't effect this one.
76 EXPECT_EQ(Counter
, 0u);
80 EXPECT_EQ(Counter
, 2u);
82 EXPECT_EQ(Counter
, 0u);
87 const auto Range1
= GetStatistics();
88 EXPECT_NE(Range1
.begin(), Range1
.end());
89 EXPECT_EQ(Range1
.begin() + 1, Range1
.end());
93 extractCounters(Range1
, S1
, S2
);
95 EXPECT_EQ(S1
.has_value(), true);
96 EXPECT_EQ(S2
.has_value(), false);
99 // Counter2 will be registered when it's first touched.
103 const auto Range
= GetStatistics();
104 EXPECT_NE(Range
.begin(), Range
.end());
105 EXPECT_EQ(Range
.begin() + 2, Range
.end());
107 OptionalStatistic S1
;
108 OptionalStatistic S2
;
109 extractCounters(Range
, S1
, S2
);
111 EXPECT_EQ(S1
.has_value(), true);
112 EXPECT_EQ(S2
.has_value(), true);
114 EXPECT_EQ(S1
->first
, "Counter");
115 EXPECT_EQ(S1
->second
, 2u);
117 EXPECT_EQ(S2
->first
, "Counter2");
118 EXPECT_EQ(S2
->second
, 1u);
122 auto Range
= GetStatistics();
123 EXPECT_EQ(Range
.begin(), Range
.end());
126 #if LLVM_ENABLE_STATS
127 // Check that resetting the statistics works correctly.
128 // It should empty the list and zero the counters.
131 auto Range
= GetStatistics();
132 EXPECT_EQ(Range
.begin(), Range
.end());
133 EXPECT_EQ(Counter
, 0u);
134 EXPECT_EQ(Counter2
, 0u);
135 OptionalStatistic S1
;
136 OptionalStatistic S2
;
137 extractCounters(Range
, S1
, S2
);
138 EXPECT_EQ(S1
.has_value(), false);
139 EXPECT_EQ(S2
.has_value(), false);
142 // Now check that they successfully re-register and count.
147 auto Range
= GetStatistics();
148 EXPECT_EQ(Range
.begin() + 2, Range
.end());
149 EXPECT_EQ(Counter
, 1u);
150 EXPECT_EQ(Counter2
, 1u);
152 OptionalStatistic S1
;
153 OptionalStatistic S2
;
154 extractCounters(Range
, S1
, S2
);
156 EXPECT_EQ(S1
.has_value(), true);
157 EXPECT_EQ(S2
.has_value(), true);
159 EXPECT_EQ(S1
->first
, "Counter");
160 EXPECT_EQ(S1
->second
, 1u);
162 EXPECT_EQ(S2
->first
, "Counter2");
163 EXPECT_EQ(S2
->second
, 1u);
166 // No need to test the output ResetStatistics(), there's nothing to reset so
167 // we can't tell if it failed anyway.
172 } // end anonymous namespace