proto_packer_ut.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "proto_packer.h"
  2. #include <library/cpp/packers/ut/test.pb.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include <util/generic/string.h>
  5. using namespace NPackers;
  6. using namespace NProtoPackerTest;
  7. void FillRequiredFields(TTestMessage& msg) {
  8. msg.SetRequiredString("required_string");
  9. msg.SetRequiredInt32(42);
  10. }
  11. void FillOptionalFields(TTestMessage& msg) {
  12. msg.SetOptionalString("optional_string");
  13. msg.SetOptionalInt32(43);
  14. }
  15. void FillRepeatedFields(TTestMessage& msg) {
  16. msg.ClearRepeatedStrings();
  17. for (ui32 idx = 0; idx < 5; ++idx) {
  18. msg.AddRepeatedStrings("repeated_string" + ToString(idx));
  19. }
  20. }
  21. // do not want to use google/protobuf/util/message_differencer because of warnings
  22. bool operator==(const TTestMessage& lhs, const TTestMessage& rhs) {
  23. if (lhs.GetRequiredString() != rhs.GetRequiredString() ||
  24. lhs.GetRequiredInt32() != rhs.GetRequiredInt32() ||
  25. lhs.HasOptionalString() != rhs.HasOptionalString() ||
  26. (lhs.HasOptionalString() && lhs.GetOptionalString() != rhs.GetOptionalString()) ||
  27. lhs.HasOptionalInt32() != rhs.HasOptionalInt32() ||
  28. (lhs.HasOptionalInt32() && lhs.GetOptionalInt32() != rhs.GetOptionalInt32()) ||
  29. lhs.RepeatedStringsSize() != rhs.RepeatedStringsSize())
  30. {
  31. return false;
  32. }
  33. for (ui32 idx = 0; idx < lhs.RepeatedStringsSize(); ++idx) {
  34. if (lhs.GetRepeatedStrings(idx) != rhs.GetRepeatedStrings(idx)) {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. Y_UNIT_TEST_SUITE(ProtoPackerTestSuite) {
  41. TProtoMessagePacker<TTestMessage> Packer;
  42. TString Buffer;
  43. void DoPackUnpackTest(const TTestMessage& msg) {
  44. const ui32 msgByteSize = Packer.MeasureLeaf(msg);
  45. Buffer.resize(msgByteSize);
  46. Packer.PackLeaf(Buffer.begin(), msg, msgByteSize);
  47. TTestMessage checkMsg;
  48. Packer.UnpackLeaf(Buffer.begin(), checkMsg);
  49. UNIT_ASSERT_EQUAL(msg, checkMsg);
  50. }
  51. Y_UNIT_TEST(TestPackUnpackOnlyRequired) {
  52. TTestMessage msg;
  53. FillRequiredFields(msg);
  54. DoPackUnpackTest(msg);
  55. }
  56. Y_UNIT_TEST(TestPackUnpackRequiredAndOptional) {
  57. TTestMessage msg;
  58. FillRequiredFields(msg);
  59. FillOptionalFields(msg);
  60. DoPackUnpackTest(msg);
  61. }
  62. Y_UNIT_TEST(TestPackUnpackAll) {
  63. TTestMessage msg;
  64. FillRequiredFields(msg);
  65. FillOptionalFields(msg);
  66. FillRepeatedFields(msg);
  67. DoPackUnpackTest(msg);
  68. }
  69. Y_UNIT_TEST(TestSkipLeaf) {
  70. TTestMessage msgFirst;
  71. FillRequiredFields(msgFirst);
  72. TTestMessage msgSecond;
  73. FillRequiredFields(msgSecond);
  74. FillOptionalFields(msgSecond);
  75. const ui32 msgFirstByteSize = Packer.MeasureLeaf(msgFirst);
  76. const ui32 msgSecondByteSize = Packer.MeasureLeaf(msgSecond);
  77. Buffer.resize(msgFirstByteSize + msgSecondByteSize);
  78. Packer.PackLeaf(Buffer.begin(), msgFirst, msgFirstByteSize);
  79. Packer.PackLeaf(Buffer.begin() + msgFirstByteSize, msgSecond, msgSecondByteSize);
  80. TTestMessage checkMsg;
  81. Packer.UnpackLeaf(Buffer.begin() + Packer.SkipLeaf(Buffer.begin()), checkMsg);
  82. UNIT_ASSERT_EQUAL(msgSecond, checkMsg);
  83. }
  84. }