blob: 3ddfe56cc8fef171bb954202548be4b3609d5de3 [file] [log] [blame]
Daniel Sandersa09751e2018-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
15namespace {
16#define DEBUG_TYPE "unittest"
17STATISTIC(Counter, "Counts things");
18STATISTIC(Counter2, "Counts other things");
19
20TEST(StatisticTest, Count) {
21 EnableStatistics();
22
23 Counter = 0;
24 EXPECT_EQ(Counter, 0u);
25 Counter++;
26 Counter++;
27#if LLVM_ENABLE_STATS
28 EXPECT_EQ(Counter, 2u);
29#else
30 EXPECT_EQ(Counter, 0u);
31#endif
32}
33
34TEST(StatisticTest, Assign) {
35 EnableStatistics();
36
37 Counter = 2;
38#if LLVM_ENABLE_STATS
39 EXPECT_EQ(Counter, 2u);
40#else
41 EXPECT_EQ(Counter, 0u);
42#endif
43}
44
45TEST(StatisticTest, API) {
46 EnableStatistics();
47
48 Counter = 0;
49 EXPECT_EQ(Counter, 0u);
50 Counter++;
51 Counter++;
52#if LLVM_ENABLE_STATS
53 EXPECT_EQ(Counter, 2u);
54#else
55 EXPECT_EQ(Counter, 0u);
56#endif
57
58#if LLVM_ENABLE_STATS
59 const auto Range1 = GetStatistics();
60 EXPECT_NE(Range1.begin(), Range1.end());
61 EXPECT_EQ(Range1.begin() + 1, Range1.end());
62
63 Optional<std::pair<StringRef, unsigned>> S1;
64 Optional<std::pair<StringRef, unsigned>> S2;
65 for (const auto &S : Range1) {
66 if (std::string(S.first) == "Counter")
67 S1 = S;
68 if (std::string(S.first) == "Counter2")
69 S2 = S;
70 }
71
72 EXPECT_NE(S1.hasValue(), false);
73 EXPECT_EQ(S2.hasValue(), false);
74
75 // Counter2 will be registered when it's first touched.
76 Counter2++;
77
78 const auto Range2 = GetStatistics();
79 EXPECT_NE(Range2.begin(), Range2.end());
80 EXPECT_EQ(Range2.begin() + 2, Range2.end());
81
82 S1 = None;
83 S2 = None;
84 for (const auto &S : Range2) {
85 if (std::string(S.first) == "Counter")
86 S1 = S;
87 if (std::string(S.first) == "Counter2")
88 S2 = S;
89 }
90
91 EXPECT_NE(S1.hasValue(), false);
92 EXPECT_NE(S2.hasValue(), false);
93
94 EXPECT_EQ(S1->first, "Counter");
95 EXPECT_EQ(S1->second, 2u);
96
97 EXPECT_EQ(S2->first, "Counter2");
98 EXPECT_EQ(S2->second, 1u);
99#else
100 Counter2++;
101 auto &Range = GetStatistics();
102 EXPECT_EQ(Range.begin(), Range.end());
103#endif
104}
105
106} // end anonymous namespace