xref: /llvm-project/llvm/unittests/ADT/StatisticTest.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
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 
21 #if LLVM_ENABLE_STATS
22 static void
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")
27       S1 = S;
28     if (S.first == "Counter2")
29       S2 = S;
30   }
31 }
32 #endif
33 
34 TEST(StatisticTest, Count) {
35   EnableStatistics();
36 
37   Counter = 0;
38   EXPECT_EQ(Counter, 0u);
39   Counter++;
40   Counter++;
41 #if LLVM_ENABLE_STATS
42   EXPECT_EQ(Counter, 2u);
43 #else
44   EXPECT_EQ(Counter, 0u);
45 #endif
46 }
47 
48 TEST(StatisticTest, Assign) {
49   EnableStatistics();
50 
51   Counter = 2;
52 #if LLVM_ENABLE_STATS
53   EXPECT_EQ(Counter, 2u);
54 #else
55   EXPECT_EQ(Counter, 0u);
56 #endif
57 }
58 
59 TEST(StatisticTest, API) {
60   EnableStatistics();
61 
62   Counter = 0;
63   EXPECT_EQ(Counter, 0u);
64   Counter++;
65   Counter++;
66 #if LLVM_ENABLE_STATS
67   EXPECT_EQ(Counter, 2u);
68 #else
69   EXPECT_EQ(Counter, 0u);
70 #endif
71 
72 #if LLVM_ENABLE_STATS
73   {
74     const auto Range1 = GetStatistics();
75     EXPECT_NE(Range1.begin(), Range1.end());
76     EXPECT_EQ(Range1.begin() + 1, Range1.end());
77 
78     OptionalStatistic S1;
79     OptionalStatistic S2;
80     extractCounters(Range1, S1, S2);
81 
82     EXPECT_EQ(S1.hasValue(), true);
83     EXPECT_EQ(S2.hasValue(), false);
84   }
85 
86   // Counter2 will be registered when it's first touched.
87   Counter2++;
88 
89   {
90     const auto Range = GetStatistics();
91     EXPECT_NE(Range.begin(), Range.end());
92     EXPECT_EQ(Range.begin() + 2, Range.end());
93 
94     OptionalStatistic S1;
95     OptionalStatistic S2;
96     extractCounters(Range, S1, S2);
97 
98     EXPECT_EQ(S1.hasValue(), true);
99     EXPECT_EQ(S2.hasValue(), true);
100 
101     EXPECT_EQ(S1->first, "Counter");
102     EXPECT_EQ(S1->second, 2u);
103 
104     EXPECT_EQ(S2->first, "Counter2");
105     EXPECT_EQ(S2->second, 1u);
106   }
107 #else
108   Counter2++;
109   auto &Range = GetStatistics();
110   EXPECT_EQ(Range.begin(), Range.end());
111 #endif
112 
113 #if LLVM_ENABLE_STATS
114   // Check that resetting the statistics works correctly.
115   // It should empty the list and zero the counters.
116   ResetStatistics();
117   {
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);
127   }
128 
129   // Now check that they successfully re-register and count.
130   Counter++;
131   Counter2++;
132 
133   {
134     auto &Range = GetStatistics();
135     EXPECT_EQ(Range.begin() + 2, Range.end());
136     EXPECT_EQ(Counter, 1u);
137     EXPECT_EQ(Counter2, 1u);
138 
139     OptionalStatistic S1;
140     OptionalStatistic S2;
141     extractCounters(Range, S1, S2);
142 
143     EXPECT_EQ(S1.hasValue(), true);
144     EXPECT_EQ(S2.hasValue(), true);
145 
146     EXPECT_EQ(S1->first, "Counter");
147     EXPECT_EQ(S1->second, 1u);
148 
149     EXPECT_EQ(S2->first, "Counter2");
150     EXPECT_EQ(S2->second, 1u);
151   }
152 #else
153   // No need to test the output ResetStatistics(), there's nothing to reset so
154   // we can't tell if it failed anyway.
155   ResetStatistics();
156 #endif
157 }
158 
159 } // end anonymous namespace
160