xref: /llvm-project/mlir/unittests/Debug/DebugCounterTest.cpp (revision 9b1fe5649e546863adba3fdde158b50a213ea954)
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 
TEST(DebugCounterTest,CounterTest)23 TEST(DebugCounterTest, CounterTest) {
24   DebugCounter counter;
25   counter.addCounter(CounterAction::tag, /*countToSkip=*/1,
26                      /*countToStopAfter=*/3);
27 
28   int count = 0;
29   auto noOp = [&]() {
30     ++count;
31     return;
32   };
33 
34   // The first execution is skipped.
35   counter(noOp, CounterAction{});
36   EXPECT_EQ(count, 0);
37 
38   // The counter stops after 3 successful executions.
39   counter(noOp, CounterAction{});
40   EXPECT_EQ(count, 1);
41   counter(noOp, CounterAction{});
42   EXPECT_EQ(count, 2);
43   counter(noOp, CounterAction{});
44   EXPECT_EQ(count, 3);
45   counter(noOp, CounterAction{});
46   EXPECT_EQ(count, 3);
47 }
48 
49 } // namespace
50