Skip to content

Commit 95a9d51

Browse files
update merge test
1 parent 79c86bd commit 95a9d51

File tree

1 file changed

+94
-65
lines changed

1 file changed

+94
-65
lines changed

Diff for: unittests/merge_message_tests.cpp

+94-65
Original file line numberDiff line numberDiff line change
@@ -79,65 +79,11 @@ auto pb_meta(const TestMessage &)
7979
hpp::proto::field_meta<113, 3, hpp::proto::field_option::explicit_presence>,
8080
hpp::proto::field_meta<114, 4, hpp::proto::field_option::explicit_presence>>>;
8181

82-
TestMessage get_source() {
83-
TestMessage source;
84-
// Optional fields
85-
source.optional_int32 = 1; // only source
86-
source.optional_int64 = 2; // both source and dest
87-
source.optional_bytes = "abc"_bytes;
88-
89-
// Optional fields with defaults
90-
source.default_int32 = 13; // only source
91-
source.default_int64 = 14; // both source and dest
92-
93-
// Nested message field
94-
source.optional_foreign_message.emplace(ForeignMessage{.c = 1, .d = 0, .e = "uvw"_bytes});
95-
96-
// Repeated fields
97-
source.repeated_int32.assign({5, 6}); // only source
98-
source.repeated_int64.assign({7LL, 8LL}); // both source and dest
99-
100-
// Map fields
101-
102-
source.map_int32_int32[2] = 12; // both source and dest
103-
source.map_int32_int32[3] = 13; // only source
104-
return source;
105-
}
106-
107-
TestMessage get_dest() {
108-
TestMessage dest;
109-
110-
// Optional fields
111-
dest.optional_int64 = 3;
112-
dest.optional_uint32 = 4; // only dest
113-
dest.optional_bytes = "def"_bytes;
114-
115-
// Optional fields with defaults
116-
dest.default_int64 = 15;
117-
dest.default_uint32 = 16; // only dest
118-
119-
// Nested message field
120-
dest.optional_foreign_message.emplace(ForeignMessage{.c = 0, .d = 2, .e = "xyz"_bytes});
121-
122-
// Repeated fields
123-
dest.repeated_int64.assign({9LL, 10LL});
124-
dest.repeated_uint32.assign({11U, 12U}); // only dest
125-
126-
// Map fields
127-
dest.map_int32_int32[1] = 1; // only dest
128-
dest.map_int32_int32[2] = 2; // both source and dest
129-
130-
return dest;
131-
}
132-
13382
const boost::ut::suite merge_test_suite = [] {
13483
using namespace boost::ut;
13584
using namespace boost::ut::literals;
13685

13786
"merge"_test = [] {
138-
TestMessage source = get_source();
139-
TestMessage dest = get_dest();
140-
14187
auto verify_merge = [](const TestMessage &dest) {
14288
// Optional fields: source overwrites dest if source is specified
14389
expect(eq(1, dest.optional_int32.value())); // only source: use source
@@ -163,22 +109,105 @@ const boost::ut::suite merge_test_suite = [] {
163109
expect(std::vector<int64_t>{9LL, 10LL, 7LL, 8LL} == dest.repeated_int64);
164110
expect(std::vector<uint32_t>{11U, 12U} == dest.repeated_uint32);
165111
expect(dest.repeated_uint64.empty());
112+
};
113+
114+
TestMessage dest;
115+
TestMessage source;
116+
117+
// Optional fields
118+
source.optional_int32 = 1; // only source
119+
source.optional_int64 = 2; // both source and dest
120+
source.optional_bytes = "abc"_bytes;
121+
122+
// Optional fields with defaults
123+
source.default_int32 = 13; // only source
124+
source.default_int64 = 14; // both source and dest
125+
126+
// Nested message field
127+
source.optional_foreign_message.emplace(ForeignMessage{.c = 1, .d = 0, .e = "uvw"_bytes});
128+
129+
// Repeated fields
130+
source.repeated_int32.assign({5, 6}); // only source
131+
source.repeated_int64.assign({7LL, 8LL}); // both source and dest
132+
133+
// Optional fields
134+
dest.optional_int64 = 3;
135+
dest.optional_uint32 = 4; // only dest
136+
dest.optional_bytes = "def"_bytes;
137+
138+
// Optional fields with defaults
139+
dest.default_int64 = 15;
140+
dest.default_uint32 = 16; // only dest
166141

167-
// Map fields
168-
expect(hpp::proto::flat_map<int32_t, int32_t>{{1, 1}, {2, 12}, {3, 13}} == dest.map_int32_int32);
142+
// Nested message field
143+
dest.optional_foreign_message.emplace(ForeignMessage{.c = 0, .d = 2, .e = "xyz"_bytes});
144+
145+
// Repeated fields
146+
dest.repeated_int64.assign({9LL, 10LL});
147+
dest.repeated_uint32.assign({11U, 12U}); // only dest
148+
149+
should("merge const reference") = [=]() mutable {
150+
hpp::proto::merge(dest, source);
151+
verify_merge(dest);
169152
};
170153

171-
hpp::proto::merge(dest, source);
172-
verify_merge(dest);
154+
should("merge rvalue reference") = [=]() mutable {
155+
hpp::proto::merge(dest, std::move(source));
156+
verify_merge(dest);
157+
};
158+
};
173159

174-
dest = get_dest();
175-
hpp::proto::merge(dest, std::move(source));
176-
verify_merge(dest);
160+
"map_merge"_test = [] {
161+
{
162+
// only source
163+
TestMessage dest;
164+
TestMessage source;
165+
source.map_int32_int32 = {{1, 1}, {2, 2}};
166+
167+
should("merge const reference") = [=]() mutable {
168+
hpp::proto::merge(dest, source);
169+
expect(hpp::proto::flat_map<int32_t, int32_t>{{1, 1}, {2, 2}} == dest.map_int32_int32);
170+
};
171+
should("merge rvalue reference") = [=]() mutable {
172+
hpp::proto::merge(dest, std::move(source));
173+
expect(hpp::proto::flat_map<int32_t, int32_t>{{1, 1}, {2, 2}} == dest.map_int32_int32);
174+
};
175+
}
176+
{
177+
// only dest
178+
TestMessage dest;
179+
TestMessage source;
180+
dest.map_int32_int32 = {{1, 1}, {2, 2}};
181+
182+
should("merge const reference") = [=]() mutable {
183+
hpp::proto::merge(dest, source);
184+
expect(hpp::proto::flat_map<int32_t, int32_t>{{1, 1}, {2, 2}} == dest.map_int32_int32);
185+
};
186+
should("merge rvalue reference") = [=]() mutable {
187+
hpp::proto::merge(dest, std::move(source));
188+
expect(hpp::proto::flat_map<int32_t, int32_t>{{1, 1}, {2, 2}} == dest.map_int32_int32);
189+
};
190+
}
191+
{
192+
// both dest and source
193+
TestMessage dest;
194+
TestMessage source;
195+
dest.map_int32_int32 = {{1, 1}, {2, 2}};
196+
source.map_int32_int32 = {{2, 12}, {3, 13}};
197+
should("merge const reference") = [=]() mutable {
198+
hpp::proto::merge(dest, source);
199+
expect(hpp::proto::flat_map<int32_t, int32_t>{{1, 1}, {2, 12}, {3, 13}} == dest.map_int32_int32);
200+
};
201+
should("merge rvalue reference") = [=]() mutable {
202+
hpp::proto::merge(dest, std::move(source));
203+
expect(hpp::proto::flat_map<int32_t, int32_t>{{1, 1}, {2, 12}, {3, 13}} == dest.map_int32_int32);
204+
};
205+
}
177206
};
178207

179208
"oneof_merge"_test = [] {
180209
using namespace std::string_literals;
181-
{
210+
{ // only source
182211
TestMessage dest;
183212
TestMessage source;
184213
source.oneof_field = 1U;
@@ -187,7 +216,7 @@ const boost::ut::suite merge_test_suite = [] {
187216
expect(fatal(eq(dest.oneof_field.index(), TestMessage::oneof_uint32)));
188217
expect(eq(1U, std::get<uint32_t>(dest.oneof_field)));
189218
}
190-
{
219+
{ // only dest
191220
TestMessage dest;
192221
TestMessage source;
193222
dest.oneof_field = 1U;
@@ -196,7 +225,7 @@ const boost::ut::suite merge_test_suite = [] {
196225
expect(fatal(eq(dest.oneof_field.index(), TestMessage::oneof_uint32)));
197226
expect(eq(1U, std::get<uint32_t>(dest.oneof_field)));
198227
}
199-
{
228+
{ // both source and dest, different types
200229
TestMessage dest;
201230
TestMessage source;
202231
dest.oneof_field = 1U;
@@ -205,7 +234,7 @@ const boost::ut::suite merge_test_suite = [] {
205234
expect(fatal(eq(dest.oneof_field.index(), TestMessage::oneof_string)));
206235
expect(eq("abc"s, std::get<std::string>(dest.oneof_field)));
207236
}
208-
{
237+
{ // both source and dest, same types
209238
TestMessage dest;
210239
TestMessage source;
211240
dest.oneof_field.emplace<TestMessage::oneof_foreign_message>().c = 1;

0 commit comments

Comments
 (0)