summaryrefslogtreecommitdiff
path: root/mlir/unittests/Support/DebugActionTest.cpp
blob: 0a73436f572d781824f2f988808190af6f2b862d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//===- 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<bool> {
  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<bool> 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<GenericHandler>();

  EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
  EXPECT_FALSE(manager.shouldExecute<ParametricAction>(true));
}

TEST(DebugActionTest, ActionSpecificHandler) {
  DebugActionManager manager;

  // Handler that simply uses the input as the decider.
  struct ActionSpecificHandler : public ParametricAction::Handler {
    FailureOr<bool> shouldExecute(bool shouldExecuteParam) final {
      return shouldExecuteParam;
    }
  };
  manager.registerActionHandler<ActionSpecificHandler>();

  EXPECT_TRUE(manager.shouldExecute<ParametricAction>(true));
  EXPECT_FALSE(manager.shouldExecute<ParametricAction>(false));

  // There is no handler for the simple action, so it is always executed.
  EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
}

TEST(DebugActionTest, DebugCounterHandler) {
  DebugActionManager manager;

  // Handler that uses the number of action executions as the decider.
  struct DebugCounterHandler : public SimpleAction::Handler {
    FailureOr<bool> shouldExecute() final {
      return numExecutions++ < 3;
    }
    unsigned numExecutions = 0;
  };
  manager.registerActionHandler<DebugCounterHandler>();

  // Check that the action is executed 3 times, but no more after.
  EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
  EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
  EXPECT_TRUE(manager.shouldExecute<SimpleAction>());
  EXPECT_FALSE(manager.shouldExecute<SimpleAction>());
  EXPECT_FALSE(manager.shouldExecute<SimpleAction>());
}

} // namespace

#endif