1 //===- llvm/unittest/Support/DebugCounterTest.cpp -------------------------===//
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 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/DebugCounter.h"
10 #include "gtest/gtest.h"
11
12 #include <string>
13 using namespace llvm;
14
15 #ifndef NDEBUG
TEST(DebugCounterTest,Basic)16 TEST(DebugCounterTest, Basic) {
17 DEBUG_COUNTER(TestCounter, "test-counter", "Counter used for unit test");
18
19 EXPECT_FALSE(DebugCounter::isCounterSet(TestCounter));
20 auto DC = &DebugCounter::instance();
21 DC->push_back("test-counter=1:3-5:78:79:89:100-102:150");
22
23 EXPECT_TRUE(DebugCounter::isCounterSet(TestCounter));
24
25 SmallVector<unsigned> Res;
26 for (unsigned Idx = 0; Idx < 200; Idx++) {
27 if (DebugCounter::shouldExecute(TestCounter))
28 Res.push_back(Idx);
29 }
30
31 SmallVector<unsigned> Expected = {1, 3, 4, 5, 78, 79, 89, 100, 101, 102, 150};
32 EXPECT_EQ(Expected, Res);
33
34 std::string Str;
35 llvm::raw_string_ostream OS(Str);
36 DC->print(OS);
37 EXPECT_TRUE(StringRef(Str).contains("{200,1:3-5:78:79:89:100-102:150}"));
38 }
39
40 #endif
41