//===- DebugActionTest.cpp - Debug Action Tests ---------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "mlir/Support/DebugAction.h" #include "gmock/gmock.h" // DebugActionManager is only enabled in DEBUG mode. #if LLVM_ENABLE_ABI_BREAKING_CHECKS using namespace mlir; namespace { struct SimpleAction : public DebugAction<> { static StringRef getTag() { return "simple-action"; } static StringRef getDescription() { return "simple-action-description"; } }; struct ParametricAction : public DebugAction { static StringRef getTag() { return "param-action"; } static StringRef getDescription() { return "param-action-description"; } }; TEST(DebugActionTest, GenericHandler) { DebugActionManager manager; // A generic handler that always executes the simple action, but not the // parametric action. struct GenericHandler : public DebugActionManager::GenericHandler { FailureOr shouldExecute(StringRef tag, StringRef desc) final { if (tag == SimpleAction::getTag()) { EXPECT_EQ(desc, SimpleAction::getDescription()); return true; } EXPECT_EQ(tag, ParametricAction::getTag()); EXPECT_EQ(desc, ParametricAction::getDescription()); return false; } }; manager.registerActionHandler(); EXPECT_TRUE(manager.shouldExecute()); EXPECT_FALSE(manager.shouldExecute(true)); } TEST(DebugActionTest, ActionSpecificHandler) { DebugActionManager manager; // Handler that simply uses the input as the decider. struct ActionSpecificHandler : public ParametricAction::Handler { FailureOr shouldExecute(bool shouldExecuteParam) final { return shouldExecuteParam; } }; manager.registerActionHandler(); EXPECT_TRUE(manager.shouldExecute(true)); EXPECT_FALSE(manager.shouldExecute(false)); // There is no handler for the simple action, so it is always executed. EXPECT_TRUE(manager.shouldExecute()); } TEST(DebugActionTest, DebugCounterHandler) { DebugActionManager manager; // Handler that uses the number of action executions as the decider. struct DebugCounterHandler : public SimpleAction::Handler { FailureOr shouldExecute() final { return numExecutions++ < 3; } unsigned numExecutions = 0; }; manager.registerActionHandler(); // Check that the action is executed 3 times, but no more after. EXPECT_TRUE(manager.shouldExecute()); EXPECT_TRUE(manager.shouldExecute()); EXPECT_TRUE(manager.shouldExecute()); EXPECT_FALSE(manager.shouldExecute()); EXPECT_FALSE(manager.shouldExecute()); } } // namespace #endif