Commit r331416 breaks the big-endian PPC bot. On the big endian build, we
[llvm-core.git] / unittests / ADT / StatisticTest.cpp
blob17a5c7fc204a1d64e715868a02ee51714211007b
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 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/Statistic.h"
11 #include "llvm/Support/raw_ostream.h"
12 #include "gtest/gtest.h"
13 using namespace llvm;
15 using OptionalStatistic = Optional<std::pair<StringRef, unsigned>>;
17 namespace {
18 #define DEBUG_TYPE "unittest"
19 STATISTIC(Counter, "Counts things");
20 STATISTIC(Counter2, "Counts other things");
22 #if LLVM_ENABLE_STATS
23 static void
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;
33 #endif
35 TEST(StatisticTest, Count) {
36 EnableStatistics();
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
49 TEST(StatisticTest, Assign) {
50 EnableStatistics();
52 Counter = 2;
53 #if LLVM_ENABLE_STATS
54 EXPECT_EQ(Counter, 2u);
55 #else
56 EXPECT_EQ(Counter, 0u);
57 #endif
60 TEST(StatisticTest, API) {
61 EnableStatistics();
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
73 #if LLVM_ENABLE_STATS
75 const auto Range1 = GetStatistics();
76 EXPECT_NE(Range1.begin(), Range1.end());
77 EXPECT_EQ(Range1.begin() + 1, Range1.end());
79 OptionalStatistic S1;
80 OptionalStatistic S2;
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.
88 Counter2++;
91 const auto Range = GetStatistics();
92 EXPECT_NE(Range.begin(), Range.end());
93 EXPECT_EQ(Range.begin() + 2, Range.end());
95 OptionalStatistic S1;
96 OptionalStatistic S2;
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);
108 #else
109 Counter2++;
110 auto &Range = GetStatistics();
111 EXPECT_EQ(Range.begin(), Range.end());
112 #endif
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();
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.
131 Counter++;
132 Counter2++;
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);
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
160 } // end anonymous namespace