xref: /llvm-project/mlir/unittests/Debug/DebugCounterTest.cpp (revision 28d04c564debff75c6d1f262a621aa4246e761ae)
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/Debug/Counter.h"
10 #include "mlir/Support/TypeID.h"
11 #include "gmock/gmock.h"
12 
13 using namespace mlir;
14 using namespace mlir::tracing;
15 
16 namespace {
17 
18 struct CounterAction : public ActionImpl<CounterAction> {
19   MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CounterAction)
20   static constexpr StringLiteral tag = "counter-action";
21 };
22 
23 TEST(DebugCounterTest, CounterTest) {
24   std::unique_ptr<DebugCounter> counter = std::make_unique<DebugCounter>();
25   counter->addCounter(CounterAction::tag, /*countToSkip=*/1,
26                       /*countToStopAfter=*/3);
27 
28   ActionManager manager;
29   manager.registerActionHandler(std::move(counter));
30 
31   auto noOp = []() { return; };
32 
33   // The first execution is skipped.
34   EXPECT_FALSE(manager.execute<CounterAction>(noOp));
35 
36   // The counter stops after 3 successful executions.
37   EXPECT_TRUE(manager.execute<CounterAction>(noOp));
38   EXPECT_TRUE(manager.execute<CounterAction>(noOp));
39   EXPECT_TRUE(manager.execute<CounterAction>(noOp));
40   EXPECT_FALSE(manager.execute<CounterAction>(noOp));
41 }
42 
43 } // namespace
44