~ubuntu-branches/ubuntu/precise/protobuf/precise

« back to all changes in this revision

Viewing changes to src/google/protobuf/extension_set_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2009-11-16 10:41:33 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20091116104133-ykhy3deg5l4975tw
Tags: 2.1.0-1ubuntu1
* Merge from Debian testing.
* Remaining Ubuntu changes:
  - Disable the death tests on IA64, now as a quilt patch.
  - Don't use python2.4, also as a quilt patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#include <google/protobuf/extension_set.h>
36
36
#include <google/protobuf/unittest.pb.h>
37
37
#include <google/protobuf/test_util.h>
 
38
#include <google/protobuf/io/coded_stream.h>
 
39
#include <google/protobuf/io/zero_copy_stream_impl.h>
38
40
 
39
41
#include <google/protobuf/stubs/common.h>
40
42
#include <google/protobuf/testing/googletest.h>
41
43
#include <gtest/gtest.h>
 
44
#include <google/protobuf/stubs/stl_util-inl.h>
42
45
 
43
46
namespace google {
44
47
namespace protobuf {
102
105
              unittest::optional_foreign_message_extension));
103
106
  EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
104
107
            &message.GetExtension(unittest::optional_import_message_extension));
 
108
 
 
109
  // Make sure setting stuff again after clearing works.  (This takes slightly
 
110
  // different code paths since the objects are reused.)
 
111
  TestUtil::SetAllExtensions(&message);
 
112
  TestUtil::ExpectAllExtensionsSet(message);
105
113
}
106
114
 
107
115
TEST(ExtensionSetTest, ClearOneField) {
166
174
  TestUtil::ExpectAllExtensionsSet(message);
167
175
}
168
176
 
169
 
TEST(ExtensionSetTest, Serialization) {
170
 
  // Serialize as TestAllExtensions and parse as TestAllTypes to insure wire
171
 
  // compatibility of extensions.
172
 
  unittest::TestAllExtensions source;
173
 
  unittest::TestAllTypes destination;
174
 
  string data;
175
 
 
176
 
  TestUtil::SetAllExtensions(&source);
177
 
  source.SerializeToString(&data);
178
 
  EXPECT_TRUE(destination.ParseFromString(data));
179
 
  TestUtil::ExpectAllFieldsSet(destination);
 
177
TEST(ExtensionSetTest, SerializationToArray) {
 
178
  // Serialize as TestAllExtensions and parse as TestAllTypes to insure wire
 
179
  // compatibility of extensions.
 
180
  //
 
181
  // This checks serialization to a flat array by explicitly reserving space in
 
182
  // the string and calling the generated message's
 
183
  // SerializeWithCachedSizesToArray.
 
184
  unittest::TestAllExtensions source;
 
185
  unittest::TestAllTypes destination;
 
186
  TestUtil::SetAllExtensions(&source);
 
187
  int size = source.ByteSize();
 
188
  string data;
 
189
  data.resize(size);
 
190
  uint8* target = reinterpret_cast<uint8*>(string_as_array(&data));
 
191
  uint8* end = source.SerializeWithCachedSizesToArray(target);
 
192
  EXPECT_EQ(size, end - target);
 
193
  EXPECT_TRUE(destination.ParseFromString(data));
 
194
  TestUtil::ExpectAllFieldsSet(destination);
 
195
}
 
196
 
 
197
TEST(ExtensionSetTest, SerializationToStream) {
 
198
  // Serialize as TestAllExtensions and parse as TestAllTypes to insure wire
 
199
  // compatibility of extensions.
 
200
  //
 
201
  // This checks serialization to an output stream by creating an array output
 
202
  // stream that can only buffer 1 byte at a time - this prevents the message
 
203
  // from ever jumping to the fast path, ensuring that serialization happens via
 
204
  // the CodedOutputStream.
 
205
  unittest::TestAllExtensions source;
 
206
  unittest::TestAllTypes destination;
 
207
  TestUtil::SetAllExtensions(&source);
 
208
  int size = source.ByteSize();
 
209
  string data;
 
210
  data.resize(size);
 
211
  {
 
212
    io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
 
213
    io::CodedOutputStream output_stream(&array_stream);
 
214
    source.SerializeWithCachedSizes(&output_stream);
 
215
    ASSERT_FALSE(output_stream.HadError());
 
216
  }
 
217
  EXPECT_TRUE(destination.ParseFromString(data));
 
218
  TestUtil::ExpectAllFieldsSet(destination);
 
219
}
 
220
 
 
221
TEST(ExtensionSetTest, PackedSerializationToArray) {
 
222
  // Serialize as TestPackedExtensions and parse as TestPackedTypes to insure
 
223
  // wire compatibility of extensions.
 
224
  //
 
225
  // This checks serialization to a flat array by explicitly reserving space in
 
226
  // the string and calling the generated message's
 
227
  // SerializeWithCachedSizesToArray.
 
228
  unittest::TestPackedExtensions source;
 
229
  unittest::TestPackedTypes destination;
 
230
  TestUtil::SetPackedExtensions(&source);
 
231
  int size = source.ByteSize();
 
232
  string data;
 
233
  data.resize(size);
 
234
  uint8* target = reinterpret_cast<uint8*>(string_as_array(&data));
 
235
  uint8* end = source.SerializeWithCachedSizesToArray(target);
 
236
  EXPECT_EQ(size, end - target);
 
237
  EXPECT_TRUE(destination.ParseFromString(data));
 
238
  TestUtil::ExpectPackedFieldsSet(destination);
 
239
}
 
240
 
 
241
TEST(ExtensionSetTest, PackedSerializationToStream) {
 
242
  // Serialize as TestPackedExtensions and parse as TestPackedTypes to insure
 
243
  // wire compatibility of extensions.
 
244
  //
 
245
  // This checks serialization to an output stream by creating an array output
 
246
  // stream that can only buffer 1 byte at a time - this prevents the message
 
247
  // from ever jumping to the fast path, ensuring that serialization happens via
 
248
  // the CodedOutputStream.
 
249
  unittest::TestPackedExtensions source;
 
250
  unittest::TestPackedTypes destination;
 
251
  TestUtil::SetPackedExtensions(&source);
 
252
  int size = source.ByteSize();
 
253
  string data;
 
254
  data.resize(size);
 
255
  {
 
256
    io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
 
257
    io::CodedOutputStream output_stream(&array_stream);
 
258
    source.SerializeWithCachedSizes(&output_stream);
 
259
    ASSERT_FALSE(output_stream.HadError());
 
260
  }
 
261
  EXPECT_TRUE(destination.ParseFromString(data));
 
262
  TestUtil::ExpectPackedFieldsSet(destination);
180
263
}
181
264
 
182
265
TEST(ExtensionSetTest, Parsing) {
191
274
  TestUtil::ExpectAllExtensionsSet(destination);
192
275
}
193
276
 
 
277
TEST(ExtensionSetTest, PackedParsing) {
 
278
  // Serialize as TestPackedTypes and parse as TestPackedExtensions.
 
279
  unittest::TestPackedTypes source;
 
280
  unittest::TestPackedExtensions destination;
 
281
  string data;
 
282
 
 
283
  TestUtil::SetPackedFields(&source);
 
284
  source.SerializeToString(&data);
 
285
  EXPECT_TRUE(destination.ParseFromString(data));
 
286
  TestUtil::ExpectPackedExtensionsSet(destination);
 
287
}
 
288
 
194
289
TEST(ExtensionSetTest, IsInitialized) {
195
290
  // Test that IsInitialized() returns false if required fields in nested
196
291
  // extensions are missing.
370
465
  }
371
466
}
372
467
 
 
468
#ifdef GTEST_HAS_DEATH_TEST
 
469
 
 
470
TEST(ExtensionSetTest, InvalidEnumDeath) {
 
471
  unittest::TestAllExtensions message;
 
472
  EXPECT_DEBUG_DEATH(
 
473
    message.SetExtension(unittest::optional_foreign_enum_extension,
 
474
                         static_cast<unittest::ForeignEnum>(53)),
 
475
    "IsValid");
 
476
}
 
477
 
 
478
#endif  // GTEST_HAS_DEATH_TEST
 
479
 
373
480
}  // namespace
374
481
}  // namespace internal
375
482
}  // namespace protobuf