1 //===- DebugCounterTest.cpp - Debug Counter 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 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/Support/DebugCounter.h"
10 #include "gmock/gmock.h"
11 
12 using namespace mlir;
13 
14 // DebugActionManager is only enabled in DEBUG mode.
15 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
16 
17 namespace {
18 
19 struct CounterAction : public DebugAction<> {
getTag__anon0710ea080111::CounterAction20   static StringRef getTag() { return "counter-action"; }
getDescription__anon0710ea080111::CounterAction21   static StringRef getDescription() { return "Test action for debug counters"; }
22 };
23 
TEST(DebugCounterTest,CounterTest)24 TEST(DebugCounterTest, CounterTest) {
25   std::unique_ptr<DebugCounter> counter = std::make_unique<DebugCounter>();
26   counter->addCounter(CounterAction::getTag(), /*countToSkip=*/1,
27                       /*countToStopAfter=*/3);
28 
29   DebugActionManager manager;
30   manager.registerActionHandler(std::move(counter));
31 
32   // The first execution is skipped.
33   EXPECT_FALSE(manager.shouldExecute<CounterAction>());
34 
35   // The counter stops after 3 successful executions.
36   EXPECT_TRUE(manager.shouldExecute<CounterAction>());
37   EXPECT_TRUE(manager.shouldExecute<CounterAction>());
38   EXPECT_TRUE(manager.shouldExecute<CounterAction>());
39   EXPECT_FALSE(manager.shouldExecute<CounterAction>());
40 }
41 
42 } // namespace
43 
44 #endif
45