protobuf/src/google/protobuf/stubs/common_unittest.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 
33 #include <vector>
34 #include <google/protobuf/stubs/callback.h>
35 #include <google/protobuf/stubs/casts.h>
36 #include <google/protobuf/stubs/common.h>
37 #include <google/protobuf/stubs/logging.h>
38 #include <google/protobuf/stubs/strutil.h>
39 #include <google/protobuf/stubs/substitute.h>
40 
41 #include <google/protobuf/testing/googletest.h>
42 #include <gtest/gtest.h>
43 
44 namespace google {
45 namespace protobuf {
46 namespace {
47 
48 // TODO(kenton): More tests.
49 
50 #ifdef PACKAGE_VERSION // only defined when using automake, not MSVC
51 
52 TEST(VersionTest, VersionMatchesConfig) {
53  // Verify that the version string specified in config.h matches the one
54  // in common.h. The config.h version is a string which may have a suffix
55  // like "beta" or "rc1", so we remove that.
57  int pos = 0;
58  while (pos < version.size() &&
59  (ascii_isdigit(version[pos]) || version[pos] == '.')) {
60  ++pos;
61  }
62  version.erase(pos);
63 
65 }
66 
67 #endif // PACKAGE_VERSION
68 
69 TEST(CommonTest, IntMinMaxConstants) {
70  // kint32min was declared incorrectly in the first release of protobufs.
71  // Ugh.
73  EXPECT_EQ(static_cast<uint32>(kint32min), static_cast<uint32>(kint32max) + 1);
75  EXPECT_EQ(static_cast<uint64>(kint64min), static_cast<uint64>(kint64max) + 1);
76  EXPECT_EQ(0, kuint32max + 1);
77  EXPECT_EQ(0, kuint64max + 1);
78 }
79 
80 std::vector<std::string> captured_messages_;
81 
82 void CaptureLog(LogLevel level, const char* filename, int line,
83  const std::string& message) {
84  captured_messages_.push_back(
85  strings::Substitute("$0 $1:$2: $3",
86  implicit_cast<int>(level), filename, line, message));
87 }
88 
89 TEST(LoggingTest, DefaultLogging) {
91  int line = __LINE__;
92  GOOGLE_LOG(INFO ) << "A message.";
93  GOOGLE_LOG(WARNING) << "A warning.";
94  GOOGLE_LOG(ERROR ) << "An error.";
95 
97  EXPECT_EQ(
98  "[libprotobuf INFO " __FILE__ ":" + SimpleItoa(line + 1) + "] A message.\n"
99  "[libprotobuf WARNING " __FILE__ ":" + SimpleItoa(line + 2) + "] A warning.\n"
100  "[libprotobuf ERROR " __FILE__ ":" + SimpleItoa(line + 3) + "] An error.\n",
101  text);
102 }
103 
104 TEST(LoggingTest, NullLogging) {
105  LogHandler* old_handler = SetLogHandler(nullptr);
106 
108  GOOGLE_LOG(INFO ) << "A message.";
109  GOOGLE_LOG(WARNING) << "A warning.";
110  GOOGLE_LOG(ERROR ) << "An error.";
111 
112  EXPECT_TRUE(SetLogHandler(old_handler) == nullptr);
113 
115  EXPECT_EQ("", text);
116 }
117 
118 TEST(LoggingTest, CaptureLogging) {
119  captured_messages_.clear();
120 
121  LogHandler* old_handler = SetLogHandler(&CaptureLog);
122 
123  int start_line = __LINE__;
124  GOOGLE_LOG(ERROR) << "An error.";
125  GOOGLE_LOG(WARNING) << "A warning.";
126 
127  EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
128 
129  ASSERT_EQ(2, captured_messages_.size());
130  EXPECT_EQ(
131  "2 " __FILE__ ":" + SimpleItoa(start_line + 1) + ": An error.",
132  captured_messages_[0]);
133  EXPECT_EQ(
134  "1 " __FILE__ ":" + SimpleItoa(start_line + 2) + ": A warning.",
135  captured_messages_[1]);
136 }
137 
138 TEST(LoggingTest, SilenceLogging) {
139  captured_messages_.clear();
140 
141  LogHandler* old_handler = SetLogHandler(&CaptureLog);
142 
143  int line1 = __LINE__; GOOGLE_LOG(INFO) << "Visible1";
144  LogSilencer* silencer1 = new LogSilencer;
145  GOOGLE_LOG(INFO) << "Not visible.";
146  LogSilencer* silencer2 = new LogSilencer;
147  GOOGLE_LOG(INFO) << "Not visible.";
148  delete silencer1;
149  GOOGLE_LOG(INFO) << "Not visible.";
150  delete silencer2;
151  int line2 = __LINE__; GOOGLE_LOG(INFO) << "Visible2";
152 
153  EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
154 
155  ASSERT_EQ(2, captured_messages_.size());
156  EXPECT_EQ(
157  "0 " __FILE__ ":" + SimpleItoa(line1) + ": Visible1",
158  captured_messages_[0]);
159  EXPECT_EQ(
160  "0 " __FILE__ ":" + SimpleItoa(line2) + ": Visible2",
161  captured_messages_[1]);
162 }
163 
164 class ClosureTest : public testing::Test {
165  public:
166  void SetA123Method() { a_ = 123; }
167  static void SetA123Function() { current_instance_->a_ = 123; }
168 
169  void SetAMethod(int a) { a_ = a; }
170  void SetCMethod(std::string c) { c_ = c; }
171 
172  static void SetAFunction(int a) { current_instance_->a_ = a; }
173  static void SetCFunction(std::string c) { current_instance_->c_ = c; }
174 
175  void SetABMethod(int a, const char* b) { a_ = a; b_ = b; }
176  static void SetABFunction(int a, const char* b) {
177  current_instance_->a_ = a;
178  current_instance_->b_ = b;
179  }
180 
181  virtual void SetUp() {
182  current_instance_ = this;
183  a_ = 0;
184  b_ = nullptr;
185  c_.clear();
186  permanent_closure_ = nullptr;
187  }
188 
189  void DeleteClosureInCallback() {
190  delete permanent_closure_;
191  }
192 
193  int a_;
194  const char* b_;
197 
198  static ClosureTest* current_instance_;
199 };
200 
201 ClosureTest* ClosureTest::current_instance_ = nullptr;
202 
203 TEST_F(ClosureTest, TestClosureFunction0) {
204  Closure* closure = NewCallback(&SetA123Function);
205  EXPECT_NE(123, a_);
206  closure->Run();
207  EXPECT_EQ(123, a_);
208 }
209 
210 TEST_F(ClosureTest, TestClosureMethod0) {
212  &ClosureTest::SetA123Method);
213  EXPECT_NE(123, a_);
214  closure->Run();
215  EXPECT_EQ(123, a_);
216 }
217 
218 TEST_F(ClosureTest, TestClosureFunction1) {
219  Closure* closure = NewCallback(&SetAFunction, 456);
220  EXPECT_NE(456, a_);
221  closure->Run();
222  EXPECT_EQ(456, a_);
223 }
224 
225 TEST_F(ClosureTest, TestClosureMethod1) {
227  &ClosureTest::SetAMethod, 456);
228  EXPECT_NE(456, a_);
229  closure->Run();
230  EXPECT_EQ(456, a_);
231 }
232 
233 TEST_F(ClosureTest, TestClosureFunction1String) {
234  Closure* closure = NewCallback(&SetCFunction, std::string("test"));
235  EXPECT_NE("test", c_);
236  closure->Run();
237  EXPECT_EQ("test", c_);
238 }
239 
240 TEST_F(ClosureTest, TestClosureMethod1String) {
241  Closure* closure = NewCallback(current_instance_, &ClosureTest::SetCMethod,
242  std::string("test"));
243  EXPECT_NE("test", c_);
244  closure->Run();
245  EXPECT_EQ("test", c_);
246 }
247 
248 TEST_F(ClosureTest, TestClosureFunction2) {
249  const char* cstr = "hello";
250  Closure* closure = NewCallback(&SetABFunction, 789, cstr);
251  EXPECT_NE(789, a_);
252  EXPECT_NE(cstr, b_);
253  closure->Run();
254  EXPECT_EQ(789, a_);
255  EXPECT_EQ(cstr, b_);
256 }
257 
258 TEST_F(ClosureTest, TestClosureMethod2) {
259  const char* cstr = "hello";
261  &ClosureTest::SetABMethod, 789, cstr);
262  EXPECT_NE(789, a_);
263  EXPECT_NE(cstr, b_);
264  closure->Run();
265  EXPECT_EQ(789, a_);
266  EXPECT_EQ(cstr, b_);
267 }
268 
269 // Repeat all of the above with NewPermanentCallback()
270 
271 TEST_F(ClosureTest, TestPermanentClosureFunction0) {
272  Closure* closure = NewPermanentCallback(&SetA123Function);
273  EXPECT_NE(123, a_);
274  closure->Run();
275  EXPECT_EQ(123, a_);
276  a_ = 0;
277  closure->Run();
278  EXPECT_EQ(123, a_);
279  delete closure;
280 }
281 
282 TEST_F(ClosureTest, TestPermanentClosureMethod0) {
284  &ClosureTest::SetA123Method);
285  EXPECT_NE(123, a_);
286  closure->Run();
287  EXPECT_EQ(123, a_);
288  a_ = 0;
289  closure->Run();
290  EXPECT_EQ(123, a_);
291  delete closure;
292 }
293 
294 TEST_F(ClosureTest, TestPermanentClosureFunction1) {
295  Closure* closure = NewPermanentCallback(&SetAFunction, 456);
296  EXPECT_NE(456, a_);
297  closure->Run();
298  EXPECT_EQ(456, a_);
299  a_ = 0;
300  closure->Run();
301  EXPECT_EQ(456, a_);
302  delete closure;
303 }
304 
305 TEST_F(ClosureTest, TestPermanentClosureMethod1) {
307  &ClosureTest::SetAMethod, 456);
308  EXPECT_NE(456, a_);
309  closure->Run();
310  EXPECT_EQ(456, a_);
311  a_ = 0;
312  closure->Run();
313  EXPECT_EQ(456, a_);
314  delete closure;
315 }
316 
317 TEST_F(ClosureTest, TestPermanentClosureFunction2) {
318  const char* cstr = "hello";
319  Closure* closure = NewPermanentCallback(&SetABFunction, 789, cstr);
320  EXPECT_NE(789, a_);
321  EXPECT_NE(cstr, b_);
322  closure->Run();
323  EXPECT_EQ(789, a_);
324  EXPECT_EQ(cstr, b_);
325  a_ = 0;
326  b_ = nullptr;
327  closure->Run();
328  EXPECT_EQ(789, a_);
329  EXPECT_EQ(cstr, b_);
330  delete closure;
331 }
332 
333 TEST_F(ClosureTest, TestPermanentClosureMethod2) {
334  const char* cstr = "hello";
336  &ClosureTest::SetABMethod, 789, cstr);
337  EXPECT_NE(789, a_);
338  EXPECT_NE(cstr, b_);
339  closure->Run();
340  EXPECT_EQ(789, a_);
341  EXPECT_EQ(cstr, b_);
342  a_ = 0;
343  b_ = nullptr;
344  closure->Run();
345  EXPECT_EQ(789, a_);
346  EXPECT_EQ(cstr, b_);
347  delete closure;
348 }
349 
350 TEST_F(ClosureTest, TestPermanentClosureDeleteInCallback) {
351  permanent_closure_ = NewPermanentCallback((ClosureTest*) this,
352  &ClosureTest::DeleteClosureInCallback);
354 }
355 
356 } // anonymous namespace
357 } // namespace protobuf
358 } // namespace google
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
pos
int pos
Definition: libuv/docs/code/tty-gravity/main.c:11
a_
int a_
Definition: protobuf/src/google/protobuf/stubs/common_unittest.cc:193
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::uint32
uint32_t uint32
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:155
google::protobuf::LogLevel
LogLevel
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:45
google::protobuf::Closure::Run
virtual void Run()=0
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
version
Definition: version.py:1
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
c_
std::string c_
Definition: protobuf/src/google/protobuf/stubs/common_unittest.cc:195
google::protobuf::kint64min
static const int64 kint64min
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:162
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
b_
const char * b_
Definition: protobuf/src/google/protobuf/stubs/common_unittest.cc:194
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
google::protobuf::NewCallback
Closure * NewCallback(void(*function)())
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/callback.h:415
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
current_instance_
static ClosureTest * current_instance_
Definition: protobuf/src/google/protobuf/stubs/common_unittest.cc:198
permanent_closure_
Closure * permanent_closure_
Definition: protobuf/src/google/protobuf/stubs/common_unittest.cc:196
GOOGLE_PROTOBUF_VERSION
#define GOOGLE_PROTOBUF_VERSION
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/common.h:84
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
google::protobuf::CaptureTestStderr
void CaptureTestStderr()
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.cc:215
google::protobuf::GetCapturedTestStderr
string GetCapturedTestStderr()
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.cc:245
python_utils.jobset.INFO
INFO
Definition: jobset.py:111
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
closure
grpc_closure closure
Definition: src/core/lib/surface/server.cc:466
google::protobuf::uint64
uint64_t uint64
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:156
google::protobuf::WARNING
static const LogLevel WARNING
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.h:71
google::protobuf::NewPermanentCallback
Closure * NewPermanentCallback(void(*function)())
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/callback.h:420
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: bloaty/third_party/protobuf/src/google/protobuf/arena_unittest.cc:156
google::protobuf::strings::Substitute
string Substitute(const char *format, const SubstituteArg &arg0, const SubstituteArg &arg1, const SubstituteArg &arg2, const SubstituteArg &arg3, const SubstituteArg &arg4, const SubstituteArg &arg5, const SubstituteArg &arg6, const SubstituteArg &arg7, const SubstituteArg &arg8, const SubstituteArg &arg9)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.cc:55
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
google::protobuf::ERROR
static const LogLevel ERROR
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.h:70
google::protobuf::kuint32max
static const uint32 kuint32max
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:163
google::protobuf.internal::VersionString
string VersionString(int version)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/common.cc:98
google::protobuf::kint32max
static const int32 kint32max
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:159
google::protobuf::SetLogHandler
LogHandler * SetLogHandler(LogHandler *new_func)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/common.cc:271
google::protobuf::TEST_F
TEST_F(DynamicMessageTest, Descriptor)
Definition: bloaty/third_party/protobuf/src/google/protobuf/dynamic_message_unittest.cc:126
google::protobuf::ascii_isdigit
bool ascii_isdigit(char c)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.h:73
google::protobuf::SimpleItoa
string SimpleItoa(int i)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1138
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
regen-readme.line
line
Definition: regen-readme.py:30
closure
Definition: proxy.cc:59
PACKAGE_VERSION
#define PACKAGE_VERSION
Definition: config_freebsd/ares_config.h:381
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
CommonTest
Definition: bloaty/third_party/googletest/googletest/test/gtest-typed-test_test.cc:49
google::protobuf::kint64max
static const int64 kint64max
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:161
google::protobuf::kuint64max
static const uint64 kuint64max
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:164
client.level
level
Definition: examples/python/async_streaming/client.py:118
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:146
google::protobuf::LogHandler
void LogHandler(LogLevel level, const char *filename, int line, const std::string &message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:205
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
google::protobuf::kint32min
static const int32 kint32min
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:160


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:59