1 //===- llvm/unittest/ADT/StatisticTest.cpp - Statistic unit tests ---------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/Statistic.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "gtest/gtest.h"
12 using namespace llvm;
13 
14 using OptionalStatistic = Optional<std::pair<StringRef, unsigned>>;
15 
16 namespace {
17 #define DEBUG_TYPE "unittest"
18 STATISTIC(Counter, "Counts things");
19 STATISTIC(Counter2, "Counts other things");
20 ALWAYS_ENABLED_STATISTIC(AlwaysCounter, "Counts things always");
21 
22 #if LLVM_ENABLE_STATS
23 static void
extractCounters(const std::vector<std::pair<StringRef,unsigned>> & Range,OptionalStatistic & S1,OptionalStatistic & S2)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")
28       S1 = S;
29     if (S.first == "Counter2")
30       S2 = S;
31   }
32 }
33 #endif
34 
TEST(StatisticTest,Count)35 TEST(StatisticTest, Count) {
36   EnableStatistics();
37 
38   Counter = 0;
39   EXPECT_EQ(Counter, 0u);
40   Counter++;
41   Counter++;
42 #if LLVM_ENABLE_STATS
43   EXPECT_EQ(Counter, 2u);
44 #else
45   EXPECT_EQ(Counter, 0u);
46 #endif
47 
48   AlwaysCounter = 0;
49   EXPECT_EQ(AlwaysCounter, 0u);
50   AlwaysCounter++;
51   ++AlwaysCounter;
52   EXPECT_EQ(AlwaysCounter, 2u);
53 }
54 
TEST(StatisticTest,Assign)55 TEST(StatisticTest, Assign) {
56   EnableStatistics();
57 
58   Counter = 2;
59 #if LLVM_ENABLE_STATS
60   EXPECT_EQ(Counter, 2u);
61 #else
62   EXPECT_EQ(Counter, 0u);
63 #endif
64 
65   AlwaysCounter = 2;
66   EXPECT_EQ(AlwaysCounter, 2u);
67 }
68 
TEST(StatisticTest,API)69 TEST(StatisticTest, API) {
70   EnableStatistics();
71   // Reset beforehand to make sure previous tests don't effect this one.
72   ResetStatistics();
73 
74   Counter = 0;
75   EXPECT_EQ(Counter, 0u);
76   Counter++;
77   Counter++;
78 #if LLVM_ENABLE_STATS
79   EXPECT_EQ(Counter, 2u);
80 #else
81   EXPECT_EQ(Counter, 0u);
82 #endif
83 
84 #if LLVM_ENABLE_STATS
85   {
86     const auto Range1 = GetStatistics();
87     EXPECT_NE(Range1.begin(), Range1.end());
88     EXPECT_EQ(Range1.begin() + 1, Range1.end());
89 
90     OptionalStatistic S1;
91     OptionalStatistic S2;
92     extractCounters(Range1, S1, S2);
93 
94     EXPECT_EQ(S1.hasValue(), true);
95     EXPECT_EQ(S2.hasValue(), false);
96   }
97 
98   // Counter2 will be registered when it's first touched.
99   Counter2++;
100 
101   {
102     const auto Range = GetStatistics();
103     EXPECT_NE(Range.begin(), Range.end());
104     EXPECT_EQ(Range.begin() + 2, Range.end());
105 
106     OptionalStatistic S1;
107     OptionalStatistic S2;
108     extractCounters(Range, S1, S2);
109 
110     EXPECT_EQ(S1.hasValue(), true);
111     EXPECT_EQ(S2.hasValue(), true);
112 
113     EXPECT_EQ(S1->first, "Counter");
114     EXPECT_EQ(S1->second, 2u);
115 
116     EXPECT_EQ(S2->first, "Counter2");
117     EXPECT_EQ(S2->second, 1u);
118   }
119 #else
120   Counter2++;
121   auto &Range = GetStatistics();
122   EXPECT_EQ(Range.begin(), Range.end());
123 #endif
124 
125 #if LLVM_ENABLE_STATS
126   // Check that resetting the statistics works correctly.
127   // It should empty the list and zero the counters.
128   ResetStatistics();
129   {
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);
139   }
140 
141   // Now check that they successfully re-register and count.
142   Counter++;
143   Counter2++;
144 
145   {
146     auto &Range = GetStatistics();
147     EXPECT_EQ(Range.begin() + 2, Range.end());
148     EXPECT_EQ(Counter, 1u);
149     EXPECT_EQ(Counter2, 1u);
150 
151     OptionalStatistic S1;
152     OptionalStatistic S2;
153     extractCounters(Range, S1, S2);
154 
155     EXPECT_EQ(S1.hasValue(), true);
156     EXPECT_EQ(S2.hasValue(), true);
157 
158     EXPECT_EQ(S1->first, "Counter");
159     EXPECT_EQ(S1->second, 1u);
160 
161     EXPECT_EQ(S2->first, "Counter2");
162     EXPECT_EQ(S2->second, 1u);
163   }
164 #else
165   // No need to test the output ResetStatistics(), there's nothing to reset so
166   // we can't tell if it failed anyway.
167   ResetStatistics();
168 #endif
169 }
170 
171 } // end anonymous namespace
172