Files
llvm-project/llvm/unittests/Support/DebugCounterTest.cpp
Benjamin Stott 202adc372e [Support] Introduce a function to reset all debug counters (#194864)
This PR adds a function to reset all debug counters, and extends the
unit test to verify that the debug counters are reset as expected. This
is required for running tools repeatedly in the same process.
2026-04-29 17:49:14 +01:00

54 lines
1.6 KiB
C++

//===- llvm/unittest/Support/DebugCounterTest.cpp -------------------------===//
//
// 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 "llvm/Support/DebugCounter.h"
#include "gtest/gtest.h"
#include <string>
using namespace llvm;
#ifndef NDEBUG
TEST(DebugCounterTest, Basic) {
DEBUG_COUNTER(TestCounter, "test-counter", "Counter used for unit test");
EXPECT_FALSE(DebugCounter::isCounterSet(TestCounter));
auto DC = &DebugCounter::instance();
DC->push_back("test-counter=1:3-5:78:79:89:100-102:150");
EXPECT_TRUE(DebugCounter::isCounterSet(TestCounter));
SmallVector<unsigned> Res;
for (unsigned Idx = 0; Idx < 200; Idx++) {
if (DebugCounter::shouldExecute(TestCounter))
Res.push_back(Idx);
}
SmallVector<unsigned> Expected = {1, 3, 4, 5, 78, 79, 89, 100, 101, 102, 150};
EXPECT_EQ(Expected, Res);
std::string Str;
llvm::raw_string_ostream OS(Str);
DC->print(OS);
EXPECT_TRUE(StringRef(Str).contains("{200,1:3-5:78:79:89:100-102:150}"));
DC->resetAllCounters();
// After resetting, counter is no longer set.
EXPECT_FALSE(DebugCounter::isCounterSet(TestCounter));
DC->push_back("test-counter=1");
EXPECT_TRUE(DebugCounter::isCounterSet(TestCounter));
EXPECT_EQ(DC->getCounterState(TestCounter).Count, 0);
DC->shouldExecute(TestCounter);
EXPECT_EQ(DC->getCounterState(TestCounter).Count, 1);
}
#endif