44#include " benchmark/benchmark.h"
55#include " gtest/gtest.h"
66
7- #define N_REPETITIONS 100
8- #define N_ITERATIONS 1
7+ namespace {
98
109using benchmark::ClearRegisteredBenchmarks;
1110using benchmark::ConsoleReporter;
@@ -15,31 +14,32 @@ using benchmark::RunSpecifiedBenchmarks;
1514using benchmark::State;
1615using benchmark::internal::Benchmark;
1716
18- namespace counts {
17+ constexpr int N_REPETITIONS = 100 ;
18+ constexpr int N_ITERATIONS = 1 ;
19+
1920int num_allocs = 0 ;
2021int max_bytes_used = 0 ;
2122int total_allocated_bytes = 0 ;
2223int net_heap_growth = 0 ;
24+
2325void reset () {
2426 num_allocs = 0 ;
2527 max_bytes_used = 0 ;
2628 total_allocated_bytes = 0 ;
2729 net_heap_growth = 0 ;
2830}
29- } // namespace counts
30-
3131class TestMemoryManager : public MemoryManager {
3232 void Start () override {}
3333 void Stop (Result& result) override {
34- result.num_allocs = counts:: num_allocs;
35- result.net_heap_growth = counts:: net_heap_growth;
36- result.max_bytes_used = counts:: max_bytes_used;
37- result.total_allocated_bytes = counts:: total_allocated_bytes;
38-
39- counts:: num_allocs += 1 ;
40- counts:: max_bytes_used += 2 ;
41- counts:: net_heap_growth += 4 ;
42- counts:: total_allocated_bytes += 10 ;
34+ result.num_allocs = num_allocs;
35+ result.net_heap_growth = net_heap_growth;
36+ result.max_bytes_used = max_bytes_used;
37+ result.total_allocated_bytes = total_allocated_bytes;
38+
39+ num_allocs += 1 ;
40+ max_bytes_used += 2 ;
41+ net_heap_growth += 4 ;
42+ total_allocated_bytes += 10 ;
4343 }
4444};
4545
@@ -73,6 +73,7 @@ class MemoryResultsTest : public testing::Test {
7373 });
7474 bm->Repetitions (N_REPETITIONS);
7575 bm->Iterations (N_ITERATIONS);
76+ reset ();
7677 }
7778 void TearDown () override { ClearRegisteredBenchmarks (); }
7879};
@@ -83,9 +84,8 @@ TEST_F(MemoryResultsTest, NoMMTest) {
8384}
8485
8586TEST_F (MemoryResultsTest, ResultsTest) {
86- counts::reset ();
87- MemoryManager* mm = new TestMemoryManager;
88- RegisterMemoryManager (mm);
87+ auto mm = std::make_unique<TestMemoryManager>();
88+ RegisterMemoryManager (mm.get ());
8989
9090 RunSpecifiedBenchmarks (&reporter);
9191 EXPECT_EQ (reporter.store .size (), N_REPETITIONS);
@@ -97,6 +97,6 @@ TEST_F(MemoryResultsTest, ResultsTest) {
9797 EXPECT_EQ (reporter.store [i].total_allocated_bytes ,
9898 static_cast <int64_t >(i) * 10 );
9999 }
100-
101- delete mm;
102100}
101+
102+ } // namespace
0 commit comments