delimited_message_util_test.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Adapted from the patch of kenton@google.com (Kenton Varda)
  2. // See https://github.com/google/protobuf/pull/710 for details.
  3. #include <google/protobuf/util/delimited_message_util.h>
  4. #include <sstream>
  5. #include <google/protobuf/test_util.h>
  6. #include <google/protobuf/unittest.pb.h>
  7. #include <google/protobuf/testing/googletest.h>
  8. #include <gtest/gtest.h>
  9. namespace google {
  10. namespace protobuf {
  11. namespace util {
  12. TEST(DelimitedMessageUtilTest, DelimitedMessages) {
  13. std::stringstream stream;
  14. {
  15. protobuf_unittest::TestAllTypes message1;
  16. TestUtil::SetAllFields(&message1);
  17. EXPECT_TRUE(SerializeDelimitedToOstream(message1, &stream));
  18. protobuf_unittest::TestPackedTypes message2;
  19. TestUtil::SetPackedFields(&message2);
  20. EXPECT_TRUE(SerializeDelimitedToOstream(message2, &stream));
  21. }
  22. {
  23. bool clean_eof;
  24. io::IstreamInputStream zstream(&stream);
  25. protobuf_unittest::TestAllTypes message1;
  26. clean_eof = true;
  27. EXPECT_TRUE(ParseDelimitedFromZeroCopyStream(&message1,
  28. &zstream, &clean_eof));
  29. EXPECT_FALSE(clean_eof);
  30. TestUtil::ExpectAllFieldsSet(message1);
  31. protobuf_unittest::TestPackedTypes message2;
  32. clean_eof = true;
  33. EXPECT_TRUE(ParseDelimitedFromZeroCopyStream(&message2,
  34. &zstream, &clean_eof));
  35. EXPECT_FALSE(clean_eof);
  36. TestUtil::ExpectPackedFieldsSet(message2);
  37. clean_eof = false;
  38. EXPECT_FALSE(ParseDelimitedFromZeroCopyStream(&message2,
  39. &zstream, &clean_eof));
  40. EXPECT_TRUE(clean_eof);
  41. }
  42. }
  43. } // namespace util
  44. } // namespace protobuf
  45. } // namespace google