1
1
// Protocol Buffers - Google's data interchange format
2
// Copyright 2008 Google Inc.
2
// Copyright 2008 Google Inc. All rights reserved.
3
3
// http://code.google.com/p/protobuf/
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
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
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.
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.
17
31
// Author: kenton@google.com (Kenton Varda)
18
32
// Based on original Protocol Buffers design by
172
187
&message.optional_import_message());
190
TEST(GeneratedMessageTest, EmbeddedNullsInBytesCharStar) {
191
unittest::TestAllTypes message;
193
const char* value = "\0lalala\0\0";
194
message.set_optional_bytes(value, 9);
195
ASSERT_EQ(9, message.optional_bytes().size());
196
EXPECT_EQ(0, memcmp(value, message.optional_bytes().data(), 9));
198
message.add_repeated_bytes(value, 9);
199
ASSERT_EQ(9, message.repeated_bytes(0).size());
200
EXPECT_EQ(0, memcmp(value, message.repeated_bytes(0).data(), 9));
175
203
TEST(GeneratedMessageTest, ClearOneField) {
176
204
// Set every field to a unique value, then clear one value and insure that
177
205
// only that one value is cleared.
208
236
TestUtil::ExpectAllFieldsSet(message2);
239
TEST(GeneratedMessageTest, SwapWithEmpty) {
240
unittest::TestAllTypes message1, message2;
241
TestUtil::SetAllFields(&message1);
243
TestUtil::ExpectAllFieldsSet(message1);
244
TestUtil::ExpectClear(message2);
245
message1.Swap(&message2);
246
TestUtil::ExpectAllFieldsSet(message2);
247
TestUtil::ExpectClear(message1);
250
TEST(GeneratedMessageTest, SwapWithSelf) {
251
unittest::TestAllTypes message;
252
TestUtil::SetAllFields(&message);
253
TestUtil::ExpectAllFieldsSet(message);
254
message.Swap(&message);
255
TestUtil::ExpectAllFieldsSet(message);
258
TEST(GeneratedMessageTest, SwapWithOther) {
259
unittest::TestAllTypes message1, message2;
261
message1.set_optional_int32(123);
262
message1.set_optional_string("abc");
263
message1.mutable_optional_nested_message()->set_bb(1);
264
message1.set_optional_nested_enum(unittest::TestAllTypes::FOO);
265
message1.add_repeated_int32(1);
266
message1.add_repeated_int32(2);
267
message1.add_repeated_string("a");
268
message1.add_repeated_string("b");
269
message1.add_repeated_nested_message()->set_bb(7);
270
message1.add_repeated_nested_message()->set_bb(8);
271
message1.add_repeated_nested_enum(unittest::TestAllTypes::FOO);
272
message1.add_repeated_nested_enum(unittest::TestAllTypes::BAR);
274
message2.set_optional_int32(456);
275
message2.set_optional_string("def");
276
message2.mutable_optional_nested_message()->set_bb(2);
277
message2.set_optional_nested_enum(unittest::TestAllTypes::BAR);
278
message2.add_repeated_int32(3);
279
message2.add_repeated_string("c");
280
message2.add_repeated_nested_message()->set_bb(9);
281
message2.add_repeated_nested_enum(unittest::TestAllTypes::BAZ);
283
message1.Swap(&message2);
285
EXPECT_EQ(456, message1.optional_int32());
286
EXPECT_EQ("def", message1.optional_string());
287
EXPECT_EQ(2, message1.optional_nested_message().bb());
288
EXPECT_EQ(unittest::TestAllTypes::BAR, message1.optional_nested_enum());
289
ASSERT_EQ(1, message1.repeated_int32_size());
290
EXPECT_EQ(3, message1.repeated_int32(0));
291
ASSERT_EQ(1, message1.repeated_string_size());
292
EXPECT_EQ("c", message1.repeated_string(0));
293
ASSERT_EQ(1, message1.repeated_nested_message_size());
294
EXPECT_EQ(9, message1.repeated_nested_message(0).bb());
295
ASSERT_EQ(1, message1.repeated_nested_enum_size());
296
EXPECT_EQ(unittest::TestAllTypes::BAZ, message1.repeated_nested_enum(0));
298
EXPECT_EQ(123, message2.optional_int32());
299
EXPECT_EQ("abc", message2.optional_string());
300
EXPECT_EQ(1, message2.optional_nested_message().bb());
301
EXPECT_EQ(unittest::TestAllTypes::FOO, message2.optional_nested_enum());
302
ASSERT_EQ(2, message2.repeated_int32_size());
303
EXPECT_EQ(1, message2.repeated_int32(0));
304
EXPECT_EQ(2, message2.repeated_int32(1));
305
ASSERT_EQ(2, message2.repeated_string_size());
306
EXPECT_EQ("a", message2.repeated_string(0));
307
EXPECT_EQ("b", message2.repeated_string(1));
308
ASSERT_EQ(2, message2.repeated_nested_message_size());
309
EXPECT_EQ(7, message2.repeated_nested_message(0).bb());
310
EXPECT_EQ(8, message2.repeated_nested_message(1).bb());
311
ASSERT_EQ(2, message2.repeated_nested_enum_size());
312
EXPECT_EQ(unittest::TestAllTypes::FOO, message2.repeated_nested_enum(0));
313
EXPECT_EQ(unittest::TestAllTypes::BAR, message2.repeated_nested_enum(1));
211
316
TEST(GeneratedMessageTest, CopyConstructor) {
212
317
unittest::TestAllTypes message1;
213
318
TestUtil::SetAllFields(&message1);
464
569
EXPECT_EQ(2, message2.repeated_message(0).msg().c());
572
TEST(GeneratedMessageTest, TestSpaceUsed) {
573
unittest::TestAllTypes message1;
574
// sizeof provides a lower bound on SpaceUsed().
575
EXPECT_LE(sizeof(unittest::TestAllTypes), message1.SpaceUsed());
576
const int empty_message_size = message1.SpaceUsed();
578
// Setting primitive types shouldn't affect the space used.
579
message1.set_optional_int32(123);
580
message1.set_optional_int64(12345);
581
message1.set_optional_uint32(123);
582
message1.set_optional_uint64(12345);
583
EXPECT_EQ(empty_message_size, message1.SpaceUsed());
585
// On some STL implementations, setting the string to a small value should
586
// only increase SpaceUsed() by the size of a string object, though this is
587
// not true everywhere.
588
message1.set_optional_string("abc");
589
EXPECT_LE(empty_message_size + sizeof(string), message1.SpaceUsed());
591
// Setting a string to a value larger than the string object itself should
592
// increase SpaceUsed(), because it cannot store the value internally.
593
message1.set_optional_string(string(sizeof(string) + 1, 'x'));
594
int min_expected_increase = message1.optional_string().capacity() +
596
EXPECT_LE(empty_message_size + min_expected_increase,
597
message1.SpaceUsed());
599
int previous_size = message1.SpaceUsed();
600
// Adding an optional message should increase the size by the size of the
601
// nested message type. NestedMessage is simple enough (1 int field) that it
602
// is equal to sizeof(NestedMessage)
603
message1.mutable_optional_nested_message();
604
ASSERT_EQ(sizeof(unittest::TestAllTypes::NestedMessage),
605
message1.optional_nested_message().SpaceUsed());
606
EXPECT_EQ(previous_size +
607
sizeof(unittest::TestAllTypes::NestedMessage),
608
message1.SpaceUsed());
467
611
// ===================================================================
469
613
TEST(GeneratedEnumTest, EnumValuesAsSwitchCases) {