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