map_test.cc 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330
  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. // A hack to include windows.h first, which ensures the GetMessage macro can
  31. // be undefined when we include <google/protobuf/stubs/common.h>
  32. #if defined(_WIN32)
  33. #define _WINSOCKAPI_ // to avoid re-definition in WinSock2.h
  34. #define NOMINMAX // to avoid defining min/max macros
  35. #include <windows.h>
  36. #endif // _WIN32
  37. #include <algorithm>
  38. #include <google/protobuf/stubs/hash.h>
  39. #include <map>
  40. #include <memory>
  41. #include <set>
  42. #include <sstream>
  43. #include <vector>
  44. #include <google/protobuf/stubs/casts.h>
  45. #include <google/protobuf/stubs/logging.h>
  46. #include <google/protobuf/stubs/common.h>
  47. #include <google/protobuf/stubs/stringprintf.h>
  48. #include <google/protobuf/testing/file.h>
  49. #include <google/protobuf/arena_test_util.h>
  50. #include <google/protobuf/map_proto2_unittest.pb.h>
  51. #include <google/protobuf/map_test_util.h>
  52. #include <google/protobuf/map_unittest.pb.h>
  53. #include <google/protobuf/test_util.h>
  54. #include <google/protobuf/unittest.pb.h>
  55. #include <google/protobuf/io/coded_stream.h>
  56. #include <google/protobuf/io/tokenizer.h>
  57. #include <google/protobuf/io/zero_copy_stream_impl.h>
  58. #include <google/protobuf/descriptor.pb.h>
  59. #include <google/protobuf/descriptor.h>
  60. #include <google/protobuf/descriptor_database.h>
  61. #include <google/protobuf/dynamic_message.h>
  62. #include <google/protobuf/map.h>
  63. #include <google/protobuf/map_field_inl.h>
  64. #include <google/protobuf/message.h>
  65. #include <google/protobuf/reflection.h>
  66. #include <google/protobuf/reflection_ops.h>
  67. #include <google/protobuf/text_format.h>
  68. #include <google/protobuf/wire_format.h>
  69. #include <google/protobuf/wire_format_lite_inl.h>
  70. #include <google/protobuf/util/message_differencer.h>
  71. #include <google/protobuf/util/time_util.h>
  72. #include <google/protobuf/stubs/substitute.h>
  73. #include <gmock/gmock.h>
  74. #include <google/protobuf/testing/googletest.h>
  75. #include <gtest/gtest.h>
  76. namespace google {
  77. using google::protobuf::unittest::ForeignMessage;
  78. using google::protobuf::unittest::TestAllTypes;
  79. using google::protobuf::unittest::TestMap;
  80. using google::protobuf::unittest::TestRecursiveMapMessage;
  81. namespace protobuf {
  82. namespace internal {
  83. void MapTestForceDeterministic() {
  84. ::google::protobuf::io::CodedOutputStream::SetDefaultSerializationDeterministic();
  85. }
  86. // Map API Test =====================================================
  87. class MapImplTest : public ::testing::Test {
  88. protected:
  89. MapImplTest()
  90. : map_ptr_(new Map<int32, int32>()),
  91. map_(*map_ptr_),
  92. const_map_(*map_ptr_) {
  93. EXPECT_TRUE(map_.empty());
  94. EXPECT_EQ(0, map_.size());
  95. }
  96. void ExpectSingleElement(int32 key, int32 value) {
  97. EXPECT_FALSE(map_.empty());
  98. EXPECT_EQ(1, map_.size());
  99. ExpectElement(key, value);
  100. }
  101. void ExpectElements(const std::map<int32, int32>& map) {
  102. EXPECT_FALSE(map_.empty());
  103. EXPECT_EQ(map.size(), map_.size());
  104. for (std::map<int32, int32>::const_iterator it = map.begin();
  105. it != map.end(); ++it) {
  106. ExpectElement(it->first, it->second);
  107. }
  108. }
  109. void ExpectElement(int32 key, int32 value) {
  110. // Test map size is correct.
  111. EXPECT_EQ(value, map_[key]);
  112. EXPECT_EQ(1, map_.count(key));
  113. // Check mutable at and find work correctly.
  114. EXPECT_EQ(value, map_.at(key));
  115. Map<int32, int32>::iterator it = map_.find(key);
  116. // iterator dereferenceable
  117. EXPECT_EQ(key, (*it).first);
  118. EXPECT_EQ(value, (*it).second);
  119. EXPECT_EQ(key, it->first);
  120. EXPECT_EQ(value, it->second);
  121. // iterator mutable
  122. ((*it).second) = value + 1;
  123. EXPECT_EQ(value + 1, map_[key]);
  124. ((*it).second) = value;
  125. EXPECT_EQ(value, map_[key]);
  126. it->second = value + 1;
  127. EXPECT_EQ(value + 1, map_[key]);
  128. it->second = value;
  129. EXPECT_EQ(value, map_[key]);
  130. // copy constructor
  131. Map<int32, int32>::iterator it_copy = it;
  132. EXPECT_EQ(key, it_copy->first);
  133. EXPECT_EQ(value, it_copy->second);
  134. // Immutable API ================================================
  135. // Check immutable at and find work correctly.
  136. EXPECT_EQ(value, const_map_.at(key));
  137. Map<int32, int32>::const_iterator const_it = const_map_.find(key);
  138. // iterator dereferenceable
  139. EXPECT_EQ(key, (*const_it).first);
  140. EXPECT_EQ(value, (*const_it).second);
  141. EXPECT_EQ(key, const_it->first);
  142. EXPECT_EQ(value, const_it->second);
  143. // copy constructor
  144. Map<int32, int32>::const_iterator const_it_copy = const_it;
  145. EXPECT_EQ(key, const_it_copy->first);
  146. EXPECT_EQ(value, const_it_copy->second);
  147. }
  148. std::unique_ptr<Map<int32, int32> > map_ptr_;
  149. Map<int32, int32>& map_;
  150. const Map<int32, int32>& const_map_;
  151. };
  152. TEST_F(MapImplTest, OperatorBracket) {
  153. int32 key = 0;
  154. int32 value1 = 100;
  155. int32 value2 = 101;
  156. EXPECT_EQ(0, map_[key]);
  157. map_[key] = value1;
  158. ExpectSingleElement(key, value1);
  159. map_[key] = value2;
  160. ExpectSingleElement(key, value2);
  161. }
  162. TEST_F(MapImplTest, OperatorBracketNonExist) {
  163. int32 key = 0;
  164. int32 default_value = 0;
  165. EXPECT_EQ(default_value, map_[key]);
  166. ExpectSingleElement(key, default_value);
  167. }
  168. TEST_F(MapImplTest, MutableAt) {
  169. int32 key = 0;
  170. int32 value1 = 100;
  171. int32 value2 = 101;
  172. map_[key] = value1;
  173. ExpectSingleElement(key, value1);
  174. map_.at(key) = value2;
  175. ExpectSingleElement(key, value2);
  176. }
  177. #ifdef PROTOBUF_HAS_DEATH_TEST
  178. TEST_F(MapImplTest, MutableAtNonExistDeathTest) {
  179. EXPECT_DEATH(map_.at(0), "");
  180. }
  181. TEST_F(MapImplTest, ImmutableAtNonExistDeathTest) {
  182. EXPECT_DEATH(const_map_.at(0), "");
  183. }
  184. TEST_F(MapImplTest, UsageErrors) {
  185. MapKey key;
  186. key.SetInt64Value(1);
  187. EXPECT_DEATH(key.GetUInt64Value(),
  188. "Protocol Buffer map usage error:\n"
  189. "MapKey::GetUInt64Value type does not match\n"
  190. " Expected : uint64\n"
  191. " Actual : int64");
  192. MapValueRef value;
  193. EXPECT_DEATH(value.SetFloatValue(0.1),
  194. "Protocol Buffer map usage error:\n"
  195. "MapValueRef::type MapValueRef is not initialized.");
  196. }
  197. #endif // PROTOBUF_HAS_DEATH_TEST
  198. TEST_F(MapImplTest, MapKeyAssignment) {
  199. MapKey from, to;
  200. from.SetStringValue("abc");
  201. to = from;
  202. EXPECT_EQ("abc", to.GetStringValue());
  203. }
  204. TEST_F(MapImplTest, CountNonExist) {
  205. EXPECT_EQ(0, map_.count(0));
  206. }
  207. TEST_F(MapImplTest, MutableFindNonExist) {
  208. EXPECT_TRUE(map_.end() == map_.find(0));
  209. }
  210. TEST_F(MapImplTest, ImmutableFindNonExist) {
  211. EXPECT_TRUE(const_map_.end() == const_map_.find(0));
  212. }
  213. TEST_F(MapImplTest, ConstEnd) {
  214. EXPECT_TRUE(const_map_.end() == const_map_.cend());
  215. }
  216. TEST_F(MapImplTest, GetReferenceFromIterator) {
  217. for (int i = 0; i < 10; i++) {
  218. map_[i] = i;
  219. }
  220. for (Map<int32, int32>::const_iterator it = map_.cbegin();
  221. it != map_.cend();) {
  222. Map<int32, int32>::const_reference entry = *it++;
  223. EXPECT_EQ(entry.first, entry.second);
  224. }
  225. for (Map<int32, int32>::const_iterator it = const_map_.begin();
  226. it != const_map_.end();) {
  227. Map<int32, int32>::const_reference entry = *it++;
  228. EXPECT_EQ(entry.first, entry.second);
  229. }
  230. for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) {
  231. Map<int32, int32>::reference entry = *it++;
  232. EXPECT_EQ(entry.first + 1, ++entry.second);
  233. }
  234. }
  235. TEST_F(MapImplTest, IteratorBasic) {
  236. map_[0] = 0;
  237. // Default constructible (per forward iterator requirements).
  238. Map<int, int>::const_iterator cit;
  239. Map<int, int>::iterator it;
  240. it = map_.begin();
  241. cit = it; // Converts to const_iterator
  242. // Can compare between them.
  243. EXPECT_TRUE(it == cit);
  244. EXPECT_FALSE(cit != it);
  245. // Pre increment.
  246. EXPECT_FALSE(it == ++cit);
  247. // Post increment.
  248. EXPECT_FALSE(it++ == cit);
  249. EXPECT_TRUE(it == cit);
  250. }
  251. template <typename Iterator>
  252. static int64 median(Iterator i0, Iterator i1) {
  253. std::vector<int64> v(i0, i1);
  254. std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end());
  255. return v[v.size() / 2];
  256. }
  257. static int64 Now() {
  258. return google::protobuf::util::TimeUtil::TimestampToNanoseconds(
  259. google::protobuf::util::TimeUtil::GetCurrentTime());
  260. }
  261. // Arbitrary odd integers for creating test data.
  262. static int k0 = 812398771;
  263. static int k1 = 1312938717;
  264. static int k2 = 1321555333;
  265. // A naive begin() implementation will cause begin() to get slower and slower
  266. // if one erases elements at the "front" of the hash map, and we'd like to
  267. // avoid that, as std::unordered_map does.
  268. TEST_F(MapImplTest, BeginIsFast) {
  269. if (true) return; // TODO(gpike): make this less flaky and re-enable it.
  270. Map<int32, int32> map;
  271. const int kTestSize = 250000;
  272. // Create a random-looking map of size n. Use non-negative integer keys.
  273. uint32 frog = 123983;
  274. int last_key = 0;
  275. int counter = 0;
  276. while (map.size() < kTestSize) {
  277. frog *= static_cast<uint32>(k0);
  278. frog ^= frog >> 17;
  279. frog += counter++;
  280. last_key =
  281. static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1;
  282. GOOGLE_DCHECK_GE(last_key, 0);
  283. map[last_key] = last_key ^ 1;
  284. }
  285. std::vector<int64> times;
  286. // We're going to do map.erase(map.begin()) over and over again. But,
  287. // just in case one iteration is fast compared to the granularity of
  288. // our time keeping, we measure kChunkSize iterations per outer-loop iter.
  289. const int kChunkSize = 1000;
  290. GOOGLE_CHECK_EQ(kTestSize % kChunkSize, 0);
  291. do {
  292. const int64 start = Now();
  293. for (int i = 0; i < kChunkSize; i++) {
  294. map.erase(map.begin());
  295. }
  296. const int64 end = Now();
  297. if (end > start) {
  298. times.push_back(end - start);
  299. }
  300. } while (!map.empty());
  301. if (times.size() < .99 * kTestSize / kChunkSize) {
  302. GOOGLE_LOG(WARNING) << "Now() isn't helping us measure time";
  303. return;
  304. }
  305. int64 x0 = median(times.begin(), times.begin() + 9);
  306. int64 x1 = median(times.begin() + times.size() - 9, times.end());
  307. GOOGLE_LOG(INFO) << "x0=" << x0 << ", x1=" << x1;
  308. // x1 will greatly exceed x0 if the code we just executed took O(n^2) time.
  309. // And we'll probably time out and never get here. So, this test is
  310. // intentionally loose: we check that x0 and x1 are within a factor of 8.
  311. EXPECT_GE(x1, x0 / 8);
  312. EXPECT_GE(x0, x1 / 8);
  313. }
  314. // Try to create kTestSize keys that will land in just a few buckets, and
  315. // time the insertions, to get a rough estimate of whether an O(n^2) worst case
  316. // was triggered. This test is a hacky, but probably better than nothing.
  317. TEST_F(MapImplTest, HashFlood) {
  318. const int kTestSize = 1024; // must be a power of 2
  319. std::set<int> s;
  320. for (int i = 0; s.size() < kTestSize; i++) {
  321. if ((map_.hash_function()(i) & (kTestSize - 1)) < 3) {
  322. s.insert(i);
  323. }
  324. }
  325. // Create hash table with kTestSize entries that hash flood a table with
  326. // 1024 (or 512 or 2048 or ...) entries. This assumes that map_ uses powers
  327. // of 2 for table sizes, and that it's sufficient to "flood" with respect to
  328. // the low bits of the output of map_.hash_function().
  329. std::vector<int64> times;
  330. std::set<int>::iterator it = s.begin();
  331. int count = 0;
  332. do {
  333. const int64 start = Now();
  334. map_[*it] = 0;
  335. const int64 end = Now();
  336. if (end > start) {
  337. times.push_back(end - start);
  338. }
  339. ++count;
  340. ++it;
  341. } while (it != s.end());
  342. if (times.size() < .99 * count) return;
  343. int64 x0 = median(times.begin(), times.begin() + 9);
  344. int64 x1 = median(times.begin() + times.size() - 9, times.end());
  345. // x1 will greatly exceed x0 if the code we just executed took O(n^2) time.
  346. // But we want to allow O(n log n). A factor of 20 should be generous enough.
  347. EXPECT_LE(x1, x0 * 20);
  348. }
  349. TEST_F(MapImplTest, CopyIteratorStressTest) {
  350. std::vector<Map<int32, int32>::iterator> v;
  351. const int kIters = 1e5;
  352. for (uint32 i = 0; i < kIters; i++) {
  353. int32 key = (3 + i * (5 + i * (-8 + i * (62 + i)))) & 0x77777777;
  354. map_[key] = i;
  355. v.push_back(map_.find(key));
  356. }
  357. for (std::vector<Map<int32, int32>::iterator>::const_iterator it = v.begin();
  358. it != v.end(); it++) {
  359. Map<int32, int32>::iterator i = *it;
  360. ASSERT_EQ(i->first, (*it)->first);
  361. ASSERT_EQ(i->second, (*it)->second);
  362. }
  363. }
  364. template <typename T, typename U>
  365. static void TestValidityForAllKeysExcept(int key_to_avoid,
  366. const T& check_map,
  367. const U& map) {
  368. typedef typename U::value_type value_type; // a key-value pair
  369. for (typename U::const_iterator it = map.begin(); it != map.end(); ++it) {
  370. const int key = it->first;
  371. if (key == key_to_avoid) continue;
  372. // All iterators relevant to this key, whether old (from check_map) or new,
  373. // must point to the same memory. So, test pointer equality here.
  374. const value_type* check_val = &*check_map.find(key)->second;
  375. EXPECT_EQ(check_val, &*it);
  376. EXPECT_EQ(check_val, &*map.find(key));
  377. }
  378. }
  379. // EXPECT i0 and i1 to be the same. Advancing them should have the same effect,
  380. // too.
  381. template <typename Iter>
  382. static void TestEqualIterators(Iter i0, Iter i1, Iter end) {
  383. const int kMaxAdvance = 10;
  384. for (int i = 0; i < kMaxAdvance; i++) {
  385. EXPECT_EQ(i0 == end, i1 == end);
  386. if (i0 == end) return;
  387. EXPECT_EQ(&*i0, &*i1) << "iter " << i;
  388. ++i0;
  389. ++i1;
  390. }
  391. }
  392. template <typename IteratorType>
  393. static void TestOldVersusNewIterator(int skip, Map<int, int>* m) {
  394. const int initial_size = m->size();
  395. IteratorType it = m->begin();
  396. for (int i = 0; i < skip && it != m->end(); it++, i++) {}
  397. if (it == m->end()) return;
  398. const IteratorType old = it;
  399. GOOGLE_LOG(INFO) << "skip=" << skip << ", old->first=" << old->first;
  400. const int target_size =
  401. initial_size < 100 ? initial_size * 5 : initial_size * 5 / 4;
  402. for (int i = 0; m->size() <= target_size; i++) {
  403. (*m)[i] = 0;
  404. }
  405. // Iterator 'old' should still work just fine despite the growth of *m.
  406. const IteratorType after_growth = m->find(old->first);
  407. TestEqualIterators<IteratorType>(old, after_growth, m->end());
  408. // Now shrink the number of elements. Do this with a mix of erases and
  409. // inserts to increase the chance that the hashtable will resize to a lower
  410. // number of buckets. (But, in any case, the test is still useful.)
  411. for (int i = 0; i < 2 * (target_size - initial_size); i++) {
  412. if (i != old->first) {
  413. m->erase(i);
  414. }
  415. if (((i ^ m->begin()->first) & 15) == 0) {
  416. (*m)[i * 342] = i;
  417. }
  418. }
  419. // Now, the table has grown and shrunk; test again.
  420. TestEqualIterators<IteratorType>(old, m->find(old->first), m->end());
  421. TestEqualIterators<IteratorType>(old, after_growth, m->end());
  422. }
  423. // Create and test an n-element Map, with emphasis on iterator correctness.
  424. static void StressTestIterators(int n) {
  425. GOOGLE_LOG(INFO) << "StressTestIterators " << n;
  426. GOOGLE_CHECK_GT(n, 0);
  427. // Create a random-looking map of size n. Use non-negative integer keys.
  428. Map<int, int> m;
  429. uint32 frog = 123987 + n;
  430. int last_key = 0;
  431. int counter = 0;
  432. while (m.size() < n) {
  433. frog *= static_cast<uint32>(k0);
  434. frog ^= frog >> 17;
  435. frog += counter++;
  436. last_key =
  437. static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1;
  438. GOOGLE_DCHECK_GE(last_key, 0);
  439. m[last_key] = last_key ^ 1;
  440. }
  441. // Test it.
  442. ASSERT_EQ(n, m.size());
  443. // Create maps of pointers and iterators.
  444. // These should remain valid even if we modify m.
  445. hash_map<int, Map<int, int>::value_type*> mp(n);
  446. hash_map<int, Map<int, int>::iterator> mi(n);
  447. for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
  448. mp[it->first] = &*it;
  449. mi[it->first] = it;
  450. }
  451. ASSERT_EQ(m.size(), mi.size());
  452. ASSERT_EQ(m.size(), mp.size());
  453. m.erase(last_key);
  454. ASSERT_EQ(n - 1, m.size());
  455. TestValidityForAllKeysExcept(last_key, mp, m);
  456. TestValidityForAllKeysExcept(last_key, mi, m);
  457. m[last_key] = 0;
  458. ASSERT_EQ(n, m.size());
  459. // Test old iterator vs new iterator, with table modification in between.
  460. TestOldVersusNewIterator<Map<int, int>::const_iterator>(n % 3, &m);
  461. TestOldVersusNewIterator<Map<int, int>::iterator>(n % (1 + (n / 40)), &m);
  462. // Finally, ensure erase(iterator) doesn't reorder anything, because that is
  463. // what its documentation says.
  464. m[last_key] = m[last_key ^ 999] = 0;
  465. std::vector<Map<int, int>::iterator> v;
  466. v.reserve(m.size());
  467. int position_of_last_key = 0;
  468. for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
  469. if (it->first == last_key) {
  470. position_of_last_key = v.size();
  471. }
  472. v.push_back(it);
  473. }
  474. ASSERT_EQ(m.size(), v.size());
  475. const Map<int, int>::iterator erase_result = m.erase(m.find(last_key));
  476. int index = 0;
  477. for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it, ++index) {
  478. if (index == position_of_last_key) {
  479. EXPECT_EQ(&*erase_result, &*v[++index]);
  480. }
  481. ASSERT_EQ(&*it, &*v[index]);
  482. }
  483. }
  484. TEST_F(MapImplTest, IteratorInvalidation) {
  485. // Create a set of pseudo-random sizes to test.
  486. #ifndef NDEBUG
  487. const int kMaxSizeToTest = 100 * 1000;
  488. #else
  489. const int kMaxSizeToTest = 1000 * 1000;
  490. #endif
  491. std::set<int> s;
  492. int n = kMaxSizeToTest;
  493. unsigned int frog = k1 + n;
  494. while (n > 1 && s.size() < 25) {
  495. s.insert(n);
  496. n = static_cast<int>(n * 100 / (101.0 + (frog & 63)));
  497. frog *= k2;
  498. frog ^= frog >> 17;
  499. }
  500. // Ensure we test a few small sizes.
  501. s.insert(1);
  502. s.insert(2);
  503. s.insert(3);
  504. // Now, the real work.
  505. for (std::set<int>::iterator i = s.begin(); i != s.end(); ++i) {
  506. StressTestIterators(*i);
  507. }
  508. }
  509. // Test that erase() revalidates iterators.
  510. TEST_F(MapImplTest, EraseRevalidates) {
  511. map_[3] = map_[13] = map_[20] = 0;
  512. const int initial_size = map_.size();
  513. EXPECT_EQ(3, initial_size);
  514. std::vector<Map<int, int>::iterator> v;
  515. for (Map<int, int>::iterator it = map_.begin(); it != map_.end(); ++it) {
  516. v.push_back(it);
  517. }
  518. EXPECT_EQ(initial_size, v.size());
  519. for (int i = 0; map_.size() <= initial_size * 20; i++) {
  520. map_[i] = 0;
  521. }
  522. const int larger_size = map_.size();
  523. // We've greatly increased the size of the map, so it is highly likely that
  524. // the following will corrupt m if erase() doesn't properly revalidate
  525. // iterators passed to it. Finishing this routine without crashing indicates
  526. // success.
  527. for (int i = 0; i < v.size(); i++) {
  528. map_.erase(v[i]);
  529. }
  530. EXPECT_EQ(larger_size - v.size(), map_.size());
  531. }
  532. template <typename T>
  533. bool IsConstHelper(T& /*t*/) { // NOLINT. We want to catch non-const refs here.
  534. return false;
  535. }
  536. template <typename T>
  537. bool IsConstHelper(const T& /*t*/) {
  538. return true;
  539. }
  540. TEST_F(MapImplTest, IteratorConstness) {
  541. map_[0] = 0;
  542. EXPECT_TRUE(IsConstHelper(*map_.cbegin()));
  543. EXPECT_TRUE(IsConstHelper(*const_map_.begin()));
  544. EXPECT_FALSE(IsConstHelper(*map_.begin()));
  545. }
  546. bool IsForwardIteratorHelper(std::forward_iterator_tag /*tag*/) { return true; }
  547. template <typename T>
  548. bool IsForwardIteratorHelper(T /*t*/) {
  549. return false;
  550. }
  551. TEST_F(MapImplTest, IteratorCategory) {
  552. EXPECT_TRUE(IsForwardIteratorHelper(
  553. std::iterator_traits<Map<int, int>::iterator>::iterator_category()));
  554. EXPECT_TRUE(IsForwardIteratorHelper(std::iterator_traits<
  555. Map<int, int>::const_iterator>::iterator_category()));
  556. }
  557. TEST_F(MapImplTest, InsertSingle) {
  558. int32 key = 0;
  559. int32 value1 = 100;
  560. int32 value2 = 101;
  561. // Insert a non-existed key.
  562. std::pair<Map<int32, int32>::iterator, bool> result1 =
  563. map_.insert(Map<int32, int32>::value_type(key, value1));
  564. ExpectSingleElement(key, value1);
  565. Map<int32, int32>::iterator it1 = result1.first;
  566. EXPECT_EQ(key, it1->first);
  567. EXPECT_EQ(value1, it1->second);
  568. EXPECT_TRUE(result1.second);
  569. // Insert an existed key.
  570. std::pair<Map<int32, int32>::iterator, bool> result2 =
  571. map_.insert(Map<int32, int32>::value_type(key, value2));
  572. ExpectSingleElement(key, value1);
  573. Map<int32, int32>::iterator it2 = result2.first;
  574. EXPECT_TRUE(it1 == it2);
  575. EXPECT_FALSE(result2.second);
  576. }
  577. TEST_F(MapImplTest, InsertByIterator) {
  578. int32 key1 = 0;
  579. int32 key2 = 1;
  580. int32 value1a = 100;
  581. int32 value1b = 101;
  582. int32 value2a = 200;
  583. int32 value2b = 201;
  584. std::map<int32, int32> map1;
  585. map1[key1] = value1a;
  586. map1[key2] = value2a;
  587. map_.insert(map1.begin(), map1.end());
  588. ExpectElements(map1);
  589. std::map<int32, int32> map2;
  590. map2[key1] = value1b;
  591. map2[key2] = value2b;
  592. map_.insert(map2.begin(), map2.end());
  593. ExpectElements(map1);
  594. }
  595. TEST_F(MapImplTest, InsertByInitializerList) {
  596. map_.insert({{1, 100}, {2, 200}});
  597. ExpectElements({{1, 100}, {2, 200}});
  598. map_.insert({{2, 201}, {3, 301}});
  599. ExpectElements({{1, 100}, {2, 200}, {3, 301}});
  600. }
  601. TEST_F(MapImplTest, EraseSingleByKey) {
  602. int32 key = 0;
  603. int32 value = 100;
  604. map_[key] = value;
  605. ExpectSingleElement(key, value);
  606. // Erase an existing key.
  607. EXPECT_EQ(1, map_.erase(key));
  608. EXPECT_TRUE(map_.empty());
  609. EXPECT_EQ(0, map_.size());
  610. EXPECT_TRUE(map_.end() == map_.find(key));
  611. EXPECT_TRUE(map_.begin() == map_.end());
  612. // Erase a non-existing key.
  613. EXPECT_EQ(0, map_.erase(key));
  614. }
  615. TEST_F(MapImplTest, EraseMutipleByKey) {
  616. // erase in one specific order to trigger corner cases
  617. for (int i = 0; i < 5; i++) {
  618. map_[i] = i;
  619. }
  620. map_.erase(0);
  621. EXPECT_EQ(4, map_.size());
  622. EXPECT_TRUE(map_.end() == map_.find(0));
  623. map_.erase(1);
  624. EXPECT_EQ(3, map_.size());
  625. EXPECT_TRUE(map_.end() == map_.find(1));
  626. map_.erase(3);
  627. EXPECT_EQ(2, map_.size());
  628. EXPECT_TRUE(map_.end() == map_.find(3));
  629. map_.erase(4);
  630. EXPECT_EQ(1, map_.size());
  631. EXPECT_TRUE(map_.end() == map_.find(4));
  632. map_.erase(2);
  633. EXPECT_EQ(0, map_.size());
  634. EXPECT_TRUE(map_.end() == map_.find(2));
  635. }
  636. TEST_F(MapImplTest, EraseSingleByIterator) {
  637. int32 key = 0;
  638. int32 value = 100;
  639. map_[key] = value;
  640. ExpectSingleElement(key, value);
  641. Map<int32, int32>::iterator it = map_.find(key);
  642. map_.erase(it);
  643. EXPECT_TRUE(map_.empty());
  644. EXPECT_EQ(0, map_.size());
  645. EXPECT_TRUE(map_.end() == map_.find(key));
  646. EXPECT_TRUE(map_.begin() == map_.end());
  647. }
  648. TEST_F(MapImplTest, ValidIteratorAfterErase) {
  649. for (int i = 0; i < 10; i++) {
  650. map_[i] = i;
  651. }
  652. int count = 0;
  653. for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) {
  654. count++;
  655. if (it->first % 2 == 1) {
  656. map_.erase(it++);
  657. } else {
  658. ++it;
  659. }
  660. }
  661. EXPECT_EQ(10, count);
  662. EXPECT_EQ(5, map_.size());
  663. }
  664. TEST_F(MapImplTest, EraseByIterator) {
  665. int32 key1 = 0;
  666. int32 key2 = 1;
  667. int32 value1 = 100;
  668. int32 value2 = 101;
  669. std::map<int32, int32> map;
  670. map[key1] = value1;
  671. map[key2] = value2;
  672. map_.insert(map.begin(), map.end());
  673. ExpectElements(map);
  674. map_.erase(map_.begin(), map_.end());
  675. EXPECT_TRUE(map_.empty());
  676. EXPECT_EQ(0, map_.size());
  677. EXPECT_TRUE(map_.end() == map_.find(key1));
  678. EXPECT_TRUE(map_.end() == map_.find(key2));
  679. EXPECT_TRUE(map_.begin() == map_.end());
  680. }
  681. TEST_F(MapImplTest, Clear) {
  682. int32 key = 0;
  683. int32 value = 100;
  684. map_[key] = value;
  685. ExpectSingleElement(key, value);
  686. map_.clear();
  687. EXPECT_TRUE(map_.empty());
  688. EXPECT_EQ(0, map_.size());
  689. EXPECT_TRUE(map_.end() == map_.find(key));
  690. EXPECT_TRUE(map_.begin() == map_.end());
  691. }
  692. static void CopyConstructorHelper(Arena* arena, Map<int32, int32>* m) {
  693. int32 key1 = 0;
  694. int32 key2 = 1;
  695. int32 value1 = 100;
  696. int32 value2 = 101;
  697. std::map<int32, int32> map;
  698. map[key1] = value1;
  699. map[key2] = value2;
  700. m->insert(map.begin(), map.end());
  701. Map<int32, int32> other(*m);
  702. EXPECT_EQ(2, other.size());
  703. EXPECT_EQ(value1, other.at(key1));
  704. EXPECT_EQ(value2, other.at(key2));
  705. }
  706. TEST_F(MapImplTest, CopyConstructorWithArena) {
  707. Arena a;
  708. CopyConstructorHelper(&a, &map_);
  709. }
  710. TEST_F(MapImplTest, CopyConstructorWithoutArena) {
  711. CopyConstructorHelper(NULL, &map_);
  712. }
  713. TEST_F(MapImplTest, IterConstructor) {
  714. int32 key1 = 0;
  715. int32 key2 = 1;
  716. int32 value1 = 100;
  717. int32 value2 = 101;
  718. std::map<int32, int32> map;
  719. map[key1] = value1;
  720. map[key2] = value2;
  721. Map<int32, int32> new_map(map.begin(), map.end());
  722. EXPECT_EQ(2, new_map.size());
  723. EXPECT_EQ(value1, new_map.at(key1));
  724. EXPECT_EQ(value2, new_map.at(key2));
  725. }
  726. TEST_F(MapImplTest, Assigner) {
  727. int32 key1 = 0;
  728. int32 key2 = 1;
  729. int32 value1 = 100;
  730. int32 value2 = 101;
  731. std::map<int32, int32> map;
  732. map[key1] = value1;
  733. map[key2] = value2;
  734. map_.insert(map.begin(), map.end());
  735. Map<int32, int32> other;
  736. int32 key_other = 123;
  737. int32 value_other = 321;
  738. other[key_other] = value_other;
  739. EXPECT_EQ(1, other.size());
  740. other = map_;
  741. EXPECT_EQ(2, other.size());
  742. EXPECT_EQ(value1, other.at(key1));
  743. EXPECT_EQ(value2, other.at(key2));
  744. EXPECT_TRUE(other.find(key_other) == other.end());
  745. // Self assign
  746. other = other;
  747. EXPECT_EQ(2, other.size());
  748. EXPECT_EQ(value1, other.at(key1));
  749. EXPECT_EQ(value2, other.at(key2));
  750. }
  751. TEST_F(MapImplTest, Rehash) {
  752. const int test_size = 50;
  753. std::map<int32, int32> reference_map;
  754. for (int i = 0; i < test_size; i++) {
  755. reference_map[i] = i;
  756. }
  757. for (int i = 0; i < test_size; i++) {
  758. map_[i] = reference_map[i];
  759. EXPECT_EQ(reference_map[i], map_[i]);
  760. }
  761. for (int i = 0; i < test_size; i++) {
  762. map_.erase(i);
  763. EXPECT_TRUE(map_.end() == map_.find(i));
  764. }
  765. EXPECT_TRUE(map_.empty());
  766. }
  767. TEST_F(MapImplTest, EqualRange) {
  768. int key = 100, key_missing = 101;
  769. map_[key] = 100;
  770. std::pair<google::protobuf::Map<int32, int32>::iterator,
  771. google::protobuf::Map<int32, int32>::iterator> range = map_.equal_range(key);
  772. EXPECT_TRUE(map_.find(key) == range.first);
  773. EXPECT_TRUE(++map_.find(key) == range.second);
  774. range = map_.equal_range(key_missing);
  775. EXPECT_TRUE(map_.end() == range.first);
  776. EXPECT_TRUE(map_.end() == range.second);
  777. std::pair<google::protobuf::Map<int32, int32>::const_iterator,
  778. google::protobuf::Map<int32, int32>::const_iterator> const_range =
  779. const_map_.equal_range(key);
  780. EXPECT_TRUE(const_map_.find(key) == const_range.first);
  781. EXPECT_TRUE(++const_map_.find(key) == const_range.second);
  782. const_range = const_map_.equal_range(key_missing);
  783. EXPECT_TRUE(const_map_.end() == const_range.first);
  784. EXPECT_TRUE(const_map_.end() == const_range.second);
  785. }
  786. TEST_F(MapImplTest, ConvertToStdMap) {
  787. map_[100] = 101;
  788. std::map<int32, int32> std_map(map_.begin(), map_.end());
  789. EXPECT_EQ(1, std_map.size());
  790. EXPECT_EQ(101, std_map[100]);
  791. }
  792. TEST_F(MapImplTest, ConvertToStdVectorOfPairs) {
  793. map_[100] = 101;
  794. std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end());
  795. EXPECT_EQ(1, std_vec.size());
  796. EXPECT_EQ(100, std_vec[0].first);
  797. EXPECT_EQ(101, std_vec[0].second);
  798. }
  799. TEST_F(MapImplTest, SwapBasic) {
  800. Map<int32, int32> another;
  801. map_[9398] = 41999;
  802. another[9398] = 41999;
  803. another[8070] = 42056;
  804. another.swap(map_);
  805. EXPECT_THAT(another, testing::UnorderedElementsAre(
  806. testing::Pair(9398, 41999)));
  807. EXPECT_THAT(map_, testing::UnorderedElementsAre(
  808. testing::Pair(8070, 42056),
  809. testing::Pair(9398, 41999)));
  810. }
  811. TEST_F(MapImplTest, SwapArena) {
  812. Arena arena1, arena2;
  813. Map<int32, int32> m1(&arena1);
  814. Map<int32, int32> m2(&arena2);
  815. map_[9398] = 41999;
  816. m1[9398] = 41999;
  817. m1[8070] = 42056;
  818. m2[10244] = 10247;
  819. m2[8070] = 42056;
  820. m1.swap(map_);
  821. EXPECT_THAT(m1, testing::UnorderedElementsAre(
  822. testing::Pair(9398, 41999)));
  823. EXPECT_THAT(map_, testing::UnorderedElementsAre(
  824. testing::Pair(8070, 42056),
  825. testing::Pair(9398, 41999)));
  826. m2.swap(m1);
  827. EXPECT_THAT(m1, testing::UnorderedElementsAre(
  828. testing::Pair(8070, 42056),
  829. testing::Pair(10244, 10247)));
  830. EXPECT_THAT(m2, testing::UnorderedElementsAre(
  831. testing::Pair(9398, 41999)));
  832. }
  833. TEST_F(MapImplTest, CopyAssignMapIterator) {
  834. TestMap message;
  835. MapReflectionTester reflection_tester(
  836. unittest::TestMap::descriptor());
  837. reflection_tester.SetMapFieldsViaMapReflection(&message);
  838. MapIterator it1 = reflection_tester.MapBegin(&message, "map_int32_int32");
  839. MapIterator it2 = reflection_tester.MapEnd(&message, "map_int32_int32");
  840. it2 = it1;
  841. EXPECT_EQ(it1.GetKey().GetInt32Value(), it2.GetKey().GetInt32Value());
  842. }
  843. // Map Field Reflection Test ========================================
  844. static int Func(int i, int j) {
  845. return i * j;
  846. }
  847. static string StrFunc(int i, int j) {
  848. string str;
  849. SStringPrintf(&str, "%d", Func(i, j));
  850. return str;
  851. }
  852. static int Int(const string& value) {
  853. int result = 0;
  854. std::istringstream(value) >> result;
  855. return result;
  856. }
  857. class MapFieldReflectionTest : public testing::Test {
  858. protected:
  859. typedef FieldDescriptor FD;
  860. int MapSize(const Reflection* reflection, const FieldDescriptor* field,
  861. const Message& message) {
  862. return reflection->MapSize(message, field);
  863. }
  864. };
  865. TEST_F(MapFieldReflectionTest, RegularFields) {
  866. TestMap message;
  867. const Reflection* refl = message.GetReflection();
  868. const Descriptor* desc = message.GetDescriptor();
  869. Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32();
  870. Map<int32, double>* map_int32_double = message.mutable_map_int32_double();
  871. Map<string, string>* map_string_string = message.mutable_map_string_string();
  872. Map<int32, ForeignMessage>* map_int32_foreign_message =
  873. message.mutable_map_int32_foreign_message();
  874. for (int i = 0; i < 10; ++i) {
  875. (*map_int32_int32)[i] = Func(i, 1);
  876. (*map_int32_double)[i] = Func(i, 2);
  877. (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
  878. (*map_int32_foreign_message)[i].set_c(Func(i, 6));
  879. }
  880. // Get FieldDescriptors for all the fields of interest.
  881. const FieldDescriptor* fd_map_int32_int32 =
  882. desc->FindFieldByName("map_int32_int32");
  883. const FieldDescriptor* fd_map_int32_double =
  884. desc->FindFieldByName("map_int32_double");
  885. const FieldDescriptor* fd_map_string_string =
  886. desc->FindFieldByName("map_string_string");
  887. const FieldDescriptor* fd_map_int32_foreign_message =
  888. desc->FindFieldByName("map_int32_foreign_message");
  889. const FieldDescriptor* fd_map_int32_in32_key =
  890. fd_map_int32_int32->message_type()->FindFieldByName("key");
  891. const FieldDescriptor* fd_map_int32_in32_value =
  892. fd_map_int32_int32->message_type()->FindFieldByName("value");
  893. const FieldDescriptor* fd_map_int32_double_key =
  894. fd_map_int32_double->message_type()->FindFieldByName("key");
  895. const FieldDescriptor* fd_map_int32_double_value =
  896. fd_map_int32_double->message_type()->FindFieldByName("value");
  897. const FieldDescriptor* fd_map_string_string_key =
  898. fd_map_string_string->message_type()->FindFieldByName("key");
  899. const FieldDescriptor* fd_map_string_string_value =
  900. fd_map_string_string->message_type()->FindFieldByName("value");
  901. const FieldDescriptor* fd_map_int32_foreign_message_key =
  902. fd_map_int32_foreign_message->message_type()->FindFieldByName("key");
  903. const FieldDescriptor* fd_map_int32_foreign_message_value =
  904. fd_map_int32_foreign_message->message_type()->FindFieldByName("value");
  905. // Get RepeatedPtrField objects for all fields of interest.
  906. const RepeatedPtrField<Message>& mf_int32_int32 =
  907. refl->GetRepeatedPtrField<Message>(message, fd_map_int32_int32);
  908. const RepeatedPtrField<Message>& mf_int32_double =
  909. refl->GetRepeatedPtrField<Message>(message, fd_map_int32_double);
  910. const RepeatedPtrField<Message>& mf_string_string =
  911. refl->GetRepeatedPtrField<Message>(message, fd_map_string_string);
  912. const RepeatedPtrField<Message>&
  913. mf_int32_foreign_message =
  914. refl->GetRepeatedPtrField<Message>(
  915. message, fd_map_int32_foreign_message);
  916. // Get mutable RepeatedPtrField objects for all fields of interest.
  917. RepeatedPtrField<Message>* mmf_int32_int32 =
  918. refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_int32);
  919. RepeatedPtrField<Message>* mmf_int32_double =
  920. refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_double);
  921. RepeatedPtrField<Message>* mmf_string_string =
  922. refl->MutableRepeatedPtrField<Message>(&message, fd_map_string_string);
  923. RepeatedPtrField<Message>* mmf_int32_foreign_message =
  924. refl->MutableRepeatedPtrField<Message>(
  925. &message, fd_map_int32_foreign_message);
  926. // Make sure we can do gets through the RepeatedPtrField objects.
  927. for (int i = 0; i < 10; ++i) {
  928. {
  929. // Check gets through const objects.
  930. const Message& message_int32_int32 = mf_int32_int32.Get(i);
  931. int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
  932. message_int32_int32, fd_map_int32_in32_key);
  933. int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
  934. message_int32_int32, fd_map_int32_in32_value);
  935. EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
  936. const Message& message_int32_double = mf_int32_double.Get(i);
  937. int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
  938. message_int32_double, fd_map_int32_double_key);
  939. double value_int32_double =
  940. message_int32_double.GetReflection()->GetDouble(
  941. message_int32_double, fd_map_int32_double_value);
  942. EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
  943. const Message& message_string_string = mf_string_string.Get(i);
  944. string key_string_string =
  945. message_string_string.GetReflection()->GetString(
  946. message_string_string, fd_map_string_string_key);
  947. string value_string_string =
  948. message_string_string.GetReflection()->GetString(
  949. message_string_string, fd_map_string_string_value);
  950. EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
  951. const Message& message_int32_message = mf_int32_foreign_message.Get(i);
  952. int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
  953. message_int32_message, fd_map_int32_foreign_message_key);
  954. const ForeignMessage& value_int32_message =
  955. down_cast<const ForeignMessage&>(
  956. message_int32_message.GetReflection()
  957. ->GetMessage(message_int32_message,
  958. fd_map_int32_foreign_message_value));
  959. EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
  960. }
  961. {
  962. // Check gets through mutable objects.
  963. const Message& message_int32_int32 = mmf_int32_int32->Get(i);
  964. int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
  965. message_int32_int32, fd_map_int32_in32_key);
  966. int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
  967. message_int32_int32, fd_map_int32_in32_value);
  968. EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
  969. const Message& message_int32_double = mmf_int32_double->Get(i);
  970. int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
  971. message_int32_double, fd_map_int32_double_key);
  972. double value_int32_double =
  973. message_int32_double.GetReflection()->GetDouble(
  974. message_int32_double, fd_map_int32_double_value);
  975. EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
  976. const Message& message_string_string = mmf_string_string->Get(i);
  977. string key_string_string =
  978. message_string_string.GetReflection()->GetString(
  979. message_string_string, fd_map_string_string_key);
  980. string value_string_string =
  981. message_string_string.GetReflection()->GetString(
  982. message_string_string, fd_map_string_string_value);
  983. EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
  984. const Message& message_int32_message = mmf_int32_foreign_message->Get(i);
  985. int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
  986. message_int32_message, fd_map_int32_foreign_message_key);
  987. const ForeignMessage& value_int32_message =
  988. down_cast<const ForeignMessage&>(
  989. message_int32_message.GetReflection()
  990. ->GetMessage(message_int32_message,
  991. fd_map_int32_foreign_message_value));
  992. EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
  993. }
  994. }
  995. // Do sets through the RepeatedPtrField objects.
  996. for (int i = 0; i < 10; i++) {
  997. {
  998. Message* message_int32_int32 = mmf_int32_int32->Mutable(i);
  999. int32 key_int32_int32 = message_int32_int32->GetReflection()->GetInt32(
  1000. *message_int32_int32, fd_map_int32_in32_key);
  1001. message_int32_int32->GetReflection()->SetInt32(message_int32_int32,
  1002. fd_map_int32_in32_value,
  1003. Func(key_int32_int32, -1));
  1004. Message* message_int32_double = mmf_int32_double->Mutable(i);
  1005. int32 key_int32_double = message_int32_double->GetReflection()->GetInt32(
  1006. *message_int32_double, fd_map_int32_double_key);
  1007. message_int32_double->GetReflection()->SetDouble(
  1008. message_int32_double, fd_map_int32_double_value,
  1009. Func(key_int32_double, -2));
  1010. Message* message_string_string = mmf_string_string->Mutable(i);
  1011. string key_string_string =
  1012. message_string_string->GetReflection()->GetString(
  1013. *message_string_string, fd_map_string_string_key);
  1014. message_string_string->GetReflection()->SetString(
  1015. message_string_string, fd_map_string_string_value,
  1016. StrFunc(Int(key_string_string), -5));
  1017. Message* message_int32_message = mmf_int32_foreign_message->Mutable(i);
  1018. int32 key_int32_message =
  1019. message_int32_message->GetReflection()->GetInt32(
  1020. *message_int32_message, fd_map_int32_foreign_message_key);
  1021. ForeignMessage* value_int32_message = down_cast<ForeignMessage*>(
  1022. message_int32_message->GetReflection()
  1023. ->MutableMessage(message_int32_message,
  1024. fd_map_int32_foreign_message_value));
  1025. value_int32_message->set_c(Func(key_int32_message, -6));
  1026. }
  1027. }
  1028. // Check gets through mutable objects.
  1029. for (int i = 0; i < 10; i++) {
  1030. EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
  1031. EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
  1032. EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
  1033. EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
  1034. }
  1035. }
  1036. TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) {
  1037. TestMap message;
  1038. const Reflection* refl = message.GetReflection();
  1039. const Descriptor* desc = message.GetDescriptor();
  1040. Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32();
  1041. Map<int32, double>* map_int32_double = message.mutable_map_int32_double();
  1042. Map<string, string>* map_string_string = message.mutable_map_string_string();
  1043. Map<int32, ForeignMessage>* map_int32_foreign_message =
  1044. message.mutable_map_int32_foreign_message();
  1045. for (int i = 0; i < 10; ++i) {
  1046. (*map_int32_int32)[i] = Func(i, 1);
  1047. (*map_int32_double)[i] = Func(i, 2);
  1048. (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
  1049. (*map_int32_foreign_message)[i].set_c(Func(i, 6));
  1050. }
  1051. // Get FieldDescriptors for all the fields of interest.
  1052. const FieldDescriptor* fd_map_int32_int32 =
  1053. desc->FindFieldByName("map_int32_int32");
  1054. const FieldDescriptor* fd_map_int32_double =
  1055. desc->FindFieldByName("map_int32_double");
  1056. const FieldDescriptor* fd_map_string_string =
  1057. desc->FindFieldByName("map_string_string");
  1058. const FieldDescriptor* fd_map_int32_foreign_message =
  1059. desc->FindFieldByName("map_int32_foreign_message");
  1060. const FieldDescriptor* fd_map_int32_in32_key =
  1061. fd_map_int32_int32->message_type()->FindFieldByName("key");
  1062. const FieldDescriptor* fd_map_int32_in32_value =
  1063. fd_map_int32_int32->message_type()->FindFieldByName("value");
  1064. const FieldDescriptor* fd_map_int32_double_key =
  1065. fd_map_int32_double->message_type()->FindFieldByName("key");
  1066. const FieldDescriptor* fd_map_int32_double_value =
  1067. fd_map_int32_double->message_type()->FindFieldByName("value");
  1068. const FieldDescriptor* fd_map_string_string_key =
  1069. fd_map_string_string->message_type()->FindFieldByName("key");
  1070. const FieldDescriptor* fd_map_string_string_value =
  1071. fd_map_string_string->message_type()->FindFieldByName("value");
  1072. const FieldDescriptor* fd_map_int32_foreign_message_key =
  1073. fd_map_int32_foreign_message->message_type()->FindFieldByName("key");
  1074. const FieldDescriptor* fd_map_int32_foreign_message_value =
  1075. fd_map_int32_foreign_message->message_type()->FindFieldByName("value");
  1076. // Get RepeatedFieldRef objects for all fields of interest.
  1077. const RepeatedFieldRef<Message> mf_int32_int32 =
  1078. refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_int32);
  1079. const RepeatedFieldRef<Message> mf_int32_double =
  1080. refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_double);
  1081. const RepeatedFieldRef<Message> mf_string_string =
  1082. refl->GetRepeatedFieldRef<Message>(message, fd_map_string_string);
  1083. const RepeatedFieldRef<Message> mf_int32_foreign_message =
  1084. refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_foreign_message);
  1085. // Get mutable RepeatedFieldRef objects for all fields of interest.
  1086. const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
  1087. refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_int32);
  1088. const MutableRepeatedFieldRef<Message> mmf_int32_double =
  1089. refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_double);
  1090. const MutableRepeatedFieldRef<Message> mmf_string_string =
  1091. refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_string_string);
  1092. const MutableRepeatedFieldRef<Message>
  1093. mmf_int32_foreign_message =
  1094. refl->GetMutableRepeatedFieldRef<Message>(
  1095. &message, fd_map_int32_foreign_message);
  1096. // Get entry default instances
  1097. std::unique_ptr<Message> entry_int32_int32(
  1098. MessageFactory::generated_factory()
  1099. ->GetPrototype(fd_map_int32_int32->message_type())
  1100. ->New());
  1101. std::unique_ptr<Message> entry_int32_double(
  1102. MessageFactory::generated_factory()
  1103. ->GetPrototype(fd_map_int32_double->message_type())
  1104. ->New());
  1105. std::unique_ptr<Message> entry_string_string(
  1106. MessageFactory::generated_factory()
  1107. ->GetPrototype(fd_map_string_string->message_type())
  1108. ->New());
  1109. std::unique_ptr<Message> entry_int32_foreign_message(
  1110. MessageFactory::generated_factory()
  1111. ->GetPrototype(fd_map_int32_foreign_message->message_type())
  1112. ->New());
  1113. EXPECT_EQ(10, mf_int32_int32.size());
  1114. EXPECT_EQ(10, mmf_int32_int32.size());
  1115. EXPECT_EQ(10, mf_int32_double.size());
  1116. EXPECT_EQ(10, mmf_int32_double.size());
  1117. EXPECT_EQ(10, mf_string_string.size());
  1118. EXPECT_EQ(10, mmf_string_string.size());
  1119. EXPECT_EQ(10, mf_int32_foreign_message.size());
  1120. EXPECT_EQ(10, mmf_int32_foreign_message.size());
  1121. EXPECT_FALSE(mf_int32_int32.empty());
  1122. EXPECT_FALSE(mmf_int32_int32.empty());
  1123. EXPECT_FALSE(mf_int32_double.empty());
  1124. EXPECT_FALSE(mmf_int32_double.empty());
  1125. EXPECT_FALSE(mf_string_string.empty());
  1126. EXPECT_FALSE(mmf_string_string.empty());
  1127. EXPECT_FALSE(mf_int32_foreign_message.empty());
  1128. EXPECT_FALSE(mmf_int32_foreign_message.empty());
  1129. // Make sure we can do gets through the RepeatedFieldRef objects.
  1130. for (int i = 0; i < 10; ++i) {
  1131. {
  1132. // Check gets through const objects.
  1133. const Message& message_int32_int32 =
  1134. mf_int32_int32.Get(i, entry_int32_int32.get());
  1135. int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
  1136. message_int32_int32, fd_map_int32_in32_key);
  1137. int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
  1138. message_int32_int32, fd_map_int32_in32_value);
  1139. EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
  1140. const Message& message_int32_double =
  1141. mf_int32_double.Get(i, entry_int32_double.get());
  1142. int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
  1143. message_int32_double, fd_map_int32_double_key);
  1144. double value_int32_double =
  1145. message_int32_double.GetReflection()->GetDouble(
  1146. message_int32_double, fd_map_int32_double_value);
  1147. EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
  1148. const Message& message_string_string =
  1149. mf_string_string.Get(i, entry_string_string.get());
  1150. string key_string_string =
  1151. message_string_string.GetReflection()->GetString(
  1152. message_string_string, fd_map_string_string_key);
  1153. string value_string_string =
  1154. message_string_string.GetReflection()->GetString(
  1155. message_string_string, fd_map_string_string_value);
  1156. EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
  1157. const Message& message_int32_message =
  1158. mf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
  1159. int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
  1160. message_int32_message, fd_map_int32_foreign_message_key);
  1161. const ForeignMessage& value_int32_message =
  1162. down_cast<const ForeignMessage&>(
  1163. message_int32_message.GetReflection()
  1164. ->GetMessage(message_int32_message,
  1165. fd_map_int32_foreign_message_value));
  1166. EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
  1167. }
  1168. {
  1169. // Check gets through mutable objects.
  1170. const Message& message_int32_int32 =
  1171. mmf_int32_int32.Get(i, entry_int32_int32.get());
  1172. int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
  1173. message_int32_int32, fd_map_int32_in32_key);
  1174. int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
  1175. message_int32_int32, fd_map_int32_in32_value);
  1176. EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
  1177. const Message& message_int32_double =
  1178. mmf_int32_double.Get(i, entry_int32_double.get());
  1179. int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
  1180. message_int32_double, fd_map_int32_double_key);
  1181. double value_int32_double =
  1182. message_int32_double.GetReflection()->GetDouble(
  1183. message_int32_double, fd_map_int32_double_value);
  1184. EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
  1185. const Message& message_string_string =
  1186. mmf_string_string.Get(i, entry_string_string.get());
  1187. string key_string_string =
  1188. message_string_string.GetReflection()->GetString(
  1189. message_string_string, fd_map_string_string_key);
  1190. string value_string_string =
  1191. message_string_string.GetReflection()->GetString(
  1192. message_string_string, fd_map_string_string_value);
  1193. EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
  1194. const Message& message_int32_message =
  1195. mmf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
  1196. int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
  1197. message_int32_message, fd_map_int32_foreign_message_key);
  1198. const ForeignMessage& value_int32_message =
  1199. down_cast<const ForeignMessage&>(
  1200. message_int32_message.GetReflection()
  1201. ->GetMessage(message_int32_message,
  1202. fd_map_int32_foreign_message_value));
  1203. EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
  1204. }
  1205. }
  1206. // Make sure we can do sets through the RepeatedFieldRef objects.
  1207. for (int i = 0; i < 10; i++) {
  1208. const Message& message_int32_int32 =
  1209. mmf_int32_int32.Get(i, entry_int32_int32.get());
  1210. int key = message_int32_int32.GetReflection()->GetInt32(
  1211. message_int32_int32, fd_map_int32_in32_key);
  1212. entry_int32_int32->GetReflection()->SetInt32(
  1213. entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
  1214. key);
  1215. entry_int32_int32->GetReflection()->SetInt32(
  1216. entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
  1217. Func(key, -1));
  1218. entry_int32_double->GetReflection()->SetInt32(
  1219. entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
  1220. key);
  1221. entry_int32_double->GetReflection()->SetDouble(
  1222. entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
  1223. Func(key, -2));
  1224. entry_string_string->GetReflection()->SetString(
  1225. entry_string_string.get(),
  1226. fd_map_string_string->message_type()->field(0), StrFunc(key, 1));
  1227. entry_string_string->GetReflection()->SetString(
  1228. entry_string_string.get(),
  1229. fd_map_string_string->message_type()->field(1), StrFunc(key, -5));
  1230. entry_int32_foreign_message->GetReflection()->SetInt32(
  1231. entry_int32_foreign_message.get(),
  1232. fd_map_int32_foreign_message->message_type()->field(0), key);
  1233. Message* value_message =
  1234. entry_int32_foreign_message->GetReflection()->MutableMessage(
  1235. entry_int32_foreign_message.get(),
  1236. fd_map_int32_foreign_message->message_type()->field(1));
  1237. value_message->GetReflection()->SetInt32(
  1238. value_message, value_message->GetDescriptor()->FindFieldByName("c"),
  1239. Func(key, -6));
  1240. mmf_int32_int32.Set(i, *entry_int32_int32);
  1241. mmf_int32_double.Set(i, *entry_int32_double);
  1242. mmf_string_string.Set(i, *entry_string_string);
  1243. mmf_int32_foreign_message.Set(i, *entry_int32_foreign_message);
  1244. }
  1245. for (int i = 0; i < 10; i++) {
  1246. EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
  1247. EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
  1248. EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
  1249. EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
  1250. }
  1251. // Test iterators.
  1252. {
  1253. int index = 0;
  1254. hash_map<int32, int32> result;
  1255. for (RepeatedFieldRef<Message>::iterator it = mf_int32_int32.begin();
  1256. it != mf_int32_int32.end(); ++it) {
  1257. const Message& message = *it;
  1258. int32 key =
  1259. message.GetReflection()->GetInt32(message, fd_map_int32_in32_key);
  1260. int32 value =
  1261. message.GetReflection()->GetInt32(message, fd_map_int32_in32_value);
  1262. result[key] = value;
  1263. ++index;
  1264. }
  1265. EXPECT_EQ(10, index);
  1266. for (hash_map<int32, int32>::const_iterator it = result.begin();
  1267. it != result.end(); ++it) {
  1268. EXPECT_EQ(message.map_int32_int32().at(it->first), it->second);
  1269. }
  1270. }
  1271. {
  1272. int index = 0;
  1273. hash_map<int32, double> result;
  1274. for (RepeatedFieldRef<Message>::iterator it = mf_int32_double.begin();
  1275. it != mf_int32_double.end(); ++it) {
  1276. const Message& message = *it;
  1277. int32 key =
  1278. message.GetReflection()->GetInt32(message, fd_map_int32_double_key);
  1279. double value = message.GetReflection()->GetDouble(
  1280. message, fd_map_int32_double_value);
  1281. result[key] = value;
  1282. ++index;
  1283. }
  1284. EXPECT_EQ(10, index);
  1285. for (hash_map<int32, double>::const_iterator it = result.begin();
  1286. it != result.end(); ++it) {
  1287. EXPECT_EQ(message.map_int32_double().at(it->first), it->second);
  1288. }
  1289. }
  1290. {
  1291. int index = 0;
  1292. hash_map<string, string> result;
  1293. for (RepeatedFieldRef<Message>::iterator it = mf_string_string.begin();
  1294. it != mf_string_string.end(); ++it) {
  1295. const Message& message = *it;
  1296. string key =
  1297. message.GetReflection()->GetString(message, fd_map_string_string_key);
  1298. string value = message.GetReflection()->GetString(
  1299. message, fd_map_string_string_value);
  1300. result[key] = value;
  1301. ++index;
  1302. }
  1303. EXPECT_EQ(10, index);
  1304. for (hash_map<string, string>::const_iterator it = result.begin();
  1305. it != result.end(); ++it) {
  1306. EXPECT_EQ(message.map_string_string().at(it->first), it->second);
  1307. }
  1308. }
  1309. {
  1310. int index = 0;
  1311. std::map<int32, ForeignMessage> result;
  1312. for (RepeatedFieldRef<Message>::iterator it =
  1313. mf_int32_foreign_message.begin();
  1314. it != mf_int32_foreign_message.end(); ++it) {
  1315. const Message& message = *it;
  1316. int32 key = message.GetReflection()->GetInt32(
  1317. message, fd_map_int32_foreign_message_key);
  1318. const ForeignMessage& sub_message = down_cast<const ForeignMessage&>(
  1319. message.GetReflection()
  1320. ->GetMessage(message, fd_map_int32_foreign_message_value));
  1321. result[key].MergeFrom(sub_message);
  1322. ++index;
  1323. }
  1324. EXPECT_EQ(10, index);
  1325. for (std::map<int32, ForeignMessage>::const_iterator it = result.begin();
  1326. it != result.end(); ++it) {
  1327. EXPECT_EQ(message.map_int32_foreign_message().at(it->first).c(),
  1328. it->second.c());
  1329. }
  1330. }
  1331. // Test MutableRepeatedFieldRef::Add()
  1332. entry_int32_int32->GetReflection()->SetInt32(
  1333. entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
  1334. 4321);
  1335. entry_int32_int32->GetReflection()->SetInt32(
  1336. entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
  1337. 1234);
  1338. mmf_int32_int32.Add(*entry_int32_int32);
  1339. EXPECT_EQ(1234, message.map_int32_int32().at(4321));
  1340. entry_int32_double->GetReflection()->SetInt32(
  1341. entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
  1342. 4321);
  1343. entry_int32_double->GetReflection()->SetDouble(
  1344. entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
  1345. 1234.0);
  1346. mmf_int32_double.Add(*entry_int32_double);
  1347. EXPECT_EQ(1234.0, message.map_int32_double().at(4321));
  1348. entry_string_string->GetReflection()->SetString(
  1349. entry_string_string.get(),
  1350. fd_map_string_string->message_type()->field(0), "4321");
  1351. entry_string_string->GetReflection()->SetString(
  1352. entry_string_string.get(), fd_map_string_string->message_type()->field(1),
  1353. "1234");
  1354. mmf_string_string.Add(*entry_string_string);
  1355. EXPECT_EQ("1234", message.map_string_string().at("4321"));
  1356. entry_int32_foreign_message->GetReflection()->SetInt32(
  1357. entry_int32_foreign_message.get(),
  1358. fd_map_int32_foreign_message->message_type()->field(0), 4321);
  1359. Message* value_message =
  1360. entry_int32_foreign_message->GetReflection()->MutableMessage(
  1361. entry_int32_foreign_message.get(),
  1362. fd_map_int32_foreign_message->message_type()->field(1));
  1363. ForeignMessage foreign_message;
  1364. foreign_message.set_c(1234);
  1365. value_message->CopyFrom(foreign_message);
  1366. mmf_int32_foreign_message.Add(*entry_int32_foreign_message);
  1367. EXPECT_EQ(1234, message.map_int32_foreign_message().at(4321).c());
  1368. // Test Reflection::AddAllocatedMessage
  1369. Message* free_entry_string_string = MessageFactory::generated_factory()
  1370. ->GetPrototype(fd_map_string_string->message_type())
  1371. ->New();
  1372. entry_string_string->GetReflection()->SetString(
  1373. free_entry_string_string,
  1374. fd_map_string_string->message_type()->field(0), "4321");
  1375. entry_string_string->GetReflection()->SetString(
  1376. free_entry_string_string, fd_map_string_string->message_type()->field(1),
  1377. "1234");
  1378. refl->AddAllocatedMessage(&message, fd_map_string_string,
  1379. free_entry_string_string);
  1380. // Test MutableRepeatedFieldRef::RemoveLast()
  1381. mmf_int32_int32.RemoveLast();
  1382. mmf_int32_double.RemoveLast();
  1383. mmf_string_string.RemoveLast();
  1384. mmf_int32_foreign_message.RemoveLast();
  1385. EXPECT_EQ(10, message.map_int32_int32().size());
  1386. EXPECT_EQ(10, message.map_int32_double().size());
  1387. EXPECT_EQ(11, message.map_string_string().size());
  1388. EXPECT_EQ(10, message.map_int32_foreign_message().size());
  1389. // Test MutableRepeatedFieldRef::SwapElements()
  1390. {
  1391. const Message& message0a = mmf_int32_int32.Get(0, entry_int32_int32.get());
  1392. int32 int32_value0a =
  1393. message0a.GetReflection()->GetInt32(message0a, fd_map_int32_in32_value);
  1394. const Message& message9a = mmf_int32_int32.Get(9, entry_int32_int32.get());
  1395. int32 int32_value9a =
  1396. message9a.GetReflection()->GetInt32(message9a, fd_map_int32_in32_value);
  1397. mmf_int32_int32.SwapElements(0, 9);
  1398. const Message& message0b = mmf_int32_int32.Get(0, entry_int32_int32.get());
  1399. int32 int32_value0b =
  1400. message0b.GetReflection()->GetInt32(message0b, fd_map_int32_in32_value);
  1401. const Message& message9b = mmf_int32_int32.Get(9, entry_int32_int32.get());
  1402. int32 int32_value9b =
  1403. message9b.GetReflection()->GetInt32(message9b, fd_map_int32_in32_value);
  1404. EXPECT_EQ(int32_value9a, int32_value0b);
  1405. EXPECT_EQ(int32_value0a, int32_value9b);
  1406. }
  1407. {
  1408. const Message& message0a =
  1409. mmf_int32_double.Get(0, entry_int32_double.get());
  1410. double double_value0a = message0a.GetReflection()->GetDouble(
  1411. message0a, fd_map_int32_double_value);
  1412. const Message& message9a =
  1413. mmf_int32_double.Get(9, entry_int32_double.get());
  1414. double double_value9a = message9a.GetReflection()->GetDouble(
  1415. message9a, fd_map_int32_double_value);
  1416. mmf_int32_double.SwapElements(0, 9);
  1417. const Message& message0b =
  1418. mmf_int32_double.Get(0, entry_int32_double.get());
  1419. double double_value0b = message0b.GetReflection()->GetDouble(
  1420. message0b, fd_map_int32_double_value);
  1421. const Message& message9b =
  1422. mmf_int32_double.Get(9, entry_int32_double.get());
  1423. double double_value9b = message9b.GetReflection()->GetDouble(
  1424. message9b, fd_map_int32_double_value);
  1425. EXPECT_EQ(double_value9a, double_value0b);
  1426. EXPECT_EQ(double_value0a, double_value9b);
  1427. }
  1428. {
  1429. const Message& message0a =
  1430. mmf_string_string.Get(0, entry_string_string.get());
  1431. string string_value0a = message0a.GetReflection()->GetString(
  1432. message0a, fd_map_string_string_value);
  1433. const Message& message9a =
  1434. mmf_string_string.Get(9, entry_string_string.get());
  1435. string string_value9a = message9a.GetReflection()->GetString(
  1436. message9a, fd_map_string_string_value);
  1437. mmf_string_string.SwapElements(0, 9);
  1438. const Message& message0b =
  1439. mmf_string_string.Get(0, entry_string_string.get());
  1440. string string_value0b = message0b.GetReflection()->GetString(
  1441. message0b, fd_map_string_string_value);
  1442. const Message& message9b =
  1443. mmf_string_string.Get(9, entry_string_string.get());
  1444. string string_value9b = message9b.GetReflection()->GetString(
  1445. message9b, fd_map_string_string_value);
  1446. EXPECT_EQ(string_value9a, string_value0b);
  1447. EXPECT_EQ(string_value0a, string_value9b);
  1448. }
  1449. {
  1450. const Message& message0a =
  1451. mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
  1452. const ForeignMessage& sub_message0a = down_cast<const ForeignMessage&>(
  1453. message0a.GetReflection()
  1454. ->GetMessage(message0a, fd_map_int32_foreign_message_value));
  1455. int32 int32_value0a = sub_message0a.c();
  1456. const Message& message9a =
  1457. mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get());
  1458. const ForeignMessage& sub_message9a = down_cast<const ForeignMessage&>(
  1459. message9a.GetReflection()
  1460. ->GetMessage(message9a, fd_map_int32_foreign_message_value));
  1461. int32 int32_value9a = sub_message9a.c();
  1462. mmf_int32_foreign_message.SwapElements(0, 9);
  1463. const Message& message0b =
  1464. mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
  1465. const ForeignMessage& sub_message0b = down_cast<const ForeignMessage&>(
  1466. message0b.GetReflection()
  1467. ->GetMessage(message0b, fd_map_int32_foreign_message_value));
  1468. int32 int32_value0b = sub_message0b.c();
  1469. const Message& message9b =
  1470. mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get());
  1471. const ForeignMessage& sub_message9b = down_cast<const ForeignMessage&>(
  1472. message9b.GetReflection()
  1473. ->GetMessage(message9b, fd_map_int32_foreign_message_value));
  1474. int32 int32_value9b = sub_message9b.c();
  1475. EXPECT_EQ(int32_value9a, int32_value0b);
  1476. EXPECT_EQ(int32_value0a, int32_value9b);
  1477. }
  1478. }
  1479. TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) {
  1480. // Set-up message content.
  1481. TestMap m0, m1, m2;
  1482. for (int i = 0; i < 10; ++i) {
  1483. (*m0.mutable_map_int32_int32())[i] = Func(i, 1);
  1484. (*m0.mutable_map_int32_double())[i] = Func(i, 2);
  1485. (*m0.mutable_map_string_string())[StrFunc(i, 1)] = StrFunc(i, 5);
  1486. (*m0.mutable_map_int32_foreign_message())[i].set_c(Func(i, 6));
  1487. (*m1.mutable_map_int32_int32())[i + 10] = Func(i, 11);
  1488. (*m1.mutable_map_int32_double())[i + 10] = Func(i, 12);
  1489. (*m1.mutable_map_string_string())[StrFunc(i + 10, 1)] = StrFunc(i, 15);
  1490. (*m1.mutable_map_int32_foreign_message())[i + 10].set_c(Func(i, 16));
  1491. (*m2.mutable_map_int32_int32())[i + 20] = Func(i, 21);
  1492. (*m2.mutable_map_int32_double())[i + 20] = Func(i, 22);
  1493. (*m2.mutable_map_string_string())[StrFunc(i + 20, 1)] = StrFunc(i, 25);
  1494. (*m2.mutable_map_int32_foreign_message())[i + 20].set_c(Func(i, 26));
  1495. }
  1496. const Reflection* refl = m0.GetReflection();
  1497. const Descriptor* desc = m0.GetDescriptor();
  1498. // Get FieldDescriptors for all the fields of interest.
  1499. const FieldDescriptor* fd_map_int32_int32 =
  1500. desc->FindFieldByName("map_int32_int32");
  1501. const FieldDescriptor* fd_map_int32_double =
  1502. desc->FindFieldByName("map_int32_double");
  1503. const FieldDescriptor* fd_map_string_string =
  1504. desc->FindFieldByName("map_string_string");
  1505. const FieldDescriptor* fd_map_int32_foreign_message =
  1506. desc->FindFieldByName("map_int32_foreign_message");
  1507. // Get MutableRepeatedFieldRef objects for all fields of interest.
  1508. const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
  1509. refl->GetMutableRepeatedFieldRef<Message>(
  1510. &m0, fd_map_int32_int32);
  1511. const MutableRepeatedFieldRef<Message> mmf_int32_double =
  1512. refl->GetMutableRepeatedFieldRef<Message>(
  1513. &m0, fd_map_int32_double);
  1514. const MutableRepeatedFieldRef<Message> mmf_string_string =
  1515. refl->GetMutableRepeatedFieldRef<Message>(
  1516. &m0, fd_map_string_string);
  1517. const MutableRepeatedFieldRef<Message>
  1518. mmf_int32_foreign_message =
  1519. refl->GetMutableRepeatedFieldRef<Message>(
  1520. &m0, fd_map_int32_foreign_message);
  1521. // Test MutableRepeatedRef::CopyFrom
  1522. mmf_int32_int32.CopyFrom(
  1523. refl->GetRepeatedFieldRef<Message>(
  1524. m1, fd_map_int32_int32));
  1525. mmf_int32_double.CopyFrom(
  1526. refl->GetRepeatedFieldRef<Message>(
  1527. m1, fd_map_int32_double));
  1528. mmf_string_string.CopyFrom(
  1529. refl->GetRepeatedFieldRef<Message>(
  1530. m1, fd_map_string_string));
  1531. mmf_int32_foreign_message.CopyFrom(
  1532. refl->GetRepeatedFieldRef<Message>(
  1533. m1, fd_map_int32_foreign_message));
  1534. for (int i = 0; i < 10; ++i) {
  1535. EXPECT_EQ(Func(i, 11), m0.map_int32_int32().at(i + 10));
  1536. EXPECT_EQ(Func(i, 12), m0.map_int32_double().at(i + 10));
  1537. EXPECT_EQ(StrFunc(i, 15), m0.map_string_string().at(StrFunc(i + 10, 1)));
  1538. EXPECT_EQ(Func(i, 16), m0.map_int32_foreign_message().at(i + 10).c());
  1539. }
  1540. // Test MutableRepeatedRef::MergeFrom
  1541. mmf_int32_int32.MergeFrom(
  1542. refl->GetRepeatedFieldRef<Message>(
  1543. m2, fd_map_int32_int32));
  1544. mmf_int32_double.MergeFrom(
  1545. refl->GetRepeatedFieldRef<Message>(
  1546. m2, fd_map_int32_double));
  1547. mmf_string_string.MergeFrom(
  1548. refl->GetRepeatedFieldRef<Message>(
  1549. m2, fd_map_string_string));
  1550. mmf_int32_foreign_message.MergeFrom(
  1551. refl->GetRepeatedFieldRef<Message>(
  1552. m2, fd_map_int32_foreign_message));
  1553. for (int i = 0; i < 10; ++i) {
  1554. EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20));
  1555. EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
  1556. EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
  1557. EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
  1558. }
  1559. // Test MutableRepeatedRef::Swap
  1560. // Swap between m0 and m2.
  1561. mmf_int32_int32.Swap(
  1562. refl->GetMutableRepeatedFieldRef<Message>(
  1563. &m2, fd_map_int32_int32));
  1564. mmf_int32_double.Swap(
  1565. refl->GetMutableRepeatedFieldRef<Message>(
  1566. &m2, fd_map_int32_double));
  1567. mmf_string_string.Swap(
  1568. refl->GetMutableRepeatedFieldRef<Message>(
  1569. &m2, fd_map_string_string));
  1570. mmf_int32_foreign_message.Swap(
  1571. refl->GetMutableRepeatedFieldRef<Message>(
  1572. &m2, fd_map_int32_foreign_message));
  1573. for (int i = 0; i < 10; ++i) {
  1574. // Check the content of m0.
  1575. EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20));
  1576. EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
  1577. EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
  1578. EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
  1579. // Check the content of m2.
  1580. EXPECT_EQ(Func(i, 11), m2.map_int32_int32().at(i + 10));
  1581. EXPECT_EQ(Func(i, 12), m2.map_int32_double().at(i + 10));
  1582. EXPECT_EQ(StrFunc(i, 15), m2.map_string_string().at(StrFunc(i + 10, 1)));
  1583. EXPECT_EQ(Func(i, 16), m2.map_int32_foreign_message().at(i + 10).c());
  1584. EXPECT_EQ(Func(i, 21), m2.map_int32_int32().at(i + 20));
  1585. EXPECT_EQ(Func(i, 22), m2.map_int32_double().at(i + 20));
  1586. EXPECT_EQ(StrFunc(i, 25), m2.map_string_string().at(StrFunc(i + 20, 1)));
  1587. EXPECT_EQ(Func(i, 26), m2.map_int32_foreign_message().at(i + 20).c());
  1588. }
  1589. // TODO(teboring): add test for duplicated key
  1590. }
  1591. TEST_F(MapFieldReflectionTest, MapSizeWithDuplicatedKey) {
  1592. // Dynamic Message
  1593. {
  1594. DynamicMessageFactory factory;
  1595. std::unique_ptr<Message> message(
  1596. factory.GetPrototype(unittest::TestMap::descriptor())->New());
  1597. const Reflection* reflection = message->GetReflection();
  1598. const FieldDescriptor* field =
  1599. unittest::TestMap::descriptor()->FindFieldByName("map_int32_int32");
  1600. Message* entry1 = reflection->AddMessage(message.get(), field);
  1601. Message* entry2 = reflection->AddMessage(message.get(), field);
  1602. const Reflection* entry_reflection = entry1->GetReflection();
  1603. const FieldDescriptor* key_field =
  1604. entry1->GetDescriptor()->FindFieldByName("key");
  1605. entry_reflection->SetInt32(entry1, key_field, 1);
  1606. entry_reflection->SetInt32(entry2, key_field, 1);
  1607. EXPECT_EQ(2, reflection->FieldSize(*message, field));
  1608. EXPECT_EQ(1, MapSize(reflection, field, *message));
  1609. EXPECT_EQ(2, reflection->FieldSize(*message, field));
  1610. }
  1611. // Generated Message
  1612. {
  1613. unittest::TestMap message;
  1614. const Reflection* reflection = message.GetReflection();
  1615. const FieldDescriptor* field =
  1616. message.GetDescriptor()->FindFieldByName("map_int32_int32");
  1617. Message* entry1 = reflection->AddMessage(&message, field);
  1618. Message* entry2 = reflection->AddMessage(&message, field);
  1619. const Reflection* entry_reflection = entry1->GetReflection();
  1620. const FieldDescriptor* key_field =
  1621. entry1->GetDescriptor()->FindFieldByName("key");
  1622. entry_reflection->SetInt32(entry1, key_field, 1);
  1623. entry_reflection->SetInt32(entry2, key_field, 1);
  1624. EXPECT_EQ(2, reflection->FieldSize(message, field));
  1625. EXPECT_EQ(1, MapSize(reflection, field, message));
  1626. }
  1627. }
  1628. // Generated Message Test ===========================================
  1629. TEST(GeneratedMapFieldTest, Accessors) {
  1630. unittest::TestMap message;
  1631. MapTestUtil::SetMapFields(&message);
  1632. MapTestUtil::ExpectMapFieldsSet(message);
  1633. MapTestUtil::ModifyMapFields(&message);
  1634. MapTestUtil::ExpectMapFieldsModified(message);
  1635. }
  1636. TEST(GeneratedMapFieldTest, SetMapFieldsInitialized) {
  1637. unittest::TestMap message;
  1638. MapTestUtil::SetMapFieldsInitialized(&message);
  1639. MapTestUtil::ExpectMapFieldsSetInitialized(message);
  1640. }
  1641. TEST(GeneratedMapFieldTest, Proto2SetMapFieldsInitialized) {
  1642. unittest::TestEnumMap message;
  1643. EXPECT_EQ(unittest::PROTO2_MAP_ENUM_FOO,
  1644. (*message.mutable_known_map_field())[0]);
  1645. }
  1646. TEST(GeneratedMapFieldTest, Clear) {
  1647. unittest::TestMap message;
  1648. MapTestUtil::SetMapFields(&message);
  1649. message.Clear();
  1650. MapTestUtil::ExpectClear(message);
  1651. }
  1652. TEST(GeneratedMapFieldTest, ClearMessageMap) {
  1653. unittest::TestMessageMap message;
  1654. // Creates a TestAllTypes with default value
  1655. TestUtil::ExpectClear((*message.mutable_map_int32_message())[0]);
  1656. }
  1657. TEST(GeneratedMapFieldTest, CopyFrom) {
  1658. unittest::TestMap message1, message2;
  1659. MapTestUtil::SetMapFields(&message1);
  1660. message2.CopyFrom(message1);
  1661. MapTestUtil::ExpectMapFieldsSet(message2);
  1662. // Copying from self should be a no-op.
  1663. message2.CopyFrom(message2);
  1664. MapTestUtil::ExpectMapFieldsSet(message2);
  1665. }
  1666. TEST(GeneratedMapFieldTest, CopyFromMessageMap) {
  1667. unittest::TestMessageMap message1, message2;
  1668. (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
  1669. (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
  1670. message1.CopyFrom(message2);
  1671. // Checks repeated field is overwritten.
  1672. EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
  1673. EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
  1674. }
  1675. TEST(GeneratedMapFieldTest, SwapWithEmpty) {
  1676. unittest::TestMap message1, message2;
  1677. MapTestUtil::SetMapFields(&message1);
  1678. MapTestUtil::ExpectMapFieldsSet(message1);
  1679. MapTestUtil::ExpectClear(message2);
  1680. message1.Swap(&message2);
  1681. MapTestUtil::ExpectMapFieldsSet(message2);
  1682. MapTestUtil::ExpectClear(message1);
  1683. }
  1684. TEST(GeneratedMapFieldTest, SwapWithSelf) {
  1685. unittest::TestMap message;
  1686. MapTestUtil::SetMapFields(&message);
  1687. MapTestUtil::ExpectMapFieldsSet(message);
  1688. message.Swap(&message);
  1689. MapTestUtil::ExpectMapFieldsSet(message);
  1690. }
  1691. TEST(GeneratedMapFieldTest, SwapWithOther) {
  1692. unittest::TestMap message1, message2;
  1693. MapTestUtil::SetMapFields(&message1);
  1694. MapTestUtil::SetMapFields(&message2);
  1695. MapTestUtil::ModifyMapFields(&message2);
  1696. message1.Swap(&message2);
  1697. MapTestUtil::ExpectMapFieldsModified(message1);
  1698. MapTestUtil::ExpectMapFieldsSet(message2);
  1699. }
  1700. TEST(GeneratedMapFieldTest, CopyConstructor) {
  1701. unittest::TestMap message1;
  1702. MapTestUtil::SetMapFields(&message1);
  1703. unittest::TestMap message2(message1);
  1704. MapTestUtil::ExpectMapFieldsSet(message2);
  1705. }
  1706. TEST(GeneratedMapFieldTest, CopyAssignmentOperator) {
  1707. unittest::TestMap message1;
  1708. MapTestUtil::SetMapFields(&message1);
  1709. unittest::TestMap message2;
  1710. message2 = message1;
  1711. MapTestUtil::ExpectMapFieldsSet(message2);
  1712. // Make sure that self-assignment does something sane.
  1713. message2.operator=(message2);
  1714. MapTestUtil::ExpectMapFieldsSet(message2);
  1715. }
  1716. #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \
  1717. !defined(GOOGLE_PROTOBUF_NO_RTTI)
  1718. TEST(GeneratedMapFieldTest, UpcastCopyFrom) {
  1719. // Test the CopyFrom method that takes in the generic const Message&
  1720. // parameter.
  1721. unittest::TestMap message1, message2;
  1722. MapTestUtil::SetMapFields(&message1);
  1723. const Message* source = implicit_cast<const Message*>(&message1);
  1724. message2.CopyFrom(*source);
  1725. MapTestUtil::ExpectMapFieldsSet(message2);
  1726. }
  1727. #endif
  1728. #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
  1729. TEST(GeneratedMapFieldTest, CopyFromDynamicMessage) {
  1730. // Test copying from a DynamicMessage, which must fall back to using
  1731. // reflection.
  1732. unittest::TestMap message2;
  1733. // Construct a new version of the dynamic message via the factory.
  1734. DynamicMessageFactory factory;
  1735. std::unique_ptr<Message> message1;
  1736. message1.reset(
  1737. factory.GetPrototype(unittest::TestMap::descriptor())->New());
  1738. MapReflectionTester reflection_tester(
  1739. unittest::TestMap::descriptor());
  1740. reflection_tester.SetMapFieldsViaReflection(message1.get());
  1741. reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
  1742. reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
  1743. message2.CopyFrom(*message1);
  1744. MapTestUtil::ExpectMapFieldsSet(message2);
  1745. }
  1746. TEST(GeneratedMapFieldTest, CopyFromDynamicMessageMapReflection) {
  1747. unittest::TestMap message2;
  1748. // Construct a new version of the dynamic message via the factory.
  1749. DynamicMessageFactory factory;
  1750. std::unique_ptr<Message> message1;
  1751. message1.reset(
  1752. factory.GetPrototype(unittest::TestMap::descriptor())->New());
  1753. MapReflectionTester reflection_tester(
  1754. unittest::TestMap::descriptor());
  1755. reflection_tester.SetMapFieldsViaMapReflection(message1.get());
  1756. reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
  1757. reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
  1758. message2.CopyFrom(*message1);
  1759. MapTestUtil::ExpectMapFieldsSet(message2);
  1760. }
  1761. TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) {
  1762. // Test copying to a DynamicMessage, which must fall back to using reflection.
  1763. unittest::TestMap message2;
  1764. MapTestUtil::SetMapFields(&message2);
  1765. // Construct a new version of the dynamic message via the factory.
  1766. DynamicMessageFactory factory;
  1767. std::unique_ptr<Message> message1;
  1768. message1.reset(
  1769. factory.GetPrototype(unittest::TestMap::descriptor())->New());
  1770. MapReflectionTester reflection_tester(
  1771. unittest::TestMap::descriptor());
  1772. message1->MergeFrom(message2);
  1773. reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
  1774. reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
  1775. }
  1776. TEST(GeneratedMapFieldTest, DynamicMessageCopyFromMapReflection) {
  1777. MapReflectionTester reflection_tester(
  1778. unittest::TestMap::descriptor());
  1779. unittest::TestMap message2;
  1780. reflection_tester.SetMapFieldsViaMapReflection(&message2);
  1781. // Construct a dynamic message via the factory.
  1782. DynamicMessageFactory factory;
  1783. std::unique_ptr<Message> message1;
  1784. message1.reset(
  1785. factory.GetPrototype(unittest::TestMap::descriptor())->New());
  1786. message1->MergeFrom(message2);
  1787. reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
  1788. reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
  1789. }
  1790. TEST(GeneratedMapFieldTest, SyncDynamicMapWithRepeatedField) {
  1791. // Construct a dynamic message via the factory.
  1792. MapReflectionTester reflection_tester(
  1793. unittest::TestMap::descriptor());
  1794. DynamicMessageFactory factory;
  1795. std::unique_ptr<Message> message;
  1796. message.reset(
  1797. factory.GetPrototype(unittest::TestMap::descriptor())->New());
  1798. reflection_tester.SetMapFieldsViaReflection(message.get());
  1799. reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message.get());
  1800. reflection_tester.ExpectMapFieldsSetViaReflection(*message);
  1801. }
  1802. #endif // !PROTOBUF_TEST_NO_DESCRIPTORS
  1803. TEST(GeneratedMapFieldTest, NonEmptyMergeFrom) {
  1804. unittest::TestMap message1, message2;
  1805. MapTestUtil::SetMapFields(&message1);
  1806. // This field will test merging into an empty spot.
  1807. (*message2.mutable_map_int32_int32())[1] = 1;
  1808. message1.mutable_map_int32_int32()->erase(1);
  1809. // This tests overwriting.
  1810. (*message2.mutable_map_int32_double())[1] = 1;
  1811. (*message1.mutable_map_int32_double())[1] = 2;
  1812. message1.MergeFrom(message2);
  1813. MapTestUtil::ExpectMapFieldsSet(message1);
  1814. }
  1815. TEST(GeneratedMapFieldTest, MergeFromMessageMap) {
  1816. unittest::TestMessageMap message1, message2;
  1817. (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
  1818. (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
  1819. message1.MergeFrom(message2);
  1820. // Checks repeated field is overwritten.
  1821. EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
  1822. EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
  1823. }
  1824. // Test the generated SerializeWithCachedSizesToArray()
  1825. TEST(GeneratedMapFieldTest, SerializationToArray) {
  1826. unittest::TestMap message1, message2;
  1827. string data;
  1828. MapTestUtil::SetMapFields(&message1);
  1829. int size = message1.ByteSize();
  1830. data.resize(size);
  1831. uint8* start = reinterpret_cast<uint8*>(string_as_array(&data));
  1832. uint8* end = message1.SerializeWithCachedSizesToArray(start);
  1833. EXPECT_EQ(size, end - start);
  1834. EXPECT_TRUE(message2.ParseFromString(data));
  1835. MapTestUtil::ExpectMapFieldsSet(message2);
  1836. }
  1837. // Test the generated SerializeWithCachedSizes()
  1838. TEST(GeneratedMapFieldTest, SerializationToStream) {
  1839. unittest::TestMap message1, message2;
  1840. MapTestUtil::SetMapFields(&message1);
  1841. int size = message1.ByteSize();
  1842. string data;
  1843. data.resize(size);
  1844. {
  1845. // Allow the output stream to buffer only one byte at a time.
  1846. io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
  1847. io::CodedOutputStream output_stream(&array_stream);
  1848. message1.SerializeWithCachedSizes(&output_stream);
  1849. EXPECT_FALSE(output_stream.HadError());
  1850. EXPECT_EQ(size, output_stream.ByteCount());
  1851. }
  1852. EXPECT_TRUE(message2.ParseFromString(data));
  1853. MapTestUtil::ExpectMapFieldsSet(message2);
  1854. }
  1855. TEST(GeneratedMapFieldTest, SameTypeMaps) {
  1856. const Descriptor* map1 = unittest::TestSameTypeMap::descriptor()
  1857. ->FindFieldByName("map1")
  1858. ->message_type();
  1859. const Descriptor* map2 = unittest::TestSameTypeMap::descriptor()
  1860. ->FindFieldByName("map2")
  1861. ->message_type();
  1862. const Message* map1_entry =
  1863. MessageFactory::generated_factory()->GetPrototype(map1);
  1864. const Message* map2_entry =
  1865. MessageFactory::generated_factory()->GetPrototype(map2);
  1866. EXPECT_EQ(map1, map1_entry->GetDescriptor());
  1867. EXPECT_EQ(map2, map2_entry->GetDescriptor());
  1868. }
  1869. TEST(GeneratedMapFieldTest, Proto2UnknownEnum) {
  1870. unittest::TestEnumMapPlusExtra from;
  1871. (*from.mutable_known_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_FOO;
  1872. (*from.mutable_unknown_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_EXTRA;
  1873. string data;
  1874. from.SerializeToString(&data);
  1875. unittest::TestEnumMap to;
  1876. EXPECT_TRUE(to.ParseFromString(data));
  1877. EXPECT_EQ(0, to.unknown_map_field().size());
  1878. const UnknownFieldSet& unknown_field_set =
  1879. to.GetReflection()->GetUnknownFields(to);
  1880. EXPECT_EQ(1, unknown_field_set.field_count());
  1881. EXPECT_EQ(1, to.known_map_field().size());
  1882. EXPECT_EQ(unittest::PROTO2_MAP_ENUM_FOO, to.known_map_field().at(0));
  1883. data.clear();
  1884. from.Clear();
  1885. to.SerializeToString(&data);
  1886. EXPECT_TRUE(from.ParseFromString(data));
  1887. EXPECT_EQ(0, from.GetReflection()->GetUnknownFields(from).field_count());
  1888. EXPECT_EQ(1, from.known_map_field().size());
  1889. EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_FOO, from.known_map_field().at(0));
  1890. EXPECT_EQ(1, from.unknown_map_field().size());
  1891. EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_EXTRA, from.unknown_map_field().at(0));
  1892. }
  1893. TEST(GeneratedMapFieldTest, StandardWireFormat) {
  1894. unittest::TestMap message;
  1895. string data = "\x0A\x04\x08\x01\x10\x01";
  1896. EXPECT_TRUE(message.ParseFromString(data));
  1897. EXPECT_EQ(1, message.map_int32_int32().size());
  1898. EXPECT_EQ(1, message.map_int32_int32().at(1));
  1899. }
  1900. TEST(GeneratedMapFieldTest, UnorderedWireFormat) {
  1901. unittest::TestMap message;
  1902. // put value before key in wire format
  1903. string data = "\x0A\x04\x10\x01\x08\x02";
  1904. EXPECT_TRUE(message.ParseFromString(data));
  1905. EXPECT_EQ(1, message.map_int32_int32().size());
  1906. EXPECT_EQ(1, message.map_int32_int32().at(2));
  1907. }
  1908. TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) {
  1909. unittest::TestMap message;
  1910. // Two key fields in wire format
  1911. string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
  1912. EXPECT_TRUE(message.ParseFromString(data));
  1913. EXPECT_EQ(1, message.map_int32_int32().size());
  1914. EXPECT_EQ(1, message.map_int32_int32().at(2));
  1915. // A similar test, but with a map from int to a message type.
  1916. // Again, we want to be sure that the "second one wins" when
  1917. // there are two separate entries with the same key.
  1918. const int key = 99;
  1919. unittest::TestRequiredMessageMap map_message;
  1920. unittest::TestRequired with_dummy4;
  1921. with_dummy4.set_a(0);
  1922. with_dummy4.set_b(0);
  1923. with_dummy4.set_c(0);
  1924. with_dummy4.set_dummy4(11);
  1925. (*map_message.mutable_map_field())[key] = with_dummy4;
  1926. string s = map_message.SerializeAsString();
  1927. unittest::TestRequired with_dummy5;
  1928. with_dummy5.set_a(0);
  1929. with_dummy5.set_b(0);
  1930. with_dummy5.set_c(0);
  1931. with_dummy5.set_dummy5(12);
  1932. (*map_message.mutable_map_field())[key] = with_dummy5;
  1933. string both = s + map_message.SerializeAsString();
  1934. // We don't expect a merge now. The "second one wins."
  1935. ASSERT_TRUE(map_message.ParseFromString(both));
  1936. ASSERT_EQ(1, map_message.map_field().size());
  1937. ASSERT_EQ(1, map_message.map_field().count(key));
  1938. EXPECT_EQ(0, map_message.map_field().find(key)->second.a());
  1939. EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
  1940. EXPECT_EQ(0, map_message.map_field().find(key)->second.c());
  1941. EXPECT_FALSE(map_message.map_field().find(key)->second.has_dummy4());
  1942. ASSERT_TRUE(map_message.map_field().find(key)->second.has_dummy5());
  1943. EXPECT_EQ(12, map_message.map_field().find(key)->second.dummy5());
  1944. }
  1945. // Exhaustive combinations of keys, values, and junk in any order.
  1946. // This re-tests some of the things tested above, but if it fails
  1947. // it's more work to determine what went wrong, so it isn't necessarily
  1948. // bad that we have the simpler tests too.
  1949. TEST(GeneratedMapFieldTest, KeysValuesUnknownsWireFormat) {
  1950. unittest::TestMap message;
  1951. const int kMaxNumKeysAndValuesAndJunk = 4;
  1952. const char kKeyTag = 0x08;
  1953. const char kValueTag = 0x10;
  1954. const char kJunkTag = 0x20;
  1955. for (int items = 0; items <= kMaxNumKeysAndValuesAndJunk; items++) {
  1956. string data = "\x0A";
  1957. // Encode length of what will follow.
  1958. data.push_back(items * 2);
  1959. static const int kBitsOfIPerItem = 4;
  1960. static const int mask = (1 << kBitsOfIPerItem) - 1;
  1961. // Each iteration of the following is a test. It uses i as bit vector
  1962. // encoding the keys and values to put in the wire format.
  1963. for (int i = 0; i < (1 << (items * kBitsOfIPerItem)); i++) {
  1964. string wire_format = data;
  1965. int expected_key = 0;
  1966. int expected_value = 0;
  1967. for (int k = i, j = 0; j < items; j++, k >>= kBitsOfIPerItem) {
  1968. bool is_key = k & 0x1;
  1969. bool is_value = !is_key && (k & 0x2);
  1970. wire_format.push_back(is_key ? kKeyTag :
  1971. is_value ? kValueTag : kJunkTag);
  1972. char c = static_cast<char>(k & mask) >> 2; // One char after the tag.
  1973. wire_format.push_back(c);
  1974. if (is_key) expected_key = static_cast<int>(c);
  1975. if (is_value) expected_value = static_cast<int>(c);
  1976. ASSERT_TRUE(message.ParseFromString(wire_format));
  1977. ASSERT_EQ(1, message.map_int32_int32().size());
  1978. ASSERT_EQ(expected_key, message.map_int32_int32().begin()->first);
  1979. ASSERT_EQ(expected_value, message.map_int32_int32().begin()->second);
  1980. }
  1981. }
  1982. }
  1983. }
  1984. TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) {
  1985. unittest::TestMap message;
  1986. // Two value fields in wire format
  1987. string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
  1988. EXPECT_TRUE(message.ParseFromString(data));
  1989. EXPECT_EQ(1, message.map_int32_int32().size());
  1990. EXPECT_EQ(2, message.map_int32_int32().at(1));
  1991. }
  1992. TEST(GeneratedMapFieldTest, MissedKeyWireFormat) {
  1993. unittest::TestMap message;
  1994. // No key field in wire format
  1995. string data = "\x0A\x02\x10\x01";
  1996. EXPECT_TRUE(message.ParseFromString(data));
  1997. EXPECT_EQ(1, message.map_int32_int32().size());
  1998. EXPECT_EQ(1, message.map_int32_int32().at(0));
  1999. }
  2000. TEST(GeneratedMapFieldTest, MissedValueWireFormat) {
  2001. unittest::TestMap message;
  2002. // No value field in wire format
  2003. string data = "\x0A\x02\x08\x01";
  2004. EXPECT_TRUE(message.ParseFromString(data));
  2005. EXPECT_EQ(1, message.map_int32_int32().size());
  2006. EXPECT_EQ(0, message.map_int32_int32().at(1));
  2007. }
  2008. TEST(GeneratedMapFieldTest, MissedValueTextFormat) {
  2009. unittest::TestMap message;
  2010. // No value field in text format
  2011. string text =
  2012. "map_int32_foreign_message {\n"
  2013. " key: 1234567890\n"
  2014. "}";
  2015. EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(text, &message));
  2016. EXPECT_EQ(1, message.map_int32_foreign_message().size());
  2017. EXPECT_EQ(11, message.ByteSize());
  2018. }
  2019. TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) {
  2020. unittest::TestMap message;
  2021. // Unknown field in wire format
  2022. string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
  2023. EXPECT_TRUE(message.ParseFromString(data));
  2024. EXPECT_EQ(1, message.map_int32_int32().size());
  2025. EXPECT_EQ(3, message.map_int32_int32().at(2));
  2026. }
  2027. TEST(GeneratedMapFieldTest, CorruptedWireFormat) {
  2028. unittest::TestMap message;
  2029. // corrupted data in wire format
  2030. string data = "\x0A\x06\x08\x02\x11\x03";
  2031. EXPECT_FALSE(message.ParseFromString(data));
  2032. }
  2033. TEST(GeneratedMapFieldTest, IsInitialized) {
  2034. unittest::TestRequiredMessageMap map_message;
  2035. // Add an uninitialized message.
  2036. (*map_message.mutable_map_field())[0];
  2037. EXPECT_FALSE(map_message.IsInitialized());
  2038. // Initialize uninitialized message
  2039. (*map_message.mutable_map_field())[0].set_a(0);
  2040. (*map_message.mutable_map_field())[0].set_b(0);
  2041. (*map_message.mutable_map_field())[0].set_c(0);
  2042. EXPECT_TRUE(map_message.IsInitialized());
  2043. }
  2044. TEST(GeneratedMapFieldTest, MessagesMustMerge) {
  2045. unittest::TestRequiredMessageMap map_message;
  2046. unittest::TestRequired with_dummy4;
  2047. with_dummy4.set_a(97);
  2048. with_dummy4.set_b(0);
  2049. with_dummy4.set_c(0);
  2050. with_dummy4.set_dummy4(98);
  2051. EXPECT_TRUE(with_dummy4.IsInitialized());
  2052. (*map_message.mutable_map_field())[0] = with_dummy4;
  2053. EXPECT_TRUE(map_message.IsInitialized());
  2054. string s = map_message.SerializeAsString();
  2055. // Modify s so that there are two values in the entry for key 0.
  2056. // The first will have no value for c. The second will have no value for a.
  2057. // Those are required fields. Also, make some other little changes, to
  2058. // ensure we are merging the two values (because they're messages).
  2059. ASSERT_EQ(s.size() - 2, s[1]); // encoding of the length of what follows
  2060. string encoded_val(s.data() + 4, s.data() + s.size());
  2061. // In s, change the encoding of c to an encoding of dummy32.
  2062. s[s.size() - 3] -= 8;
  2063. // Make encoded_val slightly different from what's in s.
  2064. encoded_val[encoded_val.size() - 1] += 33; // Encode c = 33.
  2065. for (int i = 0; i < encoded_val.size(); i++) {
  2066. if (encoded_val[i] == 97) {
  2067. // Encode b = 91 instead of a = 97. But this won't matter, because
  2068. // we also encode b = 0 right after this. The point is to leave out
  2069. // a required field, and make sure the parser doesn't complain, because
  2070. // every required field is set after the merge of the two values.
  2071. encoded_val[i - 1] += 16;
  2072. encoded_val[i] = 91;
  2073. } else if (encoded_val[i] == 98) {
  2074. // Encode dummy5 = 99 instead of dummy4 = 98.
  2075. encoded_val[i - 1] += 8; // The tag for dummy5 is 8 more.
  2076. encoded_val[i]++;
  2077. break;
  2078. }
  2079. }
  2080. s += encoded_val; // Add the second message.
  2081. s[1] += encoded_val.size(); // Adjust encoded size.
  2082. // Test key then value then value.
  2083. int key = 0;
  2084. ASSERT_TRUE(map_message.ParseFromString(s));
  2085. ASSERT_EQ(1, map_message.map_field().size());
  2086. ASSERT_EQ(1, map_message.map_field().count(key));
  2087. EXPECT_EQ(97, map_message.map_field().find(key)->second.a());
  2088. EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
  2089. EXPECT_EQ(33, map_message.map_field().find(key)->second.c());
  2090. EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4());
  2091. EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5());
  2092. // Test key then value then value then key.
  2093. s.push_back(s[2]); // Copy the key's tag.
  2094. key = 19;
  2095. s.push_back(key); // Second key is 19 instead of 0.
  2096. s[1] += 2; // Adjust encoded size.
  2097. ASSERT_TRUE(map_message.ParseFromString(s));
  2098. ASSERT_EQ(1, map_message.map_field().size());
  2099. ASSERT_EQ(1, map_message.map_field().count(key));
  2100. EXPECT_EQ(97, map_message.map_field().find(key)->second.a());
  2101. EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
  2102. EXPECT_EQ(33, map_message.map_field().find(key)->second.c());
  2103. EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4());
  2104. EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5());
  2105. }
  2106. // Generated Message Reflection Test ================================
  2107. TEST(GeneratedMapFieldReflectionTest, SpaceUsed) {
  2108. unittest::TestMap message;
  2109. MapReflectionTester reflection_tester(
  2110. unittest::TestMap::descriptor());
  2111. reflection_tester.SetMapFieldsViaReflection(&message);
  2112. EXPECT_LT(0, message.GetReflection()->SpaceUsed(message));
  2113. }
  2114. TEST(GeneratedMapFieldReflectionTest, Accessors) {
  2115. // Set every field to a unique value then go back and check all those
  2116. // values.
  2117. unittest::TestMap message;
  2118. MapReflectionTester reflection_tester(
  2119. unittest::TestMap::descriptor());
  2120. reflection_tester.SetMapFieldsViaReflection(&message);
  2121. MapTestUtil::ExpectMapFieldsSet(message);
  2122. reflection_tester.ExpectMapFieldsSetViaReflection(message);
  2123. reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message);
  2124. reflection_tester.ModifyMapFieldsViaReflection(&message);
  2125. MapTestUtil::ExpectMapFieldsModified(message);
  2126. }
  2127. TEST(GeneratedMapFieldReflectionTest, Swap) {
  2128. unittest::TestMap message1;
  2129. unittest::TestMap message2;
  2130. MapTestUtil::SetMapFields(&message1);
  2131. const Reflection* reflection = message1.GetReflection();
  2132. reflection->Swap(&message1, &message2);
  2133. MapTestUtil::ExpectClear(message1);
  2134. MapTestUtil::ExpectMapFieldsSet(message2);
  2135. }
  2136. TEST(GeneratedMapFieldReflectionTest, SwapWithBothSet) {
  2137. unittest::TestMap message1;
  2138. unittest::TestMap message2;
  2139. MapTestUtil::SetMapFields(&message1);
  2140. MapTestUtil::SetMapFields(&message2);
  2141. MapTestUtil::ModifyMapFields(&message2);
  2142. const Reflection* reflection = message1.GetReflection();
  2143. reflection->Swap(&message1, &message2);
  2144. MapTestUtil::ExpectMapFieldsModified(message1);
  2145. MapTestUtil::ExpectMapFieldsSet(message2);
  2146. }
  2147. TEST(GeneratedMapFieldReflectionTest, SwapFields) {
  2148. unittest::TestMap message1;
  2149. unittest::TestMap message2;
  2150. MapTestUtil::SetMapFields(&message2);
  2151. std::vector<const FieldDescriptor*> fields;
  2152. const Reflection* reflection = message1.GetReflection();
  2153. reflection->ListFields(message2, &fields);
  2154. reflection->SwapFields(&message1, &message2, fields);
  2155. MapTestUtil::ExpectMapFieldsSet(message1);
  2156. MapTestUtil::ExpectClear(message2);
  2157. }
  2158. TEST(GeneratedMapFieldReflectionTest, ClearField) {
  2159. unittest::TestMap message;
  2160. MapTestUtil::SetMapFields(&message);
  2161. MapTestUtil::ExpectMapFieldsSet(message);
  2162. MapReflectionTester reflection_tester(
  2163. unittest::TestMap::descriptor());
  2164. reflection_tester.ClearMapFieldsViaReflection(&message);
  2165. reflection_tester.ExpectClearViaReflection(message);
  2166. reflection_tester.ExpectClearViaReflectionIterator(&message);
  2167. }
  2168. TEST(GeneratedMapFieldReflectionTest, RemoveLast) {
  2169. unittest::TestMap message;
  2170. MapReflectionTester reflection_tester(
  2171. unittest::TestMap::descriptor());
  2172. MapTestUtil::SetMapFields(&message);
  2173. MapTestUtil::ExpectMapsSize(message, 2);
  2174. std::vector<const Message*> expected_entries =
  2175. MapTestUtil::GetMapEntries(message, 0);
  2176. reflection_tester.RemoveLastMapsViaReflection(&message);
  2177. MapTestUtil::ExpectMapsSize(message, 1);
  2178. std::vector<const Message*> remained_entries =
  2179. MapTestUtil::GetMapEntries(message, 0);
  2180. EXPECT_TRUE(expected_entries == remained_entries);
  2181. }
  2182. TEST(GeneratedMapFieldReflectionTest, ReleaseLast) {
  2183. unittest::TestMap message;
  2184. const Descriptor* descriptor = message.GetDescriptor();
  2185. MapReflectionTester reflection_tester(descriptor);
  2186. MapTestUtil::SetMapFields(&message);
  2187. MapTestUtil::ExpectMapsSize(message, 2);
  2188. reflection_tester.ReleaseLastMapsViaReflection(&message);
  2189. MapTestUtil::ExpectMapsSize(message, 1);
  2190. // Now test that we actually release the right message.
  2191. message.Clear();
  2192. MapTestUtil::SetMapFields(&message);
  2193. MapTestUtil::ExpectMapsSize(message, 2);
  2194. std::vector<const Message*> expect_last =
  2195. MapTestUtil::GetMapEntries(message, 1);
  2196. std::vector<const Message*> release_last =
  2197. MapTestUtil::GetMapEntriesFromRelease(&message);
  2198. MapTestUtil::ExpectMapsSize(message, 1);
  2199. EXPECT_TRUE(expect_last == release_last);
  2200. for (std::vector<const Message*>::iterator it = release_last.begin();
  2201. it != release_last.end(); ++it) {
  2202. delete *it;
  2203. }
  2204. }
  2205. TEST(GeneratedMapFieldReflectionTest, SwapElements) {
  2206. unittest::TestMap message;
  2207. MapReflectionTester reflection_tester(
  2208. unittest::TestMap::descriptor());
  2209. MapTestUtil::SetMapFields(&message);
  2210. // Get pointers of map entries at their original position
  2211. std::vector<const Message*> entries0 = MapTestUtil::GetMapEntries(message, 0);
  2212. std::vector<const Message*> entries1 = MapTestUtil::GetMapEntries(message, 1);
  2213. // Swap the first time.
  2214. reflection_tester.SwapMapsViaReflection(&message);
  2215. // Get pointer of map entry after swap once.
  2216. std::vector<const Message*> entries0_once =
  2217. MapTestUtil::GetMapEntries(message, 0);
  2218. std::vector<const Message*> entries1_once =
  2219. MapTestUtil::GetMapEntries(message, 1);
  2220. // Test map entries are swapped.
  2221. MapTestUtil::ExpectMapsSize(message, 2);
  2222. EXPECT_TRUE(entries0 == entries1_once);
  2223. EXPECT_TRUE(entries1 == entries0_once);
  2224. // Swap the second time.
  2225. reflection_tester.SwapMapsViaReflection(&message);
  2226. // Get pointer of map entry after swap once.
  2227. std::vector<const Message*> entries0_twice =
  2228. MapTestUtil::GetMapEntries(message, 0);
  2229. std::vector<const Message*> entries1_twice =
  2230. MapTestUtil::GetMapEntries(message, 1);
  2231. // Test map entries are swapped back.
  2232. MapTestUtil::ExpectMapsSize(message, 2);
  2233. EXPECT_TRUE(entries0 == entries0_twice);
  2234. EXPECT_TRUE(entries1 == entries1_twice);
  2235. }
  2236. TEST(GeneratedMapFieldReflectionTest, MutableUnknownFields) {
  2237. unittest::TestMap message;
  2238. MapReflectionTester reflection_tester(
  2239. unittest::TestMap::descriptor());
  2240. reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
  2241. }
  2242. TEST(GeneratedMapFieldReflectionTest, EmbedProto2Message) {
  2243. unittest::TestMessageMap message;
  2244. const FieldDescriptor* map_field =
  2245. unittest::TestMessageMap::descriptor()->FindFieldByName(
  2246. "map_int32_message");
  2247. const FieldDescriptor* value =
  2248. map_field->message_type()->FindFieldByName("value");
  2249. Message* entry_message =
  2250. message.GetReflection()->AddMessage(&message, map_field);
  2251. EXPECT_EQ(
  2252. &entry_message->GetReflection()->GetMessage(*entry_message, value),
  2253. reinterpret_cast<const Message*>(&TestAllTypes::default_instance()));
  2254. Message* proto2_message =
  2255. entry_message->GetReflection()->MutableMessage(entry_message, value);
  2256. EXPECT_EQ(unittest::TestAllTypes::descriptor(),
  2257. proto2_message->GetDescriptor());
  2258. ASSERT_EQ(1, message.map_int32_message().size());
  2259. }
  2260. TEST(GeneratedMapFieldReflectionTest, MergeFromClearMapEntry) {
  2261. unittest::TestMap message;
  2262. const FieldDescriptor* map_field =
  2263. unittest::TestMap::descriptor()->FindFieldByName("map_int32_int32");
  2264. const FieldDescriptor* key =
  2265. map_field->message_type()->FindFieldByName("key");
  2266. const FieldDescriptor* value =
  2267. map_field->message_type()->FindFieldByName("value");
  2268. Message* entry_message1 =
  2269. message.GetReflection()->AddMessage(&message, map_field);
  2270. EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
  2271. EXPECT_FALSE(
  2272. entry_message1->GetReflection()->HasField(*entry_message1, value));
  2273. Message* entry_message2 =
  2274. message.GetReflection()->AddMessage(&message, map_field);
  2275. EXPECT_FALSE(entry_message2->GetReflection()->HasField(*entry_message2, key));
  2276. EXPECT_FALSE(
  2277. entry_message2->GetReflection()->HasField(*entry_message2, value));
  2278. entry_message1->MergeFrom(*entry_message2);
  2279. EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
  2280. EXPECT_FALSE(
  2281. entry_message1->GetReflection()->HasField(*entry_message1, value));
  2282. }
  2283. TEST(GeneratedMapFieldReflectionTest, MapEntryClear) {
  2284. unittest::TestMap message;
  2285. MapReflectionTester reflection_tester(
  2286. unittest::TestMap::descriptor());
  2287. reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
  2288. }
  2289. TEST(GeneratedMapFieldReflectionTest, Proto2MapEntryClear) {
  2290. unittest::TestEnumMap message;
  2291. const Descriptor* descriptor = message.GetDescriptor();
  2292. const FieldDescriptor* field_descriptor =
  2293. descriptor->FindFieldByName("known_map_field");
  2294. const FieldDescriptor* value_descriptor =
  2295. field_descriptor->message_type()->FindFieldByName("value");
  2296. Message* sub_message =
  2297. message.GetReflection()->AddMessage(&message, field_descriptor);
  2298. EXPECT_EQ(0, sub_message->GetReflection()->GetEnumValue(*sub_message,
  2299. value_descriptor));
  2300. }
  2301. // Map Reflection API Test =========================================
  2302. TEST(GeneratedMapFieldReflectionTest, SetViaMapReflection) {
  2303. unittest::TestMap message;
  2304. MapReflectionTester reflection_tester(
  2305. unittest::TestMap::descriptor());
  2306. reflection_tester.SetMapFieldsViaMapReflection(&message);
  2307. reflection_tester.ExpectMapFieldsSetViaReflection(message);
  2308. reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message);
  2309. }
  2310. // Dynamic Message Test =============================================
  2311. class MapFieldInDynamicMessageTest : public testing::Test {
  2312. protected:
  2313. const DescriptorPool* pool_;
  2314. DynamicMessageFactory factory_;
  2315. const Descriptor* map_descriptor_;
  2316. const Descriptor* recursive_map_descriptor_;
  2317. const Message* map_prototype_;
  2318. MapFieldInDynamicMessageTest()
  2319. : pool_(DescriptorPool::generated_pool()), factory_(pool_) {}
  2320. virtual void SetUp() {
  2321. map_descriptor_ =
  2322. pool_->FindMessageTypeByName("protobuf_unittest.TestMap");
  2323. recursive_map_descriptor_ =
  2324. pool_->FindMessageTypeByName("protobuf_unittest.TestRecursiveMapMessage");
  2325. ASSERT_TRUE(map_descriptor_ != NULL);
  2326. ASSERT_TRUE(recursive_map_descriptor_ != NULL);
  2327. map_prototype_ = factory_.GetPrototype(map_descriptor_);
  2328. }
  2329. };
  2330. TEST_F(MapFieldInDynamicMessageTest, MapIndependentOffsets) {
  2331. // Check that all fields have independent offsets by setting each
  2332. // one to a unique value then checking that they all still have those
  2333. // unique values (i.e. they don't stomp each other).
  2334. std::unique_ptr<Message> message(map_prototype_->New());
  2335. MapReflectionTester reflection_tester(map_descriptor_);
  2336. reflection_tester.SetMapFieldsViaReflection(message.get());
  2337. reflection_tester.ExpectMapFieldsSetViaReflection(*message);
  2338. }
  2339. TEST_F(MapFieldInDynamicMessageTest, DynamicMapReflection) {
  2340. // Check that map fields work properly.
  2341. std::unique_ptr<Message> message(map_prototype_->New());
  2342. // Check set functions.
  2343. MapReflectionTester reflection_tester(map_descriptor_);
  2344. reflection_tester.SetMapFieldsViaMapReflection(message.get());
  2345. reflection_tester.ExpectMapFieldsSetViaReflection(*message);
  2346. }
  2347. TEST_F(MapFieldInDynamicMessageTest, MapSpaceUsed) {
  2348. // Test that SpaceUsed() works properly
  2349. // Since we share the implementation with generated messages, we don't need
  2350. // to test very much here. Just make sure it appears to be working.
  2351. std::unique_ptr<Message> message(map_prototype_->New());
  2352. MapReflectionTester reflection_tester(map_descriptor_);
  2353. int initial_space_used = message->SpaceUsed();
  2354. reflection_tester.SetMapFieldsViaReflection(message.get());
  2355. EXPECT_LT(initial_space_used, message->SpaceUsed());
  2356. }
  2357. TEST_F(MapFieldInDynamicMessageTest, RecursiveMap) {
  2358. TestRecursiveMapMessage from;
  2359. (*from.mutable_a())[""];
  2360. string data = from.SerializeAsString();
  2361. std::unique_ptr<Message> to(
  2362. factory_.GetPrototype(recursive_map_descriptor_)->New());
  2363. ASSERT_TRUE(to->ParseFromString(data));
  2364. }
  2365. TEST_F(MapFieldInDynamicMessageTest, MapValueReferernceValidAfterSerialize) {
  2366. std::unique_ptr<Message> message(map_prototype_->New());
  2367. MapReflectionTester reflection_tester(map_descriptor_);
  2368. reflection_tester.SetMapFieldsViaMapReflection(message.get());
  2369. // Get value reference before serialization, so that we know the value is from
  2370. // map.
  2371. MapKey map_key;
  2372. MapValueRef map_val;
  2373. map_key.SetInt32Value(0);
  2374. reflection_tester.GetMapValueViaMapReflection(
  2375. message.get(), "map_int32_foreign_message", map_key, &map_val);
  2376. Message* submsg = map_val.MutableMessageValue();
  2377. // In previous implementation, calling SerializeToString will cause syncing
  2378. // from map to repeated field, which will invalidate the submsg we previously
  2379. // got.
  2380. string data;
  2381. message->SerializeToString(&data);
  2382. const Reflection* submsg_reflection = submsg->GetReflection();
  2383. const Descriptor* submsg_desc = submsg->GetDescriptor();
  2384. const FieldDescriptor* submsg_field = submsg_desc->FindFieldByName("c");
  2385. submsg_reflection->SetInt32(submsg, submsg_field, 128);
  2386. message->SerializeToString(&data);
  2387. TestMap to;
  2388. to.ParseFromString(data);
  2389. EXPECT_EQ(128, to.map_int32_foreign_message().at(0).c());
  2390. }
  2391. TEST_F(MapFieldInDynamicMessageTest, MapEntryReferernceValidAfterSerialize) {
  2392. std::unique_ptr<Message> message(map_prototype_->New());
  2393. MapReflectionTester reflection_tester(map_descriptor_);
  2394. reflection_tester.SetMapFieldsViaReflection(message.get());
  2395. // Get map entry before serialization, so that we know the it is from
  2396. // repeated field.
  2397. Message* map_entry = reflection_tester.GetMapEntryViaReflection(
  2398. message.get(), "map_int32_foreign_message", 0);
  2399. const Reflection* map_entry_reflection = map_entry->GetReflection();
  2400. const Descriptor* map_entry_desc = map_entry->GetDescriptor();
  2401. const FieldDescriptor* value_field = map_entry_desc->FindFieldByName("value");
  2402. Message* submsg =
  2403. map_entry_reflection->MutableMessage(map_entry, value_field);
  2404. // In previous implementation, calling SerializeToString will cause syncing
  2405. // from repeated field to map, which will invalidate the map_entry we
  2406. // previously got.
  2407. string data;
  2408. message->SerializeToString(&data);
  2409. const Reflection* submsg_reflection = submsg->GetReflection();
  2410. const Descriptor* submsg_desc = submsg->GetDescriptor();
  2411. const FieldDescriptor* submsg_field = submsg_desc->FindFieldByName("c");
  2412. submsg_reflection->SetInt32(submsg, submsg_field, 128);
  2413. message->SerializeToString(&data);
  2414. TestMap to;
  2415. to.ParseFromString(data);
  2416. EXPECT_EQ(128, to.map_int32_foreign_message().at(0).c());
  2417. }
  2418. // ReflectionOps Test ===============================================
  2419. TEST(ReflectionOpsForMapFieldTest, MapSanityCheck) {
  2420. unittest::TestMap message;
  2421. MapTestUtil::SetMapFields(&message);
  2422. MapTestUtil::ExpectMapFieldsSet(message);
  2423. }
  2424. TEST(ReflectionOpsForMapFieldTest, MapCopy) {
  2425. unittest::TestMap message, message2;
  2426. MapTestUtil::SetMapFields(&message);
  2427. ReflectionOps::Copy(message, &message2);
  2428. MapTestUtil::ExpectMapFieldsSet(message2);
  2429. // Copying from self should be a no-op.
  2430. ReflectionOps::Copy(message2, &message2);
  2431. MapTestUtil::ExpectMapFieldsSet(message2);
  2432. }
  2433. TEST(ReflectionOpsForMapFieldTest, MergeMap) {
  2434. // Note: Copy is implemented in terms of Merge() so technically the Copy
  2435. // test already tested most of this.
  2436. unittest::TestMap message, message2;
  2437. MapTestUtil::SetMapFields(&message);
  2438. ReflectionOps::Merge(message2, &message);
  2439. MapTestUtil::ExpectMapFieldsSet(message);
  2440. }
  2441. TEST(ReflectionOpsForMapFieldTest, ClearMap) {
  2442. unittest::TestMap message;
  2443. MapTestUtil::SetMapFields(&message);
  2444. ReflectionOps::Clear(&message);
  2445. MapTestUtil::ExpectClear(message);
  2446. }
  2447. TEST(ReflectionOpsForMapFieldTest, MapDiscardUnknownFields) {
  2448. unittest::TestMap message;
  2449. MapTestUtil::SetMapFields(&message);
  2450. // Set some unknown fields in message.
  2451. message.GetReflection()->MutableUnknownFields(&message)->
  2452. AddVarint(123456, 654321);
  2453. // Discard them.
  2454. ReflectionOps::DiscardUnknownFields(&message);
  2455. MapTestUtil::ExpectMapFieldsSet(message);
  2456. EXPECT_EQ(0, message.GetReflection()->
  2457. GetUnknownFields(message).field_count());
  2458. }
  2459. TEST(ReflectionOpsForMapFieldTest, IsInitialized) {
  2460. unittest::TestRequiredMessageMap map_message;
  2461. // Add an uninitialized message.
  2462. (*map_message.mutable_map_field())[0];
  2463. EXPECT_FALSE(ReflectionOps::IsInitialized(map_message));
  2464. // Initialize uninitialized message
  2465. (*map_message.mutable_map_field())[0].set_a(0);
  2466. (*map_message.mutable_map_field())[0].set_b(0);
  2467. (*map_message.mutable_map_field())[0].set_c(0);
  2468. EXPECT_TRUE(ReflectionOps::IsInitialized(map_message));
  2469. }
  2470. // Wire Format Test =================================================
  2471. TEST(WireFormatForMapFieldTest, ParseMap) {
  2472. unittest::TestMap source, dest;
  2473. string data;
  2474. // Serialize using the generated code.
  2475. MapTestUtil::SetMapFields(&source);
  2476. source.SerializeToString(&data);
  2477. // Parse using WireFormat.
  2478. io::ArrayInputStream raw_input(data.data(), data.size());
  2479. io::CodedInputStream input(&raw_input);
  2480. WireFormat::ParseAndMergePartial(&input, &dest);
  2481. // Check.
  2482. MapTestUtil::ExpectMapFieldsSet(dest);
  2483. }
  2484. TEST(WireFormatForMapFieldTest, MapByteSize) {
  2485. unittest::TestMap message;
  2486. MapTestUtil::SetMapFields(&message);
  2487. EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
  2488. message.Clear();
  2489. EXPECT_EQ(0, message.ByteSize());
  2490. EXPECT_EQ(0, WireFormat::ByteSize(message));
  2491. }
  2492. TEST(WireFormatForMapFieldTest, SerializeMap) {
  2493. unittest::TestMap message;
  2494. string generated_data;
  2495. string dynamic_data;
  2496. MapTestUtil::SetMapFields(&message);
  2497. // Serialize using the generated code.
  2498. {
  2499. message.ByteSize();
  2500. io::StringOutputStream raw_output(&generated_data);
  2501. io::CodedOutputStream output(&raw_output);
  2502. message.SerializeWithCachedSizes(&output);
  2503. ASSERT_FALSE(output.HadError());
  2504. }
  2505. // Serialize using WireFormat.
  2506. {
  2507. io::StringOutputStream raw_output(&dynamic_data);
  2508. io::CodedOutputStream output(&raw_output);
  2509. int size = WireFormat::ByteSize(message);
  2510. WireFormat::SerializeWithCachedSizes(message, size, &output);
  2511. ASSERT_FALSE(output.HadError());
  2512. }
  2513. // Should be the same.
  2514. // Don't use EXPECT_EQ here because we're comparing raw binary data and
  2515. // we really don't want it dumped to stdout on failure.
  2516. EXPECT_TRUE(dynamic_data == generated_data);
  2517. }
  2518. TEST(WireFormatForMapFieldTest, SerializeMapDynamicMessage) {
  2519. DynamicMessageFactory factory;
  2520. std::unique_ptr<Message> dynamic_message;
  2521. dynamic_message.reset(
  2522. factory.GetPrototype(unittest::TestMap::descriptor())->New());
  2523. MapReflectionTester reflection_tester(
  2524. unittest::TestMap::descriptor());
  2525. reflection_tester.SetMapFieldsViaReflection(dynamic_message.get());
  2526. reflection_tester.ExpectMapFieldsSetViaReflection(*dynamic_message);
  2527. unittest::TestMap generated_message;
  2528. MapTestUtil::SetMapFields(&generated_message);
  2529. MapTestUtil::ExpectMapFieldsSet(generated_message);
  2530. string generated_data;
  2531. string dynamic_data;
  2532. // Serialize.
  2533. generated_message.SerializeToString(&generated_data);
  2534. dynamic_message->SerializeToString(&dynamic_data);
  2535. // Because map serialization doesn't guarantee order, we just compare
  2536. // serialized size here. This is enough to tell dynamic message doesn't miss
  2537. // anything in serialization.
  2538. EXPECT_TRUE(dynamic_data.size() == generated_data.size());
  2539. }
  2540. TEST(WireFormatForMapFieldTest, MapParseHelpers) {
  2541. string data;
  2542. {
  2543. // Set up.
  2544. protobuf_unittest::TestMap message;
  2545. MapTestUtil::SetMapFields(&message);
  2546. message.SerializeToString(&data);
  2547. }
  2548. {
  2549. // Test ParseFromString.
  2550. protobuf_unittest::TestMap message;
  2551. EXPECT_TRUE(message.ParseFromString(data));
  2552. MapTestUtil::ExpectMapFieldsSet(message);
  2553. }
  2554. {
  2555. // Test ParseFromIstream.
  2556. protobuf_unittest::TestMap message;
  2557. std::stringstream stream(data);
  2558. EXPECT_TRUE(message.ParseFromIstream(&stream));
  2559. EXPECT_TRUE(stream.eof());
  2560. MapTestUtil::ExpectMapFieldsSet(message);
  2561. }
  2562. {
  2563. // Test ParseFromBoundedZeroCopyStream.
  2564. string data_with_junk(data);
  2565. data_with_junk.append("some junk on the end");
  2566. io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size());
  2567. protobuf_unittest::TestMap message;
  2568. EXPECT_TRUE(message.ParseFromBoundedZeroCopyStream(&stream, data.size()));
  2569. MapTestUtil::ExpectMapFieldsSet(message);
  2570. }
  2571. {
  2572. // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if
  2573. // EOF is reached before the expected number of bytes.
  2574. io::ArrayInputStream stream(data.data(), data.size());
  2575. protobuf_unittest::TestAllTypes message;
  2576. EXPECT_FALSE(
  2577. message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1));
  2578. }
  2579. }
  2580. // Deterministic Serialization Test ==========================================
  2581. template <typename T>
  2582. static string DeterministicSerializationWithSerializePartialToCodedStream(
  2583. const T& t) {
  2584. const int size = t.ByteSize();
  2585. string result(size, '\0');
  2586. io::ArrayOutputStream array_stream(string_as_array(&result), size);
  2587. io::CodedOutputStream output_stream(&array_stream);
  2588. output_stream.SetSerializationDeterministic(true);
  2589. t.SerializePartialToCodedStream(&output_stream);
  2590. EXPECT_FALSE(output_stream.HadError());
  2591. EXPECT_EQ(size, output_stream.ByteCount());
  2592. return result;
  2593. }
  2594. template <typename T>
  2595. static string DeterministicSerializationWithSerializeToCodedStream(const T& t) {
  2596. const int size = t.ByteSize();
  2597. string result(size, '\0');
  2598. io::ArrayOutputStream array_stream(string_as_array(&result), size);
  2599. io::CodedOutputStream output_stream(&array_stream);
  2600. output_stream.SetSerializationDeterministic(true);
  2601. t.SerializeToCodedStream(&output_stream);
  2602. EXPECT_FALSE(output_stream.HadError());
  2603. EXPECT_EQ(size, output_stream.ByteCount());
  2604. return result;
  2605. }
  2606. template <typename T>
  2607. static string DeterministicSerialization(const T& t) {
  2608. const int size = t.ByteSize();
  2609. string result(size, '\0');
  2610. io::ArrayOutputStream array_stream(string_as_array(&result), size);
  2611. io::CodedOutputStream output_stream(&array_stream);
  2612. output_stream.SetSerializationDeterministic(true);
  2613. t.SerializeWithCachedSizes(&output_stream);
  2614. EXPECT_FALSE(output_stream.HadError());
  2615. EXPECT_EQ(size, output_stream.ByteCount());
  2616. EXPECT_EQ(result, DeterministicSerializationWithSerializeToCodedStream(t));
  2617. EXPECT_EQ(result,
  2618. DeterministicSerializationWithSerializePartialToCodedStream(t));
  2619. return result;
  2620. }
  2621. // Helper to test the serialization of the first arg against a golden file.
  2622. static void TestDeterministicSerialization(const protobuf_unittest::TestMaps& t,
  2623. const string& filename) {
  2624. string expected;
  2625. GOOGLE_CHECK_OK(File::GetContents(
  2626. TestSourceDir() + "/google/protobuf/testdata/" + filename,
  2627. &expected, true));
  2628. const string actual = DeterministicSerialization(t);
  2629. EXPECT_EQ(expected, actual);
  2630. protobuf_unittest::TestMaps u;
  2631. EXPECT_TRUE(u.ParseFromString(actual));
  2632. EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(u, t));
  2633. }
  2634. // Helper for MapSerializationTest. Return a 7-bit ASCII string.
  2635. static string ConstructKey(uint64 n) {
  2636. string s(n % static_cast<uint64>(9), '\0');
  2637. if (s.empty()) {
  2638. return StrCat(n);
  2639. } else {
  2640. while (n != 0) {
  2641. s[n % s.size()] = (n >> 10) & 0x7f;
  2642. n /= 888;
  2643. }
  2644. return s;
  2645. }
  2646. }
  2647. TEST(MapSerializationTest, Deterministic) {
  2648. const int kIters = 25;
  2649. protobuf_unittest::TestMaps t;
  2650. protobuf_unittest::TestIntIntMap inner;
  2651. (*inner.mutable_m())[0] = (*inner.mutable_m())[10] =
  2652. (*inner.mutable_m())[-200] = 0;
  2653. uint64 frog = 9;
  2654. const uint64 multiplier = 0xa29cd16f;
  2655. for (int i = 0; i < kIters; i++) {
  2656. const int32 i32 = static_cast<int32>(frog & 0xffffffff);
  2657. const uint32 u32 = static_cast<uint32>(i32) * 91919;
  2658. const int64 i64 = static_cast<int64>(frog);
  2659. const uint64 u64 = frog * static_cast<uint64>(187321);
  2660. const bool b = i32 > 0;
  2661. const string s = ConstructKey(frog);
  2662. (*inner.mutable_m())[i] = i32;
  2663. (*t.mutable_m_int32())[i32] = (*t.mutable_m_sint32())[i32] =
  2664. (*t.mutable_m_sfixed32())[i32] = inner;
  2665. (*t.mutable_m_uint32())[u32] = (*t.mutable_m_fixed32())[u32] = inner;
  2666. (*t.mutable_m_int64())[i64] = (*t.mutable_m_sint64())[i64] =
  2667. (*t.mutable_m_sfixed64())[i64] = inner;
  2668. (*t.mutable_m_uint64())[u64] = (*t.mutable_m_fixed64())[u64] = inner;
  2669. (*t.mutable_m_bool())[b] = inner;
  2670. (*t.mutable_m_string())[s] = inner;
  2671. (*t.mutable_m_string())[s + string(1 << (u32 % static_cast<uint32>(9)),
  2672. b)] = inner;
  2673. inner.mutable_m()->erase(i);
  2674. frog = frog * multiplier + i;
  2675. frog ^= (frog >> 41);
  2676. }
  2677. TestDeterministicSerialization(t, "golden_message_maps");
  2678. }
  2679. TEST(MapSerializationTest, DeterministicSubmessage) {
  2680. protobuf_unittest::TestSubmessageMaps p;
  2681. protobuf_unittest::TestMaps t;
  2682. const string filename = "golden_message_maps";
  2683. string golden;
  2684. GOOGLE_CHECK_OK(File::GetContents(
  2685. TestSourceDir() + "/google/protobuf/testdata/" + filename,
  2686. &golden, true));
  2687. t.ParseFromString(golden);
  2688. *(p.mutable_m()) = t;
  2689. std::vector<string> v;
  2690. // Use multiple attempts to increase the chance of a failure if something is
  2691. // buggy. For example, each separate copy of a map might use a different
  2692. // randomly-chosen hash function.
  2693. const int kAttempts = 10;
  2694. for (int i = 0; i < kAttempts; i++) {
  2695. protobuf_unittest::TestSubmessageMaps q(p);
  2696. ASSERT_EQ(DeterministicSerialization(q), DeterministicSerialization(p));
  2697. }
  2698. }
  2699. // Text Format Test =================================================
  2700. TEST(TextFormatMapTest, SerializeAndParse) {
  2701. unittest::TestMap source;
  2702. unittest::TestMap dest;
  2703. MapTestUtil::SetMapFields(&source);
  2704. string output;
  2705. // Test compact ASCII
  2706. TextFormat::Printer printer;
  2707. printer.PrintToString(source, &output);
  2708. TextFormat::Parser parser;
  2709. EXPECT_TRUE(parser.ParseFromString(output, &dest));
  2710. MapTestUtil::ExpectMapFieldsSet(dest);
  2711. }
  2712. TEST(TextFormatMapTest, Sorted) {
  2713. unittest::TestMap message;
  2714. MapReflectionTester tester(message.GetDescriptor());
  2715. tester.SetMapFieldsViaReflection(&message);
  2716. string expected_text;
  2717. GOOGLE_CHECK_OK(File::GetContents(
  2718. TestSourceDir() +
  2719. "/google/protobuf/"
  2720. "testdata/map_test_data.txt",
  2721. &expected_text, true));
  2722. EXPECT_EQ(message.DebugString(), expected_text);
  2723. // Test again on the reverse order.
  2724. unittest::TestMap message2;
  2725. tester.SetMapFieldsViaReflection(&message2);
  2726. tester.SwapMapsViaReflection(&message2);
  2727. EXPECT_EQ(message2.DebugString(), expected_text);
  2728. }
  2729. TEST(TextFormatMapTest, ParseCorruptedString) {
  2730. string serialized_message;
  2731. GOOGLE_CHECK_OK(File::GetContents(
  2732. TestSourceDir() +
  2733. "/google/protobuf/testdata/golden_message_maps",
  2734. &serialized_message, true));
  2735. protobuf_unittest::TestMaps message;
  2736. GOOGLE_CHECK(message.ParseFromString(serialized_message));
  2737. TestParseCorruptedString<protobuf_unittest::TestMaps, true>(message);
  2738. TestParseCorruptedString<protobuf_unittest::TestMaps, false>(message);
  2739. }
  2740. // arena support =================================================
  2741. TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) {
  2742. // Allocate a large initial block to avoid mallocs during hooked test.
  2743. std::vector<char> arena_block(128 * 1024);
  2744. ArenaOptions options;
  2745. options.initial_block = &arena_block[0];
  2746. options.initial_block_size = arena_block.size();
  2747. Arena arena(options);
  2748. string data;
  2749. data.reserve(128 * 1024);
  2750. {
  2751. // TODO(teboring): Enable no heap check when ArenaStringPtr is used in map.
  2752. // NoHeapChecker no_heap;
  2753. unittest::TestArenaMap* from =
  2754. Arena::CreateMessage<unittest::TestArenaMap>(&arena);
  2755. MapTestUtil::SetArenaMapFields(from);
  2756. from->SerializeToString(&data);
  2757. unittest::TestArenaMap* to =
  2758. Arena::CreateMessage<unittest::TestArenaMap>(&arena);
  2759. to->ParseFromString(data);
  2760. MapTestUtil::ExpectArenaMapFieldsSet(*to);
  2761. }
  2762. }
  2763. // Use text format parsing and serializing to test reflection api.
  2764. TEST(ArenaTest, ReflectionInTextFormat) {
  2765. Arena arena;
  2766. string data;
  2767. TextFormat::Printer printer;
  2768. TextFormat::Parser parser;
  2769. unittest::TestArenaMap* from =
  2770. Arena::CreateMessage<unittest::TestArenaMap>(&arena);
  2771. unittest::TestArenaMap* to =
  2772. Arena::CreateMessage<unittest::TestArenaMap>(&arena);
  2773. MapTestUtil::SetArenaMapFields(from);
  2774. printer.PrintToString(*from, &data);
  2775. EXPECT_TRUE(parser.ParseFromString(data, to));
  2776. MapTestUtil::ExpectArenaMapFieldsSet(*to);
  2777. }
  2778. // Make sure the memory allocated for string in map is deallocated.
  2779. TEST(ArenaTest, StringMapNoLeak) {
  2780. Arena arena;
  2781. unittest::TestArenaMap* message =
  2782. Arena::CreateMessage<unittest::TestArenaMap>(&arena);
  2783. string data;
  2784. // String with length less than 16 will not be allocated from heap.
  2785. int original_capacity = data.capacity();
  2786. while (data.capacity() <= original_capacity) {
  2787. data.append("a");
  2788. }
  2789. (*message->mutable_map_string_string())[data] = data;
  2790. // We rely on heap checkers to detect memory leak for us.
  2791. ASSERT_FALSE(message == NULL);
  2792. }
  2793. TEST(ArenaTest, IsInitialized) {
  2794. // Allocate a large initial polluted block.
  2795. std::vector<char> arena_block(128 * 1024);
  2796. std::fill(arena_block.begin(), arena_block.end(), '\xff');
  2797. ArenaOptions options;
  2798. options.initial_block = &arena_block[0];
  2799. options.initial_block_size = arena_block.size();
  2800. Arena arena(options);
  2801. unittest::TestArenaMap* message =
  2802. Arena::CreateMessage<unittest::TestArenaMap>(&arena);
  2803. EXPECT_EQ(0, (*message->mutable_map_int32_int32())[0]);
  2804. }
  2805. TEST(MoveTest, MoveConstructorWorks) {
  2806. Map<int32, TestAllTypes> original_map;
  2807. original_map[42].mutable_optional_nested_message()->set_bb(42);
  2808. original_map[43].mutable_optional_nested_message()->set_bb(43);
  2809. const auto* nested_msg42_ptr = &original_map[42].optional_nested_message();
  2810. const auto* nested_msg43_ptr = &original_map[43].optional_nested_message();
  2811. Map<int32, TestAllTypes> moved_to_map(std::move(original_map));
  2812. EXPECT_TRUE(original_map.empty());
  2813. EXPECT_EQ(2, moved_to_map.size());
  2814. EXPECT_EQ(42, moved_to_map[42].optional_nested_message().bb());
  2815. EXPECT_EQ(43, moved_to_map[43].optional_nested_message().bb());
  2816. // This test takes advantage of the fact that pointers are swapped, so there
  2817. // should be pointer stability.
  2818. EXPECT_EQ(nested_msg42_ptr, &moved_to_map[42].optional_nested_message());
  2819. EXPECT_EQ(nested_msg43_ptr, &moved_to_map[43].optional_nested_message());
  2820. }
  2821. TEST(MoveTest, MoveAssignmentWorks) {
  2822. Map<int32, TestAllTypes> original_map;
  2823. original_map[42].mutable_optional_nested_message()->set_bb(42);
  2824. original_map[43].mutable_optional_nested_message()->set_bb(43);
  2825. const auto* nested_msg42_ptr = &original_map[42].optional_nested_message();
  2826. const auto* nested_msg43_ptr = &original_map[43].optional_nested_message();
  2827. Map<int32, TestAllTypes> moved_to_map = std::move(original_map);
  2828. EXPECT_TRUE(original_map.empty());
  2829. EXPECT_EQ(2, moved_to_map.size());
  2830. EXPECT_EQ(42, moved_to_map[42].optional_nested_message().bb());
  2831. EXPECT_EQ(43, moved_to_map[43].optional_nested_message().bb());
  2832. // This test takes advantage of the fact that pointers are swapped, so there
  2833. // should be pointer stability.
  2834. EXPECT_EQ(nested_msg42_ptr, &moved_to_map[42].optional_nested_message());
  2835. EXPECT_EQ(nested_msg43_ptr, &moved_to_map[43].optional_nested_message());
  2836. }
  2837. } // namespace internal
  2838. } // namespace protobuf
  2839. } // namespace google