|
9 | 9 | #include <util/strencodings.h>
|
10 | 10 |
|
11 | 11 | #include <stdint.h>
|
| 12 | +#include <string> |
12 | 13 |
|
13 | 14 | #include <boost/test/unit_test.hpp>
|
14 | 15 |
|
@@ -254,4 +255,146 @@ BOOST_AUTO_TEST_CASE(class_methods)
|
254 | 255 | }
|
255 | 256 | }
|
256 | 257 |
|
| 258 | +enum class BaseFormat { |
| 259 | + RAW, |
| 260 | + HEX, |
| 261 | +}; |
| 262 | + |
| 263 | +/// (Un)serialize a number as raw byte or 2 hexadecimal chars. |
| 264 | +class Base |
| 265 | +{ |
| 266 | +public: |
| 267 | + uint8_t m_base_data; |
| 268 | + |
| 269 | + Base() : m_base_data(17) {} |
| 270 | + explicit Base(uint8_t data) : m_base_data(data) {} |
| 271 | + |
| 272 | + template <typename Stream> |
| 273 | + void Serialize(Stream& s) const |
| 274 | + { |
| 275 | + if (s.GetParams() == BaseFormat::RAW) { |
| 276 | + s << m_base_data; |
| 277 | + } else { |
| 278 | + s << Span{HexStr(Span{&m_base_data, 1})}; |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + template <typename Stream> |
| 283 | + void Unserialize(Stream& s) |
| 284 | + { |
| 285 | + if (s.GetParams() == BaseFormat::RAW) { |
| 286 | + s >> m_base_data; |
| 287 | + } else { |
| 288 | + std::string hex{"aa"}; |
| 289 | + s >> Span{hex}.first(hex.size()); |
| 290 | + m_base_data = TryParseHex<uint8_t>(hex).value().at(0); |
| 291 | + } |
| 292 | + } |
| 293 | +}; |
| 294 | + |
| 295 | +class DerivedAndBaseFormat |
| 296 | +{ |
| 297 | +public: |
| 298 | + BaseFormat m_base_format; |
| 299 | + |
| 300 | + enum class DerivedFormat { |
| 301 | + LOWER, |
| 302 | + UPPER, |
| 303 | + } m_derived_format; |
| 304 | +}; |
| 305 | + |
| 306 | +class Derived : public Base |
| 307 | +{ |
| 308 | +public: |
| 309 | + std::string m_derived_data; |
| 310 | + |
| 311 | + SERIALIZE_METHODS_PARAMS(Derived, obj, DerivedAndBaseFormat, fmt) |
| 312 | + { |
| 313 | + READWRITE(WithParams(fmt.m_base_format, AsBase<Base>(obj))); |
| 314 | + |
| 315 | + if (ser_action.ForRead()) { |
| 316 | + std::string str; |
| 317 | + s >> str; |
| 318 | + SER_READ(obj, obj.m_derived_data = str); |
| 319 | + } else { |
| 320 | + s << (fmt.m_derived_format == DerivedAndBaseFormat::DerivedFormat::LOWER ? |
| 321 | + ToLower(obj.m_derived_data) : |
| 322 | + ToUpper(obj.m_derived_data)); |
| 323 | + } |
| 324 | + } |
| 325 | +}; |
| 326 | + |
| 327 | +BOOST_AUTO_TEST_CASE(with_params_base) |
| 328 | +{ |
| 329 | + Base b{0x0F}; |
| 330 | + |
| 331 | + DataStream stream; |
| 332 | + |
| 333 | + stream << WithParams(BaseFormat::RAW, b); |
| 334 | + BOOST_CHECK_EQUAL(stream.str(), "\x0F"); |
| 335 | + |
| 336 | + b.m_base_data = 0; |
| 337 | + stream >> WithParams(BaseFormat::RAW, b); |
| 338 | + BOOST_CHECK_EQUAL(b.m_base_data, 0x0F); |
| 339 | + |
| 340 | + stream.clear(); |
| 341 | + |
| 342 | + stream << WithParams(BaseFormat::HEX, b); |
| 343 | + BOOST_CHECK_EQUAL(stream.str(), "0f"); |
| 344 | + |
| 345 | + b.m_base_data = 0; |
| 346 | + stream >> WithParams(BaseFormat::HEX, b); |
| 347 | + BOOST_CHECK_EQUAL(b.m_base_data, 0x0F); |
| 348 | +} |
| 349 | + |
| 350 | +BOOST_AUTO_TEST_CASE(with_params_vector_of_base) |
| 351 | +{ |
| 352 | + std::vector<Base> v{Base{0x0F}, Base{0xFF}}; |
| 353 | + |
| 354 | + DataStream stream; |
| 355 | + |
| 356 | + stream << WithParams(BaseFormat::RAW, v); |
| 357 | + BOOST_CHECK_EQUAL(stream.str(), "\x02\x0F\xFF"); |
| 358 | + |
| 359 | + v[0].m_base_data = 0; |
| 360 | + v[1].m_base_data = 0; |
| 361 | + stream >> WithParams(BaseFormat::RAW, v); |
| 362 | + BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F); |
| 363 | + BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF); |
| 364 | + |
| 365 | + stream.clear(); |
| 366 | + |
| 367 | + stream << WithParams(BaseFormat::HEX, v); |
| 368 | + BOOST_CHECK_EQUAL(stream.str(), "\x02" |
| 369 | + "0fff"); |
| 370 | + |
| 371 | + v[0].m_base_data = 0; |
| 372 | + v[1].m_base_data = 0; |
| 373 | + stream >> WithParams(BaseFormat::HEX, v); |
| 374 | + BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F); |
| 375 | + BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF); |
| 376 | +} |
| 377 | + |
| 378 | +BOOST_AUTO_TEST_CASE(with_params_derived) |
| 379 | +{ |
| 380 | + Derived d; |
| 381 | + d.m_base_data = 0x0F; |
| 382 | + d.m_derived_data = "xY"; |
| 383 | + |
| 384 | + DerivedAndBaseFormat fmt; |
| 385 | + |
| 386 | + DataStream stream; |
| 387 | + |
| 388 | + fmt.m_base_format = BaseFormat::RAW; |
| 389 | + fmt.m_derived_format = DerivedAndBaseFormat::DerivedFormat::LOWER; |
| 390 | + stream << WithParams(fmt, d); |
| 391 | + |
| 392 | + fmt.m_base_format = BaseFormat::HEX; |
| 393 | + fmt.m_derived_format = DerivedAndBaseFormat::DerivedFormat::UPPER; |
| 394 | + stream << WithParams(fmt, d); |
| 395 | + |
| 396 | + BOOST_CHECK_EQUAL(stream.str(), "\x0F\x02xy" |
| 397 | + "0f\x02XY"); |
| 398 | +} |
| 399 | + |
257 | 400 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments