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