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


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:01