Skip to content

Commit fae9054

Browse files
author
MarcoFalke
committed
test: Use SER_PARAMS_OPFUNC in serialize_tests.cpp
1 parent 504f1f3 commit fae9054

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

src/test/serialize_tests.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,15 @@ BOOST_AUTO_TEST_CASE(class_methods)
255255
}
256256
}
257257

258-
enum class BaseFormat {
259-
RAW,
260-
HEX,
258+
struct BaseFormat {
259+
const enum {
260+
RAW,
261+
HEX,
262+
} m_base_format;
263+
SER_PARAMS_OPFUNC
261264
};
265+
constexpr BaseFormat RAW{BaseFormat::RAW};
266+
constexpr BaseFormat HEX{BaseFormat::HEX};
262267

263268
/// (Un)serialize a number as raw byte or 2 hexadecimal chars.
264269
class Base
@@ -272,7 +277,7 @@ class Base
272277
template <typename Stream>
273278
void Serialize(Stream& s) const
274279
{
275-
if (s.GetParams() == BaseFormat::RAW) {
280+
if (s.GetParams().m_base_format == BaseFormat::RAW) {
276281
s << m_base_data;
277282
} else {
278283
s << Span{HexStr(Span{&m_base_data, 1})};
@@ -282,7 +287,7 @@ class Base
282287
template <typename Stream>
283288
void Unserialize(Stream& s)
284289
{
285-
if (s.GetParams() == BaseFormat::RAW) {
290+
if (s.GetParams().m_base_format == BaseFormat::RAW) {
286291
s >> m_base_data;
287292
} else {
288293
std::string hex{"aa"};
@@ -301,6 +306,8 @@ class DerivedAndBaseFormat
301306
LOWER,
302307
UPPER,
303308
} m_derived_format;
309+
310+
SER_PARAMS_OPFUNC
304311
};
305312

306313
class Derived : public Base
@@ -310,7 +317,7 @@ class Derived : public Base
310317

311318
SERIALIZE_METHODS_PARAMS(Derived, obj, DerivedAndBaseFormat, fmt)
312319
{
313-
READWRITE(WithParams(fmt.m_base_format, AsBase<Base>(obj)));
320+
READWRITE(fmt.m_base_format(AsBase<Base>(obj)));
314321

315322
if (ser_action.ForRead()) {
316323
std::string str;
@@ -330,20 +337,20 @@ BOOST_AUTO_TEST_CASE(with_params_base)
330337

331338
DataStream stream;
332339

333-
stream << WithParams(BaseFormat::RAW, b);
340+
stream << RAW(b);
334341
BOOST_CHECK_EQUAL(stream.str(), "\x0F");
335342

336343
b.m_base_data = 0;
337-
stream >> WithParams(BaseFormat::RAW, b);
344+
stream >> RAW(b);
338345
BOOST_CHECK_EQUAL(b.m_base_data, 0x0F);
339346

340347
stream.clear();
341348

342-
stream << WithParams(BaseFormat::HEX, b);
349+
stream << HEX(b);
343350
BOOST_CHECK_EQUAL(stream.str(), "0f");
344351

345352
b.m_base_data = 0;
346-
stream >> WithParams(BaseFormat::HEX, b);
353+
stream >> HEX(b);
347354
BOOST_CHECK_EQUAL(b.m_base_data, 0x0F);
348355
}
349356

@@ -353,45 +360,42 @@ BOOST_AUTO_TEST_CASE(with_params_vector_of_base)
353360

354361
DataStream stream;
355362

356-
stream << WithParams(BaseFormat::RAW, v);
363+
stream << RAW(v);
357364
BOOST_CHECK_EQUAL(stream.str(), "\x02\x0F\xFF");
358365

359366
v[0].m_base_data = 0;
360367
v[1].m_base_data = 0;
361-
stream >> WithParams(BaseFormat::RAW, v);
368+
stream >> RAW(v);
362369
BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F);
363370
BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF);
364371

365372
stream.clear();
366373

367-
stream << WithParams(BaseFormat::HEX, v);
374+
stream << HEX(v);
368375
BOOST_CHECK_EQUAL(stream.str(), "\x02"
369376
"0fff");
370377

371378
v[0].m_base_data = 0;
372379
v[1].m_base_data = 0;
373-
stream >> WithParams(BaseFormat::HEX, v);
380+
stream >> HEX(v);
374381
BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F);
375382
BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF);
376383
}
377384

385+
constexpr DerivedAndBaseFormat RAW_LOWER{{BaseFormat::RAW}, DerivedAndBaseFormat::DerivedFormat::LOWER};
386+
constexpr DerivedAndBaseFormat HEX_UPPER{{BaseFormat::HEX}, DerivedAndBaseFormat::DerivedFormat::UPPER};
387+
378388
BOOST_AUTO_TEST_CASE(with_params_derived)
379389
{
380390
Derived d;
381391
d.m_base_data = 0x0F;
382392
d.m_derived_data = "xY";
383393

384-
DerivedAndBaseFormat fmt;
385-
386394
DataStream stream;
387395

388-
fmt.m_base_format = BaseFormat::RAW;
389-
fmt.m_derived_format = DerivedAndBaseFormat::DerivedFormat::LOWER;
390-
stream << WithParams(fmt, d);
396+
stream << RAW_LOWER(d);
391397

392-
fmt.m_base_format = BaseFormat::HEX;
393-
fmt.m_derived_format = DerivedAndBaseFormat::DerivedFormat::UPPER;
394-
stream << WithParams(fmt, d);
398+
stream << HEX_UPPER(d);
395399

396400
BOOST_CHECK_EQUAL(stream.str(), "\x0F\x02xy"
397401
"0f\x02XY");

0 commit comments

Comments
 (0)