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