test_util.h 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. namespace google {
  43. namespace protobuf {
  44. // This file doesn't use these declarations, but some .cc files do.
  45. namespace unittest = ::protobuf_unittest;
  46. namespace unittest_import = ::protobuf_unittest_import;
  47. namespace TestUtil {
  48. class ReflectionTester {
  49. public:
  50. // base_descriptor must be a descriptor for TestAllTypes or
  51. // TestAllExtensions. In the former case, ReflectionTester fetches from
  52. // it the FieldDescriptors needed to use the reflection interface. In
  53. // the latter case, ReflectionTester searches for extension fields in
  54. // its file.
  55. explicit ReflectionTester(const Descriptor* base_descriptor);
  56. void SetAllFieldsViaReflection(Message* message);
  57. void ModifyRepeatedFieldsViaReflection(Message* message);
  58. void ExpectAllFieldsSetViaReflection(const Message& message);
  59. void ExpectClearViaReflection(const Message& message);
  60. void SetPackedFieldsViaReflection(Message* message);
  61. void ModifyPackedFieldsViaReflection(Message* message);
  62. void ExpectPackedFieldsSetViaReflection(const Message& message);
  63. void ExpectPackedClearViaReflection(const Message& message);
  64. void RemoveLastRepeatedsViaReflection(Message* message);
  65. void ReleaseLastRepeatedsViaReflection(Message* message,
  66. bool expect_extensions_notnull);
  67. void SwapRepeatedsViaReflection(Message* message);
  68. void SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message);
  69. static void SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  70. Message* from_message, Message* to_message);
  71. enum MessageReleaseState {
  72. IS_NULL,
  73. CAN_BE_NULL,
  74. NOT_NULL,
  75. };
  76. void ExpectMessagesReleasedViaReflection(
  77. Message* message, MessageReleaseState expected_release_state);
  78. // Set and check functions for TestOneof2 messages. No need to construct
  79. // the ReflectionTester by TestAllTypes nor TestAllExtensions.
  80. static void SetOneofViaReflection(Message* message);
  81. static void ExpectOneofSetViaReflection(const Message& message);
  82. private:
  83. const FieldDescriptor* F(const string& name);
  84. const Descriptor* base_descriptor_;
  85. const FieldDescriptor* group_a_;
  86. const FieldDescriptor* repeated_group_a_;
  87. const FieldDescriptor* nested_b_;
  88. const FieldDescriptor* foreign_c_;
  89. const FieldDescriptor* import_d_;
  90. const FieldDescriptor* import_e_;
  91. const EnumValueDescriptor* nested_foo_;
  92. const EnumValueDescriptor* nested_bar_;
  93. const EnumValueDescriptor* nested_baz_;
  94. const EnumValueDescriptor* foreign_foo_;
  95. const EnumValueDescriptor* foreign_bar_;
  96. const EnumValueDescriptor* foreign_baz_;
  97. const EnumValueDescriptor* import_foo_;
  98. const EnumValueDescriptor* import_bar_;
  99. const EnumValueDescriptor* import_baz_;
  100. // We have to split this into three function otherwise it creates a stack
  101. // frame so large that it triggers a warning.
  102. void ExpectAllFieldsSetViaReflection1(const Message& message);
  103. void ExpectAllFieldsSetViaReflection2(const Message& message);
  104. void ExpectAllFieldsSetViaReflection3(const Message& message);
  105. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionTester);
  106. };
  107. inline TestUtil::ReflectionTester::ReflectionTester(
  108. const Descriptor* base_descriptor)
  109. : base_descriptor_(base_descriptor) {
  110. const DescriptorPool* pool = base_descriptor->file()->pool();
  111. string package = base_descriptor->file()->package();
  112. const FieldDescriptor* import_descriptor =
  113. pool->FindFieldByName(package + ".TestAllTypes.optional_import_message");
  114. string import_package = 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 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. 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. 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. 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. 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. 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. inline void TestUtil::ReflectionTester::ExpectPackedClearViaReflection(
  934. const Message& message) {
  935. const Reflection* reflection = message.GetReflection();
  936. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_int32")));
  937. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_int64")));
  938. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_uint32")));
  939. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_uint64")));
  940. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sint32")));
  941. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sint64")));
  942. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_fixed32")));
  943. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_fixed64")));
  944. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sfixed32")));
  945. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sfixed64")));
  946. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_float")));
  947. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_double")));
  948. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_bool")));
  949. EXPECT_EQ(0, reflection->FieldSize(message, F("packed_enum")));
  950. }
  951. // -------------------------------------------------------------------
  952. inline void TestUtil::ReflectionTester::ModifyRepeatedFieldsViaReflection(
  953. Message* message) {
  954. const Reflection* reflection = message->GetReflection();
  955. Message* sub_message;
  956. reflection->SetRepeatedInt32(message, F("repeated_int32"), 1, 501);
  957. reflection->SetRepeatedInt64(message, F("repeated_int64"), 1, 502);
  958. reflection->SetRepeatedUInt32(message, F("repeated_uint32"), 1, 503);
  959. reflection->SetRepeatedUInt64(message, F("repeated_uint64"), 1, 504);
  960. reflection->SetRepeatedInt32(message, F("repeated_sint32"), 1, 505);
  961. reflection->SetRepeatedInt64(message, F("repeated_sint64"), 1, 506);
  962. reflection->SetRepeatedUInt32(message, F("repeated_fixed32"), 1, 507);
  963. reflection->SetRepeatedUInt64(message, F("repeated_fixed64"), 1, 508);
  964. reflection->SetRepeatedInt32(message, F("repeated_sfixed32"), 1, 509);
  965. reflection->SetRepeatedInt64(message, F("repeated_sfixed64"), 1, 510);
  966. reflection->SetRepeatedFloat(message, F("repeated_float"), 1, 511);
  967. reflection->SetRepeatedDouble(message, F("repeated_double"), 1, 512);
  968. reflection->SetRepeatedBool(message, F("repeated_bool"), 1, true);
  969. reflection->SetRepeatedString(message, F("repeated_string"), 1, "515");
  970. reflection->SetRepeatedString(message, F("repeated_bytes"), 1, "516");
  971. sub_message =
  972. reflection->MutableRepeatedMessage(message, F("repeatedgroup"), 1);
  973. sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 517);
  974. sub_message = reflection->MutableRepeatedMessage(
  975. message, F("repeated_nested_message"), 1);
  976. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 518);
  977. sub_message = reflection->MutableRepeatedMessage(
  978. message, F("repeated_foreign_message"), 1);
  979. sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 519);
  980. sub_message = reflection->MutableRepeatedMessage(
  981. message, F("repeated_import_message"), 1);
  982. sub_message->GetReflection()->SetInt32(sub_message, import_d_, 520);
  983. sub_message = reflection->MutableRepeatedMessage(
  984. message, F("repeated_lazy_message"), 1);
  985. sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 527);
  986. reflection->SetRepeatedEnum(message, F("repeated_nested_enum"), 1,
  987. nested_foo_);
  988. reflection->SetRepeatedEnum(message, F("repeated_foreign_enum"), 1,
  989. foreign_foo_);
  990. reflection->SetRepeatedEnum(message, F("repeated_import_enum"), 1,
  991. import_foo_);
  992. reflection->SetRepeatedString(message, F("repeated_string_piece"), 1, "524");
  993. reflection->SetRepeatedString(message, F("repeated_cord"), 1, "525");
  994. }
  995. inline void TestUtil::ReflectionTester::ModifyPackedFieldsViaReflection(
  996. Message* message) {
  997. const Reflection* reflection = message->GetReflection();
  998. reflection->SetRepeatedInt32(message, F("packed_int32"), 1, 801);
  999. reflection->SetRepeatedInt64(message, F("packed_int64"), 1, 802);
  1000. reflection->SetRepeatedUInt32(message, F("packed_uint32"), 1, 803);
  1001. reflection->SetRepeatedUInt64(message, F("packed_uint64"), 1, 804);
  1002. reflection->SetRepeatedInt32(message, F("packed_sint32"), 1, 805);
  1003. reflection->SetRepeatedInt64(message, F("packed_sint64"), 1, 806);
  1004. reflection->SetRepeatedUInt32(message, F("packed_fixed32"), 1, 807);
  1005. reflection->SetRepeatedUInt64(message, F("packed_fixed64"), 1, 808);
  1006. reflection->SetRepeatedInt32(message, F("packed_sfixed32"), 1, 809);
  1007. reflection->SetRepeatedInt64(message, F("packed_sfixed64"), 1, 810);
  1008. reflection->SetRepeatedFloat(message, F("packed_float"), 1, 811);
  1009. reflection->SetRepeatedDouble(message, F("packed_double"), 1, 812);
  1010. reflection->SetRepeatedBool(message, F("packed_bool"), 1, true);
  1011. reflection->SetRepeatedEnum(message, F("packed_enum"), 1, foreign_foo_);
  1012. }
  1013. inline void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection(
  1014. Message* message) {
  1015. const Reflection* reflection = message->GetReflection();
  1016. std::vector<const FieldDescriptor*> output;
  1017. reflection->ListFields(*message, &output);
  1018. for (int i = 0; i < output.size(); ++i) {
  1019. const FieldDescriptor* field = output[i];
  1020. if (!field->is_repeated()) continue;
  1021. reflection->RemoveLast(message, field);
  1022. }
  1023. }
  1024. inline void TestUtil::ReflectionTester::ReleaseLastRepeatedsViaReflection(
  1025. Message* message, bool expect_extensions_notnull) {
  1026. const Reflection* reflection = message->GetReflection();
  1027. std::vector<const FieldDescriptor*> output;
  1028. reflection->ListFields(*message, &output);
  1029. for (int i = 0; i < output.size(); ++i) {
  1030. const FieldDescriptor* field = output[i];
  1031. if (!field->is_repeated()) continue;
  1032. if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
  1033. Message* released = reflection->ReleaseLast(message, field);
  1034. if (!field->is_extension() || expect_extensions_notnull) {
  1035. ASSERT_TRUE(released != nullptr)
  1036. << "ReleaseLast returned nullptr for: " << field->name();
  1037. }
  1038. delete released;
  1039. }
  1040. }
  1041. inline void TestUtil::ReflectionTester::SwapRepeatedsViaReflection(
  1042. Message* message) {
  1043. const Reflection* reflection = message->GetReflection();
  1044. std::vector<const FieldDescriptor*> output;
  1045. reflection->ListFields(*message, &output);
  1046. for (int i = 0; i < output.size(); ++i) {
  1047. const FieldDescriptor* field = output[i];
  1048. if (!field->is_repeated()) continue;
  1049. reflection->SwapElements(message, field, 0, 1);
  1050. }
  1051. }
  1052. inline void TestUtil::ReflectionTester::
  1053. SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message) {
  1054. const Reflection* reflection = message->GetReflection();
  1055. std::vector<const FieldDescriptor*> fields;
  1056. reflection->ListFields(*message, &fields);
  1057. for (int i = 0; i < fields.size(); ++i) {
  1058. const FieldDescriptor* field = fields[i];
  1059. if (!field->is_optional() ||
  1060. field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
  1061. continue;
  1062. reflection->SetAllocatedMessage(message, nullptr, field);
  1063. }
  1064. }
  1065. inline void TestUtil::ReflectionTester::
  1066. SetAllocatedOptionalMessageFieldsToMessageViaReflection(
  1067. Message* from_message, Message* to_message) {
  1068. EXPECT_EQ(from_message->GetDescriptor(), to_message->GetDescriptor());
  1069. const Reflection* from_reflection = from_message->GetReflection();
  1070. const Reflection* to_reflection = to_message->GetReflection();
  1071. std::vector<const FieldDescriptor*> fields;
  1072. from_reflection->ListFields(*from_message, &fields);
  1073. for (int i = 0; i < fields.size(); ++i) {
  1074. const FieldDescriptor* field = fields[i];
  1075. if (!field->is_optional() ||
  1076. field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
  1077. continue;
  1078. Message* sub_message = from_reflection->ReleaseMessage(from_message, field);
  1079. to_reflection->SetAllocatedMessage(to_message, sub_message, field);
  1080. }
  1081. }
  1082. inline void TestUtil::ReflectionTester::ExpectMessagesReleasedViaReflection(
  1083. Message* message,
  1084. TestUtil::ReflectionTester::MessageReleaseState expected_release_state) {
  1085. const Reflection* reflection = message->GetReflection();
  1086. static const char* fields[] = {
  1087. "optionalgroup",
  1088. "optional_nested_message",
  1089. "optional_foreign_message",
  1090. "optional_import_message",
  1091. };
  1092. for (int i = 0; i < GOOGLE_ARRAYSIZE(fields); i++) {
  1093. const Message& sub_message = reflection->GetMessage(*message, F(fields[i]));
  1094. Message* released = reflection->ReleaseMessage(message, F(fields[i]));
  1095. switch (expected_release_state) {
  1096. case IS_NULL:
  1097. EXPECT_TRUE(released == nullptr);
  1098. break;
  1099. case NOT_NULL:
  1100. EXPECT_TRUE(released != nullptr);
  1101. if (message->GetArena() == nullptr) {
  1102. // released message must be same as sub_message if source message is
  1103. // not on arena.
  1104. EXPECT_EQ(&sub_message, released);
  1105. }
  1106. break;
  1107. case CAN_BE_NULL:
  1108. break;
  1109. }
  1110. delete released;
  1111. EXPECT_FALSE(reflection->HasField(*message, F(fields[i])));
  1112. }
  1113. }
  1114. // Check that the passed-in serialization is the canonical serialization we
  1115. // expect for a TestFieldOrderings message filled in by
  1116. // SetAllFieldsAndExtensions().
  1117. inline void ExpectAllFieldsAndExtensionsInOrder(
  1118. const string& serialized) {
  1119. // We set each field individually, serialize separately, and concatenate all
  1120. // the strings in canonical order to determine the expected serialization.
  1121. string expected;
  1122. unittest::TestFieldOrderings message;
  1123. message.set_my_int(1); // Field 1.
  1124. message.AppendToString(&expected);
  1125. message.Clear();
  1126. message.SetExtension(unittest::my_extension_int, 23); // Field 5.
  1127. message.AppendToString(&expected);
  1128. message.Clear();
  1129. message.set_my_string("foo"); // Field 11.
  1130. message.AppendToString(&expected);
  1131. message.Clear();
  1132. message.SetExtension(unittest::my_extension_string, "bar"); // Field 50.
  1133. message.AppendToString(&expected);
  1134. message.Clear();
  1135. message.set_my_float(1.0); // Field 101.
  1136. message.AppendToString(&expected);
  1137. message.Clear();
  1138. // We don't EXPECT_EQ() since we don't want to print raw bytes to stdout.
  1139. EXPECT_TRUE(serialized == expected);
  1140. }
  1141. } // namespace TestUtil
  1142. } // namespace protobuf
  1143. } // namespace google
  1144. #endif // GOOGLE_PROTOBUF_TEST_UTIL_H__