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