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