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
= Optional
<std::pair
<StringRef
, unsigned>>;
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
, unsigned>> &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
, 0u);
43 EXPECT_EQ(Counter
, 2u);
45 EXPECT_EQ(Counter
, 0u);
49 EXPECT_EQ(AlwaysCounter
, 0u);
52 EXPECT_EQ(AlwaysCounter
, 2u);
55 TEST(StatisticTest
, Assign
) {
60 EXPECT_EQ(Counter
, 2u);
62 EXPECT_EQ(Counter
, 0u);
66 EXPECT_EQ(AlwaysCounter
, 2u);
69 TEST(StatisticTest
, API
) {
71 // Reset beforehand to make sure previous tests don't effect this one.
75 EXPECT_EQ(Counter
, 0u);
79 EXPECT_EQ(Counter
, 2u);
81 EXPECT_EQ(Counter
, 0u);
86 const auto Range1
= GetStatistics();
87 EXPECT_NE(Range1
.begin(), Range1
.end());
88 EXPECT_EQ(Range1
.begin() + 1, Range1
.end());
92 extractCounters(Range1
, S1
, S2
);
94 EXPECT_EQ(S1
.hasValue(), true);
95 EXPECT_EQ(S2
.hasValue(), false);
98 // Counter2 will be registered when it's first touched.
102 const auto Range
= GetStatistics();
103 EXPECT_NE(Range
.begin(), Range
.end());
104 EXPECT_EQ(Range
.begin() + 2, Range
.end());
106 OptionalStatistic S1
;
107 OptionalStatistic S2
;
108 extractCounters(Range
, S1
, S2
);
110 EXPECT_EQ(S1
.hasValue(), true);
111 EXPECT_EQ(S2
.hasValue(), true);
113 EXPECT_EQ(S1
->first
, "Counter");
114 EXPECT_EQ(S1
->second
, 2u);
116 EXPECT_EQ(S2
->first
, "Counter2");
117 EXPECT_EQ(S2
->second
, 1u);
121 auto &Range
= GetStatistics();
122 EXPECT_EQ(Range
.begin(), Range
.end());
125 #if LLVM_ENABLE_STATS
126 // Check that resetting the statistics works correctly.
127 // It should empty the list and zero the counters.
130 auto &Range
= GetStatistics();
131 EXPECT_EQ(Range
.begin(), Range
.end());
132 EXPECT_EQ(Counter
, 0u);
133 EXPECT_EQ(Counter2
, 0u);
134 OptionalStatistic S1
;
135 OptionalStatistic S2
;
136 extractCounters(Range
, S1
, S2
);
137 EXPECT_EQ(S1
.hasValue(), false);
138 EXPECT_EQ(S2
.hasValue(), false);
141 // Now check that they successfully re-register and count.
146 auto &Range
= GetStatistics();
147 EXPECT_EQ(Range
.begin() + 2, Range
.end());
148 EXPECT_EQ(Counter
, 1u);
149 EXPECT_EQ(Counter2
, 1u);
151 OptionalStatistic S1
;
152 OptionalStatistic S2
;
153 extractCounters(Range
, S1
, S2
);
155 EXPECT_EQ(S1
.hasValue(), true);
156 EXPECT_EQ(S2
.hasValue(), true);
158 EXPECT_EQ(S1
->first
, "Counter");
159 EXPECT_EQ(S1
->second
, 1u);
161 EXPECT_EQ(S2
->first
, "Counter2");
162 EXPECT_EQ(S2
->second
, 1u);
165 // No need to test the output ResetStatistics(), there's nothing to reset so
166 // we can't tell if it failed anyway.
171 } // end anonymous namespace