repeated_field_unittest.cc 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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. //
  34. // TODO(kenton): Improve this unittest to bring it up to the standards of
  35. // other proto2 unittests.
  36. #include <algorithm>
  37. #include <limits>
  38. #include <list>
  39. #include <vector>
  40. #include <google/protobuf/repeated_field.h>
  41. #include <google/protobuf/stubs/logging.h>
  42. #include <google/protobuf/stubs/common.h>
  43. #include <google/protobuf/unittest.pb.h>
  44. #include <gmock/gmock.h>
  45. #include <google/protobuf/testing/googletest.h>
  46. #include <gtest/gtest.h>
  47. #include <google/protobuf/stubs/strutil.h>
  48. #include <google/protobuf/stubs/stl_util.h>
  49. namespace google {
  50. namespace protobuf {
  51. namespace {
  52. using ::protobuf_unittest::TestAllTypes;
  53. using ::testing::ElementsAre;
  54. // Test operations on a small RepeatedField.
  55. TEST(RepeatedField, Small) {
  56. RepeatedField<int> field;
  57. EXPECT_TRUE(field.empty());
  58. EXPECT_EQ(field.size(), 0);
  59. field.Add(5);
  60. EXPECT_FALSE(field.empty());
  61. EXPECT_EQ(field.size(), 1);
  62. EXPECT_EQ(field.Get(0), 5);
  63. field.Add(42);
  64. EXPECT_FALSE(field.empty());
  65. EXPECT_EQ(field.size(), 2);
  66. EXPECT_EQ(field.Get(0), 5);
  67. EXPECT_EQ(field.Get(1), 42);
  68. field.Set(1, 23);
  69. EXPECT_FALSE(field.empty());
  70. EXPECT_EQ(field.size(), 2);
  71. EXPECT_EQ(field.Get(0), 5);
  72. EXPECT_EQ(field.Get(1), 23);
  73. field.RemoveLast();
  74. EXPECT_FALSE(field.empty());
  75. EXPECT_EQ(field.size(), 1);
  76. EXPECT_EQ(field.Get(0), 5);
  77. field.Clear();
  78. EXPECT_TRUE(field.empty());
  79. EXPECT_EQ(field.size(), 0);
  80. // Additional bytes are for 'struct Rep' header.
  81. int expected_usage = 4 * sizeof(int) + sizeof(Arena*);
  82. EXPECT_GE(field.SpaceUsedExcludingSelf(), expected_usage);
  83. }
  84. // Test operations on a RepeatedField which is large enough to allocate a
  85. // separate array.
  86. TEST(RepeatedField, Large) {
  87. RepeatedField<int> field;
  88. for (int i = 0; i < 16; i++) {
  89. field.Add(i * i);
  90. }
  91. EXPECT_FALSE(field.empty());
  92. EXPECT_EQ(field.size(), 16);
  93. for (int i = 0; i < 16; i++) {
  94. EXPECT_EQ(field.Get(i), i * i);
  95. }
  96. int expected_usage = 16 * sizeof(int);
  97. EXPECT_GE(field.SpaceUsedExcludingSelf(), expected_usage);
  98. }
  99. // Test swapping between various types of RepeatedFields.
  100. TEST(RepeatedField, SwapSmallSmall) {
  101. RepeatedField<int> field1;
  102. RepeatedField<int> field2;
  103. field1.Add(5);
  104. field1.Add(42);
  105. EXPECT_FALSE(field1.empty());
  106. EXPECT_EQ(field1.size(), 2);
  107. EXPECT_EQ(field1.Get(0), 5);
  108. EXPECT_EQ(field1.Get(1), 42);
  109. EXPECT_TRUE(field2.empty());
  110. EXPECT_EQ(field2.size(), 0);
  111. field1.Swap(&field2);
  112. EXPECT_TRUE(field1.empty());
  113. EXPECT_EQ(field1.size(), 0);
  114. EXPECT_FALSE(field2.empty());
  115. EXPECT_EQ(field2.size(), 2);
  116. EXPECT_EQ(field2.Get(0), 5);
  117. EXPECT_EQ(field2.Get(1), 42);
  118. }
  119. TEST(RepeatedField, SwapLargeSmall) {
  120. RepeatedField<int> field1;
  121. RepeatedField<int> field2;
  122. for (int i = 0; i < 16; i++) {
  123. field1.Add(i * i);
  124. }
  125. field2.Add(5);
  126. field2.Add(42);
  127. field1.Swap(&field2);
  128. EXPECT_EQ(field1.size(), 2);
  129. EXPECT_EQ(field1.Get(0), 5);
  130. EXPECT_EQ(field1.Get(1), 42);
  131. EXPECT_EQ(field2.size(), 16);
  132. for (int i = 0; i < 16; i++) {
  133. EXPECT_EQ(field2.Get(i), i * i);
  134. }
  135. }
  136. TEST(RepeatedField, SwapLargeLarge) {
  137. RepeatedField<int> field1;
  138. RepeatedField<int> field2;
  139. field1.Add(5);
  140. field1.Add(42);
  141. for (int i = 0; i < 16; i++) {
  142. field1.Add(i);
  143. field2.Add(i * i);
  144. }
  145. field2.Swap(&field1);
  146. EXPECT_EQ(field1.size(), 16);
  147. for (int i = 0; i < 16; i++) {
  148. EXPECT_EQ(field1.Get(i), i * i);
  149. }
  150. EXPECT_EQ(field2.size(), 18);
  151. EXPECT_EQ(field2.Get(0), 5);
  152. EXPECT_EQ(field2.Get(1), 42);
  153. for (int i = 2; i < 18; i++) {
  154. EXPECT_EQ(field2.Get(i), i - 2);
  155. }
  156. }
  157. // Determines how much space was reserved by the given field by adding elements
  158. // to it until it re-allocates its space.
  159. static int ReservedSpace(RepeatedField<int>* field) {
  160. const int* ptr = field->data();
  161. do {
  162. field->Add(0);
  163. } while (field->data() == ptr);
  164. return field->size() - 1;
  165. }
  166. TEST(RepeatedField, ReserveMoreThanDouble) {
  167. // Reserve more than double the previous space in the field and expect the
  168. // field to reserve exactly the amount specified.
  169. RepeatedField<int> field;
  170. field.Reserve(20);
  171. EXPECT_LE(20, ReservedSpace(&field));
  172. }
  173. TEST(RepeatedField, ReserveLessThanDouble) {
  174. // Reserve less than double the previous space in the field and expect the
  175. // field to grow by double instead.
  176. RepeatedField<int> field;
  177. field.Reserve(20);
  178. int capacity = field.Capacity();
  179. field.Reserve(capacity * 1.5);
  180. EXPECT_LE(2 * capacity, ReservedSpace(&field));
  181. }
  182. TEST(RepeatedField, ReserveLessThanExisting) {
  183. // Reserve less than the previous space in the field and expect the
  184. // field to not re-allocate at all.
  185. RepeatedField<int> field;
  186. field.Reserve(20);
  187. const int* previous_ptr = field.data();
  188. field.Reserve(10);
  189. EXPECT_EQ(previous_ptr, field.data());
  190. EXPECT_LE(20, ReservedSpace(&field));
  191. }
  192. TEST(RepeatedField, Resize) {
  193. RepeatedField<int> field;
  194. field.Resize(2, 1);
  195. EXPECT_EQ(2, field.size());
  196. field.Resize(5, 2);
  197. EXPECT_EQ(5, field.size());
  198. field.Resize(4, 3);
  199. ASSERT_EQ(4, field.size());
  200. EXPECT_EQ(1, field.Get(0));
  201. EXPECT_EQ(1, field.Get(1));
  202. EXPECT_EQ(2, field.Get(2));
  203. EXPECT_EQ(2, field.Get(3));
  204. field.Resize(0, 4);
  205. EXPECT_TRUE(field.empty());
  206. }
  207. TEST(RepeatedField, MergeFrom) {
  208. RepeatedField<int> source, destination;
  209. source.Add(4);
  210. source.Add(5);
  211. destination.Add(1);
  212. destination.Add(2);
  213. destination.Add(3);
  214. destination.MergeFrom(source);
  215. ASSERT_EQ(5, destination.size());
  216. EXPECT_EQ(1, destination.Get(0));
  217. EXPECT_EQ(2, destination.Get(1));
  218. EXPECT_EQ(3, destination.Get(2));
  219. EXPECT_EQ(4, destination.Get(3));
  220. EXPECT_EQ(5, destination.Get(4));
  221. }
  222. TEST(RepeatedField, CopyFrom) {
  223. RepeatedField<int> source, destination;
  224. source.Add(4);
  225. source.Add(5);
  226. destination.Add(1);
  227. destination.Add(2);
  228. destination.Add(3);
  229. destination.CopyFrom(source);
  230. ASSERT_EQ(2, destination.size());
  231. EXPECT_EQ(4, destination.Get(0));
  232. EXPECT_EQ(5, destination.Get(1));
  233. }
  234. TEST(RepeatedField, CopyFromSelf) {
  235. RepeatedField<int> me;
  236. me.Add(3);
  237. me.CopyFrom(me);
  238. ASSERT_EQ(1, me.size());
  239. EXPECT_EQ(3, me.Get(0));
  240. }
  241. TEST(RepeatedField, Erase) {
  242. RepeatedField<int> me;
  243. RepeatedField<int>::iterator it = me.erase(me.begin(), me.end());
  244. EXPECT_TRUE(me.begin() == it);
  245. EXPECT_EQ(0, me.size());
  246. me.Add(1);
  247. me.Add(2);
  248. me.Add(3);
  249. it = me.erase(me.begin(), me.end());
  250. EXPECT_TRUE(me.begin() == it);
  251. EXPECT_EQ(0, me.size());
  252. me.Add(4);
  253. me.Add(5);
  254. me.Add(6);
  255. it = me.erase(me.begin() + 2, me.end());
  256. EXPECT_TRUE(me.begin() + 2 == it);
  257. EXPECT_EQ(2, me.size());
  258. EXPECT_EQ(4, me.Get(0));
  259. EXPECT_EQ(5, me.Get(1));
  260. me.Add(6);
  261. me.Add(7);
  262. me.Add(8);
  263. it = me.erase(me.begin() + 1, me.begin() + 3);
  264. EXPECT_TRUE(me.begin() + 1 == it);
  265. EXPECT_EQ(3, me.size());
  266. EXPECT_EQ(4, me.Get(0));
  267. EXPECT_EQ(7, me.Get(1));
  268. EXPECT_EQ(8, me.Get(2));
  269. }
  270. TEST(RepeatedField, CopyConstruct) {
  271. RepeatedField<int> source;
  272. source.Add(1);
  273. source.Add(2);
  274. RepeatedField<int> destination(source);
  275. ASSERT_EQ(2, destination.size());
  276. EXPECT_EQ(1, destination.Get(0));
  277. EXPECT_EQ(2, destination.Get(1));
  278. }
  279. TEST(RepeatedField, IteratorConstruct) {
  280. std::vector<int> values;
  281. values.push_back(1);
  282. values.push_back(2);
  283. RepeatedField<int> field(values.begin(), values.end());
  284. ASSERT_EQ(values.size(), field.size());
  285. EXPECT_EQ(values[0], field.Get(0));
  286. EXPECT_EQ(values[1], field.Get(1));
  287. RepeatedField<int> other(field.begin(), field.end());
  288. ASSERT_EQ(values.size(), other.size());
  289. EXPECT_EQ(values[0], other.Get(0));
  290. EXPECT_EQ(values[1], other.Get(1));
  291. }
  292. TEST(RepeatedField, CopyAssign) {
  293. RepeatedField<int> source, destination;
  294. source.Add(4);
  295. source.Add(5);
  296. destination.Add(1);
  297. destination.Add(2);
  298. destination.Add(3);
  299. destination = source;
  300. ASSERT_EQ(2, destination.size());
  301. EXPECT_EQ(4, destination.Get(0));
  302. EXPECT_EQ(5, destination.Get(1));
  303. }
  304. TEST(RepeatedField, SelfAssign) {
  305. // Verify that assignment to self does not destroy data.
  306. RepeatedField<int> source, *p;
  307. p = &source;
  308. source.Add(7);
  309. source.Add(8);
  310. *p = source;
  311. ASSERT_EQ(2, source.size());
  312. EXPECT_EQ(7, source.Get(0));
  313. EXPECT_EQ(8, source.Get(1));
  314. }
  315. TEST(RepeatedField, MoveConstruct) {
  316. {
  317. RepeatedField<int> source;
  318. source.Add(1);
  319. source.Add(2);
  320. const int* data = source.data();
  321. RepeatedField<int> destination = std::move(source);
  322. EXPECT_EQ(data, destination.data());
  323. EXPECT_THAT(destination, ElementsAre(1, 2));
  324. // This property isn't guaranteed but it's useful to have a test that would
  325. // catch changes in this area.
  326. EXPECT_TRUE(source.empty());
  327. }
  328. {
  329. Arena arena;
  330. RepeatedField<int>* source =
  331. Arena::CreateMessage<RepeatedField<int>>(&arena);
  332. source->Add(1);
  333. source->Add(2);
  334. RepeatedField<int> destination = std::move(*source);
  335. EXPECT_EQ(nullptr, destination.GetArena());
  336. EXPECT_THAT(destination, ElementsAre(1, 2));
  337. // This property isn't guaranteed but it's useful to have a test that would
  338. // catch changes in this area.
  339. EXPECT_THAT(*source, ElementsAre(1, 2));
  340. }
  341. }
  342. TEST(RepeatedField, MoveAssign) {
  343. {
  344. RepeatedField<int> source;
  345. source.Add(1);
  346. source.Add(2);
  347. RepeatedField<int> destination;
  348. destination.Add(3);
  349. const int* source_data = source.data();
  350. const int* destination_data = destination.data();
  351. destination = std::move(source);
  352. EXPECT_EQ(source_data, destination.data());
  353. EXPECT_THAT(destination, ElementsAre(1, 2));
  354. // This property isn't guaranteed but it's useful to have a test that would
  355. // catch changes in this area.
  356. EXPECT_EQ(destination_data, source.data());
  357. EXPECT_THAT(source, ElementsAre(3));
  358. }
  359. {
  360. Arena arena;
  361. RepeatedField<int>* source =
  362. Arena::CreateMessage<RepeatedField<int>>(&arena);
  363. source->Add(1);
  364. source->Add(2);
  365. RepeatedField<int>* destination =
  366. Arena::CreateMessage<RepeatedField<int>>(&arena);
  367. destination->Add(3);
  368. const int* source_data = source->data();
  369. const int* destination_data = destination->data();
  370. *destination = std::move(*source);
  371. EXPECT_EQ(source_data, destination->data());
  372. EXPECT_THAT(*destination, ElementsAre(1, 2));
  373. // This property isn't guaranteed but it's useful to have a test that would
  374. // catch changes in this area.
  375. EXPECT_EQ(destination_data, source->data());
  376. EXPECT_THAT(*source, ElementsAre(3));
  377. }
  378. {
  379. Arena source_arena;
  380. RepeatedField<int>* source =
  381. Arena::CreateMessage<RepeatedField<int>>(&source_arena);
  382. source->Add(1);
  383. source->Add(2);
  384. Arena destination_arena;
  385. RepeatedField<int>* destination =
  386. Arena::CreateMessage<RepeatedField<int>>(&destination_arena);
  387. destination->Add(3);
  388. *destination = std::move(*source);
  389. EXPECT_THAT(*destination, ElementsAre(1, 2));
  390. // This property isn't guaranteed but it's useful to have a test that would
  391. // catch changes in this area.
  392. EXPECT_THAT(*source, ElementsAre(1, 2));
  393. }
  394. {
  395. Arena arena;
  396. RepeatedField<int>* source =
  397. Arena::CreateMessage<RepeatedField<int>>(&arena);
  398. source->Add(1);
  399. source->Add(2);
  400. RepeatedField<int> destination;
  401. destination.Add(3);
  402. destination = std::move(*source);
  403. EXPECT_THAT(destination, ElementsAre(1, 2));
  404. // This property isn't guaranteed but it's useful to have a test that would
  405. // catch changes in this area.
  406. EXPECT_THAT(*source, ElementsAre(1, 2));
  407. }
  408. {
  409. RepeatedField<int> source;
  410. source.Add(1);
  411. source.Add(2);
  412. Arena arena;
  413. RepeatedField<int>* destination =
  414. Arena::CreateMessage<RepeatedField<int>>(&arena);
  415. destination->Add(3);
  416. *destination = std::move(source);
  417. EXPECT_THAT(*destination, ElementsAre(1, 2));
  418. // This property isn't guaranteed but it's useful to have a test that would
  419. // catch changes in this area.
  420. EXPECT_THAT(source, ElementsAre(1, 2));
  421. }
  422. {
  423. RepeatedField<int> field;
  424. // An alias to defeat -Wself-move.
  425. RepeatedField<int>& alias = field;
  426. field.Add(1);
  427. field.Add(2);
  428. const int* data = field.data();
  429. field = std::move(alias);
  430. EXPECT_EQ(data, field.data());
  431. EXPECT_THAT(field, ElementsAre(1, 2));
  432. }
  433. {
  434. Arena arena;
  435. RepeatedField<int>* field =
  436. Arena::CreateMessage<RepeatedField<int>>(&arena);
  437. field->Add(1);
  438. field->Add(2);
  439. const int* data = field->data();
  440. *field = std::move(*field);
  441. EXPECT_EQ(data, field->data());
  442. EXPECT_THAT(*field, ElementsAre(1, 2));
  443. }
  444. }
  445. TEST(RepeatedField, MutableDataIsMutable) {
  446. RepeatedField<int> field;
  447. field.Add(1);
  448. EXPECT_EQ(1, field.Get(0));
  449. // The fact that this line compiles would be enough, but we'll check the
  450. // value anyway.
  451. *field.mutable_data() = 2;
  452. EXPECT_EQ(2, field.Get(0));
  453. }
  454. TEST(RepeatedField, SubscriptOperators) {
  455. RepeatedField<int> field;
  456. field.Add(1);
  457. EXPECT_EQ(1, field.Get(0));
  458. EXPECT_EQ(1, field[0]);
  459. EXPECT_EQ(field.Mutable(0), &field[0]);
  460. const RepeatedField<int>& const_field = field;
  461. EXPECT_EQ(field.data(), &const_field[0]);
  462. }
  463. TEST(RepeatedField, Truncate) {
  464. RepeatedField<int> field;
  465. field.Add(12);
  466. field.Add(34);
  467. field.Add(56);
  468. field.Add(78);
  469. EXPECT_EQ(4, field.size());
  470. field.Truncate(3);
  471. EXPECT_EQ(3, field.size());
  472. field.Add(90);
  473. EXPECT_EQ(4, field.size());
  474. EXPECT_EQ(90, field.Get(3));
  475. // Truncations that don't change the size are allowed, but growing is not
  476. // allowed.
  477. field.Truncate(field.size());
  478. #ifdef PROTOBUF_HAS_DEATH_TEST
  479. EXPECT_DEBUG_DEATH(field.Truncate(field.size() + 1), "new_size");
  480. #endif
  481. }
  482. TEST(RepeatedField, ExtractSubrange) {
  483. // Exhaustively test every subrange in arrays of all sizes from 0 through 9.
  484. for (int sz = 0; sz < 10; ++sz) {
  485. for (int num = 0; num <= sz; ++num) {
  486. for (int start = 0; start < sz - num; ++start) {
  487. // Create RepeatedField with sz elements having values 0 through sz-1.
  488. RepeatedField<int32> field;
  489. for (int i = 0; i < sz; ++i)
  490. field.Add(i);
  491. EXPECT_EQ(field.size(), sz);
  492. // Create a catcher array and call ExtractSubrange.
  493. int32 catcher[10];
  494. for (int i = 0; i < 10; ++i)
  495. catcher[i] = -1;
  496. field.ExtractSubrange(start, num, catcher);
  497. // Does the resulting array have the right size?
  498. EXPECT_EQ(field.size(), sz - num);
  499. // Were the removed elements extracted into the catcher array?
  500. for (int i = 0; i < num; ++i)
  501. EXPECT_EQ(catcher[i], start + i);
  502. EXPECT_EQ(catcher[num], -1);
  503. // Does the resulting array contain the right values?
  504. for (int i = 0; i < start; ++i)
  505. EXPECT_EQ(field.Get(i), i);
  506. for (int i = start; i < field.size(); ++i)
  507. EXPECT_EQ(field.Get(i), i + num);
  508. }
  509. }
  510. }
  511. }
  512. TEST(RepeatedField, ClearThenReserveMore) {
  513. // Test that Reserve properly destroys the old internal array when it's forced
  514. // to allocate a new one, even when cleared-but-not-deleted objects are
  515. // present. Use a 'string' and > 16 bytes length so that the elements are
  516. // non-POD and allocate -- the leak checker will catch any skipped destructor
  517. // calls here.
  518. RepeatedField<string> field;
  519. for (int i = 0; i < 32; i++) {
  520. field.Add(string("abcdefghijklmnopqrstuvwxyz0123456789"));
  521. }
  522. EXPECT_EQ(32, field.size());
  523. field.Clear();
  524. EXPECT_EQ(0, field.size());
  525. EXPECT_LE(32, field.Capacity());
  526. field.Reserve(1024);
  527. EXPECT_EQ(0, field.size());
  528. EXPECT_LE(1024, field.Capacity());
  529. // Finish test -- |field| should destroy the cleared-but-not-yet-destroyed
  530. // strings.
  531. }
  532. // ===================================================================
  533. // RepeatedPtrField tests. These pretty much just mirror the RepeatedField
  534. // tests above.
  535. TEST(RepeatedPtrField, Small) {
  536. RepeatedPtrField<string> field;
  537. EXPECT_TRUE(field.empty());
  538. EXPECT_EQ(field.size(), 0);
  539. field.Add()->assign("foo");
  540. EXPECT_FALSE(field.empty());
  541. EXPECT_EQ(field.size(), 1);
  542. EXPECT_EQ(field.Get(0), "foo");
  543. field.Add()->assign("bar");
  544. EXPECT_FALSE(field.empty());
  545. EXPECT_EQ(field.size(), 2);
  546. EXPECT_EQ(field.Get(0), "foo");
  547. EXPECT_EQ(field.Get(1), "bar");
  548. field.Mutable(1)->assign("baz");
  549. EXPECT_FALSE(field.empty());
  550. EXPECT_EQ(field.size(), 2);
  551. EXPECT_EQ(field.Get(0), "foo");
  552. EXPECT_EQ(field.Get(1), "baz");
  553. field.RemoveLast();
  554. EXPECT_FALSE(field.empty());
  555. EXPECT_EQ(field.size(), 1);
  556. EXPECT_EQ(field.Get(0), "foo");
  557. field.Clear();
  558. EXPECT_TRUE(field.empty());
  559. EXPECT_EQ(field.size(), 0);
  560. }
  561. TEST(RepeatedPtrField, Large) {
  562. RepeatedPtrField<string> field;
  563. for (int i = 0; i < 16; i++) {
  564. *field.Add() += 'a' + i;
  565. }
  566. EXPECT_EQ(field.size(), 16);
  567. for (int i = 0; i < 16; i++) {
  568. EXPECT_EQ(field.Get(i).size(), 1);
  569. EXPECT_EQ(field.Get(i)[0], 'a' + i);
  570. }
  571. int min_expected_usage = 16 * sizeof(string);
  572. EXPECT_GE(field.SpaceUsedExcludingSelf(), min_expected_usage);
  573. }
  574. TEST(RepeatedPtrField, SwapSmallSmall) {
  575. RepeatedPtrField<string> field1;
  576. RepeatedPtrField<string> field2;
  577. EXPECT_TRUE(field1.empty());
  578. EXPECT_EQ(field1.size(), 0);
  579. EXPECT_TRUE(field2.empty());
  580. EXPECT_EQ(field2.size(), 0);
  581. field1.Add()->assign("foo");
  582. field1.Add()->assign("bar");
  583. EXPECT_FALSE(field1.empty());
  584. EXPECT_EQ(field1.size(), 2);
  585. EXPECT_EQ(field1.Get(0), "foo");
  586. EXPECT_EQ(field1.Get(1), "bar");
  587. EXPECT_TRUE(field2.empty());
  588. EXPECT_EQ(field2.size(), 0);
  589. field1.Swap(&field2);
  590. EXPECT_TRUE(field1.empty());
  591. EXPECT_EQ(field1.size(), 0);
  592. EXPECT_EQ(field2.size(), 2);
  593. EXPECT_EQ(field2.Get(0), "foo");
  594. EXPECT_EQ(field2.Get(1), "bar");
  595. }
  596. TEST(RepeatedPtrField, SwapLargeSmall) {
  597. RepeatedPtrField<string> field1;
  598. RepeatedPtrField<string> field2;
  599. field2.Add()->assign("foo");
  600. field2.Add()->assign("bar");
  601. for (int i = 0; i < 16; i++) {
  602. *field1.Add() += 'a' + i;
  603. }
  604. field1.Swap(&field2);
  605. EXPECT_EQ(field1.size(), 2);
  606. EXPECT_EQ(field1.Get(0), "foo");
  607. EXPECT_EQ(field1.Get(1), "bar");
  608. EXPECT_EQ(field2.size(), 16);
  609. for (int i = 0; i < 16; i++) {
  610. EXPECT_EQ(field2.Get(i).size(), 1);
  611. EXPECT_EQ(field2.Get(i)[0], 'a' + i);
  612. }
  613. }
  614. TEST(RepeatedPtrField, SwapLargeLarge) {
  615. RepeatedPtrField<string> field1;
  616. RepeatedPtrField<string> field2;
  617. field1.Add()->assign("foo");
  618. field1.Add()->assign("bar");
  619. for (int i = 0; i < 16; i++) {
  620. *field1.Add() += 'A' + i;
  621. *field2.Add() += 'a' + i;
  622. }
  623. field2.Swap(&field1);
  624. EXPECT_EQ(field1.size(), 16);
  625. for (int i = 0; i < 16; i++) {
  626. EXPECT_EQ(field1.Get(i).size(), 1);
  627. EXPECT_EQ(field1.Get(i)[0], 'a' + i);
  628. }
  629. EXPECT_EQ(field2.size(), 18);
  630. EXPECT_EQ(field2.Get(0), "foo");
  631. EXPECT_EQ(field2.Get(1), "bar");
  632. for (int i = 2; i < 18; i++) {
  633. EXPECT_EQ(field2.Get(i).size(), 1);
  634. EXPECT_EQ(field2.Get(i)[0], 'A' + i - 2);
  635. }
  636. }
  637. static int ReservedSpace(RepeatedPtrField<string>* field) {
  638. const string* const* ptr = field->data();
  639. do {
  640. field->Add();
  641. } while (field->data() == ptr);
  642. return field->size() - 1;
  643. }
  644. TEST(RepeatedPtrField, ReserveMoreThanDouble) {
  645. RepeatedPtrField<string> field;
  646. field.Reserve(20);
  647. EXPECT_LE(20, ReservedSpace(&field));
  648. }
  649. TEST(RepeatedPtrField, ReserveLessThanDouble) {
  650. RepeatedPtrField<string> field;
  651. field.Reserve(20);
  652. int capacity = field.Capacity();
  653. // Grow by 1.5x
  654. field.Reserve(capacity + (capacity >> 2));
  655. EXPECT_LE(2 * capacity, ReservedSpace(&field));
  656. }
  657. TEST(RepeatedPtrField, ReserveLessThanExisting) {
  658. RepeatedPtrField<string> field;
  659. field.Reserve(20);
  660. const string* const* previous_ptr = field.data();
  661. field.Reserve(10);
  662. EXPECT_EQ(previous_ptr, field.data());
  663. EXPECT_LE(20, ReservedSpace(&field));
  664. }
  665. TEST(RepeatedPtrField, ReserveDoesntLoseAllocated) {
  666. // Check that a bug is fixed: An earlier implementation of Reserve()
  667. // failed to copy pointers to allocated-but-cleared objects, possibly
  668. // leading to segfaults.
  669. RepeatedPtrField<string> field;
  670. string* first = field.Add();
  671. field.RemoveLast();
  672. field.Reserve(20);
  673. EXPECT_EQ(first, field.Add());
  674. }
  675. // Clearing elements is tricky with RepeatedPtrFields since the memory for
  676. // the elements is retained and reused.
  677. TEST(RepeatedPtrField, ClearedElements) {
  678. RepeatedPtrField<string> field;
  679. string* original = field.Add();
  680. *original = "foo";
  681. EXPECT_EQ(field.ClearedCount(), 0);
  682. field.RemoveLast();
  683. EXPECT_TRUE(original->empty());
  684. EXPECT_EQ(field.ClearedCount(), 1);
  685. EXPECT_EQ(field.Add(), original); // Should return same string for reuse.
  686. EXPECT_EQ(field.ReleaseLast(), original); // We take ownership.
  687. EXPECT_EQ(field.ClearedCount(), 0);
  688. EXPECT_NE(field.Add(), original); // Should NOT return the same string.
  689. EXPECT_EQ(field.ClearedCount(), 0);
  690. field.AddAllocated(original); // Give ownership back.
  691. EXPECT_EQ(field.ClearedCount(), 0);
  692. EXPECT_EQ(field.Mutable(1), original);
  693. field.Clear();
  694. EXPECT_EQ(field.ClearedCount(), 2);
  695. EXPECT_EQ(field.ReleaseCleared(), original); // Take ownership again.
  696. EXPECT_EQ(field.ClearedCount(), 1);
  697. EXPECT_NE(field.Add(), original);
  698. EXPECT_EQ(field.ClearedCount(), 0);
  699. EXPECT_NE(field.Add(), original);
  700. EXPECT_EQ(field.ClearedCount(), 0);
  701. field.AddCleared(original); // Give ownership back, but as a cleared object.
  702. EXPECT_EQ(field.ClearedCount(), 1);
  703. EXPECT_EQ(field.Add(), original);
  704. EXPECT_EQ(field.ClearedCount(), 0);
  705. }
  706. // Test all code paths in AddAllocated().
  707. TEST(RepeatedPtrField, AddAlocated) {
  708. RepeatedPtrField<string> field;
  709. while (field.size() < field.Capacity()) {
  710. field.Add()->assign("filler");
  711. }
  712. int index = field.size();
  713. // First branch: Field is at capacity with no cleared objects.
  714. string* foo = new string("foo");
  715. field.AddAllocated(foo);
  716. EXPECT_EQ(index + 1, field.size());
  717. EXPECT_EQ(0, field.ClearedCount());
  718. EXPECT_EQ(foo, &field.Get(index));
  719. // Last branch: Field is not at capacity and there are no cleared objects.
  720. string* bar = new string("bar");
  721. field.AddAllocated(bar);
  722. ++index;
  723. EXPECT_EQ(index + 1, field.size());
  724. EXPECT_EQ(0, field.ClearedCount());
  725. EXPECT_EQ(bar, &field.Get(index));
  726. // Third branch: Field is not at capacity and there are no cleared objects.
  727. field.RemoveLast();
  728. string* baz = new string("baz");
  729. field.AddAllocated(baz);
  730. EXPECT_EQ(index + 1, field.size());
  731. EXPECT_EQ(1, field.ClearedCount());
  732. EXPECT_EQ(baz, &field.Get(index));
  733. // Second branch: Field is at capacity but has some cleared objects.
  734. while (field.size() < field.Capacity()) {
  735. field.Add()->assign("filler2");
  736. }
  737. field.RemoveLast();
  738. index = field.size();
  739. string* qux = new string("qux");
  740. field.AddAllocated(qux);
  741. EXPECT_EQ(index + 1, field.size());
  742. // We should have discarded the cleared object.
  743. EXPECT_EQ(0, field.ClearedCount());
  744. EXPECT_EQ(qux, &field.Get(index));
  745. }
  746. TEST(RepeatedPtrField, MergeFrom) {
  747. RepeatedPtrField<string> source, destination;
  748. source.Add()->assign("4");
  749. source.Add()->assign("5");
  750. destination.Add()->assign("1");
  751. destination.Add()->assign("2");
  752. destination.Add()->assign("3");
  753. destination.MergeFrom(source);
  754. ASSERT_EQ(5, destination.size());
  755. EXPECT_EQ("1", destination.Get(0));
  756. EXPECT_EQ("2", destination.Get(1));
  757. EXPECT_EQ("3", destination.Get(2));
  758. EXPECT_EQ("4", destination.Get(3));
  759. EXPECT_EQ("5", destination.Get(4));
  760. }
  761. TEST(RepeatedPtrField, CopyFrom) {
  762. RepeatedPtrField<string> source, destination;
  763. source.Add()->assign("4");
  764. source.Add()->assign("5");
  765. destination.Add()->assign("1");
  766. destination.Add()->assign("2");
  767. destination.Add()->assign("3");
  768. destination.CopyFrom(source);
  769. ASSERT_EQ(2, destination.size());
  770. EXPECT_EQ("4", destination.Get(0));
  771. EXPECT_EQ("5", destination.Get(1));
  772. }
  773. TEST(RepeatedPtrField, CopyFromSelf) {
  774. RepeatedPtrField<string> me;
  775. me.Add()->assign("1");
  776. me.CopyFrom(me);
  777. ASSERT_EQ(1, me.size());
  778. EXPECT_EQ("1", me.Get(0));
  779. }
  780. TEST(RepeatedPtrField, Erase) {
  781. RepeatedPtrField<string> me;
  782. RepeatedPtrField<string>::iterator it = me.erase(me.begin(), me.end());
  783. EXPECT_TRUE(me.begin() == it);
  784. EXPECT_EQ(0, me.size());
  785. *me.Add() = "1";
  786. *me.Add() = "2";
  787. *me.Add() = "3";
  788. it = me.erase(me.begin(), me.end());
  789. EXPECT_TRUE(me.begin() == it);
  790. EXPECT_EQ(0, me.size());
  791. *me.Add() = "4";
  792. *me.Add() = "5";
  793. *me.Add() = "6";
  794. it = me.erase(me.begin() + 2, me.end());
  795. EXPECT_TRUE(me.begin() + 2 == it);
  796. EXPECT_EQ(2, me.size());
  797. EXPECT_EQ("4", me.Get(0));
  798. EXPECT_EQ("5", me.Get(1));
  799. *me.Add() = "6";
  800. *me.Add() = "7";
  801. *me.Add() = "8";
  802. it = me.erase(me.begin() + 1, me.begin() + 3);
  803. EXPECT_TRUE(me.begin() + 1 == it);
  804. EXPECT_EQ(3, me.size());
  805. EXPECT_EQ("4", me.Get(0));
  806. EXPECT_EQ("7", me.Get(1));
  807. EXPECT_EQ("8", me.Get(2));
  808. }
  809. TEST(RepeatedPtrField, CopyConstruct) {
  810. RepeatedPtrField<string> source;
  811. source.Add()->assign("1");
  812. source.Add()->assign("2");
  813. RepeatedPtrField<string> destination(source);
  814. ASSERT_EQ(2, destination.size());
  815. EXPECT_EQ("1", destination.Get(0));
  816. EXPECT_EQ("2", destination.Get(1));
  817. }
  818. TEST(RepeatedPtrField, IteratorConstruct_String) {
  819. std::vector<string> values;
  820. values.push_back("1");
  821. values.push_back("2");
  822. RepeatedPtrField<string> field(values.begin(), values.end());
  823. ASSERT_EQ(values.size(), field.size());
  824. EXPECT_EQ(values[0], field.Get(0));
  825. EXPECT_EQ(values[1], field.Get(1));
  826. RepeatedPtrField<string> other(field.begin(), field.end());
  827. ASSERT_EQ(values.size(), other.size());
  828. EXPECT_EQ(values[0], other.Get(0));
  829. EXPECT_EQ(values[1], other.Get(1));
  830. }
  831. TEST(RepeatedPtrField, IteratorConstruct_Proto) {
  832. typedef TestAllTypes::NestedMessage Nested;
  833. std::vector<Nested> values;
  834. values.push_back(Nested());
  835. values.back().set_bb(1);
  836. values.push_back(Nested());
  837. values.back().set_bb(2);
  838. RepeatedPtrField<Nested> field(values.begin(), values.end());
  839. ASSERT_EQ(values.size(), field.size());
  840. EXPECT_EQ(values[0].bb(), field.Get(0).bb());
  841. EXPECT_EQ(values[1].bb(), field.Get(1).bb());
  842. RepeatedPtrField<Nested> other(field.begin(), field.end());
  843. ASSERT_EQ(values.size(), other.size());
  844. EXPECT_EQ(values[0].bb(), other.Get(0).bb());
  845. EXPECT_EQ(values[1].bb(), other.Get(1).bb());
  846. }
  847. TEST(RepeatedPtrField, CopyAssign) {
  848. RepeatedPtrField<string> source, destination;
  849. source.Add()->assign("4");
  850. source.Add()->assign("5");
  851. destination.Add()->assign("1");
  852. destination.Add()->assign("2");
  853. destination.Add()->assign("3");
  854. destination = source;
  855. ASSERT_EQ(2, destination.size());
  856. EXPECT_EQ("4", destination.Get(0));
  857. EXPECT_EQ("5", destination.Get(1));
  858. }
  859. TEST(RepeatedPtrField, SelfAssign) {
  860. // Verify that assignment to self does not destroy data.
  861. RepeatedPtrField<string> source, *p;
  862. p = &source;
  863. source.Add()->assign("7");
  864. source.Add()->assign("8");
  865. *p = source;
  866. ASSERT_EQ(2, source.size());
  867. EXPECT_EQ("7", source.Get(0));
  868. EXPECT_EQ("8", source.Get(1));
  869. }
  870. TEST(RepeatedPtrField, MoveConstruct) {
  871. {
  872. RepeatedPtrField<string> source;
  873. *source.Add() = "1";
  874. *source.Add() = "2";
  875. const string* const* data = source.data();
  876. RepeatedPtrField<string> destination = std::move(source);
  877. EXPECT_EQ(data, destination.data());
  878. EXPECT_THAT(destination, ElementsAre("1", "2"));
  879. // This property isn't guaranteed but it's useful to have a test that would
  880. // catch changes in this area.
  881. EXPECT_TRUE(source.empty());
  882. }
  883. {
  884. Arena arena;
  885. RepeatedPtrField<string>* source =
  886. Arena::CreateMessage<RepeatedPtrField<string>>(&arena);
  887. *source->Add() = "1";
  888. *source->Add() = "2";
  889. RepeatedPtrField<string> destination = std::move(*source);
  890. EXPECT_EQ(nullptr, destination.GetArena());
  891. EXPECT_THAT(destination, ElementsAre("1", "2"));
  892. // This property isn't guaranteed but it's useful to have a test that would
  893. // catch changes in this area.
  894. EXPECT_THAT(*source, ElementsAre("1", "2"));
  895. }
  896. }
  897. TEST(RepeatedPtrField, MoveAssign) {
  898. {
  899. RepeatedPtrField<string> source;
  900. *source.Add() = "1";
  901. *source.Add() = "2";
  902. RepeatedPtrField<string> destination;
  903. *destination.Add() = "3";
  904. const string* const* source_data = source.data();
  905. const string* const* destination_data = destination.data();
  906. destination = std::move(source);
  907. EXPECT_EQ(source_data, destination.data());
  908. EXPECT_THAT(destination, ElementsAre("1", "2"));
  909. // This property isn't guaranteed but it's useful to have a test that would
  910. // catch changes in this area.
  911. EXPECT_EQ(destination_data, source.data());
  912. EXPECT_THAT(source, ElementsAre("3"));
  913. }
  914. {
  915. Arena arena;
  916. RepeatedPtrField<string>* source =
  917. Arena::CreateMessage<RepeatedPtrField<string>>(&arena);
  918. *source->Add() = "1";
  919. *source->Add() = "2";
  920. RepeatedPtrField<string>* destination =
  921. Arena::CreateMessage<RepeatedPtrField<string>>(&arena);
  922. *destination->Add() = "3";
  923. const string* const* source_data = source->data();
  924. const string* const* destination_data = destination->data();
  925. *destination = std::move(*source);
  926. EXPECT_EQ(source_data, destination->data());
  927. EXPECT_THAT(*destination, ElementsAre("1", "2"));
  928. // This property isn't guaranteed but it's useful to have a test that would
  929. // catch changes in this area.
  930. EXPECT_EQ(destination_data, source->data());
  931. EXPECT_THAT(*source, ElementsAre("3"));
  932. }
  933. {
  934. Arena source_arena;
  935. RepeatedPtrField<string>* source =
  936. Arena::CreateMessage<RepeatedPtrField<string>>(&source_arena);
  937. *source->Add() = "1";
  938. *source->Add() = "2";
  939. Arena destination_arena;
  940. RepeatedPtrField<string>* destination =
  941. Arena::CreateMessage<RepeatedPtrField<string>>(&destination_arena);
  942. *destination->Add() = "3";
  943. *destination = std::move(*source);
  944. EXPECT_THAT(*destination, ElementsAre("1", "2"));
  945. // This property isn't guaranteed but it's useful to have a test that would
  946. // catch changes in this area.
  947. EXPECT_THAT(*source, ElementsAre("1", "2"));
  948. }
  949. {
  950. Arena arena;
  951. RepeatedPtrField<string>* source =
  952. Arena::CreateMessage<RepeatedPtrField<string>>(&arena);
  953. *source->Add() = "1";
  954. *source->Add() = "2";
  955. RepeatedPtrField<string> destination;
  956. *destination.Add() = "3";
  957. destination = std::move(*source);
  958. EXPECT_THAT(destination, ElementsAre("1", "2"));
  959. // This property isn't guaranteed but it's useful to have a test that would
  960. // catch changes in this area.
  961. EXPECT_THAT(*source, ElementsAre("1", "2"));
  962. }
  963. {
  964. RepeatedPtrField<string> source;
  965. *source.Add() = "1";
  966. *source.Add() = "2";
  967. Arena arena;
  968. RepeatedPtrField<string>* destination =
  969. Arena::CreateMessage<RepeatedPtrField<string>>(&arena);
  970. *destination->Add() = "3";
  971. *destination = std::move(source);
  972. EXPECT_THAT(*destination, ElementsAre("1", "2"));
  973. // This property isn't guaranteed but it's useful to have a test that would
  974. // catch changes in this area.
  975. EXPECT_THAT(source, ElementsAre("1", "2"));
  976. }
  977. {
  978. RepeatedPtrField<string> field;
  979. // An alias to defeat -Wself-move.
  980. RepeatedPtrField<string>& alias = field;
  981. *field.Add() = "1";
  982. *field.Add() = "2";
  983. const string* const* data = field.data();
  984. field = std::move(alias);
  985. EXPECT_EQ(data, field.data());
  986. EXPECT_THAT(field, ElementsAre("1", "2"));
  987. }
  988. {
  989. Arena arena;
  990. RepeatedPtrField<string>* field =
  991. Arena::CreateMessage<RepeatedPtrField<string>>(&arena);
  992. *field->Add() = "1";
  993. *field->Add() = "2";
  994. const string* const* data = field->data();
  995. *field = std::move(*field);
  996. EXPECT_EQ(data, field->data());
  997. EXPECT_THAT(*field, ElementsAre("1", "2"));
  998. }
  999. }
  1000. TEST(RepeatedPtrField, MutableDataIsMutable) {
  1001. RepeatedPtrField<string> field;
  1002. *field.Add() = "1";
  1003. EXPECT_EQ("1", field.Get(0));
  1004. // The fact that this line compiles would be enough, but we'll check the
  1005. // value anyway.
  1006. string** data = field.mutable_data();
  1007. **data = "2";
  1008. EXPECT_EQ("2", field.Get(0));
  1009. }
  1010. TEST(RepeatedPtrField, SubscriptOperators) {
  1011. RepeatedPtrField<string> field;
  1012. *field.Add() = "1";
  1013. EXPECT_EQ("1", field.Get(0));
  1014. EXPECT_EQ("1", field[0]);
  1015. EXPECT_EQ(field.Mutable(0), &field[0]);
  1016. const RepeatedPtrField<string>& const_field = field;
  1017. EXPECT_EQ(*field.data(), &const_field[0]);
  1018. }
  1019. TEST(RepeatedPtrField, ExtractSubrange) {
  1020. // Exhaustively test every subrange in arrays of all sizes from 0 through 9
  1021. // with 0 through 3 cleared elements at the end.
  1022. for (int sz = 0; sz < 10; ++sz) {
  1023. for (int num = 0; num <= sz; ++num) {
  1024. for (int start = 0; start < sz - num; ++start) {
  1025. for (int extra = 0; extra < 4; ++extra) {
  1026. std::vector<string*> subject;
  1027. // Create an array with "sz" elements and "extra" cleared elements.
  1028. RepeatedPtrField<string> field;
  1029. for (int i = 0; i < sz + extra; ++i) {
  1030. subject.push_back(new string());
  1031. field.AddAllocated(subject[i]);
  1032. }
  1033. EXPECT_EQ(field.size(), sz + extra);
  1034. for (int i = 0; i < extra; ++i)
  1035. field.RemoveLast();
  1036. EXPECT_EQ(field.size(), sz);
  1037. EXPECT_EQ(field.ClearedCount(), extra);
  1038. // Create a catcher array and call ExtractSubrange.
  1039. string* catcher[10];
  1040. for (int i = 0; i < 10; ++i)
  1041. catcher[i] = NULL;
  1042. field.ExtractSubrange(start, num, catcher);
  1043. // Does the resulting array have the right size?
  1044. EXPECT_EQ(field.size(), sz - num);
  1045. // Were the removed elements extracted into the catcher array?
  1046. for (int i = 0; i < num; ++i)
  1047. EXPECT_EQ(catcher[i], subject[start + i]);
  1048. EXPECT_EQ(NULL, catcher[num]);
  1049. // Does the resulting array contain the right values?
  1050. for (int i = 0; i < start; ++i)
  1051. EXPECT_EQ(field.Mutable(i), subject[i]);
  1052. for (int i = start; i < field.size(); ++i)
  1053. EXPECT_EQ(field.Mutable(i), subject[i + num]);
  1054. // Reinstate the cleared elements.
  1055. EXPECT_EQ(field.ClearedCount(), extra);
  1056. for (int i = 0; i < extra; ++i)
  1057. field.Add();
  1058. EXPECT_EQ(field.ClearedCount(), 0);
  1059. EXPECT_EQ(field.size(), sz - num + extra);
  1060. // Make sure the extra elements are all there (in some order).
  1061. for (int i = sz; i < sz + extra; ++i) {
  1062. int count = 0;
  1063. for (int j = sz; j < sz + extra; ++j) {
  1064. if (field.Mutable(j - num) == subject[i])
  1065. count += 1;
  1066. }
  1067. EXPECT_EQ(count, 1);
  1068. }
  1069. // Release the caught elements.
  1070. for (int i = 0; i < num; ++i)
  1071. delete catcher[i];
  1072. }
  1073. }
  1074. }
  1075. }
  1076. }
  1077. TEST(RepeatedPtrField, DeleteSubrange) {
  1078. // DeleteSubrange is a trivial extension of ExtendSubrange.
  1079. }
  1080. // ===================================================================
  1081. // Iterator tests stolen from net/proto/proto-array_unittest.
  1082. class RepeatedFieldIteratorTest : public testing::Test {
  1083. protected:
  1084. virtual void SetUp() {
  1085. for (int i = 0; i < 3; ++i) {
  1086. proto_array_.Add(i);
  1087. }
  1088. }
  1089. RepeatedField<int> proto_array_;
  1090. };
  1091. TEST_F(RepeatedFieldIteratorTest, Convertible) {
  1092. RepeatedField<int>::iterator iter = proto_array_.begin();
  1093. RepeatedField<int>::const_iterator c_iter = iter;
  1094. RepeatedField<int>::value_type value = *c_iter;
  1095. EXPECT_EQ(0, value);
  1096. }
  1097. TEST_F(RepeatedFieldIteratorTest, MutableIteration) {
  1098. RepeatedField<int>::iterator iter = proto_array_.begin();
  1099. EXPECT_EQ(0, *iter);
  1100. ++iter;
  1101. EXPECT_EQ(1, *iter++);
  1102. EXPECT_EQ(2, *iter);
  1103. ++iter;
  1104. EXPECT_TRUE(proto_array_.end() == iter);
  1105. EXPECT_EQ(2, *(proto_array_.end() - 1));
  1106. }
  1107. TEST_F(RepeatedFieldIteratorTest, ConstIteration) {
  1108. const RepeatedField<int>& const_proto_array = proto_array_;
  1109. RepeatedField<int>::const_iterator iter = const_proto_array.begin();
  1110. EXPECT_EQ(0, *iter);
  1111. ++iter;
  1112. EXPECT_EQ(1, *iter++);
  1113. EXPECT_EQ(2, *iter);
  1114. ++iter;
  1115. EXPECT_TRUE(proto_array_.end() == iter);
  1116. EXPECT_EQ(2, *(proto_array_.end() - 1));
  1117. }
  1118. TEST_F(RepeatedFieldIteratorTest, Mutation) {
  1119. RepeatedField<int>::iterator iter = proto_array_.begin();
  1120. *iter = 7;
  1121. EXPECT_EQ(7, proto_array_.Get(0));
  1122. }
  1123. // -------------------------------------------------------------------
  1124. class RepeatedPtrFieldIteratorTest : public testing::Test {
  1125. protected:
  1126. virtual void SetUp() {
  1127. proto_array_.Add()->assign("foo");
  1128. proto_array_.Add()->assign("bar");
  1129. proto_array_.Add()->assign("baz");
  1130. }
  1131. RepeatedPtrField<string> proto_array_;
  1132. };
  1133. TEST_F(RepeatedPtrFieldIteratorTest, Convertible) {
  1134. RepeatedPtrField<string>::iterator iter = proto_array_.begin();
  1135. RepeatedPtrField<string>::const_iterator c_iter = iter;
  1136. RepeatedPtrField<string>::value_type value = *c_iter;
  1137. EXPECT_EQ("foo", value);
  1138. }
  1139. TEST_F(RepeatedPtrFieldIteratorTest, MutableIteration) {
  1140. RepeatedPtrField<string>::iterator iter = proto_array_.begin();
  1141. EXPECT_EQ("foo", *iter);
  1142. ++iter;
  1143. EXPECT_EQ("bar", *(iter++));
  1144. EXPECT_EQ("baz", *iter);
  1145. ++iter;
  1146. EXPECT_TRUE(proto_array_.end() == iter);
  1147. EXPECT_EQ("baz", *(--proto_array_.end()));
  1148. }
  1149. TEST_F(RepeatedPtrFieldIteratorTest, ConstIteration) {
  1150. const RepeatedPtrField<string>& const_proto_array = proto_array_;
  1151. RepeatedPtrField<string>::const_iterator iter = const_proto_array.begin();
  1152. EXPECT_EQ("foo", *iter);
  1153. ++iter;
  1154. EXPECT_EQ("bar", *(iter++));
  1155. EXPECT_EQ("baz", *iter);
  1156. ++iter;
  1157. EXPECT_TRUE(const_proto_array.end() == iter);
  1158. EXPECT_EQ("baz", *(--const_proto_array.end()));
  1159. }
  1160. TEST_F(RepeatedPtrFieldIteratorTest, MutableReverseIteration) {
  1161. RepeatedPtrField<string>::reverse_iterator iter = proto_array_.rbegin();
  1162. EXPECT_EQ("baz", *iter);
  1163. ++iter;
  1164. EXPECT_EQ("bar", *(iter++));
  1165. EXPECT_EQ("foo", *iter);
  1166. ++iter;
  1167. EXPECT_TRUE(proto_array_.rend() == iter);
  1168. EXPECT_EQ("foo", *(--proto_array_.rend()));
  1169. }
  1170. TEST_F(RepeatedPtrFieldIteratorTest, ConstReverseIteration) {
  1171. const RepeatedPtrField<string>& const_proto_array = proto_array_;
  1172. RepeatedPtrField<string>::const_reverse_iterator iter
  1173. = const_proto_array.rbegin();
  1174. EXPECT_EQ("baz", *iter);
  1175. ++iter;
  1176. EXPECT_EQ("bar", *(iter++));
  1177. EXPECT_EQ("foo", *iter);
  1178. ++iter;
  1179. EXPECT_TRUE(const_proto_array.rend() == iter);
  1180. EXPECT_EQ("foo", *(--const_proto_array.rend()));
  1181. }
  1182. TEST_F(RepeatedPtrFieldIteratorTest, RandomAccess) {
  1183. RepeatedPtrField<string>::iterator iter = proto_array_.begin();
  1184. RepeatedPtrField<string>::iterator iter2 = iter;
  1185. ++iter2;
  1186. ++iter2;
  1187. EXPECT_TRUE(iter + 2 == iter2);
  1188. EXPECT_TRUE(iter == iter2 - 2);
  1189. EXPECT_EQ("baz", iter[2]);
  1190. EXPECT_EQ("baz", *(iter + 2));
  1191. EXPECT_EQ(3, proto_array_.end() - proto_array_.begin());
  1192. }
  1193. TEST_F(RepeatedPtrFieldIteratorTest, Comparable) {
  1194. RepeatedPtrField<string>::const_iterator iter = proto_array_.begin();
  1195. RepeatedPtrField<string>::const_iterator iter2 = iter + 1;
  1196. EXPECT_TRUE(iter == iter);
  1197. EXPECT_TRUE(iter != iter2);
  1198. EXPECT_TRUE(iter < iter2);
  1199. EXPECT_TRUE(iter <= iter2);
  1200. EXPECT_TRUE(iter <= iter);
  1201. EXPECT_TRUE(iter2 > iter);
  1202. EXPECT_TRUE(iter2 >= iter);
  1203. EXPECT_TRUE(iter >= iter);
  1204. }
  1205. // Uninitialized iterator does not point to any of the RepeatedPtrField.
  1206. TEST_F(RepeatedPtrFieldIteratorTest, UninitializedIterator) {
  1207. RepeatedPtrField<string>::iterator iter;
  1208. EXPECT_TRUE(iter != proto_array_.begin());
  1209. EXPECT_TRUE(iter != proto_array_.begin() + 1);
  1210. EXPECT_TRUE(iter != proto_array_.begin() + 2);
  1211. EXPECT_TRUE(iter != proto_array_.begin() + 3);
  1212. EXPECT_TRUE(iter != proto_array_.end());
  1213. }
  1214. TEST_F(RepeatedPtrFieldIteratorTest, STLAlgorithms_lower_bound) {
  1215. proto_array_.Clear();
  1216. proto_array_.Add()->assign("a");
  1217. proto_array_.Add()->assign("c");
  1218. proto_array_.Add()->assign("d");
  1219. proto_array_.Add()->assign("n");
  1220. proto_array_.Add()->assign("p");
  1221. proto_array_.Add()->assign("x");
  1222. proto_array_.Add()->assign("y");
  1223. string v = "f";
  1224. RepeatedPtrField<string>::const_iterator it =
  1225. std::lower_bound(proto_array_.begin(), proto_array_.end(), v);
  1226. EXPECT_EQ(*it, "n");
  1227. EXPECT_TRUE(it == proto_array_.begin() + 3);
  1228. }
  1229. TEST_F(RepeatedPtrFieldIteratorTest, Mutation) {
  1230. RepeatedPtrField<string>::iterator iter = proto_array_.begin();
  1231. *iter = "qux";
  1232. EXPECT_EQ("qux", proto_array_.Get(0));
  1233. }
  1234. // -------------------------------------------------------------------
  1235. class RepeatedPtrFieldPtrsIteratorTest : public testing::Test {
  1236. protected:
  1237. virtual void SetUp() {
  1238. proto_array_.Add()->assign("foo");
  1239. proto_array_.Add()->assign("bar");
  1240. proto_array_.Add()->assign("baz");
  1241. const_proto_array_ = &proto_array_;
  1242. }
  1243. RepeatedPtrField<string> proto_array_;
  1244. const RepeatedPtrField<string>* const_proto_array_;
  1245. };
  1246. TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertiblePtr) {
  1247. RepeatedPtrField<string>::pointer_iterator iter =
  1248. proto_array_.pointer_begin();
  1249. static_cast<void>(iter);
  1250. }
  1251. TEST_F(RepeatedPtrFieldPtrsIteratorTest, ConvertibleConstPtr) {
  1252. RepeatedPtrField<string>::const_pointer_iterator iter =
  1253. const_proto_array_->pointer_begin();
  1254. static_cast<void>(iter);
  1255. }
  1256. TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutablePtrIteration) {
  1257. RepeatedPtrField<string>::pointer_iterator iter =
  1258. proto_array_.pointer_begin();
  1259. EXPECT_EQ("foo", **iter);
  1260. ++iter;
  1261. EXPECT_EQ("bar", **(iter++));
  1262. EXPECT_EQ("baz", **iter);
  1263. ++iter;
  1264. EXPECT_TRUE(proto_array_.pointer_end() == iter);
  1265. EXPECT_EQ("baz", **(--proto_array_.pointer_end()));
  1266. }
  1267. TEST_F(RepeatedPtrFieldPtrsIteratorTest, MutableConstPtrIteration) {
  1268. RepeatedPtrField<string>::const_pointer_iterator iter =
  1269. const_proto_array_->pointer_begin();
  1270. EXPECT_EQ("foo", **iter);
  1271. ++iter;
  1272. EXPECT_EQ("bar", **(iter++));
  1273. EXPECT_EQ("baz", **iter);
  1274. ++iter;
  1275. EXPECT_TRUE(const_proto_array_->pointer_end() == iter);
  1276. EXPECT_EQ("baz", **(--const_proto_array_->pointer_end()));
  1277. }
  1278. TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomPtrAccess) {
  1279. RepeatedPtrField<string>::pointer_iterator iter =
  1280. proto_array_.pointer_begin();
  1281. RepeatedPtrField<string>::pointer_iterator iter2 = iter;
  1282. ++iter2;
  1283. ++iter2;
  1284. EXPECT_TRUE(iter + 2 == iter2);
  1285. EXPECT_TRUE(iter == iter2 - 2);
  1286. EXPECT_EQ("baz", *iter[2]);
  1287. EXPECT_EQ("baz", **(iter + 2));
  1288. EXPECT_EQ(3, proto_array_.end() - proto_array_.begin());
  1289. }
  1290. TEST_F(RepeatedPtrFieldPtrsIteratorTest, RandomConstPtrAccess) {
  1291. RepeatedPtrField<string>::const_pointer_iterator iter =
  1292. const_proto_array_->pointer_begin();
  1293. RepeatedPtrField<string>::const_pointer_iterator iter2 = iter;
  1294. ++iter2;
  1295. ++iter2;
  1296. EXPECT_TRUE(iter + 2 == iter2);
  1297. EXPECT_TRUE(iter == iter2 - 2);
  1298. EXPECT_EQ("baz", *iter[2]);
  1299. EXPECT_EQ("baz", **(iter + 2));
  1300. EXPECT_EQ(3, const_proto_array_->end() - const_proto_array_->begin());
  1301. }
  1302. TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparablePtr) {
  1303. RepeatedPtrField<string>::pointer_iterator iter =
  1304. proto_array_.pointer_begin();
  1305. RepeatedPtrField<string>::pointer_iterator iter2 = iter + 1;
  1306. EXPECT_TRUE(iter == iter);
  1307. EXPECT_TRUE(iter != iter2);
  1308. EXPECT_TRUE(iter < iter2);
  1309. EXPECT_TRUE(iter <= iter2);
  1310. EXPECT_TRUE(iter <= iter);
  1311. EXPECT_TRUE(iter2 > iter);
  1312. EXPECT_TRUE(iter2 >= iter);
  1313. EXPECT_TRUE(iter >= iter);
  1314. }
  1315. TEST_F(RepeatedPtrFieldPtrsIteratorTest, ComparableConstPtr) {
  1316. RepeatedPtrField<string>::const_pointer_iterator iter =
  1317. const_proto_array_->pointer_begin();
  1318. RepeatedPtrField<string>::const_pointer_iterator iter2 = iter + 1;
  1319. EXPECT_TRUE(iter == iter);
  1320. EXPECT_TRUE(iter != iter2);
  1321. EXPECT_TRUE(iter < iter2);
  1322. EXPECT_TRUE(iter <= iter2);
  1323. EXPECT_TRUE(iter <= iter);
  1324. EXPECT_TRUE(iter2 > iter);
  1325. EXPECT_TRUE(iter2 >= iter);
  1326. EXPECT_TRUE(iter >= iter);
  1327. }
  1328. // Uninitialized iterator does not point to any of the RepeatedPtrOverPtrs.
  1329. // Dereferencing an uninitialized iterator crashes the process.
  1330. TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedPtrIterator) {
  1331. RepeatedPtrField<string>::pointer_iterator iter;
  1332. EXPECT_TRUE(iter != proto_array_.pointer_begin());
  1333. EXPECT_TRUE(iter != proto_array_.pointer_begin() + 1);
  1334. EXPECT_TRUE(iter != proto_array_.pointer_begin() + 2);
  1335. EXPECT_TRUE(iter != proto_array_.pointer_begin() + 3);
  1336. EXPECT_TRUE(iter != proto_array_.pointer_end());
  1337. }
  1338. TEST_F(RepeatedPtrFieldPtrsIteratorTest, UninitializedConstPtrIterator) {
  1339. RepeatedPtrField<string>::const_pointer_iterator iter;
  1340. EXPECT_TRUE(iter != const_proto_array_->pointer_begin());
  1341. EXPECT_TRUE(iter != const_proto_array_->pointer_begin() + 1);
  1342. EXPECT_TRUE(iter != const_proto_array_->pointer_begin() + 2);
  1343. EXPECT_TRUE(iter != const_proto_array_->pointer_begin() + 3);
  1344. EXPECT_TRUE(iter != const_proto_array_->pointer_end());
  1345. }
  1346. // This comparison functor is required by the tests for RepeatedPtrOverPtrs.
  1347. // They operate on strings and need to compare strings as strings in
  1348. // any stl algorithm, even though the iterator returns a pointer to a string
  1349. // - i.e. *iter has type string*.
  1350. struct StringLessThan {
  1351. bool operator()(const string* z, const string& y) {
  1352. return *z < y;
  1353. }
  1354. bool operator()(const string* z, const string* y) const { return *z < *y; }
  1355. };
  1356. TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrSTLAlgorithms_lower_bound) {
  1357. proto_array_.Clear();
  1358. proto_array_.Add()->assign("a");
  1359. proto_array_.Add()->assign("c");
  1360. proto_array_.Add()->assign("d");
  1361. proto_array_.Add()->assign("n");
  1362. proto_array_.Add()->assign("p");
  1363. proto_array_.Add()->assign("x");
  1364. proto_array_.Add()->assign("y");
  1365. {
  1366. string v = "f";
  1367. RepeatedPtrField<string>::pointer_iterator it =
  1368. std::lower_bound(proto_array_.pointer_begin(),
  1369. proto_array_.pointer_end(), &v, StringLessThan());
  1370. GOOGLE_CHECK(*it != NULL);
  1371. EXPECT_EQ(**it, "n");
  1372. EXPECT_TRUE(it == proto_array_.pointer_begin() + 3);
  1373. }
  1374. {
  1375. string v = "f";
  1376. RepeatedPtrField<string>::const_pointer_iterator it = std::lower_bound(
  1377. const_proto_array_->pointer_begin(), const_proto_array_->pointer_end(),
  1378. &v, StringLessThan());
  1379. GOOGLE_CHECK(*it != NULL);
  1380. EXPECT_EQ(**it, "n");
  1381. EXPECT_TRUE(it == const_proto_array_->pointer_begin() + 3);
  1382. }
  1383. }
  1384. TEST_F(RepeatedPtrFieldPtrsIteratorTest, PtrMutation) {
  1385. RepeatedPtrField<string>::pointer_iterator iter =
  1386. proto_array_.pointer_begin();
  1387. **iter = "qux";
  1388. EXPECT_EQ("qux", proto_array_.Get(0));
  1389. EXPECT_EQ("bar", proto_array_.Get(1));
  1390. EXPECT_EQ("baz", proto_array_.Get(2));
  1391. ++iter;
  1392. delete *iter;
  1393. *iter = new string("a");
  1394. ++iter;
  1395. delete *iter;
  1396. *iter = new string("b");
  1397. EXPECT_EQ("a", proto_array_.Get(1));
  1398. EXPECT_EQ("b", proto_array_.Get(2));
  1399. }
  1400. TEST_F(RepeatedPtrFieldPtrsIteratorTest, Sort) {
  1401. proto_array_.Add()->assign("c");
  1402. proto_array_.Add()->assign("d");
  1403. proto_array_.Add()->assign("n");
  1404. proto_array_.Add()->assign("p");
  1405. proto_array_.Add()->assign("a");
  1406. proto_array_.Add()->assign("y");
  1407. proto_array_.Add()->assign("x");
  1408. EXPECT_EQ("foo", proto_array_.Get(0));
  1409. EXPECT_EQ("n", proto_array_.Get(5));
  1410. EXPECT_EQ("x", proto_array_.Get(9));
  1411. std::sort(proto_array_.pointer_begin(), proto_array_.pointer_end(),
  1412. StringLessThan());
  1413. EXPECT_EQ("a", proto_array_.Get(0));
  1414. EXPECT_EQ("baz", proto_array_.Get(2));
  1415. EXPECT_EQ("y", proto_array_.Get(9));
  1416. }
  1417. // -----------------------------------------------------------------------------
  1418. // Unit-tests for the insert iterators
  1419. // google::protobuf::RepeatedFieldBackInserter,
  1420. // google::protobuf::AllocatedRepeatedPtrFieldBackInserter
  1421. // Ported from util/gtl/proto-array-iterators_unittest.
  1422. class RepeatedFieldInsertionIteratorsTest : public testing::Test {
  1423. protected:
  1424. std::list<double> halves;
  1425. std::list<int> fibonacci;
  1426. std::vector<string> words;
  1427. typedef TestAllTypes::NestedMessage Nested;
  1428. Nested nesteds[2];
  1429. std::vector<Nested*> nested_ptrs;
  1430. TestAllTypes protobuffer;
  1431. virtual void SetUp() {
  1432. fibonacci.push_back(1);
  1433. fibonacci.push_back(1);
  1434. fibonacci.push_back(2);
  1435. fibonacci.push_back(3);
  1436. fibonacci.push_back(5);
  1437. fibonacci.push_back(8);
  1438. std::copy(fibonacci.begin(), fibonacci.end(),
  1439. RepeatedFieldBackInserter(protobuffer.mutable_repeated_int32()));
  1440. halves.push_back(1.0);
  1441. halves.push_back(0.5);
  1442. halves.push_back(0.25);
  1443. halves.push_back(0.125);
  1444. halves.push_back(0.0625);
  1445. std::copy(halves.begin(), halves.end(),
  1446. RepeatedFieldBackInserter(protobuffer.mutable_repeated_double()));
  1447. words.push_back("Able");
  1448. words.push_back("was");
  1449. words.push_back("I");
  1450. words.push_back("ere");
  1451. words.push_back("I");
  1452. words.push_back("saw");
  1453. words.push_back("Elba");
  1454. std::copy(words.begin(), words.end(),
  1455. RepeatedFieldBackInserter(protobuffer.mutable_repeated_string()));
  1456. nesteds[0].set_bb(17);
  1457. nesteds[1].set_bb(4711);
  1458. std::copy(&nesteds[0], &nesteds[2],
  1459. RepeatedFieldBackInserter(
  1460. protobuffer.mutable_repeated_nested_message()));
  1461. nested_ptrs.push_back(new Nested);
  1462. nested_ptrs.back()->set_bb(170);
  1463. nested_ptrs.push_back(new Nested);
  1464. nested_ptrs.back()->set_bb(47110);
  1465. std::copy(nested_ptrs.begin(), nested_ptrs.end(),
  1466. RepeatedFieldBackInserter(
  1467. protobuffer.mutable_repeated_nested_message()));
  1468. }
  1469. virtual void TearDown() {
  1470. STLDeleteContainerPointers(nested_ptrs.begin(), nested_ptrs.end());
  1471. }
  1472. };
  1473. TEST_F(RepeatedFieldInsertionIteratorsTest, Fibonacci) {
  1474. EXPECT_TRUE(std::equal(fibonacci.begin(),
  1475. fibonacci.end(),
  1476. protobuffer.repeated_int32().begin()));
  1477. EXPECT_TRUE(std::equal(protobuffer.repeated_int32().begin(),
  1478. protobuffer.repeated_int32().end(),
  1479. fibonacci.begin()));
  1480. }
  1481. TEST_F(RepeatedFieldInsertionIteratorsTest, Halves) {
  1482. EXPECT_TRUE(std::equal(halves.begin(),
  1483. halves.end(),
  1484. protobuffer.repeated_double().begin()));
  1485. EXPECT_TRUE(std::equal(protobuffer.repeated_double().begin(),
  1486. protobuffer.repeated_double().end(),
  1487. halves.begin()));
  1488. }
  1489. TEST_F(RepeatedFieldInsertionIteratorsTest, Words) {
  1490. ASSERT_EQ(words.size(), protobuffer.repeated_string_size());
  1491. for (int i = 0; i < words.size(); ++i)
  1492. EXPECT_EQ(words.at(i), protobuffer.repeated_string(i));
  1493. }
  1494. TEST_F(RepeatedFieldInsertionIteratorsTest, Words2) {
  1495. words.clear();
  1496. words.push_back("sing");
  1497. words.push_back("a");
  1498. words.push_back("song");
  1499. words.push_back("of");
  1500. words.push_back("six");
  1501. words.push_back("pence");
  1502. protobuffer.mutable_repeated_string()->Clear();
  1503. std::copy(words.begin(), words.end(), RepeatedPtrFieldBackInserter(
  1504. protobuffer.mutable_repeated_string()));
  1505. ASSERT_EQ(words.size(), protobuffer.repeated_string_size());
  1506. for (int i = 0; i < words.size(); ++i)
  1507. EXPECT_EQ(words.at(i), protobuffer.repeated_string(i));
  1508. }
  1509. TEST_F(RepeatedFieldInsertionIteratorsTest, Nesteds) {
  1510. ASSERT_EQ(protobuffer.repeated_nested_message_size(), 4);
  1511. EXPECT_EQ(protobuffer.repeated_nested_message(0).bb(), 17);
  1512. EXPECT_EQ(protobuffer.repeated_nested_message(1).bb(), 4711);
  1513. EXPECT_EQ(protobuffer.repeated_nested_message(2).bb(), 170);
  1514. EXPECT_EQ(protobuffer.repeated_nested_message(3).bb(), 47110);
  1515. }
  1516. TEST_F(RepeatedFieldInsertionIteratorsTest,
  1517. AllocatedRepeatedPtrFieldWithStringIntData) {
  1518. std::vector<Nested*> data;
  1519. TestAllTypes goldenproto;
  1520. for (int i = 0; i < 10; ++i) {
  1521. Nested* new_data = new Nested;
  1522. new_data->set_bb(i);
  1523. data.push_back(new_data);
  1524. new_data = goldenproto.add_repeated_nested_message();
  1525. new_data->set_bb(i);
  1526. }
  1527. TestAllTypes testproto;
  1528. std::copy(data.begin(), data.end(),
  1529. AllocatedRepeatedPtrFieldBackInserter(
  1530. testproto.mutable_repeated_nested_message()));
  1531. EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
  1532. }
  1533. TEST_F(RepeatedFieldInsertionIteratorsTest,
  1534. AllocatedRepeatedPtrFieldWithString) {
  1535. std::vector<string*> data;
  1536. TestAllTypes goldenproto;
  1537. for (int i = 0; i < 10; ++i) {
  1538. string* new_data = new string;
  1539. *new_data = "name-" + SimpleItoa(i);
  1540. data.push_back(new_data);
  1541. new_data = goldenproto.add_repeated_string();
  1542. *new_data = "name-" + SimpleItoa(i);
  1543. }
  1544. TestAllTypes testproto;
  1545. std::copy(data.begin(), data.end(), AllocatedRepeatedPtrFieldBackInserter(
  1546. testproto.mutable_repeated_string()));
  1547. EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
  1548. }
  1549. TEST_F(RepeatedFieldInsertionIteratorsTest,
  1550. UnsafeArenaAllocatedRepeatedPtrFieldWithStringIntData) {
  1551. std::vector<Nested*> data;
  1552. TestAllTypes goldenproto;
  1553. for (int i = 0; i < 10; ++i) {
  1554. Nested* new_data = new Nested;
  1555. new_data->set_bb(i);
  1556. data.push_back(new_data);
  1557. new_data = goldenproto.add_repeated_nested_message();
  1558. new_data->set_bb(i);
  1559. }
  1560. TestAllTypes testproto;
  1561. std::copy(data.begin(), data.end(),
  1562. UnsafeArenaAllocatedRepeatedPtrFieldBackInserter(
  1563. testproto.mutable_repeated_nested_message()));
  1564. EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
  1565. }
  1566. TEST_F(RepeatedFieldInsertionIteratorsTest,
  1567. UnsafeArenaAllocatedRepeatedPtrFieldWithString) {
  1568. std::vector<string*> data;
  1569. TestAllTypes goldenproto;
  1570. for (int i = 0; i < 10; ++i) {
  1571. string* new_data = new string;
  1572. *new_data = "name-" + SimpleItoa(i);
  1573. data.push_back(new_data);
  1574. new_data = goldenproto.add_repeated_string();
  1575. *new_data = "name-" + SimpleItoa(i);
  1576. }
  1577. TestAllTypes testproto;
  1578. std::copy(data.begin(), data.end(),
  1579. UnsafeArenaAllocatedRepeatedPtrFieldBackInserter(
  1580. testproto.mutable_repeated_string()));
  1581. EXPECT_EQ(testproto.DebugString(), goldenproto.DebugString());
  1582. }
  1583. TEST_F(RepeatedFieldInsertionIteratorsTest, MoveStrings) {
  1584. std::vector<string> src = {"a", "b", "c", "d"};
  1585. std::vector<string> copy = src; // copy since move leaves in undefined state
  1586. TestAllTypes testproto;
  1587. std::move(copy.begin(), copy.end(),
  1588. RepeatedFieldBackInserter(testproto.mutable_repeated_string()));
  1589. ASSERT_THAT(testproto.repeated_string(), testing::ElementsAreArray(src));
  1590. }
  1591. TEST_F(RepeatedFieldInsertionIteratorsTest, MoveProtos) {
  1592. auto make_nested = [](int32 x) {
  1593. Nested ret;
  1594. ret.set_bb(x);
  1595. return ret;
  1596. };
  1597. std::vector<Nested> src = {make_nested(3), make_nested(5), make_nested(7)};
  1598. std::vector<Nested> copy = src; // copy since move leaves in undefined state
  1599. TestAllTypes testproto;
  1600. std::move(
  1601. copy.begin(), copy.end(),
  1602. RepeatedFieldBackInserter(testproto.mutable_repeated_nested_message()));
  1603. ASSERT_EQ(src.size(), testproto.repeated_nested_message_size());
  1604. for (int i = 0; i < src.size(); ++i) {
  1605. EXPECT_EQ(src[i].DebugString(),
  1606. testproto.repeated_nested_message(i).DebugString());
  1607. }
  1608. }
  1609. } // namespace
  1610. } // namespace protobuf
  1611. } // namespace google