[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / unittests / ADT / StatisticTest.cpp
blob1b5530fb7296f7e47b1dd157cc2f3a842f62182b
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 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/Statistic.h"
10 #include "llvm/Support/raw_ostream.h"
11 #include "gtest/gtest.h"
12 using namespace llvm;
14 using OptionalStatistic = Optional<std::pair<StringRef, unsigned>>;
16 namespace {
17 #define DEBUG_TYPE "unittest"
18 STATISTIC(Counter, "Counts things");
19 STATISTIC(Counter2, "Counts other things");
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;
32 #endif
34 TEST(StatisticTest, Count) {
35 EnableStatistics();
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
48 TEST(StatisticTest, Assign) {
49 EnableStatistics();
51 Counter = 2;
52 #if LLVM_ENABLE_STATS
53 EXPECT_EQ(Counter, 2u);
54 #else
55 EXPECT_EQ(Counter, 0u);
56 #endif
59 TEST(StatisticTest, API) {
60 EnableStatistics();
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
72 #if LLVM_ENABLE_STATS
74 const auto Range1 = GetStatistics();
75 EXPECT_NE(Range1.begin(), Range1.end());
76 EXPECT_EQ(Range1.begin() + 1, Range1.end());
78 OptionalStatistic S1;
79 OptionalStatistic S2;
80 extractCounters(Range1, S1, S2);
82 EXPECT_EQ(S1.hasValue(), true);
83 EXPECT_EQ(S2.hasValue(), false);
86 // Counter2 will be registered when it's first touched.
87 Counter2++;
90 const auto Range = GetStatistics();
91 EXPECT_NE(Range.begin(), Range.end());
92 EXPECT_EQ(Range.begin() + 2, Range.end());
94 OptionalStatistic S1;
95 OptionalStatistic S2;
96 extractCounters(Range, S1, S2);
98 EXPECT_EQ(S1.hasValue(), true);
99 EXPECT_EQ(S2.hasValue(), true);
101 EXPECT_EQ(S1->first, "Counter");
102 EXPECT_EQ(S1->second, 2u);
104 EXPECT_EQ(S2->first, "Counter2");
105 EXPECT_EQ(S2->second, 1u);
107 #else
108 Counter2++;
109 auto &Range = GetStatistics();
110 EXPECT_EQ(Range.begin(), Range.end());
111 #endif
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();
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);
129 // Now check that they successfully re-register and count.
130 Counter++;
131 Counter2++;
134 auto &Range = GetStatistics();
135 EXPECT_EQ(Range.begin() + 2, Range.end());
136 EXPECT_EQ(Counter, 1u);
137 EXPECT_EQ(Counter2, 1u);
139 OptionalStatistic S1;
140 OptionalStatistic S2;
141 extractCounters(Range, S1, S2);
143 EXPECT_EQ(S1.hasValue(), true);
144 EXPECT_EQ(S2.hasValue(), true);
146 EXPECT_EQ(S1->first, "Counter");
147 EXPECT_EQ(S1->second, 1u);
149 EXPECT_EQ(S2->first, "Counter2");
150 EXPECT_EQ(S2->second, 1u);
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
159 } // end anonymous namespace