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