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