blob: 17a5c7fc204a1d64e715868a02ee51714211007b [file] [log] [blame]
Daniel Sandersa3170b62018-03-05 19:38:16 +00001//===- 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"
13using namespace llvm;
14
Daniel Sanders50adb3d2018-03-08 02:36:25 +000015using OptionalStatistic = Optional<std::pair<StringRef, unsigned>>;
16
Daniel Sandersa3170b62018-03-05 19:38:16 +000017namespace {
18#define DEBUG_TYPE "unittest"
19STATISTIC(Counter, "Counts things");
20STATISTIC(Counter2, "Counts other things");
21
Daniel Sandersec47f702018-03-08 15:52:45 +000022#if LLVM_ENABLE_STATS
Daniel Sanders50adb3d2018-03-08 02:36:25 +000023static void
24extractCounters(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}
Daniel Sandersec47f702018-03-08 15:52:45 +000033#endif
Daniel Sanders50adb3d2018-03-08 02:36:25 +000034
Daniel Sandersa3170b62018-03-05 19:38:16 +000035TEST(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
49TEST(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
60TEST(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
Daniel Sanders50adb3d2018-03-08 02:36:25 +000074 {
75 const auto Range1 = GetStatistics();
76 EXPECT_NE(Range1.begin(), Range1.end());
77 EXPECT_EQ(Range1.begin() + 1, Range1.end());
Daniel Sandersa3170b62018-03-05 19:38:16 +000078
Daniel Sanders50adb3d2018-03-08 02:36:25 +000079 OptionalStatistic S1;
80 OptionalStatistic S2;
81 extractCounters(Range1, S1, S2);
82
83 EXPECT_EQ(S1.hasValue(), true);
84 EXPECT_EQ(S2.hasValue(), false);
Daniel Sandersa3170b62018-03-05 19:38:16 +000085 }
86
Daniel Sandersa3170b62018-03-05 19:38:16 +000087 // Counter2 will be registered when it's first touched.
88 Counter2++;
89
Daniel Sanders50adb3d2018-03-08 02:36:25 +000090 {
91 const auto Range = GetStatistics();
92 EXPECT_NE(Range.begin(), Range.end());
93 EXPECT_EQ(Range.begin() + 2, Range.end());
Daniel Sandersa3170b62018-03-05 19:38:16 +000094
Daniel Sanders50adb3d2018-03-08 02:36:25 +000095 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);
Daniel Sandersa3170b62018-03-05 19:38:16 +0000107 }
Daniel Sandersa3170b62018-03-05 19:38:16 +0000108#else
109 Counter2++;
110 auto &Range = GetStatistics();
111 EXPECT_EQ(Range.begin(), Range.end());
112#endif
Daniel Sanders50adb3d2018-03-08 02:36:25 +0000113
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
Daniel Sandersa3170b62018-03-05 19:38:16 +0000158}
159
160} // end anonymous namespace