Debian Codegraph @

llvm-project/mlir/unittests/Support/DebugCounterTest.cpp

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