repeated_field_reflection_unittest.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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: tgs@google.com (Tom Szymanski)
  31. //
  32. // Test reflection methods for aggregate access to Repeated[Ptr]Fields.
  33. // This test proto2 methods on a proto2 layout.
  34. #include <google/protobuf/stubs/casts.h>
  35. #include <google/protobuf/stubs/stringprintf.h>
  36. #include <google/protobuf/dynamic_message.h>
  37. #include <google/protobuf/unittest.pb.h>
  38. #include <google/protobuf/test_util.h>
  39. #include <google/protobuf/reflection.h>
  40. #include <gtest/gtest.h>
  41. namespace google {
  42. namespace protobuf {
  43. using unittest::ForeignMessage;
  44. using unittest::TestAllTypes;
  45. using unittest::TestAllExtensions;
  46. namespace {
  47. static int Func(int i, int j) {
  48. return i * j;
  49. }
  50. static string StrFunc(int i, int j) {
  51. string str;
  52. SStringPrintf(&str, "%d", Func(i, 4));
  53. return str;
  54. }
  55. TEST(RepeatedFieldReflectionTest, RegularFields) {
  56. TestAllTypes message;
  57. const Reflection* refl = message.GetReflection();
  58. const Descriptor* desc = message.GetDescriptor();
  59. for (int i = 0; i < 10; ++i) {
  60. message.add_repeated_int32(Func(i, 1));
  61. message.add_repeated_double(Func(i, 2));
  62. message.add_repeated_string(StrFunc(i, 5));
  63. message.add_repeated_foreign_message()->set_c(Func(i, 6));
  64. }
  65. // Get FieldDescriptors for all the fields of interest.
  66. const FieldDescriptor* fd_repeated_int32 =
  67. desc->FindFieldByName("repeated_int32");
  68. const FieldDescriptor* fd_repeated_double =
  69. desc->FindFieldByName("repeated_double");
  70. const FieldDescriptor* fd_repeated_string =
  71. desc->FindFieldByName("repeated_string");
  72. const FieldDescriptor* fd_repeated_foreign_message =
  73. desc->FindFieldByName("repeated_foreign_message");
  74. // Get RepeatedField objects for all fields of interest.
  75. const RepeatedField<int32>& rf_int32 =
  76. refl->GetRepeatedField<int32>(message, fd_repeated_int32);
  77. const RepeatedField<double>& rf_double =
  78. refl->GetRepeatedField<double>(message, fd_repeated_double);
  79. // Get mutable RepeatedField objects for all fields of interest.
  80. RepeatedField<int32>* mrf_int32 =
  81. refl->MutableRepeatedField<int32>(&message, fd_repeated_int32);
  82. RepeatedField<double>* mrf_double =
  83. refl->MutableRepeatedField<double>(&message, fd_repeated_double);
  84. // Get RepeatedPtrField objects for all fields of interest.
  85. const RepeatedPtrField<string>& rpf_string =
  86. refl->GetRepeatedPtrField<string>(message, fd_repeated_string);
  87. const RepeatedPtrField<ForeignMessage>& rpf_foreign_message =
  88. refl->GetRepeatedPtrField<ForeignMessage>(
  89. message, fd_repeated_foreign_message);
  90. const RepeatedPtrField<Message>& rpf_message =
  91. refl->GetRepeatedPtrField<Message>(
  92. message, fd_repeated_foreign_message);
  93. // Get mutable RepeatedPtrField objects for all fields of interest.
  94. RepeatedPtrField<string>* mrpf_string =
  95. refl->MutableRepeatedPtrField<string>(&message, fd_repeated_string);
  96. RepeatedPtrField<ForeignMessage>* mrpf_foreign_message =
  97. refl->MutableRepeatedPtrField<ForeignMessage>(
  98. &message, fd_repeated_foreign_message);
  99. RepeatedPtrField<Message>* mrpf_message =
  100. refl->MutableRepeatedPtrField<Message>(
  101. &message, fd_repeated_foreign_message);
  102. // Make sure we can do gets and sets through the Repeated[Ptr]Field objects.
  103. for (int i = 0; i < 10; ++i) {
  104. // Check gets through const objects.
  105. EXPECT_EQ(rf_int32.Get(i), Func(i, 1));
  106. EXPECT_EQ(rf_double.Get(i), Func(i, 2));
  107. EXPECT_EQ(rpf_string.Get(i), StrFunc(i, 5));
  108. EXPECT_EQ(rpf_foreign_message.Get(i).c(), Func(i, 6));
  109. EXPECT_EQ(down_cast<const ForeignMessage*>(&rpf_message.Get(i))->c(),
  110. Func(i, 6));
  111. // Check gets through mutable objects.
  112. EXPECT_EQ(mrf_int32->Get(i), Func(i, 1));
  113. EXPECT_EQ(mrf_double->Get(i), Func(i, 2));
  114. EXPECT_EQ(mrpf_string->Get(i), StrFunc(i, 5));
  115. EXPECT_EQ(mrpf_foreign_message->Get(i).c(), Func(i, 6));
  116. EXPECT_EQ(down_cast<const ForeignMessage*>(&mrpf_message->Get(i))->c(),
  117. Func(i, 6));
  118. // Check sets through mutable objects.
  119. mrf_int32->Set(i, Func(i, -1));
  120. mrf_double->Set(i, Func(i, -2));
  121. mrpf_string->Mutable(i)->assign(StrFunc(i, -5));
  122. mrpf_foreign_message->Mutable(i)->set_c(Func(i, -6));
  123. EXPECT_EQ(message.repeated_int32(i), Func(i, -1));
  124. EXPECT_EQ(message.repeated_double(i), Func(i, -2));
  125. EXPECT_EQ(message.repeated_string(i), StrFunc(i, -5));
  126. EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, -6));
  127. down_cast<ForeignMessage*>(mrpf_message->Mutable(i))->set_c(Func(i, 7));
  128. EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, 7));
  129. }
  130. #ifdef PROTOBUF_HAS_DEATH_TEST
  131. // Make sure types are checked correctly at runtime.
  132. const FieldDescriptor* fd_optional_int32 =
  133. desc->FindFieldByName("optional_int32");
  134. EXPECT_DEATH(refl->GetRepeatedField<int32>(
  135. message, fd_optional_int32), "requires a repeated field");
  136. EXPECT_DEATH(refl->GetRepeatedField<double>(
  137. message, fd_repeated_int32), "not the right type");
  138. EXPECT_DEATH(refl->GetRepeatedPtrField<TestAllTypes>(
  139. message, fd_repeated_foreign_message), "wrong submessage type");
  140. #endif // PROTOBUF_HAS_DEATH_TEST
  141. }
  142. TEST(RepeatedFieldReflectionTest, ExtensionFields) {
  143. TestAllExtensions extended_message;
  144. const Reflection* refl = extended_message.GetReflection();
  145. const Descriptor* desc = extended_message.GetDescriptor();
  146. for (int i = 0; i < 10; ++i) {
  147. extended_message.AddExtension(
  148. unittest::repeated_int64_extension, Func(i, 1));
  149. }
  150. const FieldDescriptor* fd_repeated_int64_extension =
  151. desc->file()->FindExtensionByName("repeated_int64_extension");
  152. GOOGLE_CHECK(fd_repeated_int64_extension != NULL);
  153. const RepeatedField<int64>& rf_int64_extension =
  154. refl->GetRepeatedField<int64>(extended_message,
  155. fd_repeated_int64_extension);
  156. RepeatedField<int64>* mrf_int64_extension =
  157. refl->MutableRepeatedField<int64>(&extended_message,
  158. fd_repeated_int64_extension);
  159. for (int i = 0; i < 10; ++i) {
  160. EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i));
  161. mrf_int64_extension->Set(i, Func(i, -1));
  162. EXPECT_EQ(Func(i, -1),
  163. extended_message.GetExtension(unittest::repeated_int64_extension, i));
  164. }
  165. }
  166. template <typename Ref, typename MessageType, typename ValueType>
  167. void TestRepeatedFieldRefIteratorForPrimitive(
  168. const Ref& handle, const MessageType& message,
  169. ValueType (MessageType::*GetFunc)(int) const) {
  170. int index = 0;
  171. for (typename Ref::const_iterator it = handle.begin();
  172. it != handle.end(); ++it) {
  173. EXPECT_EQ((message.*GetFunc)(index), *it);
  174. ++index;
  175. }
  176. EXPECT_EQ(handle.size(), index);
  177. }
  178. template <typename MessageType, typename ValueType>
  179. void TestRepeatedFieldRefIteratorForString(
  180. const RepeatedFieldRef<string>& handle, const MessageType& message,
  181. ValueType (MessageType::*GetFunc)(int) const) {
  182. int index = 0;
  183. for (typename RepeatedFieldRef<string>::const_iterator it = handle.begin();
  184. it != handle.end(); ++it) {
  185. // Test both operator* and operator->
  186. EXPECT_EQ((message.*GetFunc)(index), *it);
  187. EXPECT_EQ((message.*GetFunc)(index).size(), it->size());
  188. ++index;
  189. }
  190. EXPECT_EQ(handle.size(), index);
  191. }
  192. TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) {
  193. TestAllTypes message;
  194. const Reflection* refl = message.GetReflection();
  195. const Descriptor* desc = message.GetDescriptor();
  196. for (int i = 0; i < 10; ++i) {
  197. message.add_repeated_int32(Func(i, 1));
  198. message.add_repeated_double(Func(i, 2));
  199. message.add_repeated_string(StrFunc(i, 5));
  200. message.add_repeated_foreign_message()->set_c(Func(i, 6));
  201. }
  202. // Get FieldDescriptors for all the fields of interest.
  203. const FieldDescriptor* fd_repeated_int32 =
  204. desc->FindFieldByName("repeated_int32");
  205. const FieldDescriptor* fd_repeated_double =
  206. desc->FindFieldByName("repeated_double");
  207. const FieldDescriptor* fd_repeated_string =
  208. desc->FindFieldByName("repeated_string");
  209. const FieldDescriptor* fd_repeated_foreign_message =
  210. desc->FindFieldByName("repeated_foreign_message");
  211. // Get RepeatedFieldRef objects for all fields of interest.
  212. const RepeatedFieldRef<int32> rf_int32 =
  213. refl->GetRepeatedFieldRef<int32>(message, fd_repeated_int32);
  214. const RepeatedFieldRef<double> rf_double =
  215. refl->GetRepeatedFieldRef<double>(message, fd_repeated_double);
  216. const RepeatedFieldRef<string> rf_string =
  217. refl->GetRepeatedFieldRef<string>(message, fd_repeated_string);
  218. const RepeatedFieldRef<ForeignMessage> rf_foreign_message =
  219. refl->GetRepeatedFieldRef<ForeignMessage>(
  220. message, fd_repeated_foreign_message);
  221. const RepeatedFieldRef<Message> rf_message =
  222. refl->GetRepeatedFieldRef<Message>(
  223. message, fd_repeated_foreign_message);
  224. // Get MutableRepeatedFieldRef objects for all fields of interest.
  225. const MutableRepeatedFieldRef<int32> mrf_int32 =
  226. refl->GetMutableRepeatedFieldRef<int32>(&message, fd_repeated_int32);
  227. const MutableRepeatedFieldRef<double> mrf_double =
  228. refl->GetMutableRepeatedFieldRef<double>(&message, fd_repeated_double);
  229. const MutableRepeatedFieldRef<string> mrf_string =
  230. refl->GetMutableRepeatedFieldRef<string>(&message, fd_repeated_string);
  231. const MutableRepeatedFieldRef<ForeignMessage> mrf_foreign_message =
  232. refl->GetMutableRepeatedFieldRef<ForeignMessage>(
  233. &message, fd_repeated_foreign_message);
  234. const MutableRepeatedFieldRef<Message> mrf_message =
  235. refl->GetMutableRepeatedFieldRef<Message>(
  236. &message, fd_repeated_foreign_message);
  237. EXPECT_EQ(message.repeated_int32_size(), rf_int32.size());
  238. EXPECT_EQ(message.repeated_int32_size(), mrf_int32.size());
  239. EXPECT_EQ(message.repeated_double_size(), rf_double.size());
  240. EXPECT_EQ(message.repeated_double_size(), mrf_double.size());
  241. EXPECT_EQ(message.repeated_string_size(), rf_string.size());
  242. EXPECT_EQ(message.repeated_string_size(), mrf_string.size());
  243. EXPECT_EQ(message.repeated_foreign_message_size(),
  244. rf_foreign_message.size());
  245. EXPECT_EQ(message.repeated_foreign_message_size(),
  246. mrf_foreign_message.size());
  247. EXPECT_EQ(message.repeated_foreign_message_size(), rf_message.size());
  248. EXPECT_EQ(message.repeated_foreign_message_size(), mrf_message.size());
  249. EXPECT_FALSE(rf_int32.empty());
  250. EXPECT_FALSE(mrf_int32.empty());
  251. EXPECT_FALSE(rf_double.empty());
  252. EXPECT_FALSE(mrf_double.empty());
  253. EXPECT_FALSE(rf_string.empty());
  254. EXPECT_FALSE(mrf_string.empty());
  255. EXPECT_FALSE(rf_foreign_message.empty());
  256. EXPECT_FALSE(mrf_foreign_message.empty());
  257. EXPECT_FALSE(rf_message.empty());
  258. EXPECT_FALSE(mrf_message.empty());
  259. // Make sure we can do gets and sets through the RepeatedFieldRef objects.
  260. for (int i = 0; i < 10; ++i) {
  261. // Check gets through const objects.
  262. EXPECT_EQ(rf_int32.Get(i), Func(i, 1));
  263. EXPECT_EQ(rf_double.Get(i), Func(i, 2));
  264. EXPECT_EQ(rf_string.Get(i), StrFunc(i, 5));
  265. ForeignMessage scratch_space;
  266. EXPECT_EQ(rf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6));
  267. EXPECT_EQ(down_cast<const ForeignMessage&>(
  268. rf_message.Get(i, &scratch_space)).c(), Func(i, 6));
  269. // Check gets through mutable objects.
  270. EXPECT_EQ(mrf_int32.Get(i), Func(i, 1));
  271. EXPECT_EQ(mrf_double.Get(i), Func(i, 2));
  272. EXPECT_EQ(mrf_string.Get(i), StrFunc(i, 5));
  273. EXPECT_EQ(mrf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6));
  274. EXPECT_EQ(down_cast<const ForeignMessage&>(
  275. mrf_message.Get(i, &scratch_space)).c(), Func(i, 6));
  276. // Check sets through mutable objects.
  277. mrf_int32.Set(i, Func(i, -1));
  278. mrf_double.Set(i, Func(i, -2));
  279. mrf_string.Set(i, StrFunc(i, -5));
  280. ForeignMessage foreign_message;
  281. foreign_message.set_c(Func(i, -6));
  282. mrf_foreign_message.Set(i, foreign_message);
  283. EXPECT_EQ(message.repeated_int32(i), Func(i, -1));
  284. EXPECT_EQ(message.repeated_double(i), Func(i, -2));
  285. EXPECT_EQ(message.repeated_string(i), StrFunc(i, -5));
  286. EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, -6));
  287. foreign_message.set_c(Func(i, 7));
  288. mrf_message.Set(i, foreign_message);
  289. EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, 7));
  290. }
  291. // Test iterators.
  292. TestRepeatedFieldRefIteratorForPrimitive(rf_int32, message,
  293. &TestAllTypes::repeated_int32);
  294. TestRepeatedFieldRefIteratorForPrimitive(rf_double, message,
  295. &TestAllTypes::repeated_double);
  296. TestRepeatedFieldRefIteratorForString(rf_string, message,
  297. &TestAllTypes::repeated_string);
  298. // Test iterators for message fields.
  299. typedef RepeatedFieldRef<ForeignMessage>::iterator MessageIterator;
  300. int index = 0;
  301. for (MessageIterator it = rf_foreign_message.begin();
  302. it != rf_foreign_message.end(); ++it) {
  303. EXPECT_EQ(message.repeated_foreign_message(index).c(), it->c());
  304. ++index;
  305. }
  306. EXPECT_EQ(10, index);
  307. // Test iterator operators that are not ususally used in regular for-loops.
  308. // Including: post increment, assign, ==.
  309. MessageIterator old_it = rf_foreign_message.begin();
  310. MessageIterator new_it = old_it++;
  311. EXPECT_FALSE(old_it == new_it);
  312. // Check that old_it++ increments old_it once.
  313. for (index = 1; old_it != rf_foreign_message.end(); ++old_it, ++index) {
  314. EXPECT_EQ(message.repeated_foreign_message(index).c(), old_it->c());
  315. }
  316. EXPECT_EQ(10, index);
  317. // Test assign operator.
  318. old_it = new_it;
  319. for (index = 0; old_it != rf_foreign_message.end(); ++old_it, ++index) {
  320. EXPECT_EQ(message.repeated_foreign_message(index).c(), old_it->c());
  321. }
  322. EXPECT_EQ(10, index);
  323. // Check that the returned value of old_it++ is the one before increment.
  324. for (index = 0; new_it != rf_foreign_message.end(); ++new_it, ++index) {
  325. EXPECT_EQ(message.repeated_foreign_message(index).c(), new_it->c());
  326. }
  327. EXPECT_EQ(10, index);
  328. // Test MutableRepeatedFieldRef::Add()
  329. mrf_int32.Add(1234);
  330. mrf_double.Add(1234.0);
  331. mrf_string.Add("1234");
  332. ForeignMessage foreign_message;
  333. foreign_message.set_c(1234);
  334. mrf_foreign_message.Add(foreign_message);
  335. EXPECT_EQ(1234, message.repeated_int32(10));
  336. EXPECT_EQ(1234.0, message.repeated_double(10));
  337. EXPECT_EQ("1234", message.repeated_string(10));
  338. EXPECT_EQ(1234, message.repeated_foreign_message(10).c());
  339. // Test MutableRepeatedFieldRef::RemoveLast()
  340. mrf_int32.RemoveLast();
  341. mrf_double.RemoveLast();
  342. mrf_string.RemoveLast();
  343. mrf_foreign_message.RemoveLast();
  344. EXPECT_EQ(10, message.repeated_int32_size());
  345. EXPECT_EQ(10, message.repeated_double_size());
  346. EXPECT_EQ(10, message.repeated_string_size());
  347. EXPECT_EQ(10, message.repeated_foreign_message_size());
  348. // Test MutableRepeatedFieldRef::SwapElements()
  349. mrf_int32.SwapElements(0, 9);
  350. mrf_double.SwapElements(0, 9);
  351. mrf_string.SwapElements(0, 9);
  352. mrf_foreign_message.SwapElements(0, 9);
  353. EXPECT_EQ(Func(9, -1), message.repeated_int32(0));
  354. EXPECT_EQ(Func(0, -1), message.repeated_int32(9));
  355. EXPECT_EQ(Func(9, -2), message.repeated_double(0));
  356. EXPECT_EQ(Func(0, -2), message.repeated_double(9));
  357. EXPECT_EQ(StrFunc(9, -5), message.repeated_string(0));
  358. EXPECT_EQ(StrFunc(0, -5), message.repeated_string(9));
  359. EXPECT_EQ(Func(9, 7), message.repeated_foreign_message(0).c());
  360. EXPECT_EQ(Func(0, 7), message.repeated_foreign_message(9).c());
  361. // Test MutableRepeatedFieldRef::Clear()
  362. mrf_int32.Clear();
  363. mrf_double.Clear();
  364. mrf_string.Clear();
  365. mrf_foreign_message.Clear();
  366. EXPECT_EQ(0, message.repeated_int32_size());
  367. EXPECT_EQ(0, message.repeated_double_size());
  368. EXPECT_EQ(0, message.repeated_string_size());
  369. EXPECT_EQ(0, message.repeated_foreign_message_size());
  370. // Test (Mutable)RepeatedFieldRef::empty()
  371. EXPECT_TRUE(rf_int32.empty());
  372. EXPECT_TRUE(mrf_int32.empty());
  373. EXPECT_TRUE(rf_double.empty());
  374. EXPECT_TRUE(mrf_double.empty());
  375. EXPECT_TRUE(rf_string.empty());
  376. EXPECT_TRUE(mrf_string.empty());
  377. EXPECT_TRUE(rf_foreign_message.empty());
  378. EXPECT_TRUE(mrf_foreign_message.empty());
  379. EXPECT_TRUE(rf_message.empty());
  380. EXPECT_TRUE(mrf_message.empty());
  381. #ifdef PROTOBUF_HAS_DEATH_TEST
  382. // Make sure types are checked correctly at runtime.
  383. const FieldDescriptor* fd_optional_int32 =
  384. desc->FindFieldByName("optional_int32");
  385. EXPECT_DEATH(refl->GetRepeatedFieldRef<int32>(
  386. message, fd_optional_int32), "");
  387. EXPECT_DEATH(refl->GetRepeatedFieldRef<double>(
  388. message, fd_repeated_int32), "");
  389. EXPECT_DEATH(refl->GetRepeatedFieldRef<TestAllTypes>(
  390. message, fd_repeated_foreign_message), "");
  391. #endif // PROTOBUF_HAS_DEATH_TEST
  392. }
  393. TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForEnums) {
  394. TestAllTypes message;
  395. const Reflection* refl = message.GetReflection();
  396. const Descriptor* desc = message.GetDescriptor();
  397. for (int i = 0; i < 10; ++i) {
  398. message.add_repeated_nested_enum(TestAllTypes::BAR);
  399. }
  400. const FieldDescriptor* fd_repeated_nested_enum =
  401. desc->FindFieldByName("repeated_nested_enum");
  402. const RepeatedFieldRef<TestAllTypes::NestedEnum> enum_ref =
  403. refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
  404. message, fd_repeated_nested_enum);
  405. const MutableRepeatedFieldRef<TestAllTypes::NestedEnum>
  406. mutable_enum_ref =
  407. refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
  408. &message, fd_repeated_nested_enum);
  409. const RepeatedFieldRef<int32> int32_ref =
  410. refl->GetRepeatedFieldRef<int32>(
  411. message, fd_repeated_nested_enum);
  412. const MutableRepeatedFieldRef<int32> mutable_int32_ref =
  413. refl->GetMutableRepeatedFieldRef<int32>(
  414. &message, fd_repeated_nested_enum);
  415. EXPECT_EQ(message.repeated_nested_enum_size(), enum_ref.size());
  416. EXPECT_EQ(message.repeated_nested_enum_size(), mutable_enum_ref.size());
  417. EXPECT_EQ(message.repeated_nested_enum_size(), int32_ref.size());
  418. EXPECT_EQ(message.repeated_nested_enum_size(), mutable_int32_ref.size());
  419. EXPECT_FALSE(enum_ref.empty());
  420. EXPECT_FALSE(mutable_enum_ref.empty());
  421. EXPECT_FALSE(int32_ref.empty());
  422. EXPECT_FALSE(mutable_int32_ref.empty());
  423. for (int i = 0; i < 10; ++i) {
  424. EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(i));
  425. EXPECT_EQ(TestAllTypes::BAR, mutable_enum_ref.Get(i));
  426. mutable_enum_ref.Set(i, TestAllTypes::BAZ);
  427. EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(i));
  428. EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(i));
  429. message.set_repeated_nested_enum(i, TestAllTypes::BAR);
  430. EXPECT_EQ(TestAllTypes::BAR, int32_ref.Get(i));
  431. EXPECT_EQ(TestAllTypes::BAR, mutable_int32_ref.Get(i));
  432. mutable_int32_ref.Set(i, TestAllTypes::BAZ);
  433. EXPECT_EQ(TestAllTypes::BAZ, int32_ref.Get(i));
  434. EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(i));
  435. }
  436. TestRepeatedFieldRefIteratorForPrimitive(enum_ref, message,
  437. &TestAllTypes::repeated_nested_enum);
  438. TestRepeatedFieldRefIteratorForPrimitive(int32_ref, message,
  439. &TestAllTypes::repeated_nested_enum);
  440. // Test Add()
  441. mutable_enum_ref.Add(TestAllTypes::FOO);
  442. EXPECT_EQ(TestAllTypes::FOO, message.repeated_nested_enum(10));
  443. mutable_int32_ref.Add(TestAllTypes::BAR);
  444. EXPECT_EQ(TestAllTypes::BAR, message.repeated_nested_enum(11));
  445. // Test RemoveLast()
  446. mutable_enum_ref.RemoveLast();
  447. EXPECT_EQ(11, message.repeated_nested_enum_size());
  448. mutable_int32_ref.RemoveLast();
  449. EXPECT_EQ(10, message.repeated_nested_enum_size());
  450. // Test SwapElements()
  451. mutable_enum_ref.Set(0, TestAllTypes::BAR);
  452. mutable_enum_ref.Set(9, TestAllTypes::BAZ);
  453. mutable_enum_ref.SwapElements(0, 9);
  454. EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(0));
  455. EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(9));
  456. mutable_int32_ref.SwapElements(0, 9);
  457. EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(0));
  458. EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(9));
  459. // Test Clear()
  460. mutable_enum_ref.Clear();
  461. EXPECT_EQ(0, message.repeated_nested_enum_size());
  462. mutable_enum_ref.Add(TestAllTypes::FOO);
  463. EXPECT_EQ(1, message.repeated_nested_enum_size());
  464. mutable_int32_ref.Clear();
  465. EXPECT_EQ(0, message.repeated_nested_enum_size());
  466. // Test empty()
  467. EXPECT_TRUE(enum_ref.empty());
  468. EXPECT_TRUE(mutable_enum_ref.empty());
  469. EXPECT_TRUE(int32_ref.empty());
  470. EXPECT_TRUE(mutable_int32_ref.empty());
  471. }
  472. TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForExtensionFields) {
  473. TestAllExtensions extended_message;
  474. const Reflection* refl = extended_message.GetReflection();
  475. const Descriptor* desc = extended_message.GetDescriptor();
  476. for (int i = 0; i < 10; ++i) {
  477. extended_message.AddExtension(
  478. unittest::repeated_int64_extension, Func(i, 1));
  479. }
  480. const FieldDescriptor* fd_repeated_int64_extension =
  481. desc->file()->FindExtensionByName("repeated_int64_extension");
  482. GOOGLE_CHECK(fd_repeated_int64_extension != NULL);
  483. const RepeatedFieldRef<int64> rf_int64_extension =
  484. refl->GetRepeatedFieldRef<int64>(extended_message,
  485. fd_repeated_int64_extension);
  486. const MutableRepeatedFieldRef<int64> mrf_int64_extension =
  487. refl->GetMutableRepeatedFieldRef<int64>(&extended_message,
  488. fd_repeated_int64_extension);
  489. for (int i = 0; i < 10; ++i) {
  490. EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i));
  491. mrf_int64_extension.Set(i, Func(i, -1));
  492. EXPECT_EQ(Func(i, -1),
  493. extended_message.GetExtension(unittest::repeated_int64_extension, i));
  494. }
  495. }
  496. TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) {
  497. // Set-up message content.
  498. TestAllTypes m0, m1, m2;
  499. for (int i = 0; i < 10; ++i) {
  500. m0.add_repeated_int32(Func(i, 1));
  501. m0.add_repeated_double(Func(i, 2));
  502. m0.add_repeated_string(StrFunc(i, 5));
  503. m0.add_repeated_foreign_message()->set_c(Func(i, 6));
  504. m0.add_repeated_nested_enum(TestAllTypes::FOO);
  505. m1.add_repeated_int32(Func(i, 11));
  506. m1.add_repeated_double(Func(i, 12));
  507. m1.add_repeated_string(StrFunc(i, 15));
  508. m1.add_repeated_foreign_message()->set_c(Func(i, 16));
  509. m1.add_repeated_nested_enum(TestAllTypes::BAR);
  510. m2.add_repeated_int32(Func(i, 21));
  511. m2.add_repeated_double(Func(i, 22));
  512. m2.add_repeated_string(StrFunc(i, 25));
  513. m2.add_repeated_foreign_message()->set_c(Func(i, 26));
  514. m2.add_repeated_nested_enum(TestAllTypes::BAZ);
  515. }
  516. const Reflection* refl = m0.GetReflection();
  517. const Descriptor* desc = m0.GetDescriptor();
  518. // Get FieldDescriptors for all the fields of interest.
  519. const FieldDescriptor* fd_repeated_int32 =
  520. desc->FindFieldByName("repeated_int32");
  521. const FieldDescriptor* fd_repeated_double =
  522. desc->FindFieldByName("repeated_double");
  523. const FieldDescriptor* fd_repeated_string =
  524. desc->FindFieldByName("repeated_string");
  525. const FieldDescriptor* fd_repeated_foreign_message =
  526. desc->FindFieldByName("repeated_foreign_message");
  527. const FieldDescriptor* fd_repeated_nested_enum =
  528. desc->FindFieldByName("repeated_nested_enum");
  529. // Get MutableRepeatedFieldRef objects for all fields of interest.
  530. const MutableRepeatedFieldRef<int32> mrf_int32 =
  531. refl->GetMutableRepeatedFieldRef<int32>(&m0, fd_repeated_int32);
  532. const MutableRepeatedFieldRef<double> mrf_double =
  533. refl->GetMutableRepeatedFieldRef<double>(&m0, fd_repeated_double);
  534. const MutableRepeatedFieldRef<string> mrf_string =
  535. refl->GetMutableRepeatedFieldRef<string>(&m0, fd_repeated_string);
  536. const MutableRepeatedFieldRef<ForeignMessage> mrf_foreign_message =
  537. refl->GetMutableRepeatedFieldRef<ForeignMessage>(
  538. &m0, fd_repeated_foreign_message);
  539. const MutableRepeatedFieldRef<TestAllTypes::NestedEnum>
  540. mrf_nested_enum =
  541. refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
  542. &m0, fd_repeated_nested_enum);
  543. // Test MutableRepeatedRef::CopyFrom
  544. mrf_int32.CopyFrom(
  545. refl->GetRepeatedFieldRef<int32>(m1, fd_repeated_int32));
  546. mrf_double.CopyFrom(
  547. refl->GetRepeatedFieldRef<double>(m1, fd_repeated_double));
  548. mrf_string.CopyFrom(
  549. refl->GetRepeatedFieldRef<string>(m1, fd_repeated_string));
  550. mrf_foreign_message.CopyFrom(
  551. refl->GetRepeatedFieldRef<ForeignMessage>(
  552. m1, fd_repeated_foreign_message));
  553. mrf_nested_enum.CopyFrom(
  554. refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
  555. m1, fd_repeated_nested_enum));
  556. for (int i = 0; i < 10; ++i) {
  557. EXPECT_EQ(Func(i, 11), m0.repeated_int32(i));
  558. EXPECT_EQ(Func(i, 12), m0.repeated_double(i));
  559. EXPECT_EQ(StrFunc(i, 15), m0.repeated_string(i));
  560. EXPECT_EQ(Func(i, 16), m0.repeated_foreign_message(i).c());
  561. EXPECT_EQ(TestAllTypes::BAR, m0.repeated_nested_enum(i));
  562. }
  563. // Test MutableRepeatedRef::MergeFrom
  564. mrf_int32.MergeFrom(
  565. refl->GetRepeatedFieldRef<int32>(m2, fd_repeated_int32));
  566. mrf_double.MergeFrom(
  567. refl->GetRepeatedFieldRef<double>(m2, fd_repeated_double));
  568. mrf_string.MergeFrom(
  569. refl->GetRepeatedFieldRef<string>(m2, fd_repeated_string));
  570. mrf_foreign_message.MergeFrom(
  571. refl->GetRepeatedFieldRef<ForeignMessage>(
  572. m2, fd_repeated_foreign_message));
  573. mrf_nested_enum.MergeFrom(
  574. refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
  575. m2, fd_repeated_nested_enum));
  576. for (int i = 0; i < 10; ++i) {
  577. EXPECT_EQ(Func(i, 21), m0.repeated_int32(i + 10));
  578. EXPECT_EQ(Func(i, 22), m0.repeated_double(i + 10));
  579. EXPECT_EQ(StrFunc(i, 25), m0.repeated_string(i + 10));
  580. EXPECT_EQ(Func(i, 26), m0.repeated_foreign_message(i + 10).c());
  581. EXPECT_EQ(TestAllTypes::BAZ, m0.repeated_nested_enum(i + 10));
  582. }
  583. // Test MutableRepeatedRef::Swap
  584. // Swap between m0 and m2.
  585. mrf_int32.Swap(
  586. refl->GetMutableRepeatedFieldRef<int32>(&m2, fd_repeated_int32));
  587. mrf_double.Swap(
  588. refl->GetMutableRepeatedFieldRef<double>(&m2, fd_repeated_double));
  589. mrf_string.Swap(
  590. refl->GetMutableRepeatedFieldRef<string>(&m2, fd_repeated_string));
  591. mrf_foreign_message.Swap(
  592. refl->GetMutableRepeatedFieldRef<ForeignMessage>(
  593. &m2, fd_repeated_foreign_message));
  594. mrf_nested_enum.Swap(
  595. refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
  596. &m2, fd_repeated_nested_enum));
  597. for (int i = 0; i < 10; ++i) {
  598. // Check the content of m0.
  599. EXPECT_EQ(Func(i, 21), m0.repeated_int32(i));
  600. EXPECT_EQ(Func(i, 22), m0.repeated_double(i));
  601. EXPECT_EQ(StrFunc(i, 25), m0.repeated_string(i));
  602. EXPECT_EQ(Func(i, 26), m0.repeated_foreign_message(i).c());
  603. EXPECT_EQ(TestAllTypes::BAZ, m0.repeated_nested_enum(i));
  604. // Check the content of m2.
  605. EXPECT_EQ(Func(i, 11), m2.repeated_int32(i));
  606. EXPECT_EQ(Func(i, 12), m2.repeated_double(i));
  607. EXPECT_EQ(StrFunc(i, 15), m2.repeated_string(i));
  608. EXPECT_EQ(Func(i, 16), m2.repeated_foreign_message(i).c());
  609. EXPECT_EQ(TestAllTypes::BAR, m2.repeated_nested_enum(i));
  610. EXPECT_EQ(Func(i, 21), m2.repeated_int32(i + 10));
  611. EXPECT_EQ(Func(i, 22), m2.repeated_double(i + 10));
  612. EXPECT_EQ(StrFunc(i, 25), m2.repeated_string(i + 10));
  613. EXPECT_EQ(Func(i, 26), m2.repeated_foreign_message(i + 10).c());
  614. EXPECT_EQ(TestAllTypes::BAZ, m2.repeated_nested_enum(i + 10));
  615. }
  616. }
  617. // Test that GetRepeatedFieldRef/MutableRepeatedFieldRef works with
  618. // DynamicMessage.
  619. TEST(RepeatedFieldReflectionTest, RepeatedFieldRefDynamicMessage) {
  620. // DynamicMessage shares the same memory layout as generated message
  621. // and use the same GeneratedMessageReflection code for reflection.
  622. // All code paths should already be covered by the other tests for
  623. // generated messages. Here we just test one field.
  624. const Descriptor* desc = TestAllTypes::descriptor();
  625. const FieldDescriptor* fd_repeated_int32 =
  626. desc->FindFieldByName("repeated_int32");
  627. DynamicMessageFactory factory;
  628. std::unique_ptr<Message> dynamic_message(factory.GetPrototype(desc)->New());
  629. const Reflection* refl = dynamic_message->GetReflection();
  630. MutableRepeatedFieldRef<int32> rf_int32 =
  631. refl->GetMutableRepeatedFieldRef<int32>(
  632. dynamic_message.get(), fd_repeated_int32);
  633. rf_int32.Add(1234);
  634. EXPECT_EQ(1, refl->FieldSize(*dynamic_message, fd_repeated_int32));
  635. EXPECT_EQ(1234, refl->GetRepeatedInt32(*dynamic_message,
  636. fd_repeated_int32, 0));
  637. }
  638. } // namespace
  639. } // namespace protobuf
  640. } // namespace google