1 //===- llvm/unittest/ADT/StatisticTest.cpp - Statistic unit tests ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
15 using OptionalStatistic
= Optional
<std::pair
<StringRef
, unsigned>>;
18 #define DEBUG_TYPE "unittest"
19 STATISTIC(Counter
, "Counts things");
20 STATISTIC(Counter2
, "Counts other things");
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 TEST(StatisticTest
, Assign
) {
54 EXPECT_EQ(Counter
, 2u);
56 EXPECT_EQ(Counter
, 0u);
60 TEST(StatisticTest
, API
) {
64 EXPECT_EQ(Counter
, 0u);
68 EXPECT_EQ(Counter
, 2u);
70 EXPECT_EQ(Counter
, 0u);
75 const auto Range1
= GetStatistics();
76 EXPECT_NE(Range1
.begin(), Range1
.end());
77 EXPECT_EQ(Range1
.begin() + 1, Range1
.end());
81 extractCounters(Range1
, S1
, S2
);
83 EXPECT_EQ(S1
.hasValue(), true);
84 EXPECT_EQ(S2
.hasValue(), false);
87 // Counter2 will be registered when it's first touched.
91 const auto Range
= GetStatistics();
92 EXPECT_NE(Range
.begin(), Range
.end());
93 EXPECT_EQ(Range
.begin() + 2, Range
.end());
97 extractCounters(Range
, S1
, S2
);
99 EXPECT_EQ(S1
.hasValue(), true);
100 EXPECT_EQ(S2
.hasValue(), true);
102 EXPECT_EQ(S1
->first
, "Counter");
103 EXPECT_EQ(S1
->second
, 2u);
105 EXPECT_EQ(S2
->first
, "Counter2");
106 EXPECT_EQ(S2
->second
, 1u);
110 auto &Range
= GetStatistics();
111 EXPECT_EQ(Range
.begin(), Range
.end());
114 #if LLVM_ENABLE_STATS
115 // Check that resetting the statistics works correctly.
116 // It should empty the list and zero the counters.
119 auto &Range
= GetStatistics();
120 EXPECT_EQ(Range
.begin(), Range
.end());
121 EXPECT_EQ(Counter
, 0u);
122 EXPECT_EQ(Counter2
, 0u);
123 OptionalStatistic S1
;
124 OptionalStatistic S2
;
125 extractCounters(Range
, S1
, S2
);
126 EXPECT_EQ(S1
.hasValue(), false);
127 EXPECT_EQ(S2
.hasValue(), false);
130 // Now check that they successfully re-register and count.
135 auto &Range
= GetStatistics();
136 EXPECT_EQ(Range
.begin() + 2, Range
.end());
137 EXPECT_EQ(Counter
, 1u);
138 EXPECT_EQ(Counter2
, 1u);
140 OptionalStatistic S1
;
141 OptionalStatistic S2
;
142 extractCounters(Range
, S1
, S2
);
144 EXPECT_EQ(S1
.hasValue(), true);
145 EXPECT_EQ(S2
.hasValue(), true);
147 EXPECT_EQ(S1
->first
, "Counter");
148 EXPECT_EQ(S1
->second
, 1u);
150 EXPECT_EQ(S2
->first
, "Counter2");
151 EXPECT_EQ(S2
->second
, 1u);
154 // No need to test the output ResetStatistics(), there's nothing to reset so
155 // we can't tell if it failed anyway.
160 } // end anonymous namespace