Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 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/casts.h> |
| 35 | #include <google/protobuf/stubs/common.h> |
| 36 | #include <google/protobuf/stubs/strutil.h> |
| 37 | #include <google/protobuf/stubs/substitute.h> |
| 38 | |
| 39 | #include <google/protobuf/testing/googletest.h> |
| 40 | #include <gtest/gtest.h> |
| 41 | |
| 42 | namespace google { |
| 43 | namespace protobuf { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | // TODO(kenton): More tests. |
| 47 | |
| 48 | #ifdef PACKAGE_VERSION // only defined when using automake, not MSVC |
| 49 | |
| 50 | TEST(VersionTest, VersionMatchesConfig) { |
| 51 | // Verify that the version string specified in config.h matches the one |
| 52 | // in common.h. The config.h version is a string which may have a suffix |
| 53 | // like "beta" or "rc1", so we remove that. |
| 54 | string version = PACKAGE_VERSION; |
| 55 | int pos = 0; |
| 56 | while (pos < version.size() && |
| 57 | (ascii_isdigit(version[pos]) || version[pos] == '.')) { |
| 58 | ++pos; |
| 59 | } |
| 60 | version.erase(pos); |
| 61 | |
| 62 | EXPECT_EQ(version, internal::VersionString(GOOGLE_PROTOBUF_VERSION)); |
| 63 | } |
| 64 | |
| 65 | #endif // PACKAGE_VERSION |
| 66 | |
| 67 | TEST(CommonTest, IntMinMaxConstants) { |
| 68 | // kint32min was declared incorrectly in the first release of protobufs. |
| 69 | // Ugh. |
| 70 | EXPECT_LT(kint32min, kint32max); |
| 71 | EXPECT_EQ(static_cast<uint32>(kint32min), static_cast<uint32>(kint32max) + 1); |
| 72 | EXPECT_LT(kint64min, kint64max); |
| 73 | EXPECT_EQ(static_cast<uint64>(kint64min), static_cast<uint64>(kint64max) + 1); |
| 74 | EXPECT_EQ(0, kuint32max + 1); |
| 75 | EXPECT_EQ(0, kuint64max + 1); |
| 76 | } |
| 77 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 78 | std::vector<string> captured_messages_; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 79 | |
| 80 | void CaptureLog(LogLevel level, const char* filename, int line, |
| 81 | const string& message) { |
| 82 | captured_messages_.push_back( |
| 83 | strings::Substitute("$0 $1:$2: $3", |
| 84 | implicit_cast<int>(level), filename, line, message)); |
| 85 | } |
| 86 | |
| 87 | TEST(LoggingTest, DefaultLogging) { |
| 88 | CaptureTestStderr(); |
| 89 | int line = __LINE__; |
| 90 | GOOGLE_LOG(INFO ) << "A message."; |
| 91 | GOOGLE_LOG(WARNING) << "A warning."; |
| 92 | GOOGLE_LOG(ERROR ) << "An error."; |
| 93 | |
| 94 | string text = GetCapturedTestStderr(); |
| 95 | EXPECT_EQ( |
| 96 | "[libprotobuf INFO " __FILE__ ":" + SimpleItoa(line + 1) + "] A message.\n" |
| 97 | "[libprotobuf WARNING " __FILE__ ":" + SimpleItoa(line + 2) + "] A warning.\n" |
| 98 | "[libprotobuf ERROR " __FILE__ ":" + SimpleItoa(line + 3) + "] An error.\n", |
| 99 | text); |
| 100 | } |
| 101 | |
| 102 | TEST(LoggingTest, NullLogging) { |
| 103 | LogHandler* old_handler = SetLogHandler(NULL); |
| 104 | |
| 105 | CaptureTestStderr(); |
| 106 | GOOGLE_LOG(INFO ) << "A message."; |
| 107 | GOOGLE_LOG(WARNING) << "A warning."; |
| 108 | GOOGLE_LOG(ERROR ) << "An error."; |
| 109 | |
| 110 | EXPECT_TRUE(SetLogHandler(old_handler) == NULL); |
| 111 | |
| 112 | string text = GetCapturedTestStderr(); |
| 113 | EXPECT_EQ("", text); |
| 114 | } |
| 115 | |
| 116 | TEST(LoggingTest, CaptureLogging) { |
| 117 | captured_messages_.clear(); |
| 118 | |
| 119 | LogHandler* old_handler = SetLogHandler(&CaptureLog); |
| 120 | |
| 121 | int start_line = __LINE__; |
| 122 | GOOGLE_LOG(ERROR) << "An error."; |
| 123 | GOOGLE_LOG(WARNING) << "A warning."; |
| 124 | |
| 125 | EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog); |
| 126 | |
| 127 | ASSERT_EQ(2, captured_messages_.size()); |
| 128 | EXPECT_EQ( |
| 129 | "2 " __FILE__ ":" + SimpleItoa(start_line + 1) + ": An error.", |
| 130 | captured_messages_[0]); |
| 131 | EXPECT_EQ( |
| 132 | "1 " __FILE__ ":" + SimpleItoa(start_line + 2) + ": A warning.", |
| 133 | captured_messages_[1]); |
| 134 | } |
| 135 | |
| 136 | TEST(LoggingTest, SilenceLogging) { |
| 137 | captured_messages_.clear(); |
| 138 | |
| 139 | LogHandler* old_handler = SetLogHandler(&CaptureLog); |
| 140 | |
| 141 | int line1 = __LINE__; GOOGLE_LOG(INFO) << "Visible1"; |
| 142 | LogSilencer* silencer1 = new LogSilencer; |
| 143 | GOOGLE_LOG(INFO) << "Not visible."; |
| 144 | LogSilencer* silencer2 = new LogSilencer; |
| 145 | GOOGLE_LOG(INFO) << "Not visible."; |
| 146 | delete silencer1; |
| 147 | GOOGLE_LOG(INFO) << "Not visible."; |
| 148 | delete silencer2; |
| 149 | int line2 = __LINE__; GOOGLE_LOG(INFO) << "Visible2"; |
| 150 | |
| 151 | EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog); |
| 152 | |
| 153 | ASSERT_EQ(2, captured_messages_.size()); |
| 154 | EXPECT_EQ( |
| 155 | "0 " __FILE__ ":" + SimpleItoa(line1) + ": Visible1", |
| 156 | captured_messages_[0]); |
| 157 | EXPECT_EQ( |
| 158 | "0 " __FILE__ ":" + SimpleItoa(line2) + ": Visible2", |
| 159 | captured_messages_[1]); |
| 160 | } |
| 161 | |
| 162 | class ClosureTest : public testing::Test { |
| 163 | public: |
| 164 | void SetA123Method() { a_ = 123; } |
| 165 | static void SetA123Function() { current_instance_->a_ = 123; } |
| 166 | |
| 167 | void SetAMethod(int a) { a_ = a; } |
| 168 | void SetCMethod(string c) { c_ = c; } |
| 169 | |
| 170 | static void SetAFunction(int a) { current_instance_->a_ = a; } |
| 171 | static void SetCFunction(string c) { current_instance_->c_ = c; } |
| 172 | |
| 173 | void SetABMethod(int a, const char* b) { a_ = a; b_ = b; } |
| 174 | static void SetABFunction(int a, const char* b) { |
| 175 | current_instance_->a_ = a; |
| 176 | current_instance_->b_ = b; |
| 177 | } |
| 178 | |
| 179 | virtual void SetUp() { |
| 180 | current_instance_ = this; |
| 181 | a_ = 0; |
| 182 | b_ = NULL; |
| 183 | c_.clear(); |
| 184 | permanent_closure_ = NULL; |
| 185 | } |
| 186 | |
| 187 | void DeleteClosureInCallback() { |
| 188 | delete permanent_closure_; |
| 189 | } |
| 190 | |
| 191 | int a_; |
| 192 | const char* b_; |
| 193 | string c_; |
| 194 | Closure* permanent_closure_; |
| 195 | |
| 196 | static ClosureTest* current_instance_; |
| 197 | }; |
| 198 | |
| 199 | ClosureTest* ClosureTest::current_instance_ = NULL; |
| 200 | |
| 201 | TEST_F(ClosureTest, TestClosureFunction0) { |
| 202 | Closure* closure = NewCallback(&SetA123Function); |
| 203 | EXPECT_NE(123, a_); |
| 204 | closure->Run(); |
| 205 | EXPECT_EQ(123, a_); |
| 206 | } |
| 207 | |
| 208 | TEST_F(ClosureTest, TestClosureMethod0) { |
| 209 | Closure* closure = NewCallback(current_instance_, |
| 210 | &ClosureTest::SetA123Method); |
| 211 | EXPECT_NE(123, a_); |
| 212 | closure->Run(); |
| 213 | EXPECT_EQ(123, a_); |
| 214 | } |
| 215 | |
| 216 | TEST_F(ClosureTest, TestClosureFunction1) { |
| 217 | Closure* closure = NewCallback(&SetAFunction, 456); |
| 218 | EXPECT_NE(456, a_); |
| 219 | closure->Run(); |
| 220 | EXPECT_EQ(456, a_); |
| 221 | } |
| 222 | |
| 223 | TEST_F(ClosureTest, TestClosureMethod1) { |
| 224 | Closure* closure = NewCallback(current_instance_, |
| 225 | &ClosureTest::SetAMethod, 456); |
| 226 | EXPECT_NE(456, a_); |
| 227 | closure->Run(); |
| 228 | EXPECT_EQ(456, a_); |
| 229 | } |
| 230 | |
| 231 | TEST_F(ClosureTest, TestClosureFunction1String) { |
| 232 | Closure* closure = NewCallback(&SetCFunction, string("test")); |
| 233 | EXPECT_NE("test", c_); |
| 234 | closure->Run(); |
| 235 | EXPECT_EQ("test", c_); |
| 236 | } |
| 237 | |
| 238 | TEST_F(ClosureTest, TestClosureMethod1String) { |
| 239 | Closure* closure = NewCallback(current_instance_, |
| 240 | &ClosureTest::SetCMethod, string("test")); |
| 241 | EXPECT_NE("test", c_); |
| 242 | closure->Run(); |
| 243 | EXPECT_EQ("test", c_); |
| 244 | } |
| 245 | |
| 246 | TEST_F(ClosureTest, TestClosureFunction2) { |
| 247 | const char* cstr = "hello"; |
| 248 | Closure* closure = NewCallback(&SetABFunction, 789, cstr); |
| 249 | EXPECT_NE(789, a_); |
| 250 | EXPECT_NE(cstr, b_); |
| 251 | closure->Run(); |
| 252 | EXPECT_EQ(789, a_); |
| 253 | EXPECT_EQ(cstr, b_); |
| 254 | } |
| 255 | |
| 256 | TEST_F(ClosureTest, TestClosureMethod2) { |
| 257 | const char* cstr = "hello"; |
| 258 | Closure* closure = NewCallback(current_instance_, |
| 259 | &ClosureTest::SetABMethod, 789, cstr); |
| 260 | EXPECT_NE(789, a_); |
| 261 | EXPECT_NE(cstr, b_); |
| 262 | closure->Run(); |
| 263 | EXPECT_EQ(789, a_); |
| 264 | EXPECT_EQ(cstr, b_); |
| 265 | } |
| 266 | |
| 267 | // Repeat all of the above with NewPermanentCallback() |
| 268 | |
| 269 | TEST_F(ClosureTest, TestPermanentClosureFunction0) { |
| 270 | Closure* closure = NewPermanentCallback(&SetA123Function); |
| 271 | EXPECT_NE(123, a_); |
| 272 | closure->Run(); |
| 273 | EXPECT_EQ(123, a_); |
| 274 | a_ = 0; |
| 275 | closure->Run(); |
| 276 | EXPECT_EQ(123, a_); |
| 277 | delete closure; |
| 278 | } |
| 279 | |
| 280 | TEST_F(ClosureTest, TestPermanentClosureMethod0) { |
| 281 | Closure* closure = NewPermanentCallback(current_instance_, |
| 282 | &ClosureTest::SetA123Method); |
| 283 | EXPECT_NE(123, a_); |
| 284 | closure->Run(); |
| 285 | EXPECT_EQ(123, a_); |
| 286 | a_ = 0; |
| 287 | closure->Run(); |
| 288 | EXPECT_EQ(123, a_); |
| 289 | delete closure; |
| 290 | } |
| 291 | |
| 292 | TEST_F(ClosureTest, TestPermanentClosureFunction1) { |
| 293 | Closure* closure = NewPermanentCallback(&SetAFunction, 456); |
| 294 | EXPECT_NE(456, a_); |
| 295 | closure->Run(); |
| 296 | EXPECT_EQ(456, a_); |
| 297 | a_ = 0; |
| 298 | closure->Run(); |
| 299 | EXPECT_EQ(456, a_); |
| 300 | delete closure; |
| 301 | } |
| 302 | |
| 303 | TEST_F(ClosureTest, TestPermanentClosureMethod1) { |
| 304 | Closure* closure = NewPermanentCallback(current_instance_, |
| 305 | &ClosureTest::SetAMethod, 456); |
| 306 | EXPECT_NE(456, a_); |
| 307 | closure->Run(); |
| 308 | EXPECT_EQ(456, a_); |
| 309 | a_ = 0; |
| 310 | closure->Run(); |
| 311 | EXPECT_EQ(456, a_); |
| 312 | delete closure; |
| 313 | } |
| 314 | |
| 315 | TEST_F(ClosureTest, TestPermanentClosureFunction2) { |
| 316 | const char* cstr = "hello"; |
| 317 | Closure* closure = NewPermanentCallback(&SetABFunction, 789, cstr); |
| 318 | EXPECT_NE(789, a_); |
| 319 | EXPECT_NE(cstr, b_); |
| 320 | closure->Run(); |
| 321 | EXPECT_EQ(789, a_); |
| 322 | EXPECT_EQ(cstr, b_); |
| 323 | a_ = 0; |
| 324 | b_ = NULL; |
| 325 | closure->Run(); |
| 326 | EXPECT_EQ(789, a_); |
| 327 | EXPECT_EQ(cstr, b_); |
| 328 | delete closure; |
| 329 | } |
| 330 | |
| 331 | TEST_F(ClosureTest, TestPermanentClosureMethod2) { |
| 332 | const char* cstr = "hello"; |
| 333 | Closure* closure = NewPermanentCallback(current_instance_, |
| 334 | &ClosureTest::SetABMethod, 789, cstr); |
| 335 | EXPECT_NE(789, a_); |
| 336 | EXPECT_NE(cstr, b_); |
| 337 | closure->Run(); |
| 338 | EXPECT_EQ(789, a_); |
| 339 | EXPECT_EQ(cstr, b_); |
| 340 | a_ = 0; |
| 341 | b_ = NULL; |
| 342 | closure->Run(); |
| 343 | EXPECT_EQ(789, a_); |
| 344 | EXPECT_EQ(cstr, b_); |
| 345 | delete closure; |
| 346 | } |
| 347 | |
| 348 | TEST_F(ClosureTest, TestPermanentClosureDeleteInCallback) { |
| 349 | permanent_closure_ = NewPermanentCallback((ClosureTest*) this, |
| 350 | &ClosureTest::DeleteClosureInCallback); |
| 351 | permanent_closure_->Run(); |
| 352 | } |
| 353 | |
| 354 | } // anonymous namespace |
| 355 | } // namespace protobuf |
| 356 | } // namespace google |