[InstCombine] Signed saturation patterns
[llvm-core.git] / unittests / ADT / StatisticTest.cpp
blobe5a0cad26d685ce510637de5d3d6579cf799c22b
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");
20 ALWAYS_ENABLED_STATISTIC(AlwaysCounter, "Counts things always");
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
48 AlwaysCounter = 0;
49 EXPECT_EQ(AlwaysCounter, 0u);
50 AlwaysCounter++;
51 ++AlwaysCounter;
52 EXPECT_EQ(AlwaysCounter, 2u);
55 TEST(StatisticTest, Assign) {
56 EnableStatistics();
58 Counter = 2;
59 #if LLVM_ENABLE_STATS
60 EXPECT_EQ(Counter, 2u);
61 #else
62 EXPECT_EQ(Counter, 0u);
63 #endif
65 AlwaysCounter = 2;
66 EXPECT_EQ(AlwaysCounter, 2u);
69 TEST(StatisticTest, API) {
70 EnableStatistics();
71 // Reset beforehand to make sure previous tests don't effect this one.
72 ResetStatistics();
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
84 #if LLVM_ENABLE_STATS
86 const auto Range1 = GetStatistics();
87 EXPECT_NE(Range1.begin(), Range1.end());
88 EXPECT_EQ(Range1.begin() + 1, Range1.end());
90 OptionalStatistic S1;
91 OptionalStatistic S2;
92 extractCounters(Range1, S1, S2);
94 EXPECT_EQ(S1.hasValue(), true);
95 EXPECT_EQ(S2.hasValue(), false);
98 // Counter2 will be registered when it's first touched.
99 Counter2++;
102 const auto Range = GetStatistics();
103 EXPECT_NE(Range.begin(), Range.end());
104 EXPECT_EQ(Range.begin() + 2, Range.end());
106 OptionalStatistic S1;
107 OptionalStatistic S2;
108 extractCounters(Range, S1, S2);
110 EXPECT_EQ(S1.hasValue(), true);
111 EXPECT_EQ(S2.hasValue(), true);
113 EXPECT_EQ(S1->first, "Counter");
114 EXPECT_EQ(S1->second, 2u);
116 EXPECT_EQ(S2->first, "Counter2");
117 EXPECT_EQ(S2->second, 1u);
119 #else
120 Counter2++;
121 auto &Range = GetStatistics();
122 EXPECT_EQ(Range.begin(), Range.end());
123 #endif
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();
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);
141 // Now check that they successfully re-register and count.
142 Counter++;
143 Counter2++;
146 auto &Range = GetStatistics();
147 EXPECT_EQ(Range.begin() + 2, Range.end());
148 EXPECT_EQ(Counter, 1u);
149 EXPECT_EQ(Counter2, 1u);
151 OptionalStatistic S1;
152 OptionalStatistic S2;
153 extractCounters(Range, S1, S2);
155 EXPECT_EQ(S1.hasValue(), true);
156 EXPECT_EQ(S2.hasValue(), true);
158 EXPECT_EQ(S1->first, "Counter");
159 EXPECT_EQ(S1->second, 1u);
161 EXPECT_EQ(S2->first, "Counter2");
162 EXPECT_EQ(S2->second, 1u);
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
171 } // end anonymous namespace