1 //===- DebugActionTest.cpp - Debug Action 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/DebugAction.h"
10 #include "gmock/gmock.h"
11
12 // DebugActionManager is only enabled in DEBUG mode.
13 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
14
15 using namespace mlir;
16
17 namespace {
18 struct SimpleAction : public DebugAction<> {
getTag__anonf43d55660111::SimpleAction19 static StringRef getTag() { return "simple-action"; }
getDescription__anonf43d55660111::SimpleAction20 static StringRef getDescription() { return "simple-action-description"; }
21 };
22 struct ParametricAction : public DebugAction<bool> {
getTag__anonf43d55660111::ParametricAction23 static StringRef getTag() { return "param-action"; }
getDescription__anonf43d55660111::ParametricAction24 static StringRef getDescription() { return "param-action-description"; }
25 };
26
TEST(DebugActionTest,GenericHandler)27 TEST(DebugActionTest, GenericHandler) {
28 DebugActionManager manager;
29
30 // A generic handler that always executes the simple action, but not the
31 // parametric action.
32 struct GenericHandler : public DebugActionManager::GenericHandler {
33 FailureOr<bool> shouldExecute(StringRef tag, StringRef desc) final {
34 if (tag == SimpleAction::getTag()) {
35 EXPECT_EQ(desc, SimpleAction::getDescription());
36 return true;
37 }
38
39 EXPECT_EQ(tag, ParametricAction::getTag());
40 EXPECT_EQ(desc, ParametricAction::getDescription());
41 return false;
42 }
43 };
44 manager.registerActionHandler<GenericHandler>();
45
46 EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
47 EXPECT_FALSE(manager.shouldExecute<ParametricAction>(true));
48 }
49
TEST(DebugActionTest,ActionSpecificHandler)50 TEST(DebugActionTest, ActionSpecificHandler) {
51 DebugActionManager manager;
52
53 // Handler that simply uses the input as the decider.
54 struct ActionSpecificHandler : public ParametricAction::Handler {
55 FailureOr<bool> shouldExecute(bool shouldExecuteParam) final {
56 return shouldExecuteParam;
57 }
58 };
59 manager.registerActionHandler<ActionSpecificHandler>();
60
61 EXPECT_TRUE(manager.shouldExecute<ParametricAction>(true));
62 EXPECT_FALSE(manager.shouldExecute<ParametricAction>(false));
63
64 // There is no handler for the simple action, so it is always executed.
65 EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
66 }
67
TEST(DebugActionTest,DebugCounterHandler)68 TEST(DebugActionTest, DebugCounterHandler) {
69 DebugActionManager manager;
70
71 // Handler that uses the number of action executions as the decider.
72 struct DebugCounterHandler : public SimpleAction::Handler {
73 FailureOr<bool> shouldExecute() final { return numExecutions++ < 3; }
74 unsigned numExecutions = 0;
75 };
76 manager.registerActionHandler<DebugCounterHandler>();
77
78 // Check that the action is executed 3 times, but no more after.
79 EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
80 EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
81 EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
82 EXPECT_FALSE(manager.shouldExecute<SimpleAction>());
83 EXPECT_FALSE(manager.shouldExecute<SimpleAction>());
84 }
85
86 } // namespace
87
88 #endif
89