PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/stubs/common_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 357 lines | 265 code | 55 blank | 37 comment | 6 complexity | fd2a0c0a6becc304257dfb559bff2b96 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. #include <vector>
  32. #include <google/protobuf/stubs/common.h>
  33. #include <google/protobuf/stubs/strutil.h>
  34. #include <google/protobuf/stubs/substitute.h>
  35. #include <google/protobuf/testing/googletest.h>
  36. #include <gtest/gtest.h>
  37. #include "config.h"
  38. namespace google {
  39. namespace protobuf {
  40. namespace {
  41. // TODO(kenton): More tests.
  42. #ifdef PACKAGE_VERSION // only defined when using automake, not MSVC
  43. TEST(VersionTest, VersionMatchesConfig) {
  44. // Verify that the version string specified in config.h matches the one
  45. // in common.h. The config.h version is a string which may have a suffix
  46. // like "beta" or "rc1", so we remove that.
  47. string version = PACKAGE_VERSION;
  48. int pos = 0;
  49. while (pos < version.size() &&
  50. (ascii_isdigit(version[pos]) || version[pos] == '.')) {
  51. ++pos;
  52. }
  53. version.erase(pos);
  54. EXPECT_EQ(version, internal::VersionString(GOOGLE_PROTOBUF_VERSION));
  55. }
  56. #endif // PACKAGE_VERSION
  57. TEST(CommonTest, IntMinMaxConstants) {
  58. // kint32min was declared incorrectly in the first release of protobufs.
  59. // Ugh.
  60. EXPECT_LT(kint32min, kint32max);
  61. EXPECT_EQ(static_cast<uint32>(kint32min), static_cast<uint32>(kint32max) + 1);
  62. EXPECT_LT(kint64min, kint64max);
  63. EXPECT_EQ(static_cast<uint64>(kint64min), static_cast<uint64>(kint64max) + 1);
  64. EXPECT_EQ(0, kuint32max + 1);
  65. EXPECT_EQ(0, kuint64max + 1);
  66. }
  67. vector<string> captured_messages_;
  68. void CaptureLog(LogLevel level, const char* filename, int line,
  69. const string& message) {
  70. captured_messages_.push_back(
  71. strings::Substitute("$0 $1:$2: $3",
  72. implicit_cast<int>(level), filename, line, message));
  73. }
  74. TEST(LoggingTest, DefaultLogging) {
  75. CaptureTestStderr();
  76. int line = __LINE__;
  77. GOOGLE_LOG(INFO ) << "A message.";
  78. GOOGLE_LOG(WARNING) << "A warning.";
  79. GOOGLE_LOG(ERROR ) << "An error.";
  80. string text = GetCapturedTestStderr();
  81. EXPECT_EQ(
  82. "libprotobuf INFO "__FILE__":" + SimpleItoa(line + 1) + "] A message.\n"
  83. "libprotobuf WARNING "__FILE__":" + SimpleItoa(line + 2) + "] A warning.\n"
  84. "libprotobuf ERROR "__FILE__":" + SimpleItoa(line + 3) + "] An error.\n",
  85. text);
  86. }
  87. TEST(LoggingTest, NullLogging) {
  88. LogHandler* old_handler = SetLogHandler(NULL);
  89. CaptureTestStderr();
  90. GOOGLE_LOG(INFO ) << "A message.";
  91. GOOGLE_LOG(WARNING) << "A warning.";
  92. GOOGLE_LOG(ERROR ) << "An error.";
  93. EXPECT_TRUE(SetLogHandler(old_handler) == NULL);
  94. string text = GetCapturedTestStderr();
  95. EXPECT_EQ("", text);
  96. }
  97. TEST(LoggingTest, CaptureLogging) {
  98. captured_messages_.clear();
  99. LogHandler* old_handler = SetLogHandler(&CaptureLog);
  100. int start_line = __LINE__;
  101. GOOGLE_LOG(ERROR) << "An error.";
  102. GOOGLE_LOG(WARNING) << "A warning.";
  103. EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
  104. ASSERT_EQ(2, captured_messages_.size());
  105. EXPECT_EQ(
  106. "2 "__FILE__":" + SimpleItoa(start_line + 1) + ": An error.",
  107. captured_messages_[0]);
  108. EXPECT_EQ(
  109. "1 "__FILE__":" + SimpleItoa(start_line + 2) + ": A warning.",
  110. captured_messages_[1]);
  111. }
  112. TEST(LoggingTest, SilenceLogging) {
  113. captured_messages_.clear();
  114. LogHandler* old_handler = SetLogHandler(&CaptureLog);
  115. int line1 = __LINE__; GOOGLE_LOG(INFO) << "Visible1";
  116. LogSilencer* silencer1 = new LogSilencer;
  117. GOOGLE_LOG(INFO) << "Not visible.";
  118. LogSilencer* silencer2 = new LogSilencer;
  119. GOOGLE_LOG(INFO) << "Not visible.";
  120. delete silencer1;
  121. GOOGLE_LOG(INFO) << "Not visible.";
  122. delete silencer2;
  123. int line2 = __LINE__; GOOGLE_LOG(INFO) << "Visible2";
  124. EXPECT_TRUE(SetLogHandler(old_handler) == &CaptureLog);
  125. ASSERT_EQ(2, captured_messages_.size());
  126. EXPECT_EQ(
  127. "0 "__FILE__":" + SimpleItoa(line1) + ": Visible1",
  128. captured_messages_[0]);
  129. EXPECT_EQ(
  130. "0 "__FILE__":" + SimpleItoa(line2) + ": Visible2",
  131. captured_messages_[1]);
  132. }
  133. class ClosureTest : public testing::Test {
  134. public:
  135. void SetA123Method() { a_ = 123; }
  136. static void SetA123Function() { current_instance_->a_ = 123; }
  137. void SetAMethod(int a) { a_ = a; }
  138. void SetCMethod(string c) { c_ = c; }
  139. static void SetAFunction(int a) { current_instance_->a_ = a; }
  140. static void SetCFunction(string c) { current_instance_->c_ = c; }
  141. void SetABMethod(int a, const char* b) { a_ = a; b_ = b; }
  142. static void SetABFunction(int a, const char* b) {
  143. current_instance_->a_ = a;
  144. current_instance_->b_ = b;
  145. }
  146. virtual void SetUp() {
  147. current_instance_ = this;
  148. a_ = 0;
  149. b_ = NULL;
  150. c_.clear();
  151. permanent_closure_ = NULL;
  152. }
  153. void DeleteClosureInCallback() {
  154. delete permanent_closure_;
  155. }
  156. int a_;
  157. const char* b_;
  158. string c_;
  159. Closure* permanent_closure_;
  160. static ClosureTest* current_instance_;
  161. };
  162. ClosureTest* ClosureTest::current_instance_ = NULL;
  163. TEST_F(ClosureTest, TestClosureFunction0) {
  164. Closure* closure = NewCallback(&SetA123Function);
  165. EXPECT_NE(123, a_);
  166. closure->Run();
  167. EXPECT_EQ(123, a_);
  168. }
  169. TEST_F(ClosureTest, TestClosureMethod0) {
  170. Closure* closure = NewCallback(current_instance_,
  171. &ClosureTest::SetA123Method);
  172. EXPECT_NE(123, a_);
  173. closure->Run();
  174. EXPECT_EQ(123, a_);
  175. }
  176. TEST_F(ClosureTest, TestClosureFunction1) {
  177. Closure* closure = NewCallback(&SetAFunction, 456);
  178. EXPECT_NE(456, a_);
  179. closure->Run();
  180. EXPECT_EQ(456, a_);
  181. }
  182. TEST_F(ClosureTest, TestClosureMethod1) {
  183. Closure* closure = NewCallback(current_instance_,
  184. &ClosureTest::SetAMethod, 456);
  185. EXPECT_NE(456, a_);
  186. closure->Run();
  187. EXPECT_EQ(456, a_);
  188. }
  189. TEST_F(ClosureTest, TestClosureFunction1String) {
  190. Closure* closure = NewCallback(&SetCFunction, string("test"));
  191. EXPECT_NE("test", c_);
  192. closure->Run();
  193. EXPECT_EQ("test", c_);
  194. }
  195. TEST_F(ClosureTest, TestClosureMethod1String) {
  196. Closure* closure = NewCallback(current_instance_,
  197. &ClosureTest::SetCMethod, string("test"));
  198. EXPECT_NE("test", c_);
  199. closure->Run();
  200. EXPECT_EQ("test", c_);
  201. }
  202. TEST_F(ClosureTest, TestClosureFunction2) {
  203. const char* cstr = "hello";
  204. Closure* closure = NewCallback(&SetABFunction, 789, cstr);
  205. EXPECT_NE(789, a_);
  206. EXPECT_NE(cstr, b_);
  207. closure->Run();
  208. EXPECT_EQ(789, a_);
  209. EXPECT_EQ(cstr, b_);
  210. }
  211. TEST_F(ClosureTest, TestClosureMethod2) {
  212. const char* cstr = "hello";
  213. Closure* closure = NewCallback(current_instance_,
  214. &ClosureTest::SetABMethod, 789, cstr);
  215. EXPECT_NE(789, a_);
  216. EXPECT_NE(cstr, b_);
  217. closure->Run();
  218. EXPECT_EQ(789, a_);
  219. EXPECT_EQ(cstr, b_);
  220. }
  221. // Repeat all of the above with NewPermanentCallback()
  222. TEST_F(ClosureTest, TestPermanentClosureFunction0) {
  223. Closure* closure = NewPermanentCallback(&SetA123Function);
  224. EXPECT_NE(123, a_);
  225. closure->Run();
  226. EXPECT_EQ(123, a_);
  227. a_ = 0;
  228. closure->Run();
  229. EXPECT_EQ(123, a_);
  230. delete closure;
  231. }
  232. TEST_F(ClosureTest, TestPermanentClosureMethod0) {
  233. Closure* closure = NewPermanentCallback(current_instance_,
  234. &ClosureTest::SetA123Method);
  235. EXPECT_NE(123, a_);
  236. closure->Run();
  237. EXPECT_EQ(123, a_);
  238. a_ = 0;
  239. closure->Run();
  240. EXPECT_EQ(123, a_);
  241. delete closure;
  242. }
  243. TEST_F(ClosureTest, TestPermanentClosureFunction1) {
  244. Closure* closure = NewPermanentCallback(&SetAFunction, 456);
  245. EXPECT_NE(456, a_);
  246. closure->Run();
  247. EXPECT_EQ(456, a_);
  248. a_ = 0;
  249. closure->Run();
  250. EXPECT_EQ(456, a_);
  251. delete closure;
  252. }
  253. TEST_F(ClosureTest, TestPermanentClosureMethod1) {
  254. Closure* closure = NewPermanentCallback(current_instance_,
  255. &ClosureTest::SetAMethod, 456);
  256. EXPECT_NE(456, a_);
  257. closure->Run();
  258. EXPECT_EQ(456, a_);
  259. a_ = 0;
  260. closure->Run();
  261. EXPECT_EQ(456, a_);
  262. delete closure;
  263. }
  264. TEST_F(ClosureTest, TestPermanentClosureFunction2) {
  265. const char* cstr = "hello";
  266. Closure* closure = NewPermanentCallback(&SetABFunction, 789, cstr);
  267. EXPECT_NE(789, a_);
  268. EXPECT_NE(cstr, b_);
  269. closure->Run();
  270. EXPECT_EQ(789, a_);
  271. EXPECT_EQ(cstr, b_);
  272. a_ = 0;
  273. b_ = NULL;
  274. closure->Run();
  275. EXPECT_EQ(789, a_);
  276. EXPECT_EQ(cstr, b_);
  277. delete closure;
  278. }
  279. TEST_F(ClosureTest, TestPermanentClosureMethod2) {
  280. const char* cstr = "hello";
  281. Closure* closure = NewPermanentCallback(current_instance_,
  282. &ClosureTest::SetABMethod, 789, cstr);
  283. EXPECT_NE(789, a_);
  284. EXPECT_NE(cstr, b_);
  285. closure->Run();
  286. EXPECT_EQ(789, a_);
  287. EXPECT_EQ(cstr, b_);
  288. a_ = 0;
  289. b_ = NULL;
  290. closure->Run();
  291. EXPECT_EQ(789, a_);
  292. EXPECT_EQ(cstr, b_);
  293. delete closure;
  294. }
  295. TEST_F(ClosureTest, TestPermanentClosureDeleteInCallback) {
  296. permanent_closure_ = NewPermanentCallback((ClosureTest*) this,
  297. &ClosureTest::DeleteClosureInCallback);
  298. permanent_closure_->Run();
  299. }
  300. } // anonymous namespace
  301. } // namespace protobuf
  302. } // namespace google