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");
23 extractCounters(const std::vector
<std::pair
<StringRef
, unsigned>> &Range
,
24 OptionalStatistic
&S1
, OptionalStatistic
&S2
) {
25 for (const auto &S
: Range
) {
26 if (S
.first
== "Counter")
28 if (S
.first
== "Counter2")
34 TEST(StatisticTest
, Count
) {
38 EXPECT_EQ(Counter
, 0u);
42 EXPECT_EQ(Counter
, 2u);
44 EXPECT_EQ(Counter
, 0u);
48 TEST(StatisticTest
, Assign
) {
53 EXPECT_EQ(Counter
, 2u);
55 EXPECT_EQ(Counter
, 0u);
59 TEST(StatisticTest
, API
) {
63 EXPECT_EQ(Counter
, 0u);
67 EXPECT_EQ(Counter
, 2u);
69 EXPECT_EQ(Counter
, 0u);
74 const auto Range1
= GetStatistics();
75 EXPECT_NE(Range1
.begin(), Range1
.end());
76 EXPECT_EQ(Range1
.begin() + 1, Range1
.end());
80 extractCounters(Range1
, S1
, S2
);
82 EXPECT_EQ(S1
.hasValue(), true);
83 EXPECT_EQ(S2
.hasValue(), false);
86 // Counter2 will be registered when it's first touched.
90 const auto Range
= GetStatistics();
91 EXPECT_NE(Range
.begin(), Range
.end());
92 EXPECT_EQ(Range
.begin() + 2, Range
.end());
96 extractCounters(Range
, S1
, S2
);
98 EXPECT_EQ(S1
.hasValue(), true);
99 EXPECT_EQ(S2
.hasValue(), true);
101 EXPECT_EQ(S1
->first
, "Counter");
102 EXPECT_EQ(S1
->second
, 2u);
104 EXPECT_EQ(S2
->first
, "Counter2");
105 EXPECT_EQ(S2
->second
, 1u);
109 auto &Range
= GetStatistics();
110 EXPECT_EQ(Range
.begin(), Range
.end());
113 #if LLVM_ENABLE_STATS
114 // Check that resetting the statistics works correctly.
115 // It should empty the list and zero the counters.
118 auto &Range
= GetStatistics();
119 EXPECT_EQ(Range
.begin(), Range
.end());
120 EXPECT_EQ(Counter
, 0u);
121 EXPECT_EQ(Counter2
, 0u);
122 OptionalStatistic S1
;
123 OptionalStatistic S2
;
124 extractCounters(Range
, S1
, S2
);
125 EXPECT_EQ(S1
.hasValue(), false);
126 EXPECT_EQ(S2
.hasValue(), false);
129 // Now check that they successfully re-register and count.
134 auto &Range
= GetStatistics();
135 EXPECT_EQ(Range
.begin() + 2, Range
.end());
136 EXPECT_EQ(Counter
, 1u);
137 EXPECT_EQ(Counter2
, 1u);
139 OptionalStatistic S1
;
140 OptionalStatistic S2
;
141 extractCounters(Range
, S1
, S2
);
143 EXPECT_EQ(S1
.hasValue(), true);
144 EXPECT_EQ(S2
.hasValue(), true);
146 EXPECT_EQ(S1
->first
, "Counter");
147 EXPECT_EQ(S1
->second
, 1u);
149 EXPECT_EQ(S2
->first
, "Counter2");
150 EXPECT_EQ(S2
->second
, 1u);
153 // No need to test the output ResetStatistics(), there's nothing to reset so
154 // we can't tell if it failed anyway.
159 } // end anonymous namespace