1 //===- llvm/unittest/ADT/StatisticTest.cpp - Statistic unit tests ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
13 using namespace llvm;
14
15 using OptionalStatistic = Optional<std::pair<StringRef, unsigned>>;
16
17 namespace {
18 #define DEBUG_TYPE "unittest"
19 STATISTIC(Counter, "Counts things");
20 STATISTIC(Counter2, "Counts other things");
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
TEST(StatisticTest,Assign)49 TEST(StatisticTest, Assign) {
50 EnableStatistics();
51
52 Counter = 2;
53 #if LLVM_ENABLE_STATS
54 EXPECT_EQ(Counter, 2u);
55 #else
56 EXPECT_EQ(Counter, 0u);
57 #endif
58 }
59
TEST(StatisticTest,API)60 TEST(StatisticTest, API) {
61 EnableStatistics();
62
63 Counter = 0;
64 EXPECT_EQ(Counter, 0u);
65 Counter++;
66 Counter++;
67 #if LLVM_ENABLE_STATS
68 EXPECT_EQ(Counter, 2u);
69 #else
70 EXPECT_EQ(Counter, 0u);
71 #endif
72
73 #if LLVM_ENABLE_STATS
74 {
75 const auto Range1 = GetStatistics();
76 EXPECT_NE(Range1.begin(), Range1.end());
77 EXPECT_EQ(Range1.begin() + 1, Range1.end());
78
79 OptionalStatistic S1;
80 OptionalStatistic S2;
81 extractCounters(Range1, S1, S2);
82
83 EXPECT_EQ(S1.hasValue(), true);
84 EXPECT_EQ(S2.hasValue(), false);
85 }
86
87 // Counter2 will be registered when it's first touched.
88 Counter2++;
89
90 {
91 const auto Range = GetStatistics();
92 EXPECT_NE(Range.begin(), Range.end());
93 EXPECT_EQ(Range.begin() + 2, Range.end());
94
95 OptionalStatistic S1;
96 OptionalStatistic S2;
97 extractCounters(Range, S1, S2);
98
99 EXPECT_EQ(S1.hasValue(), true);
100 EXPECT_EQ(S2.hasValue(), true);
101
102 EXPECT_EQ(S1->first, "Counter");
103 EXPECT_EQ(S1->second, 2u);
104
105 EXPECT_EQ(S2->first, "Counter2");
106 EXPECT_EQ(S2->second, 1u);
107 }
108 #else
109 Counter2++;
110 auto &Range = GetStatistics();
111 EXPECT_EQ(Range.begin(), Range.end());
112 #endif
113
114 #if LLVM_ENABLE_STATS
115 // Check that resetting the statistics works correctly.
116 // It should empty the list and zero the counters.
117 ResetStatistics();
118 {
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);
128 }
129
130 // Now check that they successfully re-register and count.
131 Counter++;
132 Counter2++;
133
134 {
135 auto &Range = GetStatistics();
136 EXPECT_EQ(Range.begin() + 2, Range.end());
137 EXPECT_EQ(Counter, 1u);
138 EXPECT_EQ(Counter2, 1u);
139
140 OptionalStatistic S1;
141 OptionalStatistic S2;
142 extractCounters(Range, S1, S2);
143
144 EXPECT_EQ(S1.hasValue(), true);
145 EXPECT_EQ(S2.hasValue(), true);
146
147 EXPECT_EQ(S1->first, "Counter");
148 EXPECT_EQ(S1->second, 1u);
149
150 EXPECT_EQ(S2->first, "Counter2");
151 EXPECT_EQ(S2->second, 1u);
152 }
153 #else
154 // No need to test the output ResetStatistics(), there's nothing to reset so
155 // we can't tell if it failed anyway.
156 ResetStatistics();
157 #endif
158 }
159
160 } // end anonymous namespace
161