1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Author: tgs@google.com (Tom Szymanski)
32 //
33 // Test reflection methods for aggregate access to Repeated[Ptr]Fields.
34 // This test proto2 methods on a proto2 layout.
35
36 #include <google/protobuf/stubs/casts.h>
37 #include <google/protobuf/stubs/stringprintf.h>
38 #include <google/protobuf/test_util.h>
39 #include <google/protobuf/unittest.pb.h>
40 #include <google/protobuf/dynamic_message.h>
41 #include <google/protobuf/reflection.h>
42 #include <gtest/gtest.h>
43
44 namespace google {
45 namespace protobuf {
46
47 using unittest::ForeignMessage;
48 using unittest::TestAllExtensions;
49 using unittest::TestAllTypes;
50
51 namespace {
52
Func(int i,int j)53 static int Func(int i, int j) { return i * j; }
54
StrFunc(int i,int j)55 static std::string StrFunc(int i, int j) { return StrCat(Func(i, 4)); }
56
TEST(RepeatedFieldReflectionTest,RegularFields)57 TEST(RepeatedFieldReflectionTest, RegularFields) {
58 TestAllTypes message;
59 const Reflection* refl = message.GetReflection();
60 const Descriptor* desc = message.GetDescriptor();
61
62 for (int i = 0; i < 10; ++i) {
63 message.add_repeated_int32(Func(i, 1));
64 message.add_repeated_double(Func(i, 2));
65 message.add_repeated_string(StrFunc(i, 5));
66 message.add_repeated_foreign_message()->set_c(Func(i, 6));
67 }
68
69 // Get FieldDescriptors for all the fields of interest.
70 const FieldDescriptor* fd_repeated_int32 =
71 desc->FindFieldByName("repeated_int32");
72 const FieldDescriptor* fd_repeated_double =
73 desc->FindFieldByName("repeated_double");
74 const FieldDescriptor* fd_repeated_string =
75 desc->FindFieldByName("repeated_string");
76 const FieldDescriptor* fd_repeated_foreign_message =
77 desc->FindFieldByName("repeated_foreign_message");
78
79 // Get RepeatedField objects for all fields of interest.
80 const RepeatedField<int32>& rf_int32 =
81 refl->GetRepeatedField<int32>(message, fd_repeated_int32);
82 const RepeatedField<double>& rf_double =
83 refl->GetRepeatedField<double>(message, fd_repeated_double);
84
85 // Get mutable RepeatedField objects for all fields of interest.
86 RepeatedField<int32>* mrf_int32 =
87 refl->MutableRepeatedField<int32>(&message, fd_repeated_int32);
88 RepeatedField<double>* mrf_double =
89 refl->MutableRepeatedField<double>(&message, fd_repeated_double);
90
91 // Get RepeatedPtrField objects for all fields of interest.
92 const RepeatedPtrField<std::string>& rpf_string =
93 refl->GetRepeatedPtrField<std::string>(message, fd_repeated_string);
94 const RepeatedPtrField<ForeignMessage>& rpf_foreign_message =
95 refl->GetRepeatedPtrField<ForeignMessage>(message,
96 fd_repeated_foreign_message);
97 const RepeatedPtrField<Message>& rpf_message =
98 refl->GetRepeatedPtrField<Message>(message, fd_repeated_foreign_message);
99
100 // Get mutable RepeatedPtrField objects for all fields of interest.
101 RepeatedPtrField<std::string>* mrpf_string =
102 refl->MutableRepeatedPtrField<std::string>(&message, fd_repeated_string);
103 RepeatedPtrField<ForeignMessage>* mrpf_foreign_message =
104 refl->MutableRepeatedPtrField<ForeignMessage>(
105 &message, fd_repeated_foreign_message);
106 RepeatedPtrField<Message>* mrpf_message =
107 refl->MutableRepeatedPtrField<Message>(&message,
108 fd_repeated_foreign_message);
109
110 // Make sure we can do gets and sets through the Repeated[Ptr]Field objects.
111 for (int i = 0; i < 10; ++i) {
112 // Check gets through const objects.
113 EXPECT_EQ(rf_int32.Get(i), Func(i, 1));
114 EXPECT_EQ(rf_double.Get(i), Func(i, 2));
115 EXPECT_EQ(rpf_string.Get(i), StrFunc(i, 5));
116 EXPECT_EQ(rpf_foreign_message.Get(i).c(), Func(i, 6));
117 EXPECT_EQ(down_cast<const ForeignMessage*>(&rpf_message.Get(i))->c(),
118 Func(i, 6));
119
120 // Check gets through mutable objects.
121 EXPECT_EQ(mrf_int32->Get(i), Func(i, 1));
122 EXPECT_EQ(mrf_double->Get(i), Func(i, 2));
123 EXPECT_EQ(mrpf_string->Get(i), StrFunc(i, 5));
124 EXPECT_EQ(mrpf_foreign_message->Get(i).c(), Func(i, 6));
125 EXPECT_EQ(down_cast<const ForeignMessage*>(&mrpf_message->Get(i))->c(),
126 Func(i, 6));
127
128 // Check sets through mutable objects.
129 mrf_int32->Set(i, Func(i, -1));
130 mrf_double->Set(i, Func(i, -2));
131 mrpf_string->Mutable(i)->assign(StrFunc(i, -5));
132 mrpf_foreign_message->Mutable(i)->set_c(Func(i, -6));
133 EXPECT_EQ(message.repeated_int32(i), Func(i, -1));
134 EXPECT_EQ(message.repeated_double(i), Func(i, -2));
135 EXPECT_EQ(message.repeated_string(i), StrFunc(i, -5));
136 EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, -6));
137 down_cast<ForeignMessage*>(mrpf_message->Mutable(i))->set_c(Func(i, 7));
138 EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, 7));
139 }
140
141 #ifdef PROTOBUF_HAS_DEATH_TEST
142 // Make sure types are checked correctly at runtime.
143 const FieldDescriptor* fd_optional_int32 =
144 desc->FindFieldByName("optional_int32");
145 EXPECT_DEATH(refl->GetRepeatedField<int32>(message, fd_optional_int32),
146 "requires a repeated field");
147 EXPECT_DEATH(refl->GetRepeatedField<double>(message, fd_repeated_int32),
148 "not the right type");
149 EXPECT_DEATH(refl->GetRepeatedPtrField<TestAllTypes>(
150 message, fd_repeated_foreign_message),
151 "wrong submessage type");
152 #endif // PROTOBUF_HAS_DEATH_TEST
153 }
154
155
TEST(RepeatedFieldReflectionTest,ExtensionFields)156 TEST(RepeatedFieldReflectionTest, ExtensionFields) {
157 TestAllExtensions extended_message;
158 const Reflection* refl = extended_message.GetReflection();
159 const Descriptor* desc = extended_message.GetDescriptor();
160
161 for (int i = 0; i < 10; ++i) {
162 extended_message.AddExtension(unittest::repeated_int64_extension,
163 Func(i, 1));
164 }
165
166 const FieldDescriptor* fd_repeated_int64_extension =
167 desc->file()->FindExtensionByName("repeated_int64_extension");
168 GOOGLE_CHECK(fd_repeated_int64_extension != NULL);
169
170 const RepeatedField<int64>& rf_int64_extension =
171 refl->GetRepeatedField<int64>(extended_message,
172 fd_repeated_int64_extension);
173
174 RepeatedField<int64>* mrf_int64_extension = refl->MutableRepeatedField<int64>(
175 &extended_message, fd_repeated_int64_extension);
176
177 for (int i = 0; i < 10; ++i) {
178 EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i));
179 mrf_int64_extension->Set(i, Func(i, -1));
180 EXPECT_EQ(Func(i, -1), extended_message.GetExtension(
181 unittest::repeated_int64_extension, i));
182 }
183 }
184
185 template <typename Ref, typename MessageType, typename ValueType>
TestRepeatedFieldRefIteratorForPrimitive(const Ref & handle,const MessageType & message,ValueType (MessageType::* GetFunc)(int)const)186 void TestRepeatedFieldRefIteratorForPrimitive(
187 const Ref& handle, const MessageType& message,
188 ValueType (MessageType::*GetFunc)(int) const) {
189 int index = 0;
190 for (typename Ref::const_iterator it = handle.begin(); it != handle.end();
191 ++it) {
192 EXPECT_EQ((message.*GetFunc)(index), *it);
193 ++index;
194 }
195 EXPECT_EQ(handle.size(), index);
196 }
197
198 template <typename MessageType, typename ValueType>
TestRepeatedFieldRefIteratorForString(const RepeatedFieldRef<std::string> & handle,const MessageType & message,ValueType (MessageType::* GetFunc)(int)const)199 void TestRepeatedFieldRefIteratorForString(
200 const RepeatedFieldRef<std::string>& handle, const MessageType& message,
201 ValueType (MessageType::*GetFunc)(int) const) {
202 int index = 0;
203 for (typename RepeatedFieldRef<std::string>::const_iterator it =
204 handle.begin();
205 it != handle.end(); ++it) {
206 // Test both operator* and operator->
207 EXPECT_EQ((message.*GetFunc)(index), *it);
208 EXPECT_EQ((message.*GetFunc)(index).size(), it->size());
209 ++index;
210 }
211 EXPECT_EQ(handle.size(), index);
212 }
213
TEST(RepeatedFieldReflectionTest,RepeatedFieldRefForRegularFields)214 TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForRegularFields) {
215 TestAllTypes message;
216 const Reflection* refl = message.GetReflection();
217 const Descriptor* desc = message.GetDescriptor();
218
219 for (int i = 0; i < 10; ++i) {
220 message.add_repeated_int32(Func(i, 1));
221 message.add_repeated_double(Func(i, 2));
222 message.add_repeated_string(StrFunc(i, 5));
223 message.add_repeated_foreign_message()->set_c(Func(i, 6));
224 }
225
226 // Get FieldDescriptors for all the fields of interest.
227 const FieldDescriptor* fd_repeated_int32 =
228 desc->FindFieldByName("repeated_int32");
229 const FieldDescriptor* fd_repeated_double =
230 desc->FindFieldByName("repeated_double");
231 const FieldDescriptor* fd_repeated_string =
232 desc->FindFieldByName("repeated_string");
233 const FieldDescriptor* fd_repeated_foreign_message =
234 desc->FindFieldByName("repeated_foreign_message");
235
236 // Get RepeatedFieldRef objects for all fields of interest.
237 const RepeatedFieldRef<int32> rf_int32 =
238 refl->GetRepeatedFieldRef<int32>(message, fd_repeated_int32);
239 const RepeatedFieldRef<double> rf_double =
240 refl->GetRepeatedFieldRef<double>(message, fd_repeated_double);
241 const RepeatedFieldRef<std::string> rf_string =
242 refl->GetRepeatedFieldRef<std::string>(message, fd_repeated_string);
243 const RepeatedFieldRef<ForeignMessage> rf_foreign_message =
244 refl->GetRepeatedFieldRef<ForeignMessage>(message,
245 fd_repeated_foreign_message);
246 const RepeatedFieldRef<Message> rf_message =
247 refl->GetRepeatedFieldRef<Message>(message, fd_repeated_foreign_message);
248
249 // Get MutableRepeatedFieldRef objects for all fields of interest.
250 const MutableRepeatedFieldRef<int32> mrf_int32 =
251 refl->GetMutableRepeatedFieldRef<int32>(&message, fd_repeated_int32);
252 const MutableRepeatedFieldRef<double> mrf_double =
253 refl->GetMutableRepeatedFieldRef<double>(&message, fd_repeated_double);
254 const MutableRepeatedFieldRef<std::string> mrf_string =
255 refl->GetMutableRepeatedFieldRef<std::string>(&message,
256 fd_repeated_string);
257 const MutableRepeatedFieldRef<ForeignMessage> mrf_foreign_message =
258 refl->GetMutableRepeatedFieldRef<ForeignMessage>(
259 &message, fd_repeated_foreign_message);
260 const MutableRepeatedFieldRef<Message> mrf_message =
261 refl->GetMutableRepeatedFieldRef<Message>(&message,
262 fd_repeated_foreign_message);
263
264 EXPECT_EQ(message.repeated_int32_size(), rf_int32.size());
265 EXPECT_EQ(message.repeated_int32_size(), mrf_int32.size());
266 EXPECT_EQ(message.repeated_double_size(), rf_double.size());
267 EXPECT_EQ(message.repeated_double_size(), mrf_double.size());
268 EXPECT_EQ(message.repeated_string_size(), rf_string.size());
269 EXPECT_EQ(message.repeated_string_size(), mrf_string.size());
270 EXPECT_EQ(message.repeated_foreign_message_size(), rf_foreign_message.size());
271 EXPECT_EQ(message.repeated_foreign_message_size(),
272 mrf_foreign_message.size());
273 EXPECT_EQ(message.repeated_foreign_message_size(), rf_message.size());
274 EXPECT_EQ(message.repeated_foreign_message_size(), mrf_message.size());
275
276 EXPECT_FALSE(rf_int32.empty());
277 EXPECT_FALSE(mrf_int32.empty());
278 EXPECT_FALSE(rf_double.empty());
279 EXPECT_FALSE(mrf_double.empty());
280 EXPECT_FALSE(rf_string.empty());
281 EXPECT_FALSE(mrf_string.empty());
282 EXPECT_FALSE(rf_foreign_message.empty());
283 EXPECT_FALSE(mrf_foreign_message.empty());
284 EXPECT_FALSE(rf_message.empty());
285 EXPECT_FALSE(mrf_message.empty());
286
287 // Make sure we can do gets and sets through the RepeatedFieldRef objects.
288 for (int i = 0; i < 10; ++i) {
289 // Check gets through const objects.
290 EXPECT_EQ(rf_int32.Get(i), Func(i, 1));
291 EXPECT_EQ(rf_double.Get(i), Func(i, 2));
292 EXPECT_EQ(rf_string.Get(i), StrFunc(i, 5));
293 ForeignMessage scratch_space;
294 EXPECT_EQ(rf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6));
295 EXPECT_EQ(
296 down_cast<const ForeignMessage&>(rf_message.Get(i, &scratch_space)).c(),
297 Func(i, 6));
298
299 // Check gets through mutable objects.
300 EXPECT_EQ(mrf_int32.Get(i), Func(i, 1));
301 EXPECT_EQ(mrf_double.Get(i), Func(i, 2));
302 EXPECT_EQ(mrf_string.Get(i), StrFunc(i, 5));
303 EXPECT_EQ(mrf_foreign_message.Get(i, &scratch_space).c(), Func(i, 6));
304 EXPECT_EQ(
305 down_cast<const ForeignMessage&>(mrf_message.Get(i, &scratch_space))
306 .c(),
307 Func(i, 6));
308
309 // Check sets through mutable objects.
310 mrf_int32.Set(i, Func(i, -1));
311 mrf_double.Set(i, Func(i, -2));
312 mrf_string.Set(i, StrFunc(i, -5));
313 ForeignMessage foreign_message;
314 foreign_message.set_c(Func(i, -6));
315 mrf_foreign_message.Set(i, foreign_message);
316 EXPECT_EQ(message.repeated_int32(i), Func(i, -1));
317 EXPECT_EQ(message.repeated_double(i), Func(i, -2));
318 EXPECT_EQ(message.repeated_string(i), StrFunc(i, -5));
319 EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, -6));
320 foreign_message.set_c(Func(i, 7));
321 mrf_message.Set(i, foreign_message);
322 EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, 7));
323 }
324
325 // Test iterators.
326 TestRepeatedFieldRefIteratorForPrimitive(rf_int32, message,
327 &TestAllTypes::repeated_int32);
328 TestRepeatedFieldRefIteratorForPrimitive(rf_double, message,
329 &TestAllTypes::repeated_double);
330 TestRepeatedFieldRefIteratorForString(rf_string, message,
331 &TestAllTypes::repeated_string);
332
333 // Test iterators for message fields.
334 typedef RepeatedFieldRef<ForeignMessage>::iterator MessageIterator;
335 int index = 0;
336 for (MessageIterator it = rf_foreign_message.begin();
337 it != rf_foreign_message.end(); ++it) {
338 EXPECT_EQ(message.repeated_foreign_message(index).c(), it->c());
339 ++index;
340 }
341 EXPECT_EQ(10, index);
342
343 // Test iterator operators that are not usually used in regular for-loops.
344 // Including: post increment, assign, ==.
345 MessageIterator old_it = rf_foreign_message.begin();
346 MessageIterator new_it = old_it++;
347 EXPECT_FALSE(old_it == new_it);
348 // Check that old_it++ increments old_it once.
349 for (index = 1; old_it != rf_foreign_message.end(); ++old_it, ++index) {
350 EXPECT_EQ(message.repeated_foreign_message(index).c(), old_it->c());
351 }
352 EXPECT_EQ(10, index);
353 // Test assign operator.
354 old_it = new_it;
355 for (index = 0; old_it != rf_foreign_message.end(); ++old_it, ++index) {
356 EXPECT_EQ(message.repeated_foreign_message(index).c(), old_it->c());
357 }
358 EXPECT_EQ(10, index);
359 // Check that the returned value of old_it++ is the one before increment.
360 for (index = 0; new_it != rf_foreign_message.end(); ++new_it, ++index) {
361 EXPECT_EQ(message.repeated_foreign_message(index).c(), new_it->c());
362 }
363 EXPECT_EQ(10, index);
364
365 // Test MutableRepeatedFieldRef::Add()
366 mrf_int32.Add(1234);
367 mrf_double.Add(1234.0);
368 mrf_string.Add("1234");
369 ForeignMessage foreign_message;
370 foreign_message.set_c(1234);
371 mrf_foreign_message.Add(foreign_message);
372 EXPECT_EQ(1234, message.repeated_int32(10));
373 EXPECT_EQ(1234.0, message.repeated_double(10));
374 EXPECT_EQ("1234", message.repeated_string(10));
375 EXPECT_EQ(1234, message.repeated_foreign_message(10).c());
376
377 // Test MutableRepeatedFieldRef::RemoveLast()
378 mrf_int32.RemoveLast();
379 mrf_double.RemoveLast();
380 mrf_string.RemoveLast();
381 mrf_foreign_message.RemoveLast();
382 EXPECT_EQ(10, message.repeated_int32_size());
383 EXPECT_EQ(10, message.repeated_double_size());
384 EXPECT_EQ(10, message.repeated_string_size());
385 EXPECT_EQ(10, message.repeated_foreign_message_size());
386
387 // Test MutableRepeatedFieldRef::SwapElements()
388 mrf_int32.SwapElements(0, 9);
389 mrf_double.SwapElements(0, 9);
390 mrf_string.SwapElements(0, 9);
391 mrf_foreign_message.SwapElements(0, 9);
392 EXPECT_EQ(Func(9, -1), message.repeated_int32(0));
393 EXPECT_EQ(Func(0, -1), message.repeated_int32(9));
394 EXPECT_EQ(Func(9, -2), message.repeated_double(0));
395 EXPECT_EQ(Func(0, -2), message.repeated_double(9));
396 EXPECT_EQ(StrFunc(9, -5), message.repeated_string(0));
397 EXPECT_EQ(StrFunc(0, -5), message.repeated_string(9));
398 EXPECT_EQ(Func(9, 7), message.repeated_foreign_message(0).c());
399 EXPECT_EQ(Func(0, 7), message.repeated_foreign_message(9).c());
400
401 // Test MutableRepeatedFieldRef::Clear()
402 mrf_int32.Clear();
403 mrf_double.Clear();
404 mrf_string.Clear();
405 mrf_foreign_message.Clear();
406 EXPECT_EQ(0, message.repeated_int32_size());
407 EXPECT_EQ(0, message.repeated_double_size());
408 EXPECT_EQ(0, message.repeated_string_size());
409 EXPECT_EQ(0, message.repeated_foreign_message_size());
410
411 // Test (Mutable)RepeatedFieldRef::empty()
412 EXPECT_TRUE(rf_int32.empty());
413 EXPECT_TRUE(mrf_int32.empty());
414 EXPECT_TRUE(rf_double.empty());
415 EXPECT_TRUE(mrf_double.empty());
416 EXPECT_TRUE(rf_string.empty());
417 EXPECT_TRUE(mrf_string.empty());
418 EXPECT_TRUE(rf_foreign_message.empty());
419 EXPECT_TRUE(mrf_foreign_message.empty());
420 EXPECT_TRUE(rf_message.empty());
421 EXPECT_TRUE(mrf_message.empty());
422
423 #ifdef PROTOBUF_HAS_DEATH_TEST
424
425 // Make sure types are checked correctly at runtime.
426 const FieldDescriptor* fd_optional_int32 =
427 desc->FindFieldByName("optional_int32");
428 EXPECT_DEATH(refl->GetRepeatedFieldRef<int32>(message, fd_optional_int32),
429 "");
430 EXPECT_DEATH(refl->GetRepeatedFieldRef<double>(message, fd_repeated_int32),
431 "");
432 EXPECT_DEATH(refl->GetRepeatedFieldRef<TestAllTypes>(
433 message, fd_repeated_foreign_message),
434 "");
435
436 #endif // PROTOBUF_HAS_DEATH_TEST
437 }
438
TEST(RepeatedFieldReflectionTest,RepeatedFieldRefForEnums)439 TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForEnums) {
440 TestAllTypes message;
441 const Reflection* refl = message.GetReflection();
442 const Descriptor* desc = message.GetDescriptor();
443
444 for (int i = 0; i < 10; ++i) {
445 message.add_repeated_nested_enum(TestAllTypes::BAR);
446 }
447
448 const FieldDescriptor* fd_repeated_nested_enum =
449 desc->FindFieldByName("repeated_nested_enum");
450 const RepeatedFieldRef<TestAllTypes::NestedEnum> enum_ref =
451 refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
452 message, fd_repeated_nested_enum);
453 const MutableRepeatedFieldRef<TestAllTypes::NestedEnum> mutable_enum_ref =
454 refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
455 &message, fd_repeated_nested_enum);
456 const RepeatedFieldRef<int32> int32_ref =
457 refl->GetRepeatedFieldRef<int32>(message, fd_repeated_nested_enum);
458 const MutableRepeatedFieldRef<int32> mutable_int32_ref =
459 refl->GetMutableRepeatedFieldRef<int32>(&message,
460 fd_repeated_nested_enum);
461
462 EXPECT_EQ(message.repeated_nested_enum_size(), enum_ref.size());
463 EXPECT_EQ(message.repeated_nested_enum_size(), mutable_enum_ref.size());
464 EXPECT_EQ(message.repeated_nested_enum_size(), int32_ref.size());
465 EXPECT_EQ(message.repeated_nested_enum_size(), mutable_int32_ref.size());
466
467 EXPECT_FALSE(enum_ref.empty());
468 EXPECT_FALSE(mutable_enum_ref.empty());
469 EXPECT_FALSE(int32_ref.empty());
470 EXPECT_FALSE(mutable_int32_ref.empty());
471
472 for (int i = 0; i < 10; ++i) {
473 EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(i));
474 EXPECT_EQ(TestAllTypes::BAR, mutable_enum_ref.Get(i));
475 mutable_enum_ref.Set(i, TestAllTypes::BAZ);
476 EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(i));
477 EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(i));
478
479 message.set_repeated_nested_enum(i, TestAllTypes::BAR);
480 EXPECT_EQ(TestAllTypes::BAR, int32_ref.Get(i));
481 EXPECT_EQ(TestAllTypes::BAR, mutable_int32_ref.Get(i));
482 mutable_int32_ref.Set(i, TestAllTypes::BAZ);
483 EXPECT_EQ(TestAllTypes::BAZ, int32_ref.Get(i));
484 EXPECT_EQ(TestAllTypes::BAZ, message.repeated_nested_enum(i));
485 }
486
487 TestRepeatedFieldRefIteratorForPrimitive(enum_ref, message,
488 &TestAllTypes::repeated_nested_enum);
489 TestRepeatedFieldRefIteratorForPrimitive(int32_ref, message,
490 &TestAllTypes::repeated_nested_enum);
491
492 // Test Add()
493 mutable_enum_ref.Add(TestAllTypes::FOO);
494 EXPECT_EQ(TestAllTypes::FOO, message.repeated_nested_enum(10));
495 mutable_int32_ref.Add(TestAllTypes::BAR);
496 EXPECT_EQ(TestAllTypes::BAR, message.repeated_nested_enum(11));
497
498 // Test RemoveLast()
499 mutable_enum_ref.RemoveLast();
500 EXPECT_EQ(11, message.repeated_nested_enum_size());
501 mutable_int32_ref.RemoveLast();
502 EXPECT_EQ(10, message.repeated_nested_enum_size());
503
504 // Test SwapElements()
505 mutable_enum_ref.Set(0, TestAllTypes::BAR);
506 mutable_enum_ref.Set(9, TestAllTypes::BAZ);
507 mutable_enum_ref.SwapElements(0, 9);
508 EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(0));
509 EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(9));
510 mutable_int32_ref.SwapElements(0, 9);
511 EXPECT_EQ(TestAllTypes::BAR, enum_ref.Get(0));
512 EXPECT_EQ(TestAllTypes::BAZ, enum_ref.Get(9));
513
514 // Test Clear()
515 mutable_enum_ref.Clear();
516 EXPECT_EQ(0, message.repeated_nested_enum_size());
517 mutable_enum_ref.Add(TestAllTypes::FOO);
518 EXPECT_EQ(1, message.repeated_nested_enum_size());
519 mutable_int32_ref.Clear();
520 EXPECT_EQ(0, message.repeated_nested_enum_size());
521
522 // Test empty()
523 EXPECT_TRUE(enum_ref.empty());
524 EXPECT_TRUE(mutable_enum_ref.empty());
525 EXPECT_TRUE(int32_ref.empty());
526 EXPECT_TRUE(mutable_int32_ref.empty());
527 }
528
TEST(RepeatedFieldReflectionTest,RepeatedFieldRefForExtensionFields)529 TEST(RepeatedFieldReflectionTest, RepeatedFieldRefForExtensionFields) {
530 TestAllExtensions extended_message;
531 const Reflection* refl = extended_message.GetReflection();
532 const Descriptor* desc = extended_message.GetDescriptor();
533
534 for (int i = 0; i < 10; ++i) {
535 extended_message.AddExtension(unittest::repeated_int64_extension,
536 Func(i, 1));
537 }
538
539 const FieldDescriptor* fd_repeated_int64_extension =
540 desc->file()->FindExtensionByName("repeated_int64_extension");
541 GOOGLE_CHECK(fd_repeated_int64_extension != NULL);
542
543 const RepeatedFieldRef<int64> rf_int64_extension =
544 refl->GetRepeatedFieldRef<int64>(extended_message,
545 fd_repeated_int64_extension);
546
547 const MutableRepeatedFieldRef<int64> mrf_int64_extension =
548 refl->GetMutableRepeatedFieldRef<int64>(&extended_message,
549 fd_repeated_int64_extension);
550
551 for (int i = 0; i < 10; ++i) {
552 EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i));
553 mrf_int64_extension.Set(i, Func(i, -1));
554 EXPECT_EQ(Func(i, -1), extended_message.GetExtension(
555 unittest::repeated_int64_extension, i));
556 }
557 }
558
559
TEST(RepeatedFieldReflectionTest,RepeatedFieldRefMergeFromAndSwap)560 TEST(RepeatedFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) {
561 // Set-up message content.
562 TestAllTypes m0, m1, m2;
563 for (int i = 0; i < 10; ++i) {
564 m0.add_repeated_int32(Func(i, 1));
565 m0.add_repeated_double(Func(i, 2));
566 m0.add_repeated_string(StrFunc(i, 5));
567 m0.add_repeated_foreign_message()->set_c(Func(i, 6));
568 m0.add_repeated_nested_enum(TestAllTypes::FOO);
569 m1.add_repeated_int32(Func(i, 11));
570 m1.add_repeated_double(Func(i, 12));
571 m1.add_repeated_string(StrFunc(i, 15));
572 m1.add_repeated_foreign_message()->set_c(Func(i, 16));
573 m1.add_repeated_nested_enum(TestAllTypes::BAR);
574 m2.add_repeated_int32(Func(i, 21));
575 m2.add_repeated_double(Func(i, 22));
576 m2.add_repeated_string(StrFunc(i, 25));
577 m2.add_repeated_foreign_message()->set_c(Func(i, 26));
578 m2.add_repeated_nested_enum(TestAllTypes::BAZ);
579 }
580
581 const Reflection* refl = m0.GetReflection();
582 const Descriptor* desc = m0.GetDescriptor();
583
584 // Get FieldDescriptors for all the fields of interest.
585 const FieldDescriptor* fd_repeated_int32 =
586 desc->FindFieldByName("repeated_int32");
587 const FieldDescriptor* fd_repeated_double =
588 desc->FindFieldByName("repeated_double");
589 const FieldDescriptor* fd_repeated_string =
590 desc->FindFieldByName("repeated_string");
591 const FieldDescriptor* fd_repeated_foreign_message =
592 desc->FindFieldByName("repeated_foreign_message");
593 const FieldDescriptor* fd_repeated_nested_enum =
594 desc->FindFieldByName("repeated_nested_enum");
595
596 // Get MutableRepeatedFieldRef objects for all fields of interest.
597 const MutableRepeatedFieldRef<int32> mrf_int32 =
598 refl->GetMutableRepeatedFieldRef<int32>(&m0, fd_repeated_int32);
599 const MutableRepeatedFieldRef<double> mrf_double =
600 refl->GetMutableRepeatedFieldRef<double>(&m0, fd_repeated_double);
601 const MutableRepeatedFieldRef<std::string> mrf_string =
602 refl->GetMutableRepeatedFieldRef<std::string>(&m0, fd_repeated_string);
603 const MutableRepeatedFieldRef<ForeignMessage> mrf_foreign_message =
604 refl->GetMutableRepeatedFieldRef<ForeignMessage>(
605 &m0, fd_repeated_foreign_message);
606 const MutableRepeatedFieldRef<TestAllTypes::NestedEnum> mrf_nested_enum =
607 refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
608 &m0, fd_repeated_nested_enum);
609
610 // Test MutableRepeatedRef::CopyFrom
611 mrf_int32.CopyFrom(refl->GetRepeatedFieldRef<int32>(m1, fd_repeated_int32));
612 mrf_double.CopyFrom(
613 refl->GetRepeatedFieldRef<double>(m1, fd_repeated_double));
614 mrf_string.CopyFrom(
615 refl->GetRepeatedFieldRef<std::string>(m1, fd_repeated_string));
616 mrf_foreign_message.CopyFrom(refl->GetRepeatedFieldRef<ForeignMessage>(
617 m1, fd_repeated_foreign_message));
618 mrf_nested_enum.CopyFrom(refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
619 m1, fd_repeated_nested_enum));
620 for (int i = 0; i < 10; ++i) {
621 EXPECT_EQ(Func(i, 11), m0.repeated_int32(i));
622 EXPECT_EQ(Func(i, 12), m0.repeated_double(i));
623 EXPECT_EQ(StrFunc(i, 15), m0.repeated_string(i));
624 EXPECT_EQ(Func(i, 16), m0.repeated_foreign_message(i).c());
625 EXPECT_EQ(TestAllTypes::BAR, m0.repeated_nested_enum(i));
626 }
627
628 // Test MutableRepeatedRef::MergeFrom
629 mrf_int32.MergeFrom(refl->GetRepeatedFieldRef<int32>(m2, fd_repeated_int32));
630 mrf_double.MergeFrom(
631 refl->GetRepeatedFieldRef<double>(m2, fd_repeated_double));
632 mrf_string.MergeFrom(
633 refl->GetRepeatedFieldRef<std::string>(m2, fd_repeated_string));
634 mrf_foreign_message.MergeFrom(refl->GetRepeatedFieldRef<ForeignMessage>(
635 m2, fd_repeated_foreign_message));
636 mrf_nested_enum.MergeFrom(refl->GetRepeatedFieldRef<TestAllTypes::NestedEnum>(
637 m2, fd_repeated_nested_enum));
638 for (int i = 0; i < 10; ++i) {
639 EXPECT_EQ(Func(i, 21), m0.repeated_int32(i + 10));
640 EXPECT_EQ(Func(i, 22), m0.repeated_double(i + 10));
641 EXPECT_EQ(StrFunc(i, 25), m0.repeated_string(i + 10));
642 EXPECT_EQ(Func(i, 26), m0.repeated_foreign_message(i + 10).c());
643 EXPECT_EQ(TestAllTypes::BAZ, m0.repeated_nested_enum(i + 10));
644 }
645
646 // Test MutableRepeatedRef::Swap
647 // Swap between m0 and m2.
648 mrf_int32.Swap(
649 refl->GetMutableRepeatedFieldRef<int32>(&m2, fd_repeated_int32));
650 mrf_double.Swap(
651 refl->GetMutableRepeatedFieldRef<double>(&m2, fd_repeated_double));
652 mrf_string.Swap(
653 refl->GetMutableRepeatedFieldRef<std::string>(&m2, fd_repeated_string));
654 mrf_foreign_message.Swap(refl->GetMutableRepeatedFieldRef<ForeignMessage>(
655 &m2, fd_repeated_foreign_message));
656 mrf_nested_enum.Swap(
657 refl->GetMutableRepeatedFieldRef<TestAllTypes::NestedEnum>(
658 &m2, fd_repeated_nested_enum));
659 for (int i = 0; i < 10; ++i) {
660 // Check the content of m0.
661 EXPECT_EQ(Func(i, 21), m0.repeated_int32(i));
662 EXPECT_EQ(Func(i, 22), m0.repeated_double(i));
663 EXPECT_EQ(StrFunc(i, 25), m0.repeated_string(i));
664 EXPECT_EQ(Func(i, 26), m0.repeated_foreign_message(i).c());
665 EXPECT_EQ(TestAllTypes::BAZ, m0.repeated_nested_enum(i));
666
667 // Check the content of m2.
668 EXPECT_EQ(Func(i, 11), m2.repeated_int32(i));
669 EXPECT_EQ(Func(i, 12), m2.repeated_double(i));
670 EXPECT_EQ(StrFunc(i, 15), m2.repeated_string(i));
671 EXPECT_EQ(Func(i, 16), m2.repeated_foreign_message(i).c());
672 EXPECT_EQ(TestAllTypes::BAR, m2.repeated_nested_enum(i));
673 EXPECT_EQ(Func(i, 21), m2.repeated_int32(i + 10));
674 EXPECT_EQ(Func(i, 22), m2.repeated_double(i + 10));
675 EXPECT_EQ(StrFunc(i, 25), m2.repeated_string(i + 10));
676 EXPECT_EQ(Func(i, 26), m2.repeated_foreign_message(i + 10).c());
677 EXPECT_EQ(TestAllTypes::BAZ, m2.repeated_nested_enum(i + 10));
678 }
679 }
680
681 // Test that GetRepeatedFieldRef/MutableRepeatedFieldRef works with
682 // DynamicMessage.
TEST(RepeatedFieldReflectionTest,RepeatedFieldRefDynamicMessage)683 TEST(RepeatedFieldReflectionTest, RepeatedFieldRefDynamicMessage) {
684 // DynamicMessage shares the same memory layout as generated message
685 // and use the same GeneratedMessageReflection code for reflection.
686 // All code paths should already be covered by the other tests for
687 // generated messages. Here we just test one field.
688
689 const Descriptor* desc = TestAllTypes::descriptor();
690 const FieldDescriptor* fd_repeated_int32 =
691 desc->FindFieldByName("repeated_int32");
692
693 DynamicMessageFactory factory;
694 std::unique_ptr<Message> dynamic_message(factory.GetPrototype(desc)->New());
695 const Reflection* refl = dynamic_message->GetReflection();
696
697 MutableRepeatedFieldRef<int32> rf_int32 =
698 refl->GetMutableRepeatedFieldRef<int32>(dynamic_message.get(),
699 fd_repeated_int32);
700 rf_int32.Add(1234);
701 EXPECT_EQ(1, refl->FieldSize(*dynamic_message, fd_repeated_int32));
702 EXPECT_EQ(1234,
703 refl->GetRepeatedInt32(*dynamic_message, fd_repeated_int32, 0));
704 }
705
706 } // namespace
707 } // namespace protobuf
708 } // namespace google
709