诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1264 lines
63 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. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #ifndef GOOGLE_PROTOBUF_TEST_UTIL_H__
  34. #define GOOGLE_PROTOBUF_TEST_UTIL_H__
  35. #include <google/protobuf/unittest.pb.h>
  36. #define UNITTEST ::protobuf_unittest
  37. #define UNITTEST_IMPORT ::protobuf_unittest_import
  38. // Must be included when the preprocessor symbols above are defined.
  39. #include <google/protobuf/test_util.inc>
  40. #undef UNITTEST
  41. #undef UNITTEST_IMPORT
  42. #include <google/protobuf/port_def.inc>
  43. namespace google {
  44. namespace protobuf {
  45. // This file doesn't use these declarations, but some .cc files do.
  46. namespace unittest = ::protobuf_unittest;
  47. namespace unittest_import = ::protobuf_unittest_import;
  48. namespace TestUtil {
  49. class ReflectionTester {
  50. public:
  51. // base_descriptor must be a descriptor for TestAllTypes or
  52. // TestAllExtensions. In the former case, ReflectionTester fetches from
  53. // it the FieldDescriptors needed to use the reflection interface. In
  54. // the latter case, ReflectionTester searches for extension fields in
  55. // its file.
  56. explicit ReflectionTester(const Descriptor* base_descriptor);
  57. void SetAllFieldsViaReflection(Message* message);
  58. void ModifyRepeatedFieldsViaReflection(Message* message);
  59. void ExpectAllFieldsSetViaReflection(const Message& message);
  60. void ExpectClearViaReflection(const Message& message);
  61. void SetPackedFieldsViaReflection(Message* message);
  62. void ExpectPackedFieldsSetViaReflection(const Message& message);
  63. void RemoveLastRepeatedsViaReflection(Message* message);
  64. void ReleaseLastRepeatedsViaReflection(Message* message,
  65. bool expect_extensions_notnull);
  66. void SwapRepeatedsViaReflection(Message* message);
  67. void SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message);
  68. static void SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  69. Message* from_message, Message* to_message);
  70. enum MessageReleaseState {
  71. IS_NULL,
  72. CAN_BE_NULL,
  73. NOT_NULL,
  74. };
  75. void ExpectMessagesReleasedViaReflection(
  76. Message* message, MessageReleaseState expected_release_state);
  77. // Set and check functions for TestOneof2 messages. No need to construct
  78. // the ReflectionTester by TestAllTypes nor TestAllExtensions.
  79. static void SetOneofViaReflection(Message* message);
  80. static void ExpectOneofSetViaReflection(const Message& message);
  81. private:
  82. const FieldDescriptor* F(const std::string& name);
  83. const Descriptor* base_descriptor_;
  84. const FieldDescriptor* group_a_;
  85. const FieldDescriptor* repeated_group_a_;
  86. const FieldDescriptor* nested_b_;
  87. const FieldDescriptor* foreign_c_;
  88. const FieldDescriptor* import_d_;
  89. const FieldDescriptor* import_e_;
  90. const EnumValueDescriptor* nested_foo_;
  91. const EnumValueDescriptor* nested_bar_;
  92. const EnumValueDescriptor* nested_baz_;
  93. const EnumValueDescriptor* foreign_foo_;
  94. const EnumValueDescriptor* foreign_bar_;
  95. const EnumValueDescriptor* foreign_baz_;
  96. const EnumValueDescriptor* import_foo_;
  97. const EnumValueDescriptor* import_bar_;
  98. const EnumValueDescriptor* import_baz_;
  99. // We have to split this into three function otherwise it creates a stack
  100. // frame so large that it triggers a warning.
  101. void ExpectAllFieldsSetViaReflection1(const Message& message);
  102. void ExpectAllFieldsSetViaReflection2(const Message& message);
  103. void ExpectAllFieldsSetViaReflection3(const Message& message);
  104. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionTester);
  105. };
  106. inline TestUtil::ReflectionTester::ReflectionTester(
  107. const Descriptor* base_descriptor)
  108. : base_descriptor_(base_descriptor) {
  109. const DescriptorPool* pool = base_descriptor->file()->pool();
  110. std::string package = base_descriptor->file()->package();
  111. const FieldDescriptor* import_descriptor =
  112. pool->FindFieldByName(package + ".TestAllTypes.optional_import_message");
  113. std::string import_package =
  114. import_descriptor->message_type()->file()->package();
  115. nested_b_ = pool->FindFieldByName(package + ".TestAllTypes.NestedMessage.bb");
  116. foreign_c_ = pool->FindFieldByName(package + ".ForeignMessage.c");
  117. import_d_ = pool->FindFieldByName(import_package + ".ImportMessage.d");
  118. import_e_ = pool->FindFieldByName(import_package + ".PublicImportMessage.e");
  119. nested_foo_ = pool->FindEnumValueByName(package + ".TestAllTypes.FOO");
  120. nested_bar_ = pool->FindEnumValueByName(package + ".TestAllTypes.BAR");
  121. nested_baz_ = pool->FindEnumValueByName(package + ".TestAllTypes.BAZ");
  122. foreign_foo_ = pool->FindEnumValueByName(package + ".FOREIGN_FOO");
  123. foreign_bar_ = pool->FindEnumValueByName(package + ".FOREIGN_BAR");
  124. foreign_baz_ = pool->FindEnumValueByName(package + ".FOREIGN_BAZ");
  125. import_foo_ = pool->FindEnumValueByName(import_package + ".IMPORT_FOO");
  126. import_bar_ = pool->FindEnumValueByName(import_package + ".IMPORT_BAR");
  127. import_baz_ = pool->FindEnumValueByName(import_package + ".IMPORT_BAZ");
  128. if (base_descriptor_->name() == "TestAllExtensions") {
  129. group_a_ = pool->FindFieldByName(package + ".OptionalGroup_extension.a");
  130. repeated_group_a_ =
  131. pool->FindFieldByName(package + ".RepeatedGroup_extension.a");
  132. } else {
  133. group_a_ = pool->FindFieldByName(package + ".TestAllTypes.OptionalGroup.a");
  134. repeated_group_a_ =
  135. pool->FindFieldByName(package + ".TestAllTypes.RepeatedGroup.a");
  136. }
  137. EXPECT_TRUE(group_a_ != nullptr);
  138. EXPECT_TRUE(repeated_group_a_ != nullptr);
  139. EXPECT_TRUE(nested_b_ != nullptr);
  140. EXPECT_TRUE(foreign_c_ != nullptr);
  141. EXPECT_TRUE(import_d_ != nullptr);
  142. EXPECT_TRUE(import_e_ != nullptr);
  143. EXPECT_TRUE(nested_foo_ != nullptr);
  144. EXPECT_TRUE(nested_bar_ != nullptr);
  145. EXPECT_TRUE(nested_baz_ != nullptr);
  146. EXPECT_TRUE(foreign_foo_ != nullptr);
  147. EXPECT_TRUE(foreign_bar_ != nullptr);
  148. EXPECT_TRUE(foreign_baz_ != nullptr);
  149. EXPECT_TRUE(import_foo_ != nullptr);
  150. EXPECT_TRUE(import_bar_ != nullptr);
  151. EXPECT_TRUE(import_baz_ != nullptr);
  152. }
  153. // Shorthand to get a FieldDescriptor for a field of TestAllTypes.
  154. inline const FieldDescriptor* TestUtil::ReflectionTester::F(
  155. const std::string& name) {
  156. const FieldDescriptor* result = nullptr;
  157. if (base_descriptor_->name() == "TestAllExtensions" ||
  158. base_descriptor_->name() == "TestPackedExtensions") {
  159. result = base_descriptor_->file()->FindExtensionByName(name + "_extension");
  160. } else {
  161. result = base_descriptor_->FindFieldByName(name);
  162. }
  163. GOOGLE_CHECK(result != nullptr);
  164. return result;
  165. }
  166. // -------------------------------------------------------------------
  167. inline void TestUtil::ReflectionTester::SetAllFieldsViaReflection(
  168. Message* message) {
  169. const Reflection* reflection = message->GetReflection();
  170. Message* sub_message;
  171. reflection->SetInt32(message, F("optional_int32"), 101);
  172. reflection->SetInt64(message, F("optional_int64"), 102);
  173. reflection->SetUInt32(message, F("optional_uint32"), 103);
  174. reflection->SetUInt64(message, F("optional_uint64"), 104);
  175. reflection->SetInt32(message, F("optional_sint32"), 105);
  176. reflection->SetInt64(message, F("optional_sint64"), 106);
  177. reflection->SetUInt32(message, F("optional_fixed32"), 107);
  178. reflection->SetUInt64(message, F("optional_fixed64"), 108);
  179. reflection->SetInt32(message, F("optional_sfixed32"), 109);
  180. reflection->SetInt64(message, F("optional_sfixed64"), 110);
  181. reflection->SetFloat(message, F("optional_float"), 111);
  182. reflection->SetDouble(message, F("optional_double"), 112);
  183. reflection->SetBool(message, F("optional_bool"), true);
  184. reflection->SetString(message, F("optional_string"), "115");
  185. reflection->SetString(message, F("optional_bytes"), "116");
  186. sub_message = reflection->MutableMessage(message, F("optionalgroup"));
  187. sub_message->GetReflection()->SetInt32(sub_message, group_a_, 117);
  188. sub_message =
  189. reflection->MutableMessage(message, F("optional_nested_message"));
  190. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 118);
  191. sub_message =
  192. reflection->MutableMessage(message, F("optional_foreign_message"));
  193. sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 119);
  194. sub_message =
  195. reflection->MutableMessage(message, F("optional_import_message"));
  196. sub_message->GetReflection()->SetInt32(sub_message, import_d_, 120);
  197. reflection->SetEnum(message, F("optional_nested_enum"), nested_baz_);
  198. reflection->SetEnum(message, F("optional_foreign_enum"), foreign_baz_);
  199. reflection->SetEnum(message, F("optional_import_enum"), import_baz_);
  200. reflection->SetString(message, F("optional_string_piece"), "124");
  201. reflection->SetString(message, F("optional_cord"), "125");
  202. sub_message =
  203. reflection->MutableMessage(message, F("optional_public_import_message"));
  204. sub_message->GetReflection()->SetInt32(sub_message, import_e_, 126);
  205. sub_message = reflection->MutableMessage(message, F("optional_lazy_message"));
  206. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 127);
  207. // -----------------------------------------------------------------
  208. reflection->AddInt32(message, F("repeated_int32"), 201);
  209. reflection->AddInt64(message, F("repeated_int64"), 202);
  210. reflection->AddUInt32(message, F("repeated_uint32"), 203);
  211. reflection->AddUInt64(message, F("repeated_uint64"), 204);
  212. reflection->AddInt32(message, F("repeated_sint32"), 205);
  213. reflection->AddInt64(message, F("repeated_sint64"), 206);
  214. reflection->AddUInt32(message, F("repeated_fixed32"), 207);
  215. reflection->AddUInt64(message, F("repeated_fixed64"), 208);
  216. reflection->AddInt32(message, F("repeated_sfixed32"), 209);
  217. reflection->AddInt64(message, F("repeated_sfixed64"), 210);
  218. reflection->AddFloat(message, F("repeated_float"), 211);
  219. reflection->AddDouble(message, F("repeated_double"), 212);
  220. reflection->AddBool(message, F("repeated_bool"), true);
  221. reflection->AddString(message, F("repeated_string"), "215");
  222. reflection->AddString(message, F("repeated_bytes"), "216");
  223. sub_message = reflection->AddMessage(message, F("repeatedgroup"));
  224. sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 217);
  225. sub_message = reflection->AddMessage(message, F("repeated_nested_message"));
  226. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 218);
  227. sub_message = reflection->AddMessage(message, F("repeated_foreign_message"));
  228. sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 219);
  229. sub_message = reflection->AddMessage(message, F("repeated_import_message"));
  230. sub_message->GetReflection()->SetInt32(sub_message, import_d_, 220);
  231. sub_message = reflection->AddMessage(message, F("repeated_lazy_message"));
  232. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 227);
  233. reflection->AddEnum(message, F("repeated_nested_enum"), nested_bar_);
  234. reflection->AddEnum(message, F("repeated_foreign_enum"), foreign_bar_);
  235. reflection->AddEnum(message, F("repeated_import_enum"), import_bar_);
  236. reflection->AddString(message, F("repeated_string_piece"), "224");
  237. reflection->AddString(message, F("repeated_cord"), "225");
  238. // Add a second one of each field.
  239. reflection->AddInt32(message, F("repeated_int32"), 301);
  240. reflection->AddInt64(message, F("repeated_int64"), 302);
  241. reflection->AddUInt32(message, F("repeated_uint32"), 303);
  242. reflection->AddUInt64(message, F("repeated_uint64"), 304);
  243. reflection->AddInt32(message, F("repeated_sint32"), 305);
  244. reflection->AddInt64(message, F("repeated_sint64"), 306);
  245. reflection->AddUInt32(message, F("repeated_fixed32"), 307);
  246. reflection->AddUInt64(message, F("repeated_fixed64"), 308);
  247. reflection->AddInt32(message, F("repeated_sfixed32"), 309);
  248. reflection->AddInt64(message, F("repeated_sfixed64"), 310);
  249. reflection->AddFloat(message, F("repeated_float"), 311);
  250. reflection->AddDouble(message, F("repeated_double"), 312);
  251. reflection->AddBool(message, F("repeated_bool"), false);
  252. reflection->AddString(message, F("repeated_string"), "315");
  253. reflection->AddString(message, F("repeated_bytes"), "316");
  254. sub_message = reflection->AddMessage(message, F("repeatedgroup"));
  255. sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 317);
  256. sub_message = reflection->AddMessage(message, F("repeated_nested_message"));
  257. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 318);
  258. sub_message = reflection->AddMessage(message, F("repeated_foreign_message"));
  259. sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 319);
  260. sub_message = reflection->AddMessage(message, F("repeated_import_message"));
  261. sub_message->GetReflection()->SetInt32(sub_message, import_d_, 320);
  262. sub_message = reflection->AddMessage(message, F("repeated_lazy_message"));
  263. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 327);
  264. reflection->AddEnum(message, F("repeated_nested_enum"), nested_baz_);
  265. reflection->AddEnum(message, F("repeated_foreign_enum"), foreign_baz_);
  266. reflection->AddEnum(message, F("repeated_import_enum"), import_baz_);
  267. reflection->AddString(message, F("repeated_string_piece"), "324");
  268. reflection->AddString(message, F("repeated_cord"), "325");
  269. // -----------------------------------------------------------------
  270. reflection->SetInt32(message, F("default_int32"), 401);
  271. reflection->SetInt64(message, F("default_int64"), 402);
  272. reflection->SetUInt32(message, F("default_uint32"), 403);
  273. reflection->SetUInt64(message, F("default_uint64"), 404);
  274. reflection->SetInt32(message, F("default_sint32"), 405);
  275. reflection->SetInt64(message, F("default_sint64"), 406);
  276. reflection->SetUInt32(message, F("default_fixed32"), 407);
  277. reflection->SetUInt64(message, F("default_fixed64"), 408);
  278. reflection->SetInt32(message, F("default_sfixed32"), 409);
  279. reflection->SetInt64(message, F("default_sfixed64"), 410);
  280. reflection->SetFloat(message, F("default_float"), 411);
  281. reflection->SetDouble(message, F("default_double"), 412);
  282. reflection->SetBool(message, F("default_bool"), false);
  283. reflection->SetString(message, F("default_string"), "415");
  284. reflection->SetString(message, F("default_bytes"), "416");
  285. reflection->SetEnum(message, F("default_nested_enum"), nested_foo_);
  286. reflection->SetEnum(message, F("default_foreign_enum"), foreign_foo_);
  287. reflection->SetEnum(message, F("default_import_enum"), import_foo_);
  288. reflection->SetString(message, F("default_string_piece"), "424");
  289. reflection->SetString(message, F("default_cord"), "425");
  290. reflection->SetUInt32(message, F("oneof_uint32"), 601);
  291. sub_message = reflection->MutableMessage(message, F("oneof_nested_message"));
  292. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 602);
  293. reflection->SetString(message, F("oneof_string"), "603");
  294. reflection->SetString(message, F("oneof_bytes"), "604");
  295. }
  296. inline void TestUtil::ReflectionTester::SetOneofViaReflection(
  297. Message* message) {
  298. const Descriptor* descriptor = message->GetDescriptor();
  299. const Reflection* reflection = message->GetReflection();
  300. Message* sub_message = reflection->MutableMessage(
  301. message, descriptor->FindFieldByName("foo_lazy_message"));
  302. sub_message->GetReflection()->SetInt64(
  303. sub_message, sub_message->GetDescriptor()->FindFieldByName("qux_int"),
  304. 100);
  305. reflection->SetString(message, descriptor->FindFieldByName("bar_cord"),
  306. "101");
  307. reflection->SetInt32(message, descriptor->FindFieldByName("baz_int"), 102);
  308. reflection->SetString(message, descriptor->FindFieldByName("baz_string"),
  309. "103");
  310. }
  311. inline void TestUtil::ReflectionTester::ExpectOneofSetViaReflection(
  312. const Message& message) {
  313. const Descriptor* descriptor = message.GetDescriptor();
  314. const Reflection* reflection = message.GetReflection();
  315. std::string scratch;
  316. EXPECT_TRUE(reflection->HasField(
  317. message, descriptor->FindFieldByName("foo_lazy_message")));
  318. EXPECT_TRUE(
  319. reflection->HasField(message, descriptor->FindFieldByName("bar_cord")));
  320. EXPECT_TRUE(
  321. reflection->HasField(message, descriptor->FindFieldByName("baz_int")));
  322. EXPECT_TRUE(
  323. reflection->HasField(message, descriptor->FindFieldByName("baz_string")));
  324. const Message* sub_message = &reflection->GetMessage(
  325. message, descriptor->FindFieldByName("foo_lazy_message"));
  326. EXPECT_EQ(100, sub_message->GetReflection()->GetInt64(
  327. *sub_message,
  328. sub_message->GetDescriptor()->FindFieldByName("qux_int")));
  329. EXPECT_EQ("101", reflection->GetString(
  330. message, descriptor->FindFieldByName("bar_cord")));
  331. EXPECT_EQ("101",
  332. reflection->GetStringReference(
  333. message, descriptor->FindFieldByName("bar_cord"), &scratch));
  334. EXPECT_EQ(102, reflection->GetInt32(message,
  335. descriptor->FindFieldByName("baz_int")));
  336. EXPECT_EQ("103", reflection->GetString(
  337. message, descriptor->FindFieldByName("baz_string")));
  338. EXPECT_EQ("103",
  339. reflection->GetStringReference(
  340. message, descriptor->FindFieldByName("baz_string"), &scratch));
  341. }
  342. inline void TestUtil::ReflectionTester::SetPackedFieldsViaReflection(
  343. Message* message) {
  344. const Reflection* reflection = message->GetReflection();
  345. reflection->AddInt32(message, F("packed_int32"), 601);
  346. reflection->AddInt64(message, F("packed_int64"), 602);
  347. reflection->AddUInt32(message, F("packed_uint32"), 603);
  348. reflection->AddUInt64(message, F("packed_uint64"), 604);
  349. reflection->AddInt32(message, F("packed_sint32"), 605);
  350. reflection->AddInt64(message, F("packed_sint64"), 606);
  351. reflection->AddUInt32(message, F("packed_fixed32"), 607);
  352. reflection->AddUInt64(message, F("packed_fixed64"), 608);
  353. reflection->AddInt32(message, F("packed_sfixed32"), 609);
  354. reflection->AddInt64(message, F("packed_sfixed64"), 610);
  355. reflection->AddFloat(message, F("packed_float"), 611);
  356. reflection->AddDouble(message, F("packed_double"), 612);
  357. reflection->AddBool(message, F("packed_bool"), true);
  358. reflection->AddEnum(message, F("packed_enum"), foreign_bar_);
  359. reflection->AddInt32(message, F("packed_int32"), 701);
  360. reflection->AddInt64(message, F("packed_int64"), 702);
  361. reflection->AddUInt32(message, F("packed_uint32"), 703);
  362. reflection->AddUInt64(message, F("packed_uint64"), 704);
  363. reflection->AddInt32(message, F("packed_sint32"), 705);
  364. reflection->AddInt64(message, F("packed_sint64"), 706);
  365. reflection->AddUInt32(message, F("packed_fixed32"), 707);
  366. reflection->AddUInt64(message, F("packed_fixed64"), 708);
  367. reflection->AddInt32(message, F("packed_sfixed32"), 709);
  368. reflection->AddInt64(message, F("packed_sfixed64"), 710);
  369. reflection->AddFloat(message, F("packed_float"), 711);
  370. reflection->AddDouble(message, F("packed_double"), 712);
  371. reflection->AddBool(message, F("packed_bool"), false);
  372. reflection->AddEnum(message, F("packed_enum"), foreign_baz_);
  373. }
  374. // -------------------------------------------------------------------
  375. inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection(
  376. const Message& message) {
  377. // We have to split this into three function otherwise it creates a stack
  378. // frame so large that it triggers a warning.
  379. ExpectAllFieldsSetViaReflection1(message);
  380. ExpectAllFieldsSetViaReflection2(message);
  381. ExpectAllFieldsSetViaReflection3(message);
  382. }
  383. inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection1(
  384. const Message& message) {
  385. const Reflection* reflection = message.GetReflection();
  386. std::string scratch;
  387. const Message* sub_message;
  388. EXPECT_TRUE(reflection->HasField(message, F("optional_int32")));
  389. EXPECT_TRUE(reflection->HasField(message, F("optional_int64")));
  390. EXPECT_TRUE(reflection->HasField(message, F("optional_uint32")));
  391. EXPECT_TRUE(reflection->HasField(message, F("optional_uint64")));
  392. EXPECT_TRUE(reflection->HasField(message, F("optional_sint32")));
  393. EXPECT_TRUE(reflection->HasField(message, F("optional_sint64")));
  394. EXPECT_TRUE(reflection->HasField(message, F("optional_fixed32")));
  395. EXPECT_TRUE(reflection->HasField(message, F("optional_fixed64")));
  396. EXPECT_TRUE(reflection->HasField(message, F("optional_sfixed32")));
  397. EXPECT_TRUE(reflection->HasField(message, F("optional_sfixed64")));
  398. EXPECT_TRUE(reflection->HasField(message, F("optional_float")));
  399. EXPECT_TRUE(reflection->HasField(message, F("optional_double")));
  400. EXPECT_TRUE(reflection->HasField(message, F("optional_bool")));
  401. EXPECT_TRUE(reflection->HasField(message, F("optional_string")));
  402. EXPECT_TRUE(reflection->HasField(message, F("optional_bytes")));
  403. EXPECT_TRUE(reflection->HasField(message, F("optionalgroup")));
  404. EXPECT_TRUE(reflection->HasField(message, F("optional_nested_message")));
  405. EXPECT_TRUE(reflection->HasField(message, F("optional_foreign_message")));
  406. EXPECT_TRUE(reflection->HasField(message, F("optional_import_message")));
  407. EXPECT_TRUE(
  408. reflection->HasField(message, F("optional_public_import_message")));
  409. EXPECT_TRUE(reflection->HasField(message, F("optional_lazy_message")));
  410. sub_message = &reflection->GetMessage(message, F("optionalgroup"));
  411. EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, group_a_));
  412. sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
  413. EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
  414. sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
  415. EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, foreign_c_));
  416. sub_message = &reflection->GetMessage(message, F("optional_import_message"));
  417. EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, import_d_));
  418. sub_message =
  419. &reflection->GetMessage(message, F("optional_public_import_message"));
  420. EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, import_e_));
  421. sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
  422. EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
  423. EXPECT_TRUE(reflection->HasField(message, F("optional_nested_enum")));
  424. EXPECT_TRUE(reflection->HasField(message, F("optional_foreign_enum")));
  425. EXPECT_TRUE(reflection->HasField(message, F("optional_import_enum")));
  426. EXPECT_TRUE(reflection->HasField(message, F("optional_string_piece")));
  427. EXPECT_TRUE(reflection->HasField(message, F("optional_cord")));
  428. EXPECT_EQ(101, reflection->GetInt32(message, F("optional_int32")));
  429. EXPECT_EQ(102, reflection->GetInt64(message, F("optional_int64")));
  430. EXPECT_EQ(103, reflection->GetUInt32(message, F("optional_uint32")));
  431. EXPECT_EQ(104, reflection->GetUInt64(message, F("optional_uint64")));
  432. EXPECT_EQ(105, reflection->GetInt32(message, F("optional_sint32")));
  433. EXPECT_EQ(106, reflection->GetInt64(message, F("optional_sint64")));
  434. EXPECT_EQ(107, reflection->GetUInt32(message, F("optional_fixed32")));
  435. EXPECT_EQ(108, reflection->GetUInt64(message, F("optional_fixed64")));
  436. EXPECT_EQ(109, reflection->GetInt32(message, F("optional_sfixed32")));
  437. EXPECT_EQ(110, reflection->GetInt64(message, F("optional_sfixed64")));
  438. EXPECT_EQ(111, reflection->GetFloat(message, F("optional_float")));
  439. EXPECT_EQ(112, reflection->GetDouble(message, F("optional_double")));
  440. EXPECT_TRUE(reflection->GetBool(message, F("optional_bool")));
  441. EXPECT_EQ("115", reflection->GetString(message, F("optional_string")));
  442. EXPECT_EQ("116", reflection->GetString(message, F("optional_bytes")));
  443. EXPECT_EQ("115", reflection->GetStringReference(message, F("optional_string"),
  444. &scratch));
  445. EXPECT_EQ("116", reflection->GetStringReference(message, F("optional_bytes"),
  446. &scratch));
  447. sub_message = &reflection->GetMessage(message, F("optionalgroup"));
  448. EXPECT_EQ(117,
  449. sub_message->GetReflection()->GetInt32(*sub_message, group_a_));
  450. sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
  451. EXPECT_EQ(118,
  452. sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  453. sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
  454. EXPECT_EQ(119,
  455. sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
  456. sub_message = &reflection->GetMessage(message, F("optional_import_message"));
  457. EXPECT_EQ(120,
  458. sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
  459. sub_message =
  460. &reflection->GetMessage(message, F("optional_public_import_message"));
  461. EXPECT_EQ(126,
  462. sub_message->GetReflection()->GetInt32(*sub_message, import_e_));
  463. sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
  464. EXPECT_EQ(127,
  465. sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  466. EXPECT_EQ(nested_baz_,
  467. reflection->GetEnum(message, F("optional_nested_enum")));
  468. EXPECT_EQ(foreign_baz_,
  469. reflection->GetEnum(message, F("optional_foreign_enum")));
  470. EXPECT_EQ(import_baz_,
  471. reflection->GetEnum(message, F("optional_import_enum")));
  472. EXPECT_EQ("124", reflection->GetString(message, F("optional_string_piece")));
  473. EXPECT_EQ("124", reflection->GetStringReference(
  474. message, F("optional_string_piece"), &scratch));
  475. EXPECT_EQ("125", reflection->GetString(message, F("optional_cord")));
  476. EXPECT_EQ("125", reflection->GetStringReference(message, F("optional_cord"),
  477. &scratch));
  478. EXPECT_TRUE(reflection->HasField(message, F("oneof_bytes")));
  479. EXPECT_EQ("604", reflection->GetString(message, F("oneof_bytes")));
  480. if (base_descriptor_->name() == "TestAllTypes") {
  481. EXPECT_FALSE(reflection->HasField(message, F("oneof_uint32")));
  482. EXPECT_FALSE(reflection->HasField(message, F("oneof_string")));
  483. } else {
  484. EXPECT_TRUE(reflection->HasField(message, F("oneof_uint32")));
  485. EXPECT_TRUE(reflection->HasField(message, F("oneof_string")));
  486. EXPECT_EQ(601, reflection->GetUInt32(message, F("oneof_uint32")));
  487. EXPECT_EQ("603", reflection->GetString(message, F("oneof_string")));
  488. sub_message = &reflection->GetMessage(message, F("oneof_nested_message"));
  489. EXPECT_EQ(602,
  490. sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  491. }
  492. }
  493. inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection2(
  494. const Message& message) {
  495. const Reflection* reflection = message.GetReflection();
  496. std::string scratch;
  497. const Message* sub_message;
  498. // -----------------------------------------------------------------
  499. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_int32")));
  500. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_int64")));
  501. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_uint32")));
  502. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_uint64")));
  503. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sint32")));
  504. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sint64")));
  505. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_fixed32")));
  506. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_fixed64")));
  507. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sfixed32")));
  508. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sfixed64")));
  509. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_float")));
  510. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_double")));
  511. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_bool")));
  512. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_string")));
  513. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_bytes")));
  514. ASSERT_EQ(2, reflection->FieldSize(message, F("repeatedgroup")));
  515. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_nested_message")));
  516. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_foreign_message")));
  517. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_import_message")));
  518. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_lazy_message")));
  519. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_nested_enum")));
  520. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_foreign_enum")));
  521. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_import_enum")));
  522. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_string_piece")));
  523. ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_cord")));
  524. EXPECT_EQ(201, reflection->GetRepeatedInt32(message, F("repeated_int32"), 0));
  525. EXPECT_EQ(202, reflection->GetRepeatedInt64(message, F("repeated_int64"), 0));
  526. EXPECT_EQ(203,
  527. reflection->GetRepeatedUInt32(message, F("repeated_uint32"), 0));
  528. EXPECT_EQ(204,
  529. reflection->GetRepeatedUInt64(message, F("repeated_uint64"), 0));
  530. EXPECT_EQ(205,
  531. reflection->GetRepeatedInt32(message, F("repeated_sint32"), 0));
  532. EXPECT_EQ(206,
  533. reflection->GetRepeatedInt64(message, F("repeated_sint64"), 0));
  534. EXPECT_EQ(207,
  535. reflection->GetRepeatedUInt32(message, F("repeated_fixed32"), 0));
  536. EXPECT_EQ(208,
  537. reflection->GetRepeatedUInt64(message, F("repeated_fixed64"), 0));
  538. EXPECT_EQ(209,
  539. reflection->GetRepeatedInt32(message, F("repeated_sfixed32"), 0));
  540. EXPECT_EQ(210,
  541. reflection->GetRepeatedInt64(message, F("repeated_sfixed64"), 0));
  542. EXPECT_EQ(211, reflection->GetRepeatedFloat(message, F("repeated_float"), 0));
  543. EXPECT_EQ(212,
  544. reflection->GetRepeatedDouble(message, F("repeated_double"), 0));
  545. EXPECT_TRUE(reflection->GetRepeatedBool(message, F("repeated_bool"), 0));
  546. EXPECT_EQ("215",
  547. reflection->GetRepeatedString(message, F("repeated_string"), 0));
  548. EXPECT_EQ("216",
  549. reflection->GetRepeatedString(message, F("repeated_bytes"), 0));
  550. EXPECT_EQ("215", reflection->GetRepeatedStringReference(
  551. message, F("repeated_string"), 0, &scratch));
  552. EXPECT_EQ("216", reflection->GetRepeatedStringReference(
  553. message, F("repeated_bytes"), 0, &scratch));
  554. sub_message = &reflection->GetRepeatedMessage(message, F("repeatedgroup"), 0);
  555. EXPECT_EQ(217, sub_message->GetReflection()->GetInt32(*sub_message,
  556. repeated_group_a_));
  557. sub_message =
  558. &reflection->GetRepeatedMessage(message, F("repeated_nested_message"), 0);
  559. EXPECT_EQ(218,
  560. sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  561. sub_message = &reflection->GetRepeatedMessage(
  562. message, F("repeated_foreign_message"), 0);
  563. EXPECT_EQ(219,
  564. sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
  565. sub_message =
  566. &reflection->GetRepeatedMessage(message, F("repeated_import_message"), 0);
  567. EXPECT_EQ(220,
  568. sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
  569. sub_message =
  570. &reflection->GetRepeatedMessage(message, F("repeated_lazy_message"), 0);
  571. EXPECT_EQ(227,
  572. sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  573. EXPECT_EQ(nested_bar_,
  574. reflection->GetRepeatedEnum(message, F("repeated_nested_enum"), 0));
  575. EXPECT_EQ(foreign_bar_, reflection->GetRepeatedEnum(
  576. message, F("repeated_foreign_enum"), 0));
  577. EXPECT_EQ(import_bar_,
  578. reflection->GetRepeatedEnum(message, F("repeated_import_enum"), 0));
  579. EXPECT_EQ("224", reflection->GetRepeatedString(
  580. message, F("repeated_string_piece"), 0));
  581. EXPECT_EQ("224", reflection->GetRepeatedStringReference(
  582. message, F("repeated_string_piece"), 0, &scratch));
  583. EXPECT_EQ("225",
  584. reflection->GetRepeatedString(message, F("repeated_cord"), 0));
  585. EXPECT_EQ("225", reflection->GetRepeatedStringReference(
  586. message, F("repeated_cord"), 0, &scratch));
  587. EXPECT_EQ(301, reflection->GetRepeatedInt32(message, F("repeated_int32"), 1));
  588. EXPECT_EQ(302, reflection->GetRepeatedInt64(message, F("repeated_int64"), 1));
  589. EXPECT_EQ(303,
  590. reflection->GetRepeatedUInt32(message, F("repeated_uint32"), 1));
  591. EXPECT_EQ(304,
  592. reflection->GetRepeatedUInt64(message, F("repeated_uint64"), 1));
  593. EXPECT_EQ(305,
  594. reflection->GetRepeatedInt32(message, F("repeated_sint32"), 1));
  595. EXPECT_EQ(306,
  596. reflection->GetRepeatedInt64(message, F("repeated_sint64"), 1));
  597. EXPECT_EQ(307,
  598. reflection->GetRepeatedUInt32(message, F("repeated_fixed32"), 1));
  599. EXPECT_EQ(308,
  600. reflection->GetRepeatedUInt64(message, F("repeated_fixed64"), 1));
  601. EXPECT_EQ(309,
  602. reflection->GetRepeatedInt32(message, F("repeated_sfixed32"), 1));
  603. EXPECT_EQ(310,
  604. reflection->GetRepeatedInt64(message, F("repeated_sfixed64"), 1));
  605. EXPECT_EQ(311, reflection->GetRepeatedFloat(message, F("repeated_float"), 1));
  606. EXPECT_EQ(312,
  607. reflection->GetRepeatedDouble(message, F("repeated_double"), 1));
  608. EXPECT_FALSE(reflection->GetRepeatedBool(message, F("repeated_bool"), 1));
  609. EXPECT_EQ("315",
  610. reflection->GetRepeatedString(message, F("repeated_string"), 1));
  611. EXPECT_EQ("316",
  612. reflection->GetRepeatedString(message, F("repeated_bytes"), 1));
  613. EXPECT_EQ("315", reflection->GetRepeatedStringReference(
  614. message, F("repeated_string"), 1, &scratch));
  615. EXPECT_EQ("316", reflection->GetRepeatedStringReference(
  616. message, F("repeated_bytes"), 1, &scratch));
  617. sub_message = &reflection->GetRepeatedMessage(message, F("repeatedgroup"), 1);
  618. EXPECT_EQ(317, sub_message->GetReflection()->GetInt32(*sub_message,
  619. repeated_group_a_));
  620. sub_message =
  621. &reflection->GetRepeatedMessage(message, F("repeated_nested_message"), 1);
  622. EXPECT_EQ(318,
  623. sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  624. sub_message = &reflection->GetRepeatedMessage(
  625. message, F("repeated_foreign_message"), 1);
  626. EXPECT_EQ(319,
  627. sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
  628. sub_message =
  629. &reflection->GetRepeatedMessage(message, F("repeated_import_message"), 1);
  630. EXPECT_EQ(320,
  631. sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
  632. sub_message =
  633. &reflection->GetRepeatedMessage(message, F("repeated_lazy_message"), 1);
  634. EXPECT_EQ(327,
  635. sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  636. EXPECT_EQ(nested_baz_,
  637. reflection->GetRepeatedEnum(message, F("repeated_nested_enum"), 1));
  638. EXPECT_EQ(foreign_baz_, reflection->GetRepeatedEnum(
  639. message, F("repeated_foreign_enum"), 1));
  640. EXPECT_EQ(import_baz_,
  641. reflection->GetRepeatedEnum(message, F("repeated_import_enum"), 1));
  642. EXPECT_EQ("324", reflection->GetRepeatedString(
  643. message, F("repeated_string_piece"), 1));
  644. EXPECT_EQ("324", reflection->GetRepeatedStringReference(
  645. message, F("repeated_string_piece"), 1, &scratch));
  646. EXPECT_EQ("325",
  647. reflection->GetRepeatedString(message, F("repeated_cord"), 1));
  648. EXPECT_EQ("325", reflection->GetRepeatedStringReference(
  649. message, F("repeated_cord"), 1, &scratch));
  650. }
  651. inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection3(
  652. const Message& message) {
  653. const Reflection* reflection = message.GetReflection();
  654. std::string scratch;
  655. // -----------------------------------------------------------------
  656. EXPECT_TRUE(reflection->HasField(message, F("default_int32")));
  657. EXPECT_TRUE(reflection->HasField(message, F("default_int64")));
  658. EXPECT_TRUE(reflection->HasField(message, F("default_uint32")));
  659. EXPECT_TRUE(reflection->HasField(message, F("default_uint64")));
  660. EXPECT_TRUE(reflection->HasField(message, F("default_sint32")));
  661. EXPECT_TRUE(reflection->HasField(message, F("default_sint64")));
  662. EXPECT_TRUE(reflection->HasField(message, F("default_fixed32")));
  663. EXPECT_TRUE(reflection->HasField(message, F("default_fixed64")));
  664. EXPECT_TRUE(reflection->HasField(message, F("default_sfixed32")));
  665. EXPECT_TRUE(reflection->HasField(message, F("default_sfixed64")));
  666. EXPECT_TRUE(reflection->HasField(message, F("default_float")));
  667. EXPECT_TRUE(reflection->HasField(message, F("default_double")));
  668. EXPECT_TRUE(reflection->HasField(message, F("default_bool")));
  669. EXPECT_TRUE(reflection->HasField(message, F("default_string")));
  670. EXPECT_TRUE(reflection->HasField(message, F("default_bytes")));
  671. EXPECT_TRUE(reflection->HasField(message, F("default_nested_enum")));
  672. EXPECT_TRUE(reflection->HasField(message, F("default_foreign_enum")));
  673. EXPECT_TRUE(reflection->HasField(message, F("default_import_enum")));
  674. EXPECT_TRUE(reflection->HasField(message, F("default_string_piece")));
  675. EXPECT_TRUE(reflection->HasField(message, F("default_cord")));
  676. EXPECT_EQ(401, reflection->GetInt32(message, F("default_int32")));
  677. EXPECT_EQ(402, reflection->GetInt64(message, F("default_int64")));
  678. EXPECT_EQ(403, reflection->GetUInt32(message, F("default_uint32")));
  679. EXPECT_EQ(404, reflection->GetUInt64(message, F("default_uint64")));
  680. EXPECT_EQ(405, reflection->GetInt32(message, F("default_sint32")));
  681. EXPECT_EQ(406, reflection->GetInt64(message, F("default_sint64")));
  682. EXPECT_EQ(407, reflection->GetUInt32(message, F("default_fixed32")));
  683. EXPECT_EQ(408, reflection->GetUInt64(message, F("default_fixed64")));
  684. EXPECT_EQ(409, reflection->GetInt32(message, F("default_sfixed32")));
  685. EXPECT_EQ(410, reflection->GetInt64(message, F("default_sfixed64")));
  686. EXPECT_EQ(411, reflection->GetFloat(message, F("default_float")));
  687. EXPECT_EQ(412, reflection->GetDouble(message, F("default_double")));
  688. EXPECT_FALSE(reflection->GetBool(message, F("default_bool")));
  689. EXPECT_EQ("415", reflection->GetString(message, F("default_string")));
  690. EXPECT_EQ("416", reflection->GetString(message, F("default_bytes")));
  691. EXPECT_EQ("415", reflection->GetStringReference(message, F("default_string"),
  692. &scratch));
  693. EXPECT_EQ("416", reflection->GetStringReference(message, F("default_bytes"),
  694. &scratch));
  695. EXPECT_EQ(nested_foo_,
  696. reflection->GetEnum(message, F("default_nested_enum")));
  697. EXPECT_EQ(foreign_foo_,
  698. reflection->GetEnum(message, F("default_foreign_enum")));
  699. EXPECT_EQ(import_foo_,
  700. reflection->GetEnum(message, F("default_import_enum")));
  701. EXPECT_EQ("424", reflection->GetString(message, F("default_string_piece")));
  702. EXPECT_EQ("424", reflection->GetStringReference(
  703. message, F("default_string_piece"), &scratch));
  704. EXPECT_EQ("425", reflection->GetString(message, F("default_cord")));
  705. EXPECT_EQ("425", reflection->GetStringReference(message, F("default_cord"),
  706. &scratch));
  707. }
  708. inline void TestUtil::ReflectionTester::ExpectPackedFieldsSetViaReflection(
  709. const Message& message) {
  710. const Reflection* reflection = message.GetReflection();
  711. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_int32")));
  712. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_int64")));
  713. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_uint32")));
  714. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_uint64")));
  715. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sint32")));
  716. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sint64")));
  717. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_fixed32")));
  718. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_fixed64")));
  719. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sfixed32")));
  720. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sfixed64")));
  721. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_float")));
  722. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_double")));
  723. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_bool")));
  724. ASSERT_EQ(2, reflection->FieldSize(message, F("packed_enum")));
  725. EXPECT_EQ(601, reflection->GetRepeatedInt32(message, F("packed_int32"), 0));
  726. EXPECT_EQ(602, reflection->GetRepeatedInt64(message, F("packed_int64"), 0));
  727. EXPECT_EQ(603, reflection->GetRepeatedUInt32(message, F("packed_uint32"), 0));
  728. EXPECT_EQ(604, reflection->GetRepeatedUInt64(message, F("packed_uint64"), 0));
  729. EXPECT_EQ(605, reflection->GetRepeatedInt32(message, F("packed_sint32"), 0));
  730. EXPECT_EQ(606, reflection->GetRepeatedInt64(message, F("packed_sint64"), 0));
  731. EXPECT_EQ(607,
  732. reflection->GetRepeatedUInt32(message, F("packed_fixed32"), 0));
  733. EXPECT_EQ(608,
  734. reflection->GetRepeatedUInt64(message, F("packed_fixed64"), 0));
  735. EXPECT_EQ(609,
  736. reflection->GetRepeatedInt32(message, F("packed_sfixed32"), 0));
  737. EXPECT_EQ(610,
  738. reflection->GetRepeatedInt64(message, F("packed_sfixed64"), 0));
  739. EXPECT_EQ(611, reflection->GetRepeatedFloat(message, F("packed_float"), 0));
  740. EXPECT_EQ(612, reflection->GetRepeatedDouble(message, F("packed_double"), 0));
  741. EXPECT_TRUE(reflection->GetRepeatedBool(message, F("packed_bool"), 0));
  742. EXPECT_EQ(foreign_bar_,
  743. reflection->GetRepeatedEnum(message, F("packed_enum"), 0));
  744. EXPECT_EQ(701, reflection->GetRepeatedInt32(message, F("packed_int32"), 1));
  745. EXPECT_EQ(702, reflection->GetRepeatedInt64(message, F("packed_int64"), 1));
  746. EXPECT_EQ(703, reflection->GetRepeatedUInt32(message, F("packed_uint32"), 1));
  747. EXPECT_EQ(704, reflection->GetRepeatedUInt64(message, F("packed_uint64"), 1));
  748. EXPECT_EQ(705, reflection->GetRepeatedInt32(message, F("packed_sint32"), 1));
  749. EXPECT_EQ(706, reflection->GetRepeatedInt64(message, F("packed_sint64"), 1));
  750. EXPECT_EQ(707,
  751. reflection->GetRepeatedUInt32(message, F("packed_fixed32"), 1));
  752. EXPECT_EQ(708,
  753. reflection->GetRepeatedUInt64(message, F("packed_fixed64"), 1));
  754. EXPECT_EQ(709,
  755. reflection->GetRepeatedInt32(message, F("packed_sfixed32"), 1));
  756. EXPECT_EQ(710,
  757. reflection->GetRepeatedInt64(message, F("packed_sfixed64"), 1));
  758. EXPECT_EQ(711, reflection->GetRepeatedFloat(message, F("packed_float"), 1));
  759. EXPECT_EQ(712, reflection->GetRepeatedDouble(message, F("packed_double"), 1));
  760. EXPECT_FALSE(reflection->GetRepeatedBool(message, F("packed_bool"), 1));
  761. EXPECT_EQ(foreign_baz_,
  762. reflection->GetRepeatedEnum(message, F("packed_enum"), 1));
  763. }
  764. // -------------------------------------------------------------------
  765. inline void TestUtil::ReflectionTester::ExpectClearViaReflection(
  766. const Message& message) {
  767. const Reflection* reflection = message.GetReflection();
  768. std::string scratch;
  769. const Message* sub_message;
  770. // has_blah() should initially be false for all optional fields.
  771. EXPECT_FALSE(reflection->HasField(message, F("optional_int32")));
  772. EXPECT_FALSE(reflection->HasField(message, F("optional_int64")));
  773. EXPECT_FALSE(reflection->HasField(message, F("optional_uint32")));
  774. EXPECT_FALSE(reflection->HasField(message, F("optional_uint64")));
  775. EXPECT_FALSE(reflection->HasField(message, F("optional_sint32")));
  776. EXPECT_FALSE(reflection->HasField(message, F("optional_sint64")));
  777. EXPECT_FALSE(reflection->HasField(message, F("optional_fixed32")));
  778. EXPECT_FALSE(reflection->HasField(message, F("optional_fixed64")));
  779. EXPECT_FALSE(reflection->HasField(message, F("optional_sfixed32")));
  780. EXPECT_FALSE(reflection->HasField(message, F("optional_sfixed64")));
  781. EXPECT_FALSE(reflection->HasField(message, F("optional_float")));
  782. EXPECT_FALSE(reflection->HasField(message, F("optional_double")));
  783. EXPECT_FALSE(reflection->HasField(message, F("optional_bool")));
  784. EXPECT_FALSE(reflection->HasField(message, F("optional_string")));
  785. EXPECT_FALSE(reflection->HasField(message, F("optional_bytes")));
  786. EXPECT_FALSE(reflection->HasField(message, F("optionalgroup")));
  787. EXPECT_FALSE(reflection->HasField(message, F("optional_nested_message")));
  788. EXPECT_FALSE(reflection->HasField(message, F("optional_foreign_message")));
  789. EXPECT_FALSE(reflection->HasField(message, F("optional_import_message")));
  790. EXPECT_FALSE(
  791. reflection->HasField(message, F("optional_public_import_message")));
  792. EXPECT_FALSE(reflection->HasField(message, F("optional_lazy_message")));
  793. EXPECT_FALSE(reflection->HasField(message, F("optional_nested_enum")));
  794. EXPECT_FALSE(reflection->HasField(message, F("optional_foreign_enum")));
  795. EXPECT_FALSE(reflection->HasField(message, F("optional_import_enum")));
  796. EXPECT_FALSE(reflection->HasField(message, F("optional_string_piece")));
  797. EXPECT_FALSE(reflection->HasField(message, F("optional_cord")));
  798. // Optional fields without defaults are set to zero or something like it.
  799. EXPECT_EQ(0, reflection->GetInt32(message, F("optional_int32")));
  800. EXPECT_EQ(0, reflection->GetInt64(message, F("optional_int64")));
  801. EXPECT_EQ(0, reflection->GetUInt32(message, F("optional_uint32")));
  802. EXPECT_EQ(0, reflection->GetUInt64(message, F("optional_uint64")));
  803. EXPECT_EQ(0, reflection->GetInt32(message, F("optional_sint32")));
  804. EXPECT_EQ(0, reflection->GetInt64(message, F("optional_sint64")));
  805. EXPECT_EQ(0, reflection->GetUInt32(message, F("optional_fixed32")));
  806. EXPECT_EQ(0, reflection->GetUInt64(message, F("optional_fixed64")));
  807. EXPECT_EQ(0, reflection->GetInt32(message, F("optional_sfixed32")));
  808. EXPECT_EQ(0, reflection->GetInt64(message, F("optional_sfixed64")));
  809. EXPECT_EQ(0, reflection->GetFloat(message, F("optional_float")));
  810. EXPECT_EQ(0, reflection->GetDouble(message, F("optional_double")));
  811. EXPECT_FALSE(reflection->GetBool(message, F("optional_bool")));
  812. EXPECT_EQ("", reflection->GetString(message, F("optional_string")));
  813. EXPECT_EQ("", reflection->GetString(message, F("optional_bytes")));
  814. EXPECT_EQ("", reflection->GetStringReference(message, F("optional_string"),
  815. &scratch));
  816. EXPECT_EQ("", reflection->GetStringReference(message, F("optional_bytes"),
  817. &scratch));
  818. // Embedded messages should also be clear.
  819. sub_message = &reflection->GetMessage(message, F("optionalgroup"));
  820. EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, group_a_));
  821. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, group_a_));
  822. sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
  823. EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
  824. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  825. sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
  826. EXPECT_FALSE(
  827. sub_message->GetReflection()->HasField(*sub_message, foreign_c_));
  828. EXPECT_EQ(0,
  829. sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
  830. sub_message = &reflection->GetMessage(message, F("optional_import_message"));
  831. EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, import_d_));
  832. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
  833. sub_message =
  834. &reflection->GetMessage(message, F("optional_public_import_message"));
  835. EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, import_e_));
  836. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, import_e_));
  837. sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
  838. EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
  839. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
  840. // Enums without defaults are set to the first value in the enum.
  841. EXPECT_EQ(nested_foo_,
  842. reflection->GetEnum(message, F("optional_nested_enum")));
  843. EXPECT_EQ(foreign_foo_,
  844. reflection->GetEnum(message, F("optional_foreign_enum")));
  845. EXPECT_EQ(import_foo_,
  846. reflection->GetEnum(message, F("optional_import_enum")));
  847. EXPECT_EQ("", reflection->GetString(message, F("optional_string_piece")));
  848. EXPECT_EQ("", reflection->GetStringReference(
  849. message, F("optional_string_piece"), &scratch));
  850. EXPECT_EQ("", reflection->GetString(message, F("optional_cord")));
  851. EXPECT_EQ("", reflection->GetStringReference(message, F("optional_cord"),
  852. &scratch));
  853. // Repeated fields are empty.
  854. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_int32")));
  855. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_int64")));
  856. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_uint32")));
  857. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_uint64")));
  858. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sint32")));
  859. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sint64")));
  860. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_fixed32")));
  861. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_fixed64")));
  862. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sfixed32")));
  863. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sfixed64")));
  864. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_float")));
  865. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_double")));
  866. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_bool")));
  867. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_string")));
  868. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_bytes")));
  869. EXPECT_EQ(0, reflection->FieldSize(message, F("repeatedgroup")));
  870. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_nested_message")));
  871. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_foreign_message")));
  872. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_import_message")));
  873. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_lazy_message")));
  874. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_nested_enum")));
  875. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_foreign_enum")));
  876. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_import_enum")));
  877. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_string_piece")));
  878. EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_cord")));
  879. // has_blah() should also be false for all default fields.
  880. EXPECT_FALSE(reflection->HasField(message, F("default_int32")));
  881. EXPECT_FALSE(reflection->HasField(message, F("default_int64")));
  882. EXPECT_FALSE(reflection->HasField(message, F("default_uint32")));
  883. EXPECT_FALSE(reflection->HasField(message, F("default_uint64")));
  884. EXPECT_FALSE(reflection->HasField(message, F("default_sint32")));
  885. EXPECT_FALSE(reflection->HasField(message, F("default_sint64")));
  886. EXPECT_FALSE(reflection->HasField(message, F("default_fixed32")));
  887. EXPECT_FALSE(reflection->HasField(message, F("default_fixed64")));
  888. EXPECT_FALSE(reflection->HasField(message, F("default_sfixed32")));
  889. EXPECT_FALSE(reflection->HasField(message, F("default_sfixed64")));
  890. EXPECT_FALSE(reflection->HasField(message, F("default_float")));
  891. EXPECT_FALSE(reflection->HasField(message, F("default_double")));
  892. EXPECT_FALSE(reflection->HasField(message, F("default_bool")));
  893. EXPECT_FALSE(reflection->HasField(message, F("default_string")));
  894. EXPECT_FALSE(reflection->HasField(message, F("default_bytes")));
  895. EXPECT_FALSE(reflection->HasField(message, F("default_nested_enum")));
  896. EXPECT_FALSE(reflection->HasField(message, F("default_foreign_enum")));
  897. EXPECT_FALSE(reflection->HasField(message, F("default_import_enum")));
  898. EXPECT_FALSE(reflection->HasField(message, F("default_string_piece")));
  899. EXPECT_FALSE(reflection->HasField(message, F("default_cord")));
  900. // Fields with defaults have their default values (duh).
  901. EXPECT_EQ(41, reflection->GetInt32(message, F("default_int32")));
  902. EXPECT_EQ(42, reflection->GetInt64(message, F("default_int64")));
  903. EXPECT_EQ(43, reflection->GetUInt32(message, F("default_uint32")));
  904. EXPECT_EQ(44, reflection->GetUInt64(message, F("default_uint64")));
  905. EXPECT_EQ(-45, reflection->GetInt32(message, F("default_sint32")));
  906. EXPECT_EQ(46, reflection->GetInt64(message, F("default_sint64")));
  907. EXPECT_EQ(47, reflection->GetUInt32(message, F("default_fixed32")));
  908. EXPECT_EQ(48, reflection->GetUInt64(message, F("default_fixed64")));
  909. EXPECT_EQ(49, reflection->GetInt32(message, F("default_sfixed32")));
  910. EXPECT_EQ(-50, reflection->GetInt64(message, F("default_sfixed64")));
  911. EXPECT_EQ(51.5, reflection->GetFloat(message, F("default_float")));
  912. EXPECT_EQ(52e3, reflection->GetDouble(message, F("default_double")));
  913. EXPECT_TRUE(reflection->GetBool(message, F("default_bool")));
  914. EXPECT_EQ("hello", reflection->GetString(message, F("default_string")));
  915. EXPECT_EQ("world", reflection->GetString(message, F("default_bytes")));
  916. EXPECT_EQ("hello", reflection->GetStringReference(
  917. message, F("default_string"), &scratch));
  918. EXPECT_EQ("world", reflection->GetStringReference(message, F("default_bytes"),
  919. &scratch));
  920. EXPECT_EQ(nested_bar_,
  921. reflection->GetEnum(message, F("default_nested_enum")));
  922. EXPECT_EQ(foreign_bar_,
  923. reflection->GetEnum(message, F("default_foreign_enum")));
  924. EXPECT_EQ(import_bar_,
  925. reflection->GetEnum(message, F("default_import_enum")));
  926. EXPECT_EQ("abc", reflection->GetString(message, F("default_string_piece")));
  927. EXPECT_EQ("abc", reflection->GetStringReference(
  928. message, F("default_string_piece"), &scratch));
  929. EXPECT_EQ("123", reflection->GetString(message, F("default_cord")));
  930. EXPECT_EQ("123", reflection->GetStringReference(message, F("default_cord"),
  931. &scratch));
  932. }
  933. // -------------------------------------------------------------------
  934. inline void TestUtil::ReflectionTester::ModifyRepeatedFieldsViaReflection(
  935. Message* message) {
  936. const Reflection* reflection = message->GetReflection();
  937. Message* sub_message;
  938. reflection->SetRepeatedInt32(message, F("repeated_int32"), 1, 501);
  939. reflection->SetRepeatedInt64(message, F("repeated_int64"), 1, 502);
  940. reflection->SetRepeatedUInt32(message, F("repeated_uint32"), 1, 503);
  941. reflection->SetRepeatedUInt64(message, F("repeated_uint64"), 1, 504);
  942. reflection->SetRepeatedInt32(message, F("repeated_sint32"), 1, 505);
  943. reflection->SetRepeatedInt64(message, F("repeated_sint64"), 1, 506);
  944. reflection->SetRepeatedUInt32(message, F("repeated_fixed32"), 1, 507);
  945. reflection->SetRepeatedUInt64(message, F("repeated_fixed64"), 1, 508);
  946. reflection->SetRepeatedInt32(message, F("repeated_sfixed32"), 1, 509);
  947. reflection->SetRepeatedInt64(message, F("repeated_sfixed64"), 1, 510);
  948. reflection->SetRepeatedFloat(message, F("repeated_float"), 1, 511);
  949. reflection->SetRepeatedDouble(message, F("repeated_double"), 1, 512);
  950. reflection->SetRepeatedBool(message, F("repeated_bool"), 1, true);
  951. reflection->SetRepeatedString(message, F("repeated_string"), 1, "515");
  952. reflection->SetRepeatedString(message, F("repeated_bytes"), 1, "516");
  953. sub_message =
  954. reflection->MutableRepeatedMessage(message, F("repeatedgroup"), 1);
  955. sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 517);
  956. sub_message = reflection->MutableRepeatedMessage(
  957. message, F("repeated_nested_message"), 1);
  958. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 518);
  959. sub_message = reflection->MutableRepeatedMessage(
  960. message, F("repeated_foreign_message"), 1);
  961. sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 519);
  962. sub_message = reflection->MutableRepeatedMessage(
  963. message, F("repeated_import_message"), 1);
  964. sub_message->GetReflection()->SetInt32(sub_message, import_d_, 520);
  965. sub_message = reflection->MutableRepeatedMessage(
  966. message, F("repeated_lazy_message"), 1);
  967. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 527);
  968. reflection->SetRepeatedEnum(message, F("repeated_nested_enum"), 1,
  969. nested_foo_);
  970. reflection->SetRepeatedEnum(message, F("repeated_foreign_enum"), 1,
  971. foreign_foo_);
  972. reflection->SetRepeatedEnum(message, F("repeated_import_enum"), 1,
  973. import_foo_);
  974. reflection->SetRepeatedString(message, F("repeated_string_piece"), 1, "524");
  975. reflection->SetRepeatedString(message, F("repeated_cord"), 1, "525");
  976. }
  977. inline void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection(
  978. Message* message) {
  979. const Reflection* reflection = message->GetReflection();
  980. std::vector<const FieldDescriptor*> output;
  981. reflection->ListFields(*message, &output);
  982. for (int i = 0; i < output.size(); ++i) {
  983. const FieldDescriptor* field = output[i];
  984. if (!field->is_repeated()) continue;
  985. reflection->RemoveLast(message, field);
  986. }
  987. }
  988. inline void TestUtil::ReflectionTester::ReleaseLastRepeatedsViaReflection(
  989. Message* message, bool expect_extensions_notnull) {
  990. const Reflection* reflection = message->GetReflection();
  991. std::vector<const FieldDescriptor*> output;
  992. reflection->ListFields(*message, &output);
  993. for (int i = 0; i < output.size(); ++i) {
  994. const FieldDescriptor* field = output[i];
  995. if (!field->is_repeated()) continue;
  996. if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
  997. Message* released = reflection->ReleaseLast(message, field);
  998. if (!field->is_extension() || expect_extensions_notnull) {
  999. ASSERT_TRUE(released != nullptr)
  1000. << "ReleaseLast returned nullptr for: " << field->name();
  1001. }
  1002. delete released;
  1003. }
  1004. }
  1005. inline void TestUtil::ReflectionTester::SwapRepeatedsViaReflection(
  1006. Message* message) {
  1007. const Reflection* reflection = message->GetReflection();
  1008. std::vector<const FieldDescriptor*> output;
  1009. reflection->ListFields(*message, &output);
  1010. for (int i = 0; i < output.size(); ++i) {
  1011. const FieldDescriptor* field = output[i];
  1012. if (!field->is_repeated()) continue;
  1013. reflection->SwapElements(message, field, 0, 1);
  1014. }
  1015. }
  1016. inline void TestUtil::ReflectionTester::
  1017. SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message) {
  1018. const Reflection* reflection = message->GetReflection();
  1019. std::vector<const FieldDescriptor*> fields;
  1020. reflection->ListFields(*message, &fields);
  1021. for (int i = 0; i < fields.size(); ++i) {
  1022. const FieldDescriptor* field = fields[i];
  1023. if (!field->is_optional() ||
  1024. field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
  1025. continue;
  1026. reflection->SetAllocatedMessage(message, nullptr, field);
  1027. }
  1028. }
  1029. inline void TestUtil::ReflectionTester::
  1030. SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  1031. Message* from_message, Message* to_message) {
  1032. EXPECT_EQ(from_message->GetDescriptor(), to_message->GetDescriptor());
  1033. const Reflection* from_reflection = from_message->GetReflection();
  1034. const Reflection* to_reflection = to_message->GetReflection();
  1035. std::vector<const FieldDescriptor*> fields;
  1036. from_reflection->ListFields(*from_message, &fields);
  1037. for (int i = 0; i < fields.size(); ++i) {
  1038. const FieldDescriptor* field = fields[i];
  1039. if (!field->is_optional() ||
  1040. field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
  1041. continue;
  1042. Message* sub_message = from_reflection->ReleaseMessage(from_message, field);
  1043. to_reflection->SetAllocatedMessage(to_message, sub_message, field);
  1044. }
  1045. }
  1046. inline void TestUtil::ReflectionTester::ExpectMessagesReleasedViaReflection(
  1047. Message* message,
  1048. TestUtil::ReflectionTester::MessageReleaseState expected_release_state) {
  1049. const Reflection* reflection = message->GetReflection();
  1050. static const char* fields[] = {
  1051. "optionalgroup",
  1052. "optional_nested_message",
  1053. "optional_foreign_message",
  1054. "optional_import_message",
  1055. };
  1056. for (int i = 0; i < GOOGLE_ARRAYSIZE(fields); i++) {
  1057. const Message& sub_message = reflection->GetMessage(*message, F(fields[i]));
  1058. Message* released = reflection->ReleaseMessage(message, F(fields[i]));
  1059. switch (expected_release_state) {
  1060. case IS_NULL:
  1061. EXPECT_TRUE(released == nullptr);
  1062. break;
  1063. case NOT_NULL:
  1064. EXPECT_TRUE(released != nullptr);
  1065. if (message->GetArena() == nullptr) {
  1066. // released message must be same as sub_message if source message is
  1067. // not on arena.
  1068. EXPECT_EQ(&sub_message, released);
  1069. }
  1070. break;
  1071. case CAN_BE_NULL:
  1072. break;
  1073. }
  1074. delete released;
  1075. EXPECT_FALSE(reflection->HasField(*message, F(fields[i])));
  1076. }
  1077. }
  1078. // Check that the passed-in serialization is the canonical serialization we
  1079. // expect for a TestFieldOrderings message filled in by
  1080. // SetAllFieldsAndExtensions().
  1081. inline void ExpectAllFieldsAndExtensionsInOrder(const std::string& serialized) {
  1082. // We set each field individually, serialize separately, and concatenate all
  1083. // the strings in canonical order to determine the expected serialization.
  1084. std::string expected;
  1085. unittest::TestFieldOrderings message;
  1086. message.set_my_int(1); // Field 1.
  1087. message.AppendToString(&expected);
  1088. message.Clear();
  1089. message.SetExtension(unittest::my_extension_int, 23); // Field 5.
  1090. message.AppendToString(&expected);
  1091. message.Clear();
  1092. message.set_my_string("foo"); // Field 11.
  1093. message.AppendToString(&expected);
  1094. message.Clear();
  1095. message.SetExtension(unittest::my_extension_string, "bar"); // Field 50.
  1096. message.AppendToString(&expected);
  1097. message.Clear();
  1098. message.set_my_float(1.0); // Field 101.
  1099. message.AppendToString(&expected);
  1100. message.Clear();
  1101. // We don't EXPECT_EQ() since we don't want to print raw bytes to stdout.
  1102. EXPECT_TRUE(serialized == expected);
  1103. }
  1104. } // namespace TestUtil
  1105. } // namespace protobuf
  1106. } // namespace google
  1107. #include <google/protobuf/port_undef.inc>
  1108. #endif // GOOGLE_PROTOBUF_TEST_UTIL_H__