1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
34 #include <google/protobuf/stubs/common.h>
35 #include <google/protobuf/stubs/strutil.h>
36 #include <google/protobuf/stubs/substitute.h>
38 #include <google/protobuf/testing/googletest.h>
39 #include <gtest/gtest.h>
47 // TODO(kenton): More tests.
49 #ifdef PACKAGE_VERSION // only defined when using automake, not MSVC
51 TEST(VersionTest, VersionMatchesConfig) {
52 // Verify that the version string specified in config.h matches the one
53 // in common.h. The config.h version is a string which may have a suffix
54 // like "beta" or "rc1", so we remove that.
55 string version = PACKAGE_VERSION;
57 while (pos < version.size() &&
58 (ascii_isdigit(version[pos]) || version[pos] == '.')) {
63 EXPECT_EQ(version, internal::VersionString(GOOGLE_PROTOBUF_VERSION));
66 #endif // PACKAGE_VERSION
68 TEST(CommonTest, IntMinMaxConstants) {
69 // kint32min was declared incorrectly in the first release of protobufs.
71 EXPECT_LT(kint32min, kint32max);
72 EXPECT_EQ(static_cast<uint32>(kint32min), static_cast<uint32>(kint32max) + 1);
73 EXPECT_LT(kint64min, kint64max);
74 EXPECT_EQ(static_cast<uint64>(kint64min), static_cast<uint64>(kint64max) + 1);
75 EXPECT_EQ(0, kuint32max + 1);
76 EXPECT_EQ(0, kuint64max + 1);
79 vector<string> captured_messages_;
81 void CaptureLog(LogLevel level, const char* filename, int line,
82 const string& message) {
83 captured_messages_.push_back(
84 strings::Substitute("$0 $1:$2: $3",
85 implicit_cast<int>(level), filename, line, message));
88 TEST(LoggingTest, DefaultLogging) {
91 GOOGLE_LOG(INFO ) << "A message.";
92 GOOGLE_LOG(WARNING) << "A warning.";
93 GOOGLE_LOG(ERROR ) << "An error.";
95 string text = GetCapturedTestStderr();
97 "[libprotobuf INFO "__FILE__":" + SimpleItoa(line + 1) + "] A message.\n"
98 "[libprotobuf WARNING "__FILE__":" + SimpleItoa(line + 2) + "] A warning.\n"
99 "[libprotobuf ERROR "__FILE__":" + SimpleItoa(line + 3) + "] An error.\n",
103 TEST(LoggingTest, NullLogging) {
104 LogHandler* old_handler = SetLogHandler(NULL);
107 GOOGLE_LOG(INFO ) << "A message.";
108 GOOGLE_LOG(WARNING) << "A warning.";
109 GOOGLE_LOG(ERROR ) << "An error.";
111 EXPECT_TRUE(SetLogHandler(old_handler) == NULL);
113 string text = GetCapturedTestStderr();
117 TEST(LoggingTest, CaptureLogging) {
118 captured_messages_.clear();
120 LogHandler* old_handler = SetLogHandler(&CaptureLog);
122 int start_line = __LINE__;
123 GOOGLE_LOG(ERROR) << "An error.";
124 GOOGLE_LOG(WARNING) << "A warning.";
126 EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
128 ASSERT_EQ(2, captured_messages_.size());
130 "2 "__FILE__":" + SimpleItoa(start_line + 1) + ": An error.",
131 captured_messages_[0]);
133 "1 "__FILE__":" + SimpleItoa(start_line + 2) + ": A warning.",
134 captured_messages_[1]);
137 TEST(LoggingTest, SilenceLogging) {
138 captured_messages_.clear();
140 LogHandler* old_handler = SetLogHandler(&CaptureLog);
142 int line1 = __LINE__; GOOGLE_LOG(INFO) << "Visible1";
143 LogSilencer* silencer1 = new LogSilencer;
144 GOOGLE_LOG(INFO) << "Not visible.";
145 LogSilencer* silencer2 = new LogSilencer;
146 GOOGLE_LOG(INFO) << "Not visible.";
148 GOOGLE_LOG(INFO) << "Not visible.";
150 int line2 = __LINE__; GOOGLE_LOG(INFO) << "Visible2";
152 EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
154 ASSERT_EQ(2, captured_messages_.size());
156 "0 "__FILE__":" + SimpleItoa(line1) + ": Visible1",
157 captured_messages_[0]);
159 "0 "__FILE__":" + SimpleItoa(line2) + ": Visible2",
160 captured_messages_[1]);
163 class ClosureTest : public testing::Test {
165 void SetA123Method() { a_ = 123; }
166 static void SetA123Function() { current_instance_->a_ = 123; }
168 void SetAMethod(int a) { a_ = a; }
169 void SetCMethod(string c) { c_ = c; }
171 static void SetAFunction(int a) { current_instance_->a_ = a; }
172 static void SetCFunction(string c) { current_instance_->c_ = c; }
174 void SetABMethod(int a, const char* b) { a_ = a; b_ = b; }
175 static void SetABFunction(int a, const char* b) {
176 current_instance_->a_ = a;
177 current_instance_->b_ = b;
180 virtual void SetUp() {
181 current_instance_ = this;
185 permanent_closure_ = NULL;
188 void DeleteClosureInCallback() {
189 delete permanent_closure_;
195 Closure* permanent_closure_;
197 static ClosureTest* current_instance_;
200 ClosureTest* ClosureTest::current_instance_ = NULL;
202 TEST_F(ClosureTest, TestClosureFunction0) {
203 Closure* closure = NewCallback(&SetA123Function);
209 TEST_F(ClosureTest, TestClosureMethod0) {
210 Closure* closure = NewCallback(current_instance_,
211 &ClosureTest::SetA123Method);
217 TEST_F(ClosureTest, TestClosureFunction1) {
218 Closure* closure = NewCallback(&SetAFunction, 456);
224 TEST_F(ClosureTest, TestClosureMethod1) {
225 Closure* closure = NewCallback(current_instance_,
226 &ClosureTest::SetAMethod, 456);
232 TEST_F(ClosureTest, TestClosureFunction1String) {
233 Closure* closure = NewCallback(&SetCFunction, string("test"));
234 EXPECT_NE("test", c_);
236 EXPECT_EQ("test", c_);
239 TEST_F(ClosureTest, TestClosureMethod1String) {
240 Closure* closure = NewCallback(current_instance_,
241 &ClosureTest::SetCMethod, string("test"));
242 EXPECT_NE("test", c_);
244 EXPECT_EQ("test", c_);
247 TEST_F(ClosureTest, TestClosureFunction2) {
248 const char* cstr = "hello";
249 Closure* closure = NewCallback(&SetABFunction, 789, cstr);
257 TEST_F(ClosureTest, TestClosureMethod2) {
258 const char* cstr = "hello";
259 Closure* closure = NewCallback(current_instance_,
260 &ClosureTest::SetABMethod, 789, cstr);
268 // Repeat all of the above with NewPermanentCallback()
270 TEST_F(ClosureTest, TestPermanentClosureFunction0) {
271 Closure* closure = NewPermanentCallback(&SetA123Function);
281 TEST_F(ClosureTest, TestPermanentClosureMethod0) {
282 Closure* closure = NewPermanentCallback(current_instance_,
283 &ClosureTest::SetA123Method);
293 TEST_F(ClosureTest, TestPermanentClosureFunction1) {
294 Closure* closure = NewPermanentCallback(&SetAFunction, 456);
304 TEST_F(ClosureTest, TestPermanentClosureMethod1) {
305 Closure* closure = NewPermanentCallback(current_instance_,
306 &ClosureTest::SetAMethod, 456);
316 TEST_F(ClosureTest, TestPermanentClosureFunction2) {
317 const char* cstr = "hello";
318 Closure* closure = NewPermanentCallback(&SetABFunction, 789, cstr);
332 TEST_F(ClosureTest, TestPermanentClosureMethod2) {
333 const char* cstr = "hello";
334 Closure* closure = NewPermanentCallback(current_instance_,
335 &ClosureTest::SetABMethod, 789, cstr);
349 TEST_F(ClosureTest, TestPermanentClosureDeleteInCallback) {
350 permanent_closure_ = NewPermanentCallback((ClosureTest*) this,
351 &ClosureTest::DeleteClosureInCallback);
352 permanent_closure_->Run();
355 } // anonymous namespace
356 } // namespace protobuf
357 } // namespace google