|
15 | 15 |
|
16 | 16 | BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)
|
17 | 17 |
|
| 18 | +// For testing move-semantics, declare a version of datastream that can be moved |
| 19 | +// but is not copyable. |
| 20 | +class UncopyableStream : public DataStream |
| 21 | +{ |
| 22 | +public: |
| 23 | + using DataStream::DataStream; |
| 24 | + UncopyableStream(const UncopyableStream&) = delete; |
| 25 | + UncopyableStream& operator=(const UncopyableStream&) = delete; |
| 26 | + UncopyableStream(UncopyableStream&&) noexcept = default; |
| 27 | + UncopyableStream& operator=(UncopyableStream&&) noexcept = default; |
| 28 | +}; |
| 29 | + |
18 | 30 | class CSerializeMethodsTestSingle
|
19 | 31 | {
|
20 | 32 | protected:
|
@@ -344,6 +356,73 @@ class Derived : public Base
|
344 | 356 | }
|
345 | 357 | };
|
346 | 358 |
|
| 359 | +struct OtherParam { |
| 360 | + uint8_t param; |
| 361 | + SER_PARAMS_OPFUNC |
| 362 | +}; |
| 363 | + |
| 364 | +//! Checker for value of OtherParam. When being serialized, serializes the |
| 365 | +//! param to the stream. When being unserialized, verifies the value in the |
| 366 | +//! stream matches the param. |
| 367 | +class OtherParamChecker |
| 368 | +{ |
| 369 | +public: |
| 370 | + template <typename Stream> |
| 371 | + void Serialize(Stream& s) const |
| 372 | + { |
| 373 | + const uint8_t param = s.template GetParams<OtherParam>().param; |
| 374 | + s << param; |
| 375 | + } |
| 376 | + |
| 377 | + template <typename Stream> |
| 378 | + void Unserialize(Stream& s) const |
| 379 | + { |
| 380 | + const uint8_t param = s.template GetParams<OtherParam>().param; |
| 381 | + uint8_t value; |
| 382 | + s >> value; |
| 383 | + BOOST_CHECK_EQUAL(value, param); |
| 384 | + } |
| 385 | +}; |
| 386 | + |
| 387 | +//! Test creating a stream with multiple parameters and making sure |
| 388 | +//! serialization code requiring different parameters can retrieve them. Also |
| 389 | +//! test that earlier parameters take precedence if the same parameter type is |
| 390 | +//! specified twice. (Choice of whether earlier or later values take precedence |
| 391 | +//! or multiple values of the same type are allowed was arbitrary, and just |
| 392 | +//! decided based on what would require smallest amount of ugly C++ template |
| 393 | +//! code. Intent of the test is to just ensure there is no unexpected behavior.) |
| 394 | +BOOST_AUTO_TEST_CASE(with_params_multi) |
| 395 | +{ |
| 396 | + const OtherParam other_param_used{.param = 0x10}; |
| 397 | + const OtherParam other_param_ignored{.param = 0x11}; |
| 398 | + const OtherParam other_param_override{.param = 0x12}; |
| 399 | + const OtherParamChecker check; |
| 400 | + DataStream stream; |
| 401 | + ParamsStream pstream{stream, RAW, other_param_used, other_param_ignored}; |
| 402 | + |
| 403 | + Base base1{0x20}; |
| 404 | + pstream << base1 << check << other_param_override(check); |
| 405 | + BOOST_CHECK_EQUAL(stream.str(), "\x20\x10\x12"); |
| 406 | + |
| 407 | + Base base2; |
| 408 | + pstream >> base2 >> check >> other_param_override(check); |
| 409 | + BOOST_CHECK_EQUAL(base2.m_base_data, 0x20); |
| 410 | +} |
| 411 | + |
| 412 | +//! Test creating a ParamsStream that moves from a stream argument. |
| 413 | +BOOST_AUTO_TEST_CASE(with_params_move) |
| 414 | +{ |
| 415 | + UncopyableStream stream{}; |
| 416 | + ParamsStream pstream{std::move(stream), RAW, HEX, RAW}; |
| 417 | + |
| 418 | + Base base1{0x20}; |
| 419 | + pstream << base1; |
| 420 | + |
| 421 | + Base base2; |
| 422 | + pstream >> base2; |
| 423 | + BOOST_CHECK_EQUAL(base2.m_base_data, 0x20); |
| 424 | +} |
| 425 | + |
347 | 426 | BOOST_AUTO_TEST_CASE(with_params_base)
|
348 | 427 | {
|
349 | 428 | Base b{0x0F};
|
|
0 commit comments