-
Notifications
You must be signed in to change notification settings - Fork 16.1k
Expand file tree
/
Copy pathdebug_counter_test.cc
More file actions
130 lines (115 loc) · 4.19 KB
/
debug_counter_test.cc
File metadata and controls
130 lines (115 loc) · 4.19 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
//
#include <cstdlib>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "google/protobuf/port.h"
// Must be included last.
#include "google/protobuf/port_def.inc"
namespace {
using testing::AllOf;
using testing::ExitedWithCode;
using testing::HasSubstr;
using testing::Not;
auto MatchOutput(bool expect_output) {
const auto header = HasSubstr("Protobuf debug counters:");
const auto all = AllOf(header, //
HasSubstr("Foo :"),
HasSubstr(" Bar : 1 (33.33%)"),
HasSubstr(" Baz : 2 (66.67%)"),
HasSubstr(" Total : 3"), //
HasSubstr("Num :"),
HasSubstr(" 32 : 3 (75.00%)"),
HasSubstr(" 128 : 1 (25.00%)"),
HasSubstr(" Total : 4"));
return expect_output ? testing::Matcher<const std::string&>(all)
: testing::Matcher<const std::string&>(Not(header));
}
#ifdef GTEST_HAS_DEATH_TEST
TEST(DebugCounterTest, RealProvidesReportAtExit) {
EXPECT_EXIT(
{
static google::protobuf::internal::RealDebugCounter counter1("Foo.Bar");
static google::protobuf::internal::RealDebugCounter counter2("Foo.Baz");
static google::protobuf::internal::RealDebugCounter counter3("Num.32");
static google::protobuf::internal::RealDebugCounter counter4("Num.128");
counter1.Inc();
counter2.Inc();
counter2.Inc();
counter3.Inc();
counter3.Inc();
counter3.Inc();
counter4.Inc();
exit(0);
},
ExitedWithCode(0), MatchOutput(true));
}
TEST(DebugCounterTest, NoopDoesNotProvidesReportAtExit) {
EXPECT_EXIT(
{
static google::protobuf::internal::NoopDebugCounter counter1;
static google::protobuf::internal::NoopDebugCounter counter2;
counter1.Inc();
counter2.Inc();
counter2.Inc();
exit(0);
},
ExitedWithCode(0), MatchOutput(false));
// and test that the operations have no side effects.
static_assert((google::protobuf::internal::NoopDebugCounter().Inc(), true), "");
}
TEST(DebugCounterTest, MacroProvidesReportAtExitDependingOnBuild) {
#if defined(PROTOBUF_INTERNAL_ENABLE_DEBUG_COUNTERS)
constexpr bool match_output = true;
#else
constexpr bool match_output = false;
// and test that the operations have no side effects.
static_assert((PROTOBUF_DEBUG_COUNTER("Foo.Bar").Inc(), true), "");
#endif
EXPECT_EXIT(
{
PROTOBUF_DEBUG_COUNTER("Foo.Bar").Inc();
for (int i = 0; i < 2; ++i) {
PROTOBUF_DEBUG_COUNTER("Foo.Baz").Inc();
}
for (int i = 0; i < 3; ++i) {
PROTOBUF_DEBUG_COUNTER("Num.32").Inc();
}
PROTOBUF_DEBUG_COUNTER("Num.128").Inc();
exit(0);
},
ExitedWithCode(0), MatchOutput(match_output));
}
#endif // GTEST_HAS_DEATH_TEST
template <typename T>
void CounterOnATemplate() {
static google::protobuf::internal::RealDebugCounter counter("Foo.Bar");
counter.Inc();
}
// Regression test for counters on templates.
// It used to be that duplicate names would clobber each other so the total for
// the counter would only take one instantiation into account and undercount.
TEST(DebugCounterTest, DuplicateNamesWorkTogether) {
EXPECT_EXIT(
{
static google::protobuf::internal::RealDebugCounter counter("Foo.Baz");
CounterOnATemplate<int>();
CounterOnATemplate<int>();
CounterOnATemplate<double>();
counter.Inc();
counter.Inc();
exit(0);
},
ExitedWithCode(0),
AllOf(HasSubstr(" Bar : 3 (60.00%)"),
HasSubstr(" Baz : 2 (40.00%)"),
HasSubstr(" Total : 5")));
}
} // namespace
#include "google/protobuf/port_undef.inc"