诸暨麻将添加redis
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

359 строки
11 KiB

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