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