map_test_util.cc 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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. #include <google/protobuf/map_test_util.h>
  31. #include <google/protobuf/map_test_util_impl.h>
  32. #include <google/protobuf/descriptor.h>
  33. #include <google/protobuf/message.h>
  34. namespace google {
  35. namespace protobuf {
  36. void MapTestUtil::SetMapFields(unittest::TestMap* message) {
  37. MapTestUtilImpl::SetMapFields<unittest::MapEnum, unittest::MAP_ENUM_BAR,
  38. unittest::MAP_ENUM_BAZ>(message);
  39. }
  40. void MapTestUtil::SetArenaMapFields(unittest::TestArenaMap* message) {
  41. MapTestUtilImpl::SetArenaMapFields<unittest::MapEnum, unittest::MAP_ENUM_BAR,
  42. unittest::MAP_ENUM_BAZ>(message);
  43. }
  44. void MapTestUtil::SetMapFieldsInitialized(unittest::TestMap* message) {
  45. MapTestUtilImpl::SetMapFieldsInitialized(message);
  46. }
  47. void MapTestUtil::ModifyMapFields(unittest::TestMap* message) {
  48. MapTestUtilImpl::ModifyMapFields<unittest::MapEnum, unittest::MAP_ENUM_FOO>(
  49. message);
  50. }
  51. void MapTestUtil::ExpectClear(const unittest::TestMap& message) {
  52. MapTestUtilImpl::ExpectClear(message);
  53. }
  54. void MapTestUtil::ExpectMapFieldsSet(const unittest::TestMap& message) {
  55. MapTestUtilImpl::ExpectMapFieldsSet<unittest::MapEnum, unittest::MAP_ENUM_BAR,
  56. unittest::MAP_ENUM_BAZ>(message);
  57. }
  58. void MapTestUtil::ExpectArenaMapFieldsSet(
  59. const unittest::TestArenaMap& message) {
  60. MapTestUtilImpl::ExpectArenaMapFieldsSet<
  61. unittest::MapEnum, unittest::MAP_ENUM_BAR, unittest::MAP_ENUM_BAZ>(
  62. message);
  63. }
  64. void MapTestUtil::ExpectMapFieldsSetInitialized(
  65. const unittest::TestMap& message) {
  66. MapTestUtilImpl::ExpectMapFieldsSetInitialized<unittest::MapEnum,
  67. unittest::MAP_ENUM_FOO>(
  68. message);
  69. }
  70. void MapTestUtil::ExpectMapFieldsModified(
  71. const unittest::TestMap& message) {
  72. MapTestUtilImpl::ExpectMapFieldsModified<
  73. unittest::MapEnum, unittest::MAP_ENUM_BAR, unittest::MAP_ENUM_FOO>(
  74. message);
  75. }
  76. void MapTestUtil::ExpectMapsSize(
  77. const unittest::TestMap& message, int size) {
  78. const Descriptor* descriptor = message.GetDescriptor();
  79. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  80. message, descriptor->FindFieldByName("map_int32_int32")));
  81. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  82. message, descriptor->FindFieldByName("map_int64_int64")));
  83. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  84. message, descriptor->FindFieldByName("map_uint32_uint32")));
  85. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  86. message, descriptor->FindFieldByName("map_uint64_uint64")));
  87. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  88. message, descriptor->FindFieldByName("map_sint32_sint32")));
  89. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  90. message, descriptor->FindFieldByName("map_sint64_sint64")));
  91. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  92. message, descriptor->FindFieldByName("map_fixed32_fixed32")));
  93. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  94. message, descriptor->FindFieldByName("map_fixed64_fixed64")));
  95. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  96. message, descriptor->FindFieldByName("map_sfixed32_sfixed32")));
  97. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  98. message, descriptor->FindFieldByName("map_sfixed64_sfixed64")));
  99. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  100. message, descriptor->FindFieldByName("map_int32_float")));
  101. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  102. message, descriptor->FindFieldByName("map_int32_double")));
  103. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  104. message, descriptor->FindFieldByName("map_bool_bool")));
  105. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  106. message, descriptor->FindFieldByName("map_string_string")));
  107. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  108. message, descriptor->FindFieldByName("map_int32_bytes")));
  109. EXPECT_EQ(size, message.GetReflection()->FieldSize(
  110. message, descriptor->FindFieldByName("map_int32_foreign_message")));
  111. }
  112. std::vector<const Message*> MapTestUtil::GetMapEntries(
  113. const unittest::TestMap& message, int index) {
  114. const Descriptor* descriptor = message.GetDescriptor();
  115. std::vector<const Message*> result;
  116. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  117. message, descriptor->FindFieldByName("map_int32_int32"), index));
  118. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  119. message, descriptor->FindFieldByName("map_int64_int64"), index));
  120. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  121. message, descriptor->FindFieldByName("map_uint32_uint32"), index));
  122. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  123. message, descriptor->FindFieldByName("map_uint64_uint64"), index));
  124. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  125. message, descriptor->FindFieldByName("map_sint32_sint32"), index));
  126. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  127. message, descriptor->FindFieldByName("map_sint64_sint64"), index));
  128. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  129. message, descriptor->FindFieldByName("map_fixed32_fixed32"), index));
  130. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  131. message, descriptor->FindFieldByName("map_fixed64_fixed64"), index));
  132. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  133. message, descriptor->FindFieldByName("map_sfixed32_sfixed32"), index));
  134. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  135. message, descriptor->FindFieldByName("map_sfixed64_sfixed64"), index));
  136. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  137. message, descriptor->FindFieldByName("map_int32_float"), index));
  138. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  139. message, descriptor->FindFieldByName("map_int32_double"), index));
  140. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  141. message, descriptor->FindFieldByName("map_bool_bool"), index));
  142. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  143. message, descriptor->FindFieldByName("map_string_string"), index));
  144. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  145. message, descriptor->FindFieldByName("map_int32_bytes"), index));
  146. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  147. message, descriptor->FindFieldByName("map_int32_enum"), index));
  148. result.push_back(&message.GetReflection()->GetRepeatedMessage(
  149. message, descriptor->FindFieldByName("map_int32_foreign_message"), index));
  150. return result;
  151. }
  152. std::vector<const Message*> MapTestUtil::GetMapEntriesFromRelease(
  153. unittest::TestMap* message) {
  154. const Descriptor* descriptor = message->GetDescriptor();
  155. std::vector<const Message*> result;
  156. result.push_back(message->GetReflection()->ReleaseLast(
  157. message, descriptor->FindFieldByName("map_int32_int32")));
  158. result.push_back(message->GetReflection()->ReleaseLast(
  159. message, descriptor->FindFieldByName("map_int64_int64")));
  160. result.push_back(message->GetReflection()->ReleaseLast(
  161. message, descriptor->FindFieldByName("map_uint32_uint32")));
  162. result.push_back(message->GetReflection()->ReleaseLast(
  163. message, descriptor->FindFieldByName("map_uint64_uint64")));
  164. result.push_back(message->GetReflection()->ReleaseLast(
  165. message, descriptor->FindFieldByName("map_sint32_sint32")));
  166. result.push_back(message->GetReflection()->ReleaseLast(
  167. message, descriptor->FindFieldByName("map_sint64_sint64")));
  168. result.push_back(message->GetReflection()->ReleaseLast(
  169. message, descriptor->FindFieldByName("map_fixed32_fixed32")));
  170. result.push_back(message->GetReflection()->ReleaseLast(
  171. message, descriptor->FindFieldByName("map_fixed64_fixed64")));
  172. result.push_back(message->GetReflection()->ReleaseLast(
  173. message, descriptor->FindFieldByName("map_sfixed32_sfixed32")));
  174. result.push_back(message->GetReflection()->ReleaseLast(
  175. message, descriptor->FindFieldByName("map_sfixed64_sfixed64")));
  176. result.push_back(message->GetReflection()->ReleaseLast(
  177. message, descriptor->FindFieldByName("map_int32_float")));
  178. result.push_back(message->GetReflection()->ReleaseLast(
  179. message, descriptor->FindFieldByName("map_int32_double")));
  180. result.push_back(message->GetReflection()->ReleaseLast(
  181. message, descriptor->FindFieldByName("map_bool_bool")));
  182. result.push_back(message->GetReflection()->ReleaseLast(
  183. message, descriptor->FindFieldByName("map_string_string")));
  184. result.push_back(message->GetReflection()->ReleaseLast(
  185. message, descriptor->FindFieldByName("map_int32_bytes")));
  186. result.push_back(message->GetReflection()->ReleaseLast(
  187. message, descriptor->FindFieldByName("map_int32_enum")));
  188. result.push_back(message->GetReflection()->ReleaseLast(
  189. message, descriptor->FindFieldByName("map_int32_foreign_message")));
  190. return result;
  191. }
  192. MapReflectionTester::MapReflectionTester(
  193. const Descriptor* base_descriptor)
  194. : base_descriptor_(base_descriptor) {
  195. const DescriptorPool* pool = base_descriptor->file()->pool();
  196. map_enum_foo_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_FOO");
  197. map_enum_bar_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_BAR");
  198. map_enum_baz_ = pool->FindEnumValueByName("protobuf_unittest.MAP_ENUM_BAZ");
  199. foreign_c_ = pool->FindFieldByName(
  200. "protobuf_unittest.ForeignMessage.c");
  201. map_int32_int32_key_ = pool->FindFieldByName(
  202. "protobuf_unittest.TestMap.MapInt32Int32Entry.key");
  203. map_int32_int32_val_ = pool->FindFieldByName(
  204. "protobuf_unittest.TestMap.MapInt32Int32Entry.value");
  205. map_int64_int64_key_ = pool->FindFieldByName(
  206. "protobuf_unittest.TestMap.MapInt64Int64Entry.key");
  207. map_int64_int64_val_ = pool->FindFieldByName(
  208. "protobuf_unittest.TestMap.MapInt64Int64Entry.value");
  209. map_uint32_uint32_key_ = pool->FindFieldByName(
  210. "protobuf_unittest.TestMap.MapUint32Uint32Entry.key");
  211. map_uint32_uint32_val_ = pool->FindFieldByName(
  212. "protobuf_unittest.TestMap.MapUint32Uint32Entry.value");
  213. map_uint64_uint64_key_ = pool->FindFieldByName(
  214. "protobuf_unittest.TestMap.MapUint64Uint64Entry.key");
  215. map_uint64_uint64_val_ = pool->FindFieldByName(
  216. "protobuf_unittest.TestMap.MapUint64Uint64Entry.value");
  217. map_sint32_sint32_key_ = pool->FindFieldByName(
  218. "protobuf_unittest.TestMap.MapSint32Sint32Entry.key");
  219. map_sint32_sint32_val_ = pool->FindFieldByName(
  220. "protobuf_unittest.TestMap.MapSint32Sint32Entry.value");
  221. map_sint64_sint64_key_ = pool->FindFieldByName(
  222. "protobuf_unittest.TestMap.MapSint64Sint64Entry.key");
  223. map_sint64_sint64_val_ = pool->FindFieldByName(
  224. "protobuf_unittest.TestMap.MapSint64Sint64Entry.value");
  225. map_fixed32_fixed32_key_ = pool->FindFieldByName(
  226. "protobuf_unittest.TestMap.MapFixed32Fixed32Entry.key");
  227. map_fixed32_fixed32_val_ = pool->FindFieldByName(
  228. "protobuf_unittest.TestMap.MapFixed32Fixed32Entry.value");
  229. map_fixed64_fixed64_key_ = pool->FindFieldByName(
  230. "protobuf_unittest.TestMap.MapFixed64Fixed64Entry.key");
  231. map_fixed64_fixed64_val_ = pool->FindFieldByName(
  232. "protobuf_unittest.TestMap.MapFixed64Fixed64Entry.value");
  233. map_sfixed32_sfixed32_key_ = pool->FindFieldByName(
  234. "protobuf_unittest.TestMap.MapSfixed32Sfixed32Entry.key");
  235. map_sfixed32_sfixed32_val_ = pool->FindFieldByName(
  236. "protobuf_unittest.TestMap.MapSfixed32Sfixed32Entry.value");
  237. map_sfixed64_sfixed64_key_ = pool->FindFieldByName(
  238. "protobuf_unittest.TestMap.MapSfixed64Sfixed64Entry.key");
  239. map_sfixed64_sfixed64_val_ = pool->FindFieldByName(
  240. "protobuf_unittest.TestMap.MapSfixed64Sfixed64Entry.value");
  241. map_int32_float_key_ = pool->FindFieldByName(
  242. "protobuf_unittest.TestMap.MapInt32FloatEntry.key");
  243. map_int32_float_val_ = pool->FindFieldByName(
  244. "protobuf_unittest.TestMap.MapInt32FloatEntry.value");
  245. map_int32_double_key_ = pool->FindFieldByName(
  246. "protobuf_unittest.TestMap.MapInt32DoubleEntry.key");
  247. map_int32_double_val_ = pool->FindFieldByName(
  248. "protobuf_unittest.TestMap.MapInt32DoubleEntry.value");
  249. map_bool_bool_key_ = pool->FindFieldByName(
  250. "protobuf_unittest.TestMap.MapBoolBoolEntry.key");
  251. map_bool_bool_val_ = pool->FindFieldByName(
  252. "protobuf_unittest.TestMap.MapBoolBoolEntry.value");
  253. map_string_string_key_ = pool->FindFieldByName(
  254. "protobuf_unittest.TestMap.MapStringStringEntry.key");
  255. map_string_string_val_ = pool->FindFieldByName(
  256. "protobuf_unittest.TestMap.MapStringStringEntry.value");
  257. map_int32_bytes_key_ = pool->FindFieldByName(
  258. "protobuf_unittest.TestMap.MapInt32BytesEntry.key");
  259. map_int32_bytes_val_ = pool->FindFieldByName(
  260. "protobuf_unittest.TestMap.MapInt32BytesEntry.value");
  261. map_int32_enum_key_ = pool->FindFieldByName(
  262. "protobuf_unittest.TestMap.MapInt32EnumEntry.key");
  263. map_int32_enum_val_ = pool->FindFieldByName(
  264. "protobuf_unittest.TestMap.MapInt32EnumEntry.value");
  265. map_int32_foreign_message_key_ = pool->FindFieldByName(
  266. "protobuf_unittest.TestMap.MapInt32ForeignMessageEntry.key");
  267. map_int32_foreign_message_val_ = pool->FindFieldByName(
  268. "protobuf_unittest.TestMap.MapInt32ForeignMessageEntry.value");
  269. EXPECT_FALSE(map_enum_foo_ == NULL);
  270. EXPECT_FALSE(map_enum_bar_ == NULL);
  271. EXPECT_FALSE(map_enum_baz_ == NULL);
  272. EXPECT_FALSE(map_int32_int32_key_ == NULL);
  273. EXPECT_FALSE(map_int32_int32_val_ == NULL);
  274. EXPECT_FALSE(map_int64_int64_key_ == NULL);
  275. EXPECT_FALSE(map_int64_int64_val_ == NULL);
  276. EXPECT_FALSE(map_uint32_uint32_key_ == NULL);
  277. EXPECT_FALSE(map_uint32_uint32_val_ == NULL);
  278. EXPECT_FALSE(map_uint64_uint64_key_ == NULL);
  279. EXPECT_FALSE(map_uint64_uint64_val_ == NULL);
  280. EXPECT_FALSE(map_sint32_sint32_key_ == NULL);
  281. EXPECT_FALSE(map_sint32_sint32_val_ == NULL);
  282. EXPECT_FALSE(map_sint64_sint64_key_ == NULL);
  283. EXPECT_FALSE(map_sint64_sint64_val_ == NULL);
  284. EXPECT_FALSE(map_fixed32_fixed32_key_ == NULL);
  285. EXPECT_FALSE(map_fixed32_fixed32_val_ == NULL);
  286. EXPECT_FALSE(map_fixed64_fixed64_key_ == NULL);
  287. EXPECT_FALSE(map_fixed64_fixed64_val_ == NULL);
  288. EXPECT_FALSE(map_sfixed32_sfixed32_key_ == NULL);
  289. EXPECT_FALSE(map_sfixed32_sfixed32_val_ == NULL);
  290. EXPECT_FALSE(map_sfixed64_sfixed64_key_ == NULL);
  291. EXPECT_FALSE(map_sfixed64_sfixed64_val_ == NULL);
  292. EXPECT_FALSE(map_int32_float_key_ == NULL);
  293. EXPECT_FALSE(map_int32_float_val_ == NULL);
  294. EXPECT_FALSE(map_int32_double_key_ == NULL);
  295. EXPECT_FALSE(map_int32_double_val_ == NULL);
  296. EXPECT_FALSE(map_bool_bool_key_ == NULL);
  297. EXPECT_FALSE(map_bool_bool_val_ == NULL);
  298. EXPECT_FALSE(map_string_string_key_ == NULL);
  299. EXPECT_FALSE(map_string_string_val_ == NULL);
  300. EXPECT_FALSE(map_int32_bytes_key_ == NULL);
  301. EXPECT_FALSE(map_int32_bytes_val_ == NULL);
  302. EXPECT_FALSE(map_int32_enum_key_ == NULL);
  303. EXPECT_FALSE(map_int32_enum_val_ == NULL);
  304. EXPECT_FALSE(map_int32_foreign_message_key_ == NULL);
  305. EXPECT_FALSE(map_int32_foreign_message_val_ == NULL);
  306. }
  307. // Shorthand to get a FieldDescriptor for a field of unittest::TestMap.
  308. const FieldDescriptor* MapReflectionTester::F(const string& name) {
  309. const FieldDescriptor* result = NULL;
  310. result = base_descriptor_->FindFieldByName(name);
  311. GOOGLE_CHECK(result != NULL);
  312. return result;
  313. }
  314. void MapReflectionTester::SetMapFieldsViaReflection(
  315. Message* message) {
  316. const Reflection* reflection = message->GetReflection();
  317. Message* sub_message = NULL;
  318. Message* sub_foreign_message = NULL;
  319. // Add first element.
  320. sub_message = reflection->AddMessage(message, F("map_int32_int32"));
  321. sub_message->GetReflection()
  322. ->SetInt32(sub_message, map_int32_int32_key_, 0);
  323. sub_message->GetReflection()
  324. ->SetInt32(sub_message, map_int32_int32_val_, 0);
  325. sub_message = reflection->AddMessage(message, F("map_int64_int64"));
  326. sub_message->GetReflection()
  327. ->SetInt64(sub_message, map_int64_int64_key_, 0);
  328. sub_message->GetReflection()
  329. ->SetInt64(sub_message, map_int64_int64_val_, 0);
  330. sub_message = reflection->AddMessage(message, F("map_uint32_uint32"));
  331. sub_message->GetReflection()
  332. ->SetUInt32(sub_message, map_uint32_uint32_key_, 0);
  333. sub_message->GetReflection()
  334. ->SetUInt32(sub_message, map_uint32_uint32_val_, 0);
  335. sub_message = reflection->AddMessage(message, F("map_uint64_uint64"));
  336. sub_message->GetReflection()
  337. ->SetUInt64(sub_message, map_uint64_uint64_key_, 0);
  338. sub_message->GetReflection()
  339. ->SetUInt64(sub_message, map_uint64_uint64_val_, 0);
  340. sub_message = reflection->AddMessage(message, F("map_sint32_sint32"));
  341. sub_message->GetReflection()
  342. ->SetInt32(sub_message, map_sint32_sint32_key_, 0);
  343. sub_message->GetReflection()
  344. ->SetInt32(sub_message, map_sint32_sint32_val_, 0);
  345. sub_message = reflection->AddMessage(message, F("map_sint64_sint64"));
  346. sub_message->GetReflection()
  347. ->SetInt64(sub_message, map_sint64_sint64_key_, 0);
  348. sub_message->GetReflection()
  349. ->SetInt64(sub_message, map_sint64_sint64_val_, 0);
  350. sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32"));
  351. sub_message->GetReflection()
  352. ->SetUInt32(sub_message, map_fixed32_fixed32_key_, 0);
  353. sub_message->GetReflection()
  354. ->SetUInt32(sub_message, map_fixed32_fixed32_val_, 0);
  355. sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64"));
  356. sub_message->GetReflection()
  357. ->SetUInt64(sub_message, map_fixed64_fixed64_key_, 0);
  358. sub_message->GetReflection()
  359. ->SetUInt64(sub_message, map_fixed64_fixed64_val_, 0);
  360. sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32"));
  361. sub_message->GetReflection()
  362. ->SetInt32(sub_message, map_sfixed32_sfixed32_key_, 0);
  363. sub_message->GetReflection()
  364. ->SetInt32(sub_message, map_sfixed32_sfixed32_val_, 0);
  365. sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64"));
  366. sub_message->GetReflection()
  367. ->SetInt64(sub_message, map_sfixed64_sfixed64_key_, 0);
  368. sub_message->GetReflection()
  369. ->SetInt64(sub_message, map_sfixed64_sfixed64_val_, 0);
  370. sub_message = reflection->AddMessage(message, F("map_int32_float"));
  371. sub_message->GetReflection()
  372. ->SetInt32(sub_message, map_int32_float_key_, 0);
  373. sub_message->GetReflection()
  374. ->SetFloat(sub_message, map_int32_float_val_, 0.0);
  375. sub_message = reflection->AddMessage(message, F("map_int32_double"));
  376. sub_message->GetReflection()
  377. ->SetInt32(sub_message, map_int32_double_key_, 0);
  378. sub_message->GetReflection()
  379. ->SetDouble(sub_message, map_int32_double_val_, 0.0);
  380. sub_message = reflection->AddMessage(message, F("map_bool_bool"));
  381. sub_message->GetReflection()
  382. ->SetBool(sub_message, map_bool_bool_key_, false);
  383. sub_message->GetReflection()
  384. ->SetBool(sub_message, map_bool_bool_val_, false);
  385. sub_message = reflection->AddMessage(message, F("map_string_string"));
  386. sub_message->GetReflection()
  387. ->SetString(sub_message, map_string_string_key_, "0");
  388. sub_message->GetReflection()
  389. ->SetString(sub_message, map_string_string_val_, "0");
  390. sub_message = reflection->AddMessage(message, F("map_int32_bytes"));
  391. sub_message->GetReflection()
  392. ->SetInt32(sub_message, map_int32_bytes_key_, 0);
  393. sub_message->GetReflection()
  394. ->SetString(sub_message, map_int32_bytes_val_, "0");
  395. sub_message = reflection->AddMessage(message, F("map_int32_enum"));
  396. sub_message->GetReflection()
  397. ->SetInt32(sub_message, map_int32_enum_key_, 0);
  398. sub_message->GetReflection()
  399. ->SetEnum(sub_message, map_int32_enum_val_, map_enum_bar_);
  400. sub_message = reflection
  401. ->AddMessage(message, F("map_int32_foreign_message"));
  402. sub_message->GetReflection()
  403. ->SetInt32(sub_message, map_int32_foreign_message_key_, 0);
  404. sub_foreign_message = sub_message->GetReflection()->
  405. MutableMessage(sub_message, map_int32_foreign_message_val_, NULL);
  406. sub_foreign_message->GetReflection()->
  407. SetInt32(sub_foreign_message, foreign_c_, 0);
  408. // Add second element
  409. sub_message = reflection->AddMessage(message, F("map_int32_int32"));
  410. sub_message->GetReflection()
  411. ->SetInt32(sub_message, map_int32_int32_key_, 1);
  412. sub_message->GetReflection()
  413. ->SetInt32(sub_message, map_int32_int32_val_, 1);
  414. sub_message = reflection->AddMessage(message, F("map_int64_int64"));
  415. sub_message->GetReflection()
  416. ->SetInt64(sub_message, map_int64_int64_key_, 1);
  417. sub_message->GetReflection()
  418. ->SetInt64(sub_message, map_int64_int64_val_, 1);
  419. sub_message = reflection->AddMessage(message, F("map_uint32_uint32"));
  420. sub_message->GetReflection()
  421. ->SetUInt32(sub_message, map_uint32_uint32_key_, 1);
  422. sub_message->GetReflection()
  423. ->SetUInt32(sub_message, map_uint32_uint32_val_, 1);
  424. sub_message = reflection->AddMessage(message, F("map_uint64_uint64"));
  425. sub_message->GetReflection()
  426. ->SetUInt64(sub_message, map_uint64_uint64_key_, 1);
  427. sub_message->GetReflection()
  428. ->SetUInt64(sub_message, map_uint64_uint64_val_, 1);
  429. sub_message = reflection->AddMessage(message, F("map_sint32_sint32"));
  430. sub_message->GetReflection()
  431. ->SetInt32(sub_message, map_sint32_sint32_key_, 1);
  432. sub_message->GetReflection()
  433. ->SetInt32(sub_message, map_sint32_sint32_val_, 1);
  434. sub_message = reflection->AddMessage(message, F("map_sint64_sint64"));
  435. sub_message->GetReflection()
  436. ->SetInt64(sub_message, map_sint64_sint64_key_, 1);
  437. sub_message->GetReflection()
  438. ->SetInt64(sub_message, map_sint64_sint64_val_, 1);
  439. sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32"));
  440. sub_message->GetReflection()
  441. ->SetUInt32(sub_message, map_fixed32_fixed32_key_, 1);
  442. sub_message->GetReflection()
  443. ->SetUInt32(sub_message, map_fixed32_fixed32_val_, 1);
  444. sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64"));
  445. sub_message->GetReflection()
  446. ->SetUInt64(sub_message, map_fixed64_fixed64_key_, 1);
  447. sub_message->GetReflection()
  448. ->SetUInt64(sub_message, map_fixed64_fixed64_val_, 1);
  449. sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32"));
  450. sub_message->GetReflection()
  451. ->SetInt32(sub_message, map_sfixed32_sfixed32_key_, 1);
  452. sub_message->GetReflection()
  453. ->SetInt32(sub_message, map_sfixed32_sfixed32_val_, 1);
  454. sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64"));
  455. sub_message->GetReflection()
  456. ->SetInt64(sub_message, map_sfixed64_sfixed64_key_, 1);
  457. sub_message->GetReflection()
  458. ->SetInt64(sub_message, map_sfixed64_sfixed64_val_, 1);
  459. sub_message = reflection->AddMessage(message, F("map_int32_float"));
  460. sub_message->GetReflection()
  461. ->SetInt32(sub_message, map_int32_float_key_, 1);
  462. sub_message->GetReflection()
  463. ->SetFloat(sub_message, map_int32_float_val_, 1.0);
  464. sub_message = reflection->AddMessage(message, F("map_int32_double"));
  465. sub_message->GetReflection()
  466. ->SetInt32(sub_message, map_int32_double_key_, 1);
  467. sub_message->GetReflection()
  468. ->SetDouble(sub_message, map_int32_double_val_, 1.0);
  469. sub_message = reflection->AddMessage(message, F("map_bool_bool"));
  470. sub_message->GetReflection()
  471. ->SetBool(sub_message, map_bool_bool_key_, true);
  472. sub_message->GetReflection()
  473. ->SetBool(sub_message, map_bool_bool_val_, true);
  474. sub_message = reflection->AddMessage(message, F("map_string_string"));
  475. sub_message->GetReflection()
  476. ->SetString(sub_message, map_string_string_key_, "1");
  477. sub_message->GetReflection()
  478. ->SetString(sub_message, map_string_string_val_, "1");
  479. sub_message = reflection->AddMessage(message, F("map_int32_bytes"));
  480. sub_message->GetReflection()
  481. ->SetInt32(sub_message, map_int32_bytes_key_, 1);
  482. sub_message->GetReflection()
  483. ->SetString(sub_message, map_int32_bytes_val_, "1");
  484. sub_message = reflection->AddMessage(message, F("map_int32_enum"));
  485. sub_message->GetReflection()
  486. ->SetInt32(sub_message, map_int32_enum_key_, 1);
  487. sub_message->GetReflection()
  488. ->SetEnum(sub_message, map_int32_enum_val_, map_enum_baz_);
  489. sub_message = reflection
  490. ->AddMessage(message, F("map_int32_foreign_message"));
  491. sub_message->GetReflection()
  492. ->SetInt32(sub_message, map_int32_foreign_message_key_, 1);
  493. sub_foreign_message = sub_message->GetReflection()->
  494. MutableMessage(sub_message, map_int32_foreign_message_val_, NULL);
  495. sub_foreign_message->GetReflection()->
  496. SetInt32(sub_foreign_message, foreign_c_, 1);
  497. }
  498. void MapReflectionTester::SetMapFieldsViaMapReflection(
  499. Message* message) {
  500. const Reflection* reflection = message->GetReflection();
  501. Message* sub_foreign_message = NULL;
  502. MapValueRef map_val;
  503. // Add first element.
  504. MapKey map_key;
  505. map_key.SetInt32Value(0);
  506. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  507. message, F("map_int32_int32"), map_key, &map_val));
  508. map_val.SetInt32Value(0);
  509. map_key.SetInt64Value(0);
  510. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  511. message, F("map_int64_int64"), map_key, &map_val));
  512. map_val.SetInt64Value(0);
  513. map_key.SetUInt32Value(0);
  514. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  515. message, F("map_uint32_uint32"), map_key, &map_val));
  516. map_val.SetUInt32Value(0);
  517. map_key.SetUInt64Value(0);
  518. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  519. message, F("map_uint64_uint64"), map_key, &map_val));
  520. map_val.SetUInt64Value(0);
  521. map_key.SetInt32Value(0);
  522. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  523. message, F("map_sint32_sint32"), map_key, &map_val));
  524. map_val.SetInt32Value(0);
  525. map_key.SetInt64Value(0);
  526. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  527. message, F("map_sint64_sint64"), map_key, &map_val));
  528. map_val.SetInt64Value(0);
  529. map_key.SetUInt32Value(0);
  530. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  531. message, F("map_fixed32_fixed32"), map_key, &map_val));
  532. map_val.SetUInt32Value(0);
  533. map_key.SetUInt64Value(0);
  534. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  535. message, F("map_fixed64_fixed64"), map_key, &map_val));
  536. map_val.SetUInt64Value(0);
  537. map_key.SetInt32Value(0);
  538. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  539. message, F("map_sfixed32_sfixed32"), map_key, &map_val));
  540. map_val.SetInt32Value(0);
  541. map_key.SetInt64Value(0);
  542. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  543. message, F("map_sfixed64_sfixed64"), map_key, &map_val));
  544. map_val.SetInt64Value(0);
  545. map_key.SetInt32Value(0);
  546. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  547. message, F("map_int32_float"), map_key, &map_val));
  548. map_val.SetFloatValue(0.0);
  549. map_key.SetInt32Value(0);
  550. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  551. message, F("map_int32_double"), map_key, &map_val));
  552. map_val.SetDoubleValue(0.0);
  553. map_key.SetBoolValue(false);
  554. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  555. message, F("map_bool_bool"), map_key, &map_val));
  556. map_val.SetBoolValue(false);
  557. map_key.SetStringValue("0");
  558. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  559. message, F("map_string_string"), map_key, &map_val));
  560. map_val.SetStringValue("0");
  561. map_key.SetInt32Value(0);
  562. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  563. message, F("map_int32_bytes"), map_key, &map_val));
  564. map_val.SetStringValue("0");
  565. map_key.SetInt32Value(0);
  566. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  567. message, F("map_int32_enum"), map_key, &map_val));
  568. map_val.SetEnumValue(map_enum_bar_->number());
  569. map_key.SetInt32Value(0);
  570. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  571. message, F("map_int32_foreign_message"), map_key, &map_val));
  572. sub_foreign_message = map_val.MutableMessageValue();
  573. sub_foreign_message->GetReflection()->SetInt32(
  574. sub_foreign_message, foreign_c_, 0);
  575. // Add second element
  576. map_key.SetInt32Value(1);
  577. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  578. message, F("map_int32_int32"), map_key, &map_val));
  579. map_val.SetInt32Value(1);
  580. EXPECT_FALSE(reflection->InsertOrLookupMapValue(
  581. message, F("map_int32_int32"), map_key, &map_val));
  582. map_key.SetInt64Value(1);
  583. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  584. message, F("map_int64_int64"), map_key, &map_val));
  585. map_val.SetInt64Value(1);
  586. EXPECT_FALSE(reflection->InsertOrLookupMapValue(
  587. message, F("map_int64_int64"), map_key, &map_val));
  588. map_key.SetUInt32Value(1);
  589. reflection->InsertOrLookupMapValue(
  590. message, F("map_uint32_uint32"), map_key, &map_val);
  591. map_val.SetUInt32Value(1);
  592. map_key.SetUInt64Value(1);
  593. reflection->InsertOrLookupMapValue(
  594. message, F("map_uint64_uint64"), map_key, &map_val);
  595. map_val.SetUInt64Value(1);
  596. map_key.SetInt32Value(1);
  597. reflection->InsertOrLookupMapValue(
  598. message, F("map_sint32_sint32"), map_key, &map_val);
  599. map_val.SetInt32Value(1);
  600. map_key.SetInt64Value(1);
  601. reflection->InsertOrLookupMapValue(
  602. message, F("map_sint64_sint64"), map_key, &map_val);
  603. map_val.SetInt64Value(1);
  604. map_key.SetUInt32Value(1);
  605. reflection->InsertOrLookupMapValue(
  606. message, F("map_fixed32_fixed32"), map_key, &map_val);
  607. map_val.SetUInt32Value(1);
  608. map_key.SetUInt64Value(1);
  609. reflection->InsertOrLookupMapValue(
  610. message, F("map_fixed64_fixed64"), map_key, &map_val);
  611. map_val.SetUInt64Value(1);
  612. map_key.SetInt32Value(1);
  613. reflection->InsertOrLookupMapValue(
  614. message, F("map_sfixed32_sfixed32"), map_key, &map_val);
  615. map_val.SetInt32Value(1);
  616. map_key.SetInt64Value(1);
  617. reflection->InsertOrLookupMapValue(
  618. message, F("map_sfixed64_sfixed64"), map_key, &map_val);
  619. map_val.SetInt64Value(1);
  620. map_key.SetInt32Value(1);
  621. reflection->InsertOrLookupMapValue(
  622. message, F("map_int32_float"), map_key, &map_val);
  623. map_val.SetFloatValue(1.0);
  624. map_key.SetInt32Value(1);
  625. reflection->InsertOrLookupMapValue(
  626. message, F("map_int32_double"), map_key, &map_val);
  627. map_val.SetDoubleValue(1.0);
  628. map_key.SetBoolValue(true);
  629. reflection->InsertOrLookupMapValue(
  630. message, F("map_bool_bool"), map_key, &map_val);
  631. map_val.SetBoolValue(true);
  632. map_key.SetStringValue("1");
  633. reflection->InsertOrLookupMapValue(
  634. message, F("map_string_string"), map_key, &map_val);
  635. map_val.SetStringValue("1");
  636. map_key.SetInt32Value(1);
  637. reflection->InsertOrLookupMapValue(
  638. message, F("map_int32_bytes"), map_key, &map_val);
  639. map_val.SetStringValue("1");
  640. map_key.SetInt32Value(1);
  641. reflection->InsertOrLookupMapValue(
  642. message, F("map_int32_enum"), map_key, &map_val);
  643. map_val.SetEnumValue(map_enum_baz_->number());
  644. map_key.SetInt32Value(1);
  645. EXPECT_TRUE(reflection->InsertOrLookupMapValue(
  646. message, F("map_int32_foreign_message"), map_key, &map_val));
  647. sub_foreign_message = map_val.MutableMessageValue();
  648. sub_foreign_message->GetReflection()->SetInt32(
  649. sub_foreign_message, foreign_c_, 1);
  650. }
  651. void MapReflectionTester::GetMapValueViaMapReflection(Message* message,
  652. const string& field_name,
  653. const MapKey& map_key,
  654. MapValueRef* map_val) {
  655. const Reflection* reflection = message->GetReflection();
  656. EXPECT_FALSE(reflection->InsertOrLookupMapValue(message, F(field_name),
  657. map_key, map_val));
  658. }
  659. Message* MapReflectionTester::GetMapEntryViaReflection(Message* message,
  660. const string& field_name,
  661. int index) {
  662. const Reflection* reflection = message->GetReflection();
  663. return reflection->MutableRepeatedMessage(message, F(field_name), index);
  664. }
  665. MapIterator MapReflectionTester::MapBegin(Message* message,
  666. const string& field_name) {
  667. const Reflection* reflection = message->GetReflection();
  668. return reflection->MapBegin(message, F(field_name));
  669. }
  670. MapIterator MapReflectionTester::MapEnd(Message* message,
  671. const string& field_name) {
  672. const Reflection* reflection = message->GetReflection();
  673. return reflection->MapEnd(message, F(field_name));
  674. }
  675. void MapReflectionTester::ClearMapFieldsViaReflection(
  676. Message* message) {
  677. const Reflection* reflection = message->GetReflection();
  678. reflection->ClearField(message, F("map_int32_int32"));
  679. reflection->ClearField(message, F("map_int64_int64"));
  680. reflection->ClearField(message, F("map_uint32_uint32"));
  681. reflection->ClearField(message, F("map_uint64_uint64"));
  682. reflection->ClearField(message, F("map_sint32_sint32"));
  683. reflection->ClearField(message, F("map_sint64_sint64"));
  684. reflection->ClearField(message, F("map_fixed32_fixed32"));
  685. reflection->ClearField(message, F("map_fixed64_fixed64"));
  686. reflection->ClearField(message, F("map_sfixed32_sfixed32"));
  687. reflection->ClearField(message, F("map_sfixed64_sfixed64"));
  688. reflection->ClearField(message, F("map_int32_float"));
  689. reflection->ClearField(message, F("map_int32_double"));
  690. reflection->ClearField(message, F("map_bool_bool"));
  691. reflection->ClearField(message, F("map_string_string"));
  692. reflection->ClearField(message, F("map_int32_bytes"));
  693. reflection->ClearField(message, F("map_int32_enum"));
  694. reflection->ClearField(message, F("map_int32_foreign_message"));
  695. }
  696. void MapReflectionTester::ModifyMapFieldsViaReflection(
  697. Message* message) {
  698. const Reflection* reflection = message->GetReflection();
  699. MapValueRef map_val;
  700. Message* sub_foreign_message;
  701. // Modify the second element
  702. MapKey map_key;
  703. map_key.SetInt32Value(1);
  704. EXPECT_FALSE(reflection->InsertOrLookupMapValue(
  705. message, F("map_int32_int32"), map_key, &map_val));
  706. map_val.SetInt32Value(2);
  707. map_key.SetInt64Value(1);
  708. EXPECT_FALSE(reflection->InsertOrLookupMapValue(
  709. message, F("map_int64_int64"), map_key, &map_val));
  710. map_val.SetInt64Value(2);
  711. map_key.SetUInt32Value(1);
  712. EXPECT_FALSE(reflection->InsertOrLookupMapValue(
  713. message, F("map_uint32_uint32"), map_key, &map_val));
  714. map_val.SetUInt32Value(2);
  715. map_key.SetUInt64Value(1);
  716. reflection->InsertOrLookupMapValue(
  717. message, F("map_uint64_uint64"), map_key, &map_val);
  718. map_val.SetUInt64Value(2);
  719. map_key.SetInt32Value(1);
  720. reflection->InsertOrLookupMapValue(
  721. message, F("map_sint32_sint32"), map_key, &map_val);
  722. map_val.SetInt32Value(2);
  723. map_key.SetInt64Value(1);
  724. reflection->InsertOrLookupMapValue(
  725. message, F("map_sint64_sint64"), map_key, &map_val);
  726. map_val.SetInt64Value(2);
  727. map_key.SetUInt32Value(1);
  728. reflection->InsertOrLookupMapValue(
  729. message, F("map_fixed32_fixed32"), map_key, &map_val);
  730. map_val.SetUInt32Value(2);
  731. map_key.SetUInt64Value(1);
  732. reflection->InsertOrLookupMapValue(
  733. message, F("map_fixed64_fixed64"), map_key, &map_val);
  734. map_val.SetUInt64Value(2);
  735. map_key.SetInt32Value(1);
  736. reflection->InsertOrLookupMapValue(
  737. message, F("map_sfixed32_sfixed32"), map_key, &map_val);
  738. map_val.SetInt32Value(2);
  739. map_key.SetInt64Value(1);
  740. reflection->InsertOrLookupMapValue(
  741. message, F("map_sfixed64_sfixed64"), map_key, &map_val);
  742. map_val.SetInt64Value(2);
  743. map_key.SetInt32Value(1);
  744. reflection->InsertOrLookupMapValue(
  745. message, F("map_int32_float"), map_key, &map_val);
  746. map_val.SetFloatValue(2.0);
  747. map_key.SetInt32Value(1);
  748. reflection->InsertOrLookupMapValue(
  749. message, F("map_int32_double"), map_key, &map_val);
  750. map_val.SetDoubleValue(2.0);
  751. map_key.SetBoolValue(true);
  752. reflection->InsertOrLookupMapValue(
  753. message, F("map_bool_bool"), map_key, &map_val);
  754. map_val.SetBoolValue(false);
  755. map_key.SetStringValue("1");
  756. reflection->InsertOrLookupMapValue(
  757. message, F("map_string_string"), map_key, &map_val);
  758. map_val.SetStringValue("2");
  759. map_key.SetInt32Value(1);
  760. reflection->InsertOrLookupMapValue(
  761. message, F("map_int32_bytes"), map_key, &map_val);
  762. map_val.SetStringValue("2");
  763. map_key.SetInt32Value(1);
  764. reflection->InsertOrLookupMapValue(
  765. message, F("map_int32_enum"), map_key, &map_val);
  766. map_val.SetEnumValue(map_enum_foo_->number());
  767. map_key.SetInt32Value(1);
  768. EXPECT_FALSE(reflection->InsertOrLookupMapValue(
  769. message, F("map_int32_foreign_message"), map_key, &map_val));
  770. sub_foreign_message = map_val.MutableMessageValue();
  771. sub_foreign_message->GetReflection()->SetInt32(
  772. sub_foreign_message, foreign_c_, 2);
  773. }
  774. void MapReflectionTester::RemoveLastMapsViaReflection(
  775. Message* message) {
  776. const Reflection* reflection = message->GetReflection();
  777. std::vector<const FieldDescriptor*> output;
  778. reflection->ListFields(*message, &output);
  779. for (int i = 0; i < output.size(); ++i) {
  780. const FieldDescriptor* field = output[i];
  781. if (!field->is_repeated()) continue;
  782. reflection->RemoveLast(message, field);
  783. }
  784. }
  785. void MapReflectionTester::ReleaseLastMapsViaReflection(
  786. Message* message) {
  787. const Reflection* reflection = message->GetReflection();
  788. std::vector<const FieldDescriptor*> output;
  789. reflection->ListFields(*message, &output);
  790. for (int i = 0; i < output.size(); ++i) {
  791. const FieldDescriptor* field = output[i];
  792. if (!field->is_repeated()) continue;
  793. if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
  794. Message* released = reflection->ReleaseLast(message, field);
  795. ASSERT_TRUE(released != NULL) << "ReleaseLast returned NULL for: "
  796. << field->name();
  797. delete released;
  798. }
  799. }
  800. void MapReflectionTester::SwapMapsViaReflection(Message* message) {
  801. const Reflection* reflection = message->GetReflection();
  802. std::vector<const FieldDescriptor*> output;
  803. reflection->ListFields(*message, &output);
  804. for (int i = 0; i < output.size(); ++i) {
  805. const FieldDescriptor* field = output[i];
  806. if (!field->is_repeated()) continue;
  807. reflection->SwapElements(message, field, 0, 1);
  808. }
  809. }
  810. void MapReflectionTester::
  811. MutableUnknownFieldsOfMapFieldsViaReflection(Message* message) {
  812. const Reflection* reflection = message->GetReflection();
  813. Message* sub_message = NULL;
  814. sub_message = reflection->AddMessage(message, F("map_int32_int32"));
  815. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  816. NULL);
  817. sub_message = reflection->AddMessage(message, F("map_int64_int64"));
  818. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  819. NULL);
  820. sub_message = reflection->AddMessage(message, F("map_uint32_uint32"));
  821. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  822. NULL);
  823. sub_message = reflection->AddMessage(message, F("map_uint64_uint64"));
  824. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  825. NULL);
  826. sub_message = reflection->AddMessage(message, F("map_sint32_sint32"));
  827. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  828. NULL);
  829. sub_message = reflection->AddMessage(message, F("map_sint64_sint64"));
  830. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  831. NULL);
  832. sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32"));
  833. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  834. NULL);
  835. sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64"));
  836. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  837. NULL);
  838. sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32"));
  839. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  840. NULL);
  841. sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64"));
  842. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  843. NULL);
  844. sub_message = reflection->AddMessage(message, F("map_int32_float"));
  845. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  846. NULL);
  847. sub_message = reflection->AddMessage(message, F("map_int32_double"));
  848. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  849. NULL);
  850. sub_message = reflection->AddMessage(message, F("map_bool_bool"));
  851. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  852. NULL);
  853. sub_message = reflection->AddMessage(message, F("map_string_string"));
  854. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  855. NULL);
  856. sub_message = reflection->AddMessage(message, F("map_int32_bytes"));
  857. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  858. NULL);
  859. sub_message = reflection->AddMessage(message, F("map_int32_enum"));
  860. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  861. NULL);
  862. sub_message = reflection->AddMessage(message, F("map_int32_foreign_message"));
  863. EXPECT_TRUE(sub_message->GetReflection()->MutableUnknownFields(sub_message) !=
  864. NULL);
  865. }
  866. void MapReflectionTester::ExpectMapFieldsSetViaReflection(
  867. const Message& message) {
  868. string scratch;
  869. const Reflection* reflection = message.GetReflection();
  870. const Message* sub_message;
  871. MapKey map_key;
  872. // -----------------------------------------------------------------
  873. ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_int32")));
  874. ASSERT_EQ(2, reflection->FieldSize(message, F("map_int64_int64")));
  875. ASSERT_EQ(2, reflection->FieldSize(message, F("map_uint32_uint32")));
  876. ASSERT_EQ(2, reflection->FieldSize(message, F("map_uint64_uint64")));
  877. ASSERT_EQ(2, reflection->FieldSize(message, F("map_sint32_sint32")));
  878. ASSERT_EQ(2, reflection->FieldSize(message, F("map_sint64_sint64")));
  879. ASSERT_EQ(2, reflection->FieldSize(message, F("map_fixed32_fixed32")));
  880. ASSERT_EQ(2, reflection->FieldSize(message, F("map_fixed64_fixed64")));
  881. ASSERT_EQ(2, reflection->FieldSize(message, F("map_sfixed32_sfixed32")));
  882. ASSERT_EQ(2, reflection->FieldSize(message, F("map_sfixed64_sfixed64")));
  883. ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_float")));
  884. ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_double")));
  885. ASSERT_EQ(2, reflection->FieldSize(message, F("map_bool_bool")));
  886. ASSERT_EQ(2, reflection->FieldSize(message, F("map_string_string")));
  887. ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_bytes")));
  888. ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_enum")));
  889. ASSERT_EQ(2, reflection->FieldSize(message, F("map_int32_foreign_message")));
  890. {
  891. std::map<int32, int32> map;
  892. map[0] = 0;
  893. map[1] = 1;
  894. for (int i = 0; i < 2; i++) {
  895. // Check with RepeatedField Reflection
  896. sub_message =
  897. &reflection->GetRepeatedMessage(message, F("map_int32_int32"), i);
  898. int32 key = sub_message->GetReflection()->GetInt32(
  899. *sub_message, map_int32_int32_key_);
  900. int32 val = sub_message->GetReflection()->GetInt32(
  901. *sub_message, map_int32_int32_val_);
  902. EXPECT_EQ(map[key], val);
  903. // Check with Map Reflection
  904. map_key.SetInt32Value(key);
  905. EXPECT_TRUE(reflection->ContainsMapKey(
  906. message, F("map_int32_int32"), map_key));
  907. }
  908. }
  909. {
  910. std::map<int64, int64> map;
  911. map[0] = 0;
  912. map[1] = 1;
  913. for (int i = 0; i < 2; i++) {
  914. // Check with RepeatedField Reflection
  915. sub_message =
  916. &reflection->GetRepeatedMessage(message, F("map_int64_int64"), i);
  917. int64 key = sub_message->GetReflection()->GetInt64(
  918. *sub_message, map_int64_int64_key_);
  919. int64 val = sub_message->GetReflection()->GetInt64(
  920. *sub_message, map_int64_int64_val_);
  921. EXPECT_EQ(map[key], val);
  922. // Check with Map Reflection
  923. map_key.SetInt64Value(key);
  924. EXPECT_TRUE(reflection->ContainsMapKey(
  925. message, F("map_int64_int64"), map_key));
  926. }
  927. }
  928. {
  929. std::map<uint32, uint32> map;
  930. map[0] = 0;
  931. map[1] = 1;
  932. for (int i = 0; i < 2; i++) {
  933. // Check with RepeatedField Reflection
  934. sub_message =
  935. &reflection->GetRepeatedMessage(message, F("map_uint32_uint32"), i);
  936. uint32 key = sub_message->GetReflection()->GetUInt32(
  937. *sub_message, map_uint32_uint32_key_);
  938. uint32 val = sub_message->GetReflection()->GetUInt32(
  939. *sub_message, map_uint32_uint32_val_);
  940. EXPECT_EQ(map[key], val);
  941. // Check with Map Reflection
  942. map_key.SetUInt32Value(key);
  943. EXPECT_TRUE(reflection->ContainsMapKey(
  944. message, F("map_uint32_uint32"), map_key));
  945. }
  946. }
  947. {
  948. std::map<uint64, uint64> map;
  949. map[0] = 0;
  950. map[1] = 1;
  951. for (int i = 0; i < 2; i++) {
  952. sub_message =
  953. &reflection->GetRepeatedMessage(message, F("map_uint64_uint64"), i);
  954. uint64 key = sub_message->GetReflection()->GetUInt64(
  955. *sub_message, map_uint64_uint64_key_);
  956. uint64 val = sub_message->GetReflection()->GetUInt64(
  957. *sub_message, map_uint64_uint64_val_);
  958. EXPECT_EQ(map[key], val);
  959. // Check with Map Reflection
  960. map_key.SetUInt64Value(key);
  961. EXPECT_TRUE(reflection->ContainsMapKey(
  962. message, F("map_uint64_uint64"), map_key));
  963. }
  964. }
  965. {
  966. std::map<int32, int32> map;
  967. map[0] = 0;
  968. map[1] = 1;
  969. for (int i = 0; i < 2; i++) {
  970. sub_message =
  971. &reflection->GetRepeatedMessage(message, F("map_sint32_sint32"), i);
  972. int32 key = sub_message->GetReflection()->GetInt32(
  973. *sub_message, map_sint32_sint32_key_);
  974. int32 val = sub_message->GetReflection()->GetInt32(
  975. *sub_message, map_sint32_sint32_val_);
  976. EXPECT_EQ(map[key], val);
  977. // Check with Map Reflection
  978. map_key.SetInt32Value(key);
  979. EXPECT_EQ(true, reflection->ContainsMapKey(
  980. message, F("map_sint32_sint32"), map_key));
  981. }
  982. }
  983. {
  984. std::map<int64, int64> map;
  985. map[0] = 0;
  986. map[1] = 1;
  987. for (int i = 0; i < 2; i++) {
  988. sub_message =
  989. &reflection->GetRepeatedMessage(message, F("map_sint64_sint64"), i);
  990. int64 key = sub_message->GetReflection()->GetInt64(
  991. *sub_message, map_sint64_sint64_key_);
  992. int64 val = sub_message->GetReflection()->GetInt64(
  993. *sub_message, map_sint64_sint64_val_);
  994. EXPECT_EQ(map[key], val);
  995. // Check with Map Reflection
  996. map_key.SetInt64Value(key);
  997. EXPECT_EQ(true, reflection->ContainsMapKey(
  998. message, F("map_sint64_sint64"), map_key));
  999. }
  1000. }
  1001. {
  1002. std::map<uint32, uint32> map;
  1003. map[0] = 0;
  1004. map[1] = 1;
  1005. for (int i = 0; i < 2; i++) {
  1006. sub_message =
  1007. &reflection->GetRepeatedMessage(message, F("map_fixed32_fixed32"), i);
  1008. uint32 key = sub_message->GetReflection()->GetUInt32(
  1009. *sub_message, map_fixed32_fixed32_key_);
  1010. uint32 val = sub_message->GetReflection()->GetUInt32(
  1011. *sub_message, map_fixed32_fixed32_val_);
  1012. EXPECT_EQ(map[key], val);
  1013. // Check with Map Reflection
  1014. map_key.SetUInt32Value(key);
  1015. EXPECT_EQ(true, reflection->ContainsMapKey(
  1016. message, F("map_fixed32_fixed32"), map_key));
  1017. }
  1018. }
  1019. {
  1020. std::map<uint64, uint64> map;
  1021. map[0] = 0;
  1022. map[1] = 1;
  1023. for (int i = 0; i < 2; i++) {
  1024. sub_message =
  1025. &reflection->GetRepeatedMessage(message, F("map_fixed64_fixed64"), i);
  1026. uint64 key = sub_message->GetReflection()->GetUInt64(
  1027. *sub_message, map_fixed64_fixed64_key_);
  1028. uint64 val = sub_message->GetReflection()->GetUInt64(
  1029. *sub_message, map_fixed64_fixed64_val_);
  1030. EXPECT_EQ(map[key], val);
  1031. // Check with Map Reflection
  1032. map_key.SetUInt64Value(key);
  1033. EXPECT_EQ(true, reflection->ContainsMapKey(
  1034. message, F("map_fixed64_fixed64"), map_key));
  1035. }
  1036. }
  1037. {
  1038. std::map<int32, int32> map;
  1039. map[0] = 0;
  1040. map[1] = 1;
  1041. for (int i = 0; i < 2; i++) {
  1042. sub_message = &reflection->GetRepeatedMessage(
  1043. message, F("map_sfixed32_sfixed32"), i);
  1044. int32 key = sub_message->GetReflection()->GetInt32(
  1045. *sub_message, map_sfixed32_sfixed32_key_);
  1046. int32 val = sub_message->GetReflection()->GetInt32(
  1047. *sub_message, map_sfixed32_sfixed32_val_);
  1048. EXPECT_EQ(map[key], val);
  1049. // Check with Map Reflection
  1050. map_key.SetInt32Value(key);
  1051. EXPECT_EQ(true, reflection->ContainsMapKey(
  1052. message, F("map_sfixed32_sfixed32"), map_key));
  1053. }
  1054. }
  1055. {
  1056. std::map<int64, int64> map;
  1057. map[0] = 0;
  1058. map[1] = 1;
  1059. for (int i = 0; i < 2; i++) {
  1060. sub_message = &reflection->GetRepeatedMessage(
  1061. message, F("map_sfixed64_sfixed64"), i);
  1062. int64 key = sub_message->GetReflection()->GetInt64(
  1063. *sub_message, map_sfixed64_sfixed64_key_);
  1064. int64 val = sub_message->GetReflection()->GetInt64(
  1065. *sub_message, map_sfixed64_sfixed64_val_);
  1066. EXPECT_EQ(map[key], val);
  1067. // Check with Map Reflection
  1068. map_key.SetInt64Value(key);
  1069. EXPECT_EQ(true, reflection->ContainsMapKey(
  1070. message, F("map_sfixed64_sfixed64"), map_key));
  1071. }
  1072. }
  1073. {
  1074. std::map<int32, float> map;
  1075. map[0] = 0.0;
  1076. map[1] = 1.0;
  1077. for (int i = 0; i < 2; i++) {
  1078. sub_message =
  1079. &reflection->GetRepeatedMessage(message, F("map_int32_float"), i);
  1080. int32 key = sub_message->GetReflection()->GetInt32(
  1081. *sub_message, map_int32_float_key_);
  1082. float val = sub_message->GetReflection()->GetFloat(
  1083. *sub_message, map_int32_float_val_);
  1084. EXPECT_EQ(map[key], val);
  1085. // Check with Map Reflection
  1086. map_key.SetInt32Value(key);
  1087. EXPECT_EQ(true, reflection->ContainsMapKey(
  1088. message, F("map_int32_float"), map_key));
  1089. }
  1090. }
  1091. {
  1092. std::map<int32, double> map;
  1093. map[0] = 0.0;
  1094. map[1] = 1.0;
  1095. for (int i = 0; i < 2; i++) {
  1096. sub_message =
  1097. &reflection->GetRepeatedMessage(message, F("map_int32_double"), i);
  1098. int32 key = sub_message->GetReflection()->GetInt32(
  1099. *sub_message, map_int32_double_key_);
  1100. double val = sub_message->GetReflection()->GetDouble(
  1101. *sub_message, map_int32_double_val_);
  1102. EXPECT_EQ(map[key], val);
  1103. // Check with Map Reflection
  1104. map_key.SetInt32Value(key);
  1105. EXPECT_EQ(true, reflection->ContainsMapKey(
  1106. message, F("map_int32_double"), map_key));
  1107. }
  1108. }
  1109. {
  1110. std::map<bool, bool> map;
  1111. map[false] = false;
  1112. map[true] = true;
  1113. for (int i = 0; i < 2; i++) {
  1114. sub_message =
  1115. &reflection->GetRepeatedMessage(message, F("map_bool_bool"), i);
  1116. bool key = sub_message->GetReflection()->GetBool(
  1117. *sub_message, map_bool_bool_key_);
  1118. bool val = sub_message->GetReflection()->GetBool(
  1119. *sub_message, map_bool_bool_val_);
  1120. EXPECT_EQ(map[key], val);
  1121. // Check with Map Reflection
  1122. map_key.SetBoolValue(key);
  1123. EXPECT_EQ(true, reflection->ContainsMapKey(
  1124. message, F("map_bool_bool"), map_key));
  1125. }
  1126. }
  1127. {
  1128. std::map<string, string> map;
  1129. map["0"] = "0";
  1130. map["1"] = "1";
  1131. for (int i = 0; i < 2; i++) {
  1132. sub_message =
  1133. &reflection->GetRepeatedMessage(message, F("map_string_string"), i);
  1134. string key = sub_message->GetReflection()->GetString(
  1135. *sub_message, map_string_string_key_);
  1136. string val = sub_message->GetReflection()->GetString(
  1137. *sub_message, map_string_string_val_);
  1138. EXPECT_EQ(map[key], val);
  1139. // Check with Map Reflection
  1140. map_key.SetStringValue(key);
  1141. EXPECT_EQ(true, reflection->ContainsMapKey(
  1142. message, F("map_string_string"), map_key));
  1143. }
  1144. }
  1145. {
  1146. std::map<int32, string> map;
  1147. map[0] = "0";
  1148. map[1] = "1";
  1149. for (int i = 0; i < 2; i++) {
  1150. sub_message =
  1151. &reflection->GetRepeatedMessage(message, F("map_int32_bytes"), i);
  1152. int32 key = sub_message->GetReflection()->GetInt32(
  1153. *sub_message, map_int32_bytes_key_);
  1154. string val = sub_message->GetReflection()->GetString(
  1155. *sub_message, map_int32_bytes_val_);
  1156. EXPECT_EQ(map[key], val);
  1157. // Check with Map Reflection
  1158. map_key.SetInt32Value(key);
  1159. EXPECT_EQ(true, reflection->ContainsMapKey(
  1160. message, F("map_int32_bytes"), map_key));
  1161. }
  1162. }
  1163. {
  1164. std::map<int32, const EnumValueDescriptor*> map;
  1165. map[0] = map_enum_bar_;
  1166. map[1] = map_enum_baz_;
  1167. for (int i = 0; i < 2; i++) {
  1168. sub_message = &reflection->GetRepeatedMessage(
  1169. message, F("map_int32_enum"), i);
  1170. int32 key = sub_message->GetReflection()->GetInt32(
  1171. *sub_message, map_int32_enum_key_);
  1172. const EnumValueDescriptor* val = sub_message->GetReflection()->GetEnum(
  1173. *sub_message, map_int32_enum_val_);
  1174. EXPECT_EQ(map[key], val);
  1175. // Check with Map Reflection
  1176. map_key.SetInt32Value(key);
  1177. EXPECT_EQ(true, reflection->ContainsMapKey(
  1178. message, F("map_int32_enum"), map_key));
  1179. }
  1180. }
  1181. {
  1182. std::map<int32, int32> map;
  1183. map[0] = 0;
  1184. map[1] = 1;
  1185. for (int i = 0; i < 2; i++) {
  1186. sub_message = &reflection->GetRepeatedMessage(
  1187. message, F("map_int32_foreign_message"), i);
  1188. int32 key = sub_message->GetReflection()->GetInt32(
  1189. *sub_message, map_int32_foreign_message_key_);
  1190. const Message& foreign_message = sub_message->GetReflection()->GetMessage(
  1191. *sub_message, map_int32_foreign_message_val_);
  1192. int32 val = foreign_message.GetReflection()->GetInt32(
  1193. foreign_message, foreign_c_);
  1194. EXPECT_EQ(map[key], val);
  1195. // Check with Map Reflection
  1196. map_key.SetInt32Value(key);
  1197. EXPECT_EQ(true, reflection->ContainsMapKey(
  1198. message, F("map_int32_foreign_message"), map_key));
  1199. }
  1200. }
  1201. }
  1202. void MapReflectionTester::ExpectMapFieldsSetViaReflectionIterator(
  1203. Message* message) {
  1204. string scratch;
  1205. string serialized;
  1206. const Reflection* reflection = message->GetReflection();
  1207. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_int32")));
  1208. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int64_int64")));
  1209. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_uint32_uint32")));
  1210. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_uint64_uint64")));
  1211. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sint32_sint32")));
  1212. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sint64_sint64")));
  1213. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_fixed32_fixed32")));
  1214. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_fixed64_fixed64")));
  1215. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sfixed32_sfixed32")));
  1216. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_sfixed64_sfixed64")));
  1217. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_float")));
  1218. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_double")));
  1219. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_bool_bool")));
  1220. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_string_string")));
  1221. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_bytes")));
  1222. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_enum")));
  1223. ASSERT_EQ(2, reflection->FieldSize(*message, F("map_int32_foreign_message")));
  1224. {
  1225. std::map<int32, int32> map;
  1226. map[0] = 0;
  1227. map[1] = 1;
  1228. int size = 0;
  1229. for (MapIterator iter = reflection->MapBegin(message, F("map_int32_int32"));
  1230. iter != reflection->MapEnd(message, F("map_int32_int32"));
  1231. ++iter, ++size) {
  1232. // Check const methods do not invalidate map.
  1233. message->DebugString();
  1234. message->ShortDebugString();
  1235. message->SerializeToString(&serialized);
  1236. message->SpaceUsed();
  1237. message->ByteSize();
  1238. EXPECT_EQ(map[iter.GetKey().GetInt32Value()],
  1239. iter.GetValueRef().GetInt32Value());
  1240. }
  1241. EXPECT_EQ(size, 2);
  1242. }
  1243. {
  1244. std::map<int64, int64> map;
  1245. map[0] = 0;
  1246. map[1] = 1;
  1247. for (MapIterator iter = reflection->MapBegin(message, F("map_int64_int64"));
  1248. iter != reflection->MapEnd(message, F("map_int64_int64")); ++iter) {
  1249. EXPECT_EQ(map[iter.GetKey().GetInt64Value()],
  1250. iter.GetValueRef().GetInt64Value());
  1251. }
  1252. }
  1253. {
  1254. std::map<uint32, uint32> map;
  1255. map[0] = 0;
  1256. map[1] = 1;
  1257. for (MapIterator iter = reflection->MapBegin(
  1258. message, F("map_uint32_uint32"));
  1259. iter != reflection->MapEnd(message, F("map_uint32_uint32"));
  1260. ++iter) {
  1261. EXPECT_EQ(map[iter.GetKey().GetUInt32Value()],
  1262. iter.GetValueRef().GetUInt32Value());
  1263. }
  1264. }
  1265. {
  1266. std::map<uint64, uint64> map;
  1267. map[0] = 0;
  1268. map[1] = 1;
  1269. for (MapIterator iter = reflection->MapBegin(
  1270. message, F("map_uint64_uint64"));
  1271. iter != reflection->MapEnd(message, F("map_uint64_uint64"));
  1272. ++iter) {
  1273. EXPECT_EQ(map[iter.GetKey().GetUInt64Value()],
  1274. iter.GetValueRef().GetUInt64Value());
  1275. }
  1276. }
  1277. {
  1278. std::map<int32, int32> map;
  1279. map[0] = 0;
  1280. map[1] = 1;
  1281. for (MapIterator iter = reflection->MapBegin(
  1282. message, F("map_sint32_sint32"));
  1283. iter != reflection->MapEnd(message, F("map_sint32_sint32"));
  1284. ++iter) {
  1285. EXPECT_EQ(map[iter.GetKey().GetInt32Value()],
  1286. iter.GetValueRef().GetInt32Value());
  1287. }
  1288. }
  1289. {
  1290. std::map<int64, int64> map;
  1291. map[0] = 0;
  1292. map[1] = 1;
  1293. for (MapIterator iter = reflection->MapBegin(
  1294. message, F("map_sint64_sint64"));
  1295. iter != reflection->MapEnd(message, F("map_sint64_sint64")); ++iter) {
  1296. EXPECT_EQ(map[iter.GetKey().GetInt64Value()],
  1297. iter.GetValueRef().GetInt64Value());
  1298. }
  1299. }
  1300. {
  1301. std::map<uint32, uint32> map;
  1302. map[0] = 0;
  1303. map[1] = 1;
  1304. for (MapIterator iter = reflection->MapBegin(
  1305. message, F("map_fixed32_fixed32"));
  1306. iter != reflection->MapEnd(message, F("map_fixed32_fixed32"));
  1307. ++iter) {
  1308. EXPECT_EQ(map[iter.GetKey().GetUInt32Value()],
  1309. iter.GetValueRef().GetUInt32Value());
  1310. }
  1311. }
  1312. {
  1313. std::map<uint64, uint64> map;
  1314. map[0] = 0;
  1315. map[1] = 1;
  1316. for (MapIterator iter = reflection->MapBegin(
  1317. message, F("map_fixed64_fixed64"));
  1318. iter != reflection->MapEnd(message, F("map_fixed64_fixed64"));
  1319. ++iter) {
  1320. EXPECT_EQ(map[iter.GetKey().GetUInt64Value()],
  1321. iter.GetValueRef().GetUInt64Value());
  1322. }
  1323. }
  1324. {
  1325. std::map<int32, int32> map;
  1326. map[0] = 0;
  1327. map[1] = 1;
  1328. for (MapIterator iter = reflection->MapBegin(
  1329. message, F("map_sfixed32_sfixed32"));
  1330. iter != reflection->MapEnd(message, F("map_sfixed32_sfixed32"));
  1331. ++iter) {
  1332. EXPECT_EQ(map[iter.GetKey().GetInt32Value()],
  1333. iter.GetValueRef().GetInt32Value());
  1334. }
  1335. }
  1336. {
  1337. std::map<int32, float> map;
  1338. map[0] = 0.0;
  1339. map[1] = 1.0;
  1340. for (MapIterator iter = reflection->MapBegin(message, F("map_int32_float"));
  1341. iter != reflection->MapEnd(message, F("map_int32_float")); ++iter) {
  1342. EXPECT_EQ(map[iter.GetKey().GetInt32Value()],
  1343. iter.GetValueRef().GetFloatValue());
  1344. }
  1345. }
  1346. {
  1347. std::map<int32, double> map;
  1348. map[0] = 0.0;
  1349. map[1] = 1.0;
  1350. for (MapIterator iter = reflection->MapBegin(
  1351. message, F("map_int32_double"));
  1352. iter != reflection->MapEnd(message, F("map_int32_double")); ++iter) {
  1353. EXPECT_EQ(map[iter.GetKey().GetInt32Value()],
  1354. iter.GetValueRef().GetDoubleValue());
  1355. }
  1356. }
  1357. {
  1358. std::map<bool, bool> map;
  1359. map[false] = false;
  1360. map[true] = true;
  1361. for (MapIterator iter = reflection->MapBegin(message, F("map_bool_bool"));
  1362. iter != reflection->MapEnd(message, F("map_bool_bool")); ++iter) {
  1363. EXPECT_EQ(map[iter.GetKey().GetBoolValue()],
  1364. iter.GetValueRef().GetBoolValue());
  1365. }
  1366. }
  1367. {
  1368. std::map<string, string> map;
  1369. map["0"] = "0";
  1370. map["1"] = "1";
  1371. int size = 0;
  1372. for (MapIterator iter = reflection->MapBegin(
  1373. message, F("map_string_string"));
  1374. iter != reflection->MapEnd(message, F("map_string_string"));
  1375. ++iter, ++size) {
  1376. // Check const methods do not invalidate map.
  1377. message->DebugString();
  1378. message->ShortDebugString();
  1379. message->SerializeToString(&serialized);
  1380. message->SpaceUsed();
  1381. message->ByteSize();
  1382. EXPECT_EQ(map[iter.GetKey().GetStringValue()],
  1383. iter.GetValueRef().GetStringValue());
  1384. }
  1385. EXPECT_EQ(size, 2);
  1386. }
  1387. {
  1388. std::map<int32, string> map;
  1389. map[0] = "0";
  1390. map[1] = "1";
  1391. for (MapIterator iter = reflection->MapBegin(message, F("map_int32_bytes"));
  1392. iter != reflection->MapEnd(message, F("map_int32_bytes")); ++iter) {
  1393. EXPECT_EQ(map[iter.GetKey().GetInt32Value()],
  1394. iter.GetValueRef().GetStringValue());
  1395. }
  1396. }
  1397. {
  1398. std::map<int32, const EnumValueDescriptor*> map;
  1399. map[0] = map_enum_bar_;
  1400. map[1] = map_enum_baz_;
  1401. for (MapIterator iter = reflection->MapBegin(message, F("map_int32_enum"));
  1402. iter != reflection->MapEnd(message, F("map_int32_enum")); ++iter) {
  1403. EXPECT_EQ(map[iter.GetKey().GetInt32Value()]->number(),
  1404. iter.GetValueRef().GetEnumValue());
  1405. }
  1406. }
  1407. {
  1408. std::map<int32, int32> map;
  1409. map[0] = 0;
  1410. map[1] = 1;
  1411. int size = 0;
  1412. for (MapIterator iter = reflection->MapBegin(
  1413. message, F("map_int32_foreign_message"));
  1414. iter != reflection->MapEnd(message, F("map_int32_foreign_message"));
  1415. ++iter, ++size) {
  1416. // Check const methods do not invalidate map.
  1417. message->DebugString();
  1418. message->ShortDebugString();
  1419. message->SerializeToString(&serialized);
  1420. message->SpaceUsed();
  1421. message->ByteSize();
  1422. const Message& sub_message = iter.GetValueRef().GetMessageValue();
  1423. EXPECT_EQ(map[iter.GetKey().GetInt32Value()],
  1424. sub_message.GetReflection()->GetInt32(sub_message, foreign_c_));
  1425. }
  1426. EXPECT_EQ(size, 2);
  1427. }
  1428. }
  1429. void MapReflectionTester::ExpectClearViaReflection(
  1430. const Message& message) {
  1431. const Reflection* reflection = message.GetReflection();
  1432. // Map fields are empty.
  1433. EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_int32")));
  1434. EXPECT_EQ(0, reflection->FieldSize(message, F("map_int64_int64")));
  1435. EXPECT_EQ(0, reflection->FieldSize(message, F("map_uint32_uint32")));
  1436. EXPECT_EQ(0, reflection->FieldSize(message, F("map_uint64_uint64")));
  1437. EXPECT_EQ(0, reflection->FieldSize(message, F("map_sint32_sint32")));
  1438. EXPECT_EQ(0, reflection->FieldSize(message, F("map_sint64_sint64")));
  1439. EXPECT_EQ(0, reflection->FieldSize(message, F("map_fixed32_fixed32")));
  1440. EXPECT_EQ(0, reflection->FieldSize(message, F("map_fixed64_fixed64")));
  1441. EXPECT_EQ(0, reflection->FieldSize(message, F("map_sfixed32_sfixed32")));
  1442. EXPECT_EQ(0, reflection->FieldSize(message, F("map_sfixed64_sfixed64")));
  1443. EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_float")));
  1444. EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_double")));
  1445. EXPECT_EQ(0, reflection->FieldSize(message, F("map_bool_bool")));
  1446. EXPECT_EQ(0, reflection->FieldSize(message, F("map_string_string")));
  1447. EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_bytes")));
  1448. EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_enum")));
  1449. EXPECT_EQ(0, reflection->FieldSize(message, F("map_int32_foreign_message")));
  1450. }
  1451. void MapReflectionTester::ExpectClearViaReflectionIterator(
  1452. Message* message) {
  1453. const Reflection* reflection = message->GetReflection();
  1454. EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_int32")) ==
  1455. reflection->MapEnd(message, F("map_int32_int32")));
  1456. EXPECT_TRUE(reflection->MapBegin(message, F("map_int64_int64")) ==
  1457. reflection->MapEnd(message, F("map_int64_int64")));
  1458. EXPECT_TRUE(reflection->MapBegin(message, F("map_uint32_uint32")) ==
  1459. reflection->MapEnd(message, F("map_uint32_uint32")));
  1460. EXPECT_TRUE(reflection->MapBegin(message, F("map_uint64_uint64")) ==
  1461. reflection->MapEnd(message, F("map_uint64_uint64")));
  1462. EXPECT_TRUE(reflection->MapBegin(message, F("map_sint32_sint32")) ==
  1463. reflection->MapEnd(message, F("map_sint32_sint32")));
  1464. EXPECT_TRUE(reflection->MapBegin(message, F("map_sint64_sint64")) ==
  1465. reflection->MapEnd(message, F("map_sint64_sint64")));
  1466. EXPECT_TRUE(reflection->MapBegin(message, F("map_fixed32_fixed32")) ==
  1467. reflection->MapEnd(message, F("map_fixed32_fixed32")));
  1468. EXPECT_TRUE(reflection->MapBegin(message, F("map_fixed64_fixed64")) ==
  1469. reflection->MapEnd(message, F("map_fixed64_fixed64")));
  1470. EXPECT_TRUE(reflection->MapBegin(message, F("map_sfixed32_sfixed32")) ==
  1471. reflection->MapEnd(message, F("map_sfixed32_sfixed32")));
  1472. EXPECT_TRUE(reflection->MapBegin(message, F("map_sfixed64_sfixed64")) ==
  1473. reflection->MapEnd(message, F("map_sfixed64_sfixed64")));
  1474. EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_float")) ==
  1475. reflection->MapEnd(message, F("map_int32_float")));
  1476. EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_double")) ==
  1477. reflection->MapEnd(message, F("map_int32_double")));
  1478. EXPECT_TRUE(reflection->MapBegin(message, F("map_bool_bool")) ==
  1479. reflection->MapEnd(message, F("map_bool_bool")));
  1480. EXPECT_TRUE(reflection->MapBegin(message, F("map_string_string")) ==
  1481. reflection->MapEnd(message, F("map_string_string")));
  1482. EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_bytes")) ==
  1483. reflection->MapEnd(message, F("map_int32_bytes")));
  1484. EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_enum")) ==
  1485. reflection->MapEnd(message, F("map_int32_enum")));
  1486. EXPECT_TRUE(reflection->MapBegin(message, F("map_int32_foreign_message")) ==
  1487. reflection->MapEnd(message, F("map_int32_foreign_message")));
  1488. }
  1489. void MapReflectionTester::ExpectMapEntryClearViaReflection(
  1490. Message* message) {
  1491. const Reflection* reflection = message->GetReflection();
  1492. const Message* sub_message;
  1493. {
  1494. const FieldDescriptor* descriptor = F("map_int32_int32");
  1495. const FieldDescriptor* key_descriptor =
  1496. descriptor->message_type()->FindFieldByName("key");
  1497. const FieldDescriptor* value_descriptor =
  1498. descriptor->message_type()->FindFieldByName("value");
  1499. sub_message = reflection->AddMessage(message, F("map_int32_int32"));
  1500. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1501. key_descriptor));
  1502. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1503. value_descriptor));
  1504. }
  1505. {
  1506. const FieldDescriptor* descriptor = F("map_int64_int64");
  1507. const FieldDescriptor* key_descriptor =
  1508. descriptor->message_type()->FindFieldByName("key");
  1509. const FieldDescriptor* value_descriptor =
  1510. descriptor->message_type()->FindFieldByName("value");
  1511. sub_message = reflection->AddMessage(message, F("map_int64_int64"));
  1512. EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message,
  1513. key_descriptor));
  1514. EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message,
  1515. value_descriptor));
  1516. }
  1517. {
  1518. const FieldDescriptor* descriptor = F("map_uint32_uint32");
  1519. const FieldDescriptor* key_descriptor =
  1520. descriptor->message_type()->FindFieldByName("key");
  1521. const FieldDescriptor* value_descriptor =
  1522. descriptor->message_type()->FindFieldByName("value");
  1523. sub_message = reflection->AddMessage(message, F("map_uint32_uint32"));
  1524. EXPECT_EQ(0, sub_message->GetReflection()->GetUInt32(*sub_message,
  1525. key_descriptor));
  1526. EXPECT_EQ(0, sub_message->GetReflection()->GetUInt32(*sub_message,
  1527. value_descriptor));
  1528. }
  1529. {
  1530. const FieldDescriptor* descriptor = F("map_uint64_uint64");
  1531. const FieldDescriptor* key_descriptor =
  1532. descriptor->message_type()->FindFieldByName("key");
  1533. const FieldDescriptor* value_descriptor =
  1534. descriptor->message_type()->FindFieldByName("value");
  1535. sub_message = reflection->AddMessage(message, F("map_uint64_uint64"));
  1536. EXPECT_EQ(0, sub_message->GetReflection()->GetUInt64(*sub_message,
  1537. key_descriptor));
  1538. EXPECT_EQ(0, sub_message->GetReflection()->GetUInt64(*sub_message,
  1539. value_descriptor));
  1540. }
  1541. {
  1542. const FieldDescriptor* descriptor = F("map_sint32_sint32");
  1543. const FieldDescriptor* key_descriptor =
  1544. descriptor->message_type()->FindFieldByName("key");
  1545. const FieldDescriptor* value_descriptor =
  1546. descriptor->message_type()->FindFieldByName("value");
  1547. sub_message = reflection->AddMessage(message, F("map_sint32_sint32"));
  1548. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1549. key_descriptor));
  1550. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1551. value_descriptor));
  1552. }
  1553. {
  1554. const FieldDescriptor* descriptor = F("map_sint64_sint64");
  1555. const FieldDescriptor* key_descriptor =
  1556. descriptor->message_type()->FindFieldByName("key");
  1557. const FieldDescriptor* value_descriptor =
  1558. descriptor->message_type()->FindFieldByName("value");
  1559. sub_message = reflection->AddMessage(message, F("map_sint64_sint64"));
  1560. EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message,
  1561. key_descriptor));
  1562. EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message,
  1563. value_descriptor));
  1564. }
  1565. {
  1566. const FieldDescriptor* descriptor = F("map_fixed32_fixed32");
  1567. const FieldDescriptor* key_descriptor =
  1568. descriptor->message_type()->FindFieldByName("key");
  1569. const FieldDescriptor* value_descriptor =
  1570. descriptor->message_type()->FindFieldByName("value");
  1571. sub_message = reflection->AddMessage(message, F("map_fixed32_fixed32"));
  1572. EXPECT_EQ(0, sub_message->GetReflection()->GetUInt32(*sub_message,
  1573. key_descriptor));
  1574. EXPECT_EQ(0, sub_message->GetReflection()->GetUInt32(*sub_message,
  1575. value_descriptor));
  1576. }
  1577. {
  1578. const FieldDescriptor* descriptor = F("map_fixed64_fixed64");
  1579. const FieldDescriptor* key_descriptor =
  1580. descriptor->message_type()->FindFieldByName("key");
  1581. const FieldDescriptor* value_descriptor =
  1582. descriptor->message_type()->FindFieldByName("value");
  1583. sub_message = reflection->AddMessage(message, F("map_fixed64_fixed64"));
  1584. EXPECT_EQ(0, sub_message->GetReflection()->GetUInt64(*sub_message,
  1585. key_descriptor));
  1586. EXPECT_EQ(0, sub_message->GetReflection()->GetUInt64(*sub_message,
  1587. value_descriptor));
  1588. }
  1589. {
  1590. const FieldDescriptor* descriptor = F("map_sfixed32_sfixed32");
  1591. const FieldDescriptor* key_descriptor =
  1592. descriptor->message_type()->FindFieldByName("key");
  1593. const FieldDescriptor* value_descriptor =
  1594. descriptor->message_type()->FindFieldByName("value");
  1595. sub_message = reflection->AddMessage(message, F("map_sfixed32_sfixed32"));
  1596. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1597. key_descriptor));
  1598. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1599. value_descriptor));
  1600. }
  1601. {
  1602. const FieldDescriptor* descriptor = F("map_sfixed64_sfixed64");
  1603. const FieldDescriptor* key_descriptor =
  1604. descriptor->message_type()->FindFieldByName("key");
  1605. const FieldDescriptor* value_descriptor =
  1606. descriptor->message_type()->FindFieldByName("value");
  1607. sub_message = reflection->AddMessage(message, F("map_sfixed64_sfixed64"));
  1608. EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message,
  1609. key_descriptor));
  1610. EXPECT_EQ(0, sub_message->GetReflection()->GetInt64(*sub_message,
  1611. value_descriptor));
  1612. }
  1613. {
  1614. const FieldDescriptor* descriptor = F("map_int32_float");
  1615. const FieldDescriptor* key_descriptor =
  1616. descriptor->message_type()->FindFieldByName("key");
  1617. const FieldDescriptor* value_descriptor =
  1618. descriptor->message_type()->FindFieldByName("value");
  1619. sub_message = reflection->AddMessage(message, F("map_int32_float"));
  1620. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1621. key_descriptor));
  1622. EXPECT_EQ(0, sub_message->GetReflection()->GetFloat(*sub_message,
  1623. value_descriptor));
  1624. }
  1625. {
  1626. const FieldDescriptor* descriptor = F("map_int32_double");
  1627. const FieldDescriptor* key_descriptor =
  1628. descriptor->message_type()->FindFieldByName("key");
  1629. const FieldDescriptor* value_descriptor =
  1630. descriptor->message_type()->FindFieldByName("value");
  1631. sub_message = reflection->AddMessage(message, F("map_int32_double"));
  1632. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1633. key_descriptor));
  1634. EXPECT_EQ(0, sub_message->GetReflection()->GetDouble(*sub_message,
  1635. value_descriptor));
  1636. }
  1637. {
  1638. const FieldDescriptor* descriptor = F("map_bool_bool");
  1639. const FieldDescriptor* key_descriptor =
  1640. descriptor->message_type()->FindFieldByName("key");
  1641. const FieldDescriptor* value_descriptor =
  1642. descriptor->message_type()->FindFieldByName("value");
  1643. sub_message = reflection->AddMessage(message, F("map_bool_bool"));
  1644. EXPECT_EQ(false, sub_message->GetReflection()->GetBool(*sub_message,
  1645. key_descriptor));
  1646. EXPECT_EQ(false, sub_message->GetReflection()->GetBool(*sub_message,
  1647. value_descriptor));
  1648. }
  1649. {
  1650. const FieldDescriptor* descriptor = F("map_string_string");
  1651. const FieldDescriptor* key_descriptor =
  1652. descriptor->message_type()->FindFieldByName("key");
  1653. const FieldDescriptor* value_descriptor =
  1654. descriptor->message_type()->FindFieldByName("value");
  1655. sub_message = reflection->AddMessage(message, F("map_string_string"));
  1656. EXPECT_EQ("", sub_message->GetReflection()->GetString(*sub_message,
  1657. key_descriptor));
  1658. EXPECT_EQ("", sub_message->GetReflection()->GetString(*sub_message,
  1659. value_descriptor));
  1660. }
  1661. {
  1662. const FieldDescriptor* descriptor = F("map_int32_bytes");
  1663. const FieldDescriptor* key_descriptor =
  1664. descriptor->message_type()->FindFieldByName("key");
  1665. const FieldDescriptor* value_descriptor =
  1666. descriptor->message_type()->FindFieldByName("value");
  1667. sub_message = reflection->AddMessage(message, F("map_int32_bytes"));
  1668. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1669. key_descriptor));
  1670. EXPECT_EQ("", sub_message->GetReflection()->GetString(*sub_message,
  1671. value_descriptor));
  1672. }
  1673. {
  1674. const FieldDescriptor* descriptor = F("map_int32_enum");
  1675. const FieldDescriptor* key_descriptor =
  1676. descriptor->message_type()->FindFieldByName("key");
  1677. const FieldDescriptor* value_descriptor =
  1678. descriptor->message_type()->FindFieldByName("value");
  1679. sub_message = reflection->AddMessage(message, F("map_int32_enum"));
  1680. EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message,
  1681. key_descriptor));
  1682. EXPECT_EQ(0, sub_message->GetReflection()
  1683. ->GetEnum(*sub_message, value_descriptor)
  1684. ->number());
  1685. }
  1686. // Map using message as value has been tested in other place. Thus, we don't
  1687. // test it here.
  1688. }
  1689. } // namespace protobuf
  1690. } // namespace google