tizen 2.3.1 release
[external/protobuf.git] / src / google / protobuf / repeated_field_reflection_unittest.cc
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/common.h>
37 #include <google/protobuf/stubs/stringprintf.h>
38 #include <google/protobuf/unittest.pb.h>
39 #include <google/protobuf/test_util.h>
40 #include <gtest/gtest.h>
41
42 namespace google {
43 namespace protobuf {
44
45 using unittest::ForeignMessage;
46 using unittest::TestAllTypes;
47 using unittest::TestAllExtensions;
48
49 namespace {
50
51 static int Func(int i, int j) {
52   return i * j;
53 }
54
55 static string StrFunc(int i, int j) {
56   string str;
57   SStringPrintf(&str, "%d", Func(i, 4));
58   return str;
59 }
60
61
62 TEST(RepeatedFieldReflectionTest, RegularFields) {
63   TestAllTypes message;
64   const Reflection* refl = message.GetReflection();
65   const Descriptor* desc = message.GetDescriptor();
66
67   for (int i = 0; i < 10; ++i) {
68     message.add_repeated_int32(Func(i, 1));
69     message.add_repeated_double(Func(i, 2));
70     message.add_repeated_string(StrFunc(i, 5));
71     message.add_repeated_foreign_message()->set_c(Func(i, 6));
72   }
73
74   // Get FieldDescriptors for all the fields of interest.
75   const FieldDescriptor* fd_repeated_int32 =
76       desc->FindFieldByName("repeated_int32");
77   const FieldDescriptor* fd_repeated_double =
78       desc->FindFieldByName("repeated_double");
79   const FieldDescriptor* fd_repeated_string =
80       desc->FindFieldByName("repeated_string");
81   const FieldDescriptor* fd_repeated_foreign_message =
82       desc->FindFieldByName("repeated_foreign_message");
83
84   // Get RepeatedField objects for all fields of interest.
85   const RepeatedField<int32>& rf_int32 =
86       refl->GetRepeatedField<int32>(message, fd_repeated_int32);
87   const RepeatedField<double>& rf_double =
88       refl->GetRepeatedField<double>(message, fd_repeated_double);
89
90   // Get mutable RepeatedField objects for all fields of interest.
91   RepeatedField<int32>* mrf_int32 =
92       refl->MutableRepeatedField<int32>(&message, fd_repeated_int32);
93   RepeatedField<double>* mrf_double =
94       refl->MutableRepeatedField<double>(&message, fd_repeated_double);
95
96   // Get RepeatedPtrField objects for all fields of interest.
97   const RepeatedPtrField<string>& rpf_string =
98       refl->GetRepeatedPtrField<string>(message, fd_repeated_string);
99   const RepeatedPtrField<ForeignMessage>& rpf_foreign_message =
100       refl->GetRepeatedPtrField<ForeignMessage>(
101           message, fd_repeated_foreign_message);
102   const RepeatedPtrField<Message>& rpf_message =
103       refl->GetRepeatedPtrField<Message>(
104           message, fd_repeated_foreign_message);
105
106   // Get mutable RepeatedPtrField objects for all fields of interest.
107   RepeatedPtrField<string>* mrpf_string =
108       refl->MutableRepeatedPtrField<string>(&message, fd_repeated_string);
109   RepeatedPtrField<ForeignMessage>* mrpf_foreign_message =
110       refl->MutableRepeatedPtrField<ForeignMessage>(
111           &message, fd_repeated_foreign_message);
112   RepeatedPtrField<Message>* mrpf_message =
113       refl->MutableRepeatedPtrField<Message>(
114           &message, fd_repeated_foreign_message);
115
116   // Make sure we can do get and sets through the Repeated[Ptr]Field objects.
117   for (int i = 0; i < 10; ++i) {
118     // Check gets through const objects.
119     EXPECT_EQ(rf_int32.Get(i), Func(i, 1));
120     EXPECT_EQ(rf_double.Get(i), Func(i, 2));
121     EXPECT_EQ(rpf_string.Get(i), StrFunc(i, 5));
122     EXPECT_EQ(rpf_foreign_message.Get(i).c(), Func(i, 6));
123     EXPECT_EQ(down_cast<const ForeignMessage*>(&rpf_message.Get(i))->c(),
124               Func(i, 6));
125
126     // Check gets through mutable objects.
127     EXPECT_EQ(mrf_int32->Get(i), Func(i, 1));
128     EXPECT_EQ(mrf_double->Get(i), Func(i, 2));
129     EXPECT_EQ(mrpf_string->Get(i), StrFunc(i, 5));
130     EXPECT_EQ(mrpf_foreign_message->Get(i).c(), Func(i, 6));
131     EXPECT_EQ(down_cast<const ForeignMessage*>(&mrpf_message->Get(i))->c(),
132               Func(i, 6));
133
134     // Check sets through mutable objects.
135     mrf_int32->Set(i, Func(i, -1));
136     mrf_double->Set(i, Func(i, -2));
137     mrpf_string->Mutable(i)->assign(StrFunc(i, -5));
138     mrpf_foreign_message->Mutable(i)->set_c(Func(i, -6));
139     EXPECT_EQ(message.repeated_int32(i), Func(i, -1));
140     EXPECT_EQ(message.repeated_double(i), Func(i, -2));
141     EXPECT_EQ(message.repeated_string(i), StrFunc(i, -5));
142     EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, -6));
143     down_cast<ForeignMessage*>(mrpf_message->Mutable(i))->set_c(Func(i, 7));
144     EXPECT_EQ(message.repeated_foreign_message(i).c(), Func(i, 7));
145   }
146
147 #ifdef PROTOBUF_HAS_DEATH_TEST
148   // Make sure types are checked correctly at runtime.
149   const FieldDescriptor* fd_optional_int32 =
150       desc->FindFieldByName("optional_int32");
151   EXPECT_DEATH(refl->GetRepeatedField<int32>(
152       message, fd_optional_int32), "requires a repeated field");
153   EXPECT_DEATH(refl->GetRepeatedField<double>(
154       message, fd_repeated_int32), "not the right type");
155   EXPECT_DEATH(refl->GetRepeatedPtrField<TestAllTypes>(
156       message, fd_repeated_foreign_message), "wrong submessage type");
157 #endif  // PROTOBUF_HAS_DEATH_TEST
158 }
159
160
161
162
163 TEST(RepeatedFieldReflectionTest, ExtensionFields) {
164   TestAllExtensions extended_message;
165   const Reflection* refl = extended_message.GetReflection();
166   const Descriptor* desc = extended_message.GetDescriptor();
167
168   for (int i = 0; i < 10; ++i) {
169     extended_message.AddExtension(
170         unittest::repeated_int64_extension, Func(i, 1));
171   }
172
173   const FieldDescriptor* fd_repeated_int64_extension =
174       desc->file()->FindExtensionByName("repeated_int64_extension");
175   GOOGLE_CHECK(fd_repeated_int64_extension != NULL);
176
177   const RepeatedField<int64>& rf_int64_extension =
178       refl->GetRepeatedField<int64>(extended_message,
179                                     fd_repeated_int64_extension);
180
181   RepeatedField<int64>* mrf_int64_extension =
182       refl->MutableRepeatedField<int64>(&extended_message,
183                                         fd_repeated_int64_extension);
184
185   for (int i = 0; i < 10; ++i) {
186     EXPECT_EQ(Func(i, 1), rf_int64_extension.Get(i));
187     mrf_int64_extension->Set(i, Func(i, -1));
188     EXPECT_EQ(Func(i, -1),
189         extended_message.GetExtension(unittest::repeated_int64_extension, i));
190   }
191 }
192
193 }  // namespace
194 }  // namespace protobuf
195 }  // namespace google