LCOV - code coverage report
Current view: top level - src/test - serialize_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 244 245 99.6 %
Date: 2025-02-23 09:33:43 Functions: 33 33 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2012-2013 The Bitcoin Core developers
       2             : // Copyright (c) 2019-2021 The PIVX Core developers
       3             : // Distributed under the MIT/X11 software license, see the accompanying
       4             : // file COPYING or https://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #include "optional.h"
       7             : #include "serialize.h"
       8             : #include "streams.h"
       9             : #include "hash.h"
      10             : #include "test/test_pivx.h"
      11             : 
      12             : #include <stdint.h>
      13             : 
      14             : #include <boost/test/unit_test.hpp>
      15             : 
      16             : template<typename T>
      17           3 : void check_ser_rep(T thing, std::vector<unsigned char> expected)
      18             : {
      19           3 :     CDataStream ss(SER_DISK, 0);
      20           3 :     ss << thing;
      21             : 
      22           6 :     BOOST_CHECK(GetSerializeSize(thing, 0) == ss.size());
      23             : 
      24           6 :     std::vector<unsigned char> serialized_representation(ss.begin(), ss.end());
      25             : 
      26           6 :     BOOST_CHECK(serialized_representation == expected);
      27             : 
      28           4 :     T thing_deserialized;
      29           3 :     ss >> thing_deserialized;
      30             : 
      31           9 :     BOOST_CHECK(thing_deserialized == thing);
      32           3 : }
      33             : 
      34             : BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)
      35             : 
      36             : class CSerializeMethodsTestSingle
      37             : {
      38             : protected:
      39             :     int intval;
      40             :     bool boolval;
      41             :     std::string stringval;
      42             :     char charstrval[16];
      43             :     CTransactionRef txval;
      44             : public:
      45           1 :     CSerializeMethodsTestSingle() = default;
      46           4 :     CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const char* charstrvalin, CTransaction txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(MakeTransactionRef(txvalin))
      47             :     {
      48           2 :         memcpy(charstrval, charstrvalin, sizeof(charstrval));
      49           2 :     }
      50             : 
      51           6 :     SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
      52             :     {
      53           3 :         READWRITE(obj.intval);
      54           3 :         READWRITE(obj.boolval);
      55           3 :         READWRITE(obj.stringval);
      56           3 :         READWRITE(obj.charstrval);
      57           3 :         READWRITE(obj.txval);
      58           3 :     }
      59             : 
      60           5 :     bool operator==(const CSerializeMethodsTestSingle& rhs)
      61             :     {
      62           5 :         return  intval == rhs.intval && \
      63           5 :                 boolval == rhs.boolval && \
      64           5 :                 stringval == rhs.stringval && \
      65          10 :                 strcmp(charstrval, rhs.charstrval) == 0 && \
      66           5 :                 *txval == *rhs.txval;
      67             :     }
      68             : };
      69             : 
      70           0 : class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle
      71             : {
      72             : public:
      73           1 :     using CSerializeMethodsTestSingle::CSerializeMethodsTestSingle;
      74             : 
      75           3 :     SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
      76             :     {
      77           1 :         READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, obj.txval);
      78             :     }
      79             : };
      80             : 
      81           2 : BOOST_AUTO_TEST_CASE(sizes)
      82             : {
      83           1 :     BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(char(0), 0));
      84           1 :     BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0), 0));
      85           1 :     BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0), 0));
      86           1 :     BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0), 0));
      87           1 :     BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0), 0));
      88           1 :     BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0), 0));
      89           1 :     BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0));
      90           1 :     BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0));
      91           1 :     BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0));
      92           1 :     BOOST_CHECK_EQUAL(sizeof(float), GetSerializeSize(float(0), 0));
      93           1 :     BOOST_CHECK_EQUAL(sizeof(double), GetSerializeSize(double(0), 0));
      94             : 
      95             :     // Bool is serialized as char
      96           1 :     BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(bool(0), 0));
      97             : 
      98             :     // Sanity-check GetSerializeSize and c++ type matching
      99           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(char(0), 0), 1);
     100           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0), 0), 1);
     101           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0), 0), 1);
     102           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0), 0), 2);
     103           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0), 0), 2);
     104           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0), 0), 4);
     105           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0), 0), 4);
     106           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8);
     107           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8);
     108           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(float(0), 0), 4);
     109           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(double(0), 0), 8);
     110           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1);
     111           1 : }
     112             : 
     113           2 : BOOST_AUTO_TEST_CASE(floats_conversion)
     114             : {
     115             :     // Choose values that map unambigiously to binary floating point to avoid
     116             :     // rounding issues at the compiler side.
     117           1 :     BOOST_CHECK_EQUAL(ser_uint32_to_float(0x00000000), 0.0F);
     118           1 :     BOOST_CHECK_EQUAL(ser_uint32_to_float(0x3f000000), 0.5F);
     119           1 :     BOOST_CHECK_EQUAL(ser_uint32_to_float(0x3f800000), 1.0F);
     120           1 :     BOOST_CHECK_EQUAL(ser_uint32_to_float(0x40000000), 2.0F);
     121           1 :     BOOST_CHECK_EQUAL(ser_uint32_to_float(0x40800000), 4.0F);
     122           1 :     BOOST_CHECK_EQUAL(ser_uint32_to_float(0x44444444), 785.066650390625F);
     123             : 
     124           1 :     BOOST_CHECK_EQUAL(ser_float_to_uint32(0.0F), 0x00000000);
     125           1 :     BOOST_CHECK_EQUAL(ser_float_to_uint32(0.5F), 0x3f000000);
     126           1 :     BOOST_CHECK_EQUAL(ser_float_to_uint32(1.0F), 0x3f800000);
     127           1 :     BOOST_CHECK_EQUAL(ser_float_to_uint32(2.0F), 0x40000000);
     128           1 :     BOOST_CHECK_EQUAL(ser_float_to_uint32(4.0F), 0x40800000);
     129           1 :     BOOST_CHECK_EQUAL(ser_float_to_uint32(785.066650390625F), 0x44444444);
     130           1 : }
     131             : 
     132           2 : BOOST_AUTO_TEST_CASE(doubles_conversion)
     133             : {
     134             :     // Choose values that map unambigiously to binary floating point to avoid
     135             :     // rounding issues at the compiler side.
     136           1 :     BOOST_CHECK_EQUAL(ser_uint64_to_double(0x0000000000000000ULL), 0.0);
     137           1 :     BOOST_CHECK_EQUAL(ser_uint64_to_double(0x3fe0000000000000ULL), 0.5);
     138           1 :     BOOST_CHECK_EQUAL(ser_uint64_to_double(0x3ff0000000000000ULL), 1.0);
     139           1 :     BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4000000000000000ULL), 2.0);
     140           1 :     BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4010000000000000ULL), 4.0);
     141           1 :     BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4088888880000000ULL), 785.066650390625);
     142             : 
     143           1 :     BOOST_CHECK_EQUAL(ser_double_to_uint64(0.0), 0x0000000000000000ULL);
     144           1 :     BOOST_CHECK_EQUAL(ser_double_to_uint64(0.5), 0x3fe0000000000000ULL);
     145           1 :     BOOST_CHECK_EQUAL(ser_double_to_uint64(1.0), 0x3ff0000000000000ULL);
     146           1 :     BOOST_CHECK_EQUAL(ser_double_to_uint64(2.0), 0x4000000000000000ULL);
     147           1 :     BOOST_CHECK_EQUAL(ser_double_to_uint64(4.0), 0x4010000000000000ULL);
     148           1 :     BOOST_CHECK_EQUAL(ser_double_to_uint64(785.066650390625), 0x4088888880000000ULL);
     149           1 : }
     150             : /*
     151             : Python code to generate the below hashes:
     152             :     def reversed_hex(x):
     153             :         return binascii.hexlify(''.join(reversed(x)))
     154             :     def dsha256(x):
     155             :         return hashlib.sha256(hashlib.sha256(x).digest()).digest()
     156             :     reversed_hex(dsha256(''.join(struct.pack('<f', x) for x in range(0,1000)))) == '8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85099d7f3fe0c'
     157             :     reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
     158             : */
     159           2 : BOOST_AUTO_TEST_CASE(floats)
     160             : {
     161           1 :     CDataStream ss(SER_DISK, 0);
     162             :     // encode
     163        1001 :     for (int i = 0; i < 1000; i++) {
     164        1000 :         ss << float(i);
     165             :     }
     166           3 :     BOOST_CHECK(Hash(ss.begin(), ss.end()) == uint256S("8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85099d7f3fe0c"));
     167             : 
     168             :     // decode
     169        1001 :     for (int i = 0; i < 1000; i++) {
     170        1000 :         float j;
     171        1000 :         ss >> j;
     172        2000 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
     173             :     }
     174           1 : }
     175             : 
     176           2 : BOOST_AUTO_TEST_CASE(doubles)
     177             : {
     178           1 :     CDataStream ss(SER_DISK, 0);
     179             :     // encode
     180        1001 :     for (int i = 0; i < 1000; i++) {
     181        1000 :         ss << double(i);
     182             :     }
     183           3 :     BOOST_CHECK(Hash(ss.begin(), ss.end()) == uint256S("43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96"));
     184             : 
     185             :     // decode
     186        1001 :     for (int i = 0; i < 1000; i++) {
     187        1000 :         double j;
     188        1000 :         ss >> j;
     189        2000 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
     190             :     }
     191           1 : }
     192             : 
     193           2 : BOOST_AUTO_TEST_CASE(boost_optional)
     194             : {
     195           1 :     check_ser_rep<Optional<unsigned char>>(0xff, {0x01, 0xff});
     196           1 :     check_ser_rep<Optional<unsigned char>>(boost::none, {0x00});
     197           3 :     check_ser_rep<Optional<std::string>>(std::string("Test"), {0x01, 0x04, 'T', 'e', 's', 't'});
     198           1 : }
     199             : 
     200           2 : BOOST_AUTO_TEST_CASE(varints)
     201             : {
     202             :     // encode
     203             : 
     204           1 :     CDataStream ss(SER_DISK, 0);
     205           1 :     CDataStream::size_type size = 0;
     206      100001 :     for (int i = 0; i < 100000; i++) {
     207      100000 :         ss << VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED);
     208      100000 :         size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED), 0);
     209      200000 :         BOOST_CHECK(size == ss.size());
     210             :     }
     211             : 
     212         102 :     for (uint64_t i = 0;  i < 100000000000ULL; i += 999999937) {
     213         101 :         ss << VARINT(i);
     214         101 :         size += ::GetSerializeSize(VARINT(i), 0);
     215         202 :         BOOST_CHECK(size == ss.size());
     216             :     }
     217             : 
     218             :     // decode
     219      100001 :     for (int i = 0; i < 100000; i++) {
     220      100000 :         int j = -1;
     221      100000 :         ss >> VARINT_MODE(j, VarIntMode::NONNEGATIVE_SIGNED);
     222      200000 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
     223             :     }
     224             : 
     225         102 :     for (uint64_t i = 0;  i < 100000000000ULL; i += 999999937) {
     226         101 :         uint64_t j = -1;
     227         101 :         ss >> VARINT(j);
     228         202 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
     229             :     }
     230           1 : }
     231             : 
     232           2 : BOOST_AUTO_TEST_CASE(varints_bitpatterns)
     233             : {
     234           1 :     CDataStream ss(SER_DISK, 0);
     235           1 :     ss << VARINT_MODE(0, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "00"); ss.clear();
     236           1 :     ss << VARINT_MODE(0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
     237           1 :     ss << VARINT_MODE((int8_t)0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
     238           1 :     ss << VARINT_MODE(0x80, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
     239           1 :     ss << VARINT((uint8_t)0x80); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
     240           1 :     ss << VARINT_MODE(0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
     241           1 :     ss << VARINT_MODE((int16_t)0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
     242           1 :     ss << VARINT_MODE(0xffff, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
     243           1 :     ss << VARINT((uint16_t)0xffff); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
     244           1 :     ss << VARINT_MODE(0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
     245           1 :     ss << VARINT_MODE((int32_t)0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
     246           1 :     ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
     247           1 :     ss << VARINT((uint32_t)0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
     248           1 :     ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();
     249           2 :     ss << VARINT_MODE(0x7fffffffffffffffLL, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();
     250           3 :     ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();
     251           1 : }
     252             : 
     253           2 : BOOST_AUTO_TEST_CASE(compactsize)
     254             : {
     255           1 :     CDataStream ss(SER_DISK, 0);
     256           1 :     std::vector<char>::size_type i, j;
     257             : 
     258          27 :     for (i = 1; i <= MAX_SIZE; i *= 2)
     259             :     {
     260          26 :         WriteCompactSize(ss, i-1);
     261          26 :         WriteCompactSize(ss, i);
     262             :     }
     263          27 :     for (i = 1; i <= MAX_SIZE; i *= 2)
     264             :     {
     265          26 :         j = ReadCompactSize(ss);
     266          52 :         BOOST_CHECK_MESSAGE((i-1) == j, "decoded:" << j << " expected:" << (i-1));
     267          26 :         j = ReadCompactSize(ss);
     268          52 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
     269             :     }
     270           1 : }
     271             : 
     272           6 : static bool isCanonicalException(const std::ios_base::failure& ex)
     273             : {
     274           6 :     std::ios_base::failure expectedException("non-canonical ReadCompactSize()");
     275             : 
     276             :     // The string returned by what() can be different for different platforms.
     277             :     // Instead of directly comparing the ex.what() with an expected string,
     278             :     // create an instance of exception to see if ex.what() matches
     279             :     // the expected explanatory string returned by the exception instance.
     280           6 :     return strcmp(expectedException.what(), ex.what()) == 0;
     281             : }
     282             : 
     283           2 : BOOST_AUTO_TEST_CASE(vector_bool)
     284             : {
     285           1 :     std::vector<uint8_t> vec1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
     286           2 :     std::vector<bool> vec2{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
     287             : 
     288           3 :     BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
     289           2 :     BOOST_CHECK(SerializeHash(vec1) == SerializeHash(vec2));
     290           1 : }
     291             : 
     292           2 : BOOST_AUTO_TEST_CASE(noncanonical)
     293             : {
     294             :     // Write some non-canonical CompactSize encodings, and
     295             :     // make sure an exception is thrown when read back.
     296           1 :     CDataStream ss(SER_DISK, 0);
     297           1 :     std::vector<char>::size_type n;
     298             : 
     299             :     // zero encoded with three bytes:
     300           1 :     ss.write("\xfd\x00\x00", 3);
     301           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
     302             : 
     303             :     // 0xfc encoded with three bytes:
     304           1 :     ss.write("\xfd\xfc\x00", 3);
     305           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
     306             : 
     307             :     // 0xfd encoded with three bytes is OK:
     308           1 :     ss.write("\xfd\xfd\x00", 3);
     309           1 :     n = ReadCompactSize(ss);
     310           2 :     BOOST_CHECK(n == 0xfd);
     311             : 
     312             :     // zero encoded with five bytes:
     313           1 :     ss.write("\xfe\x00\x00\x00\x00", 5);
     314           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
     315             : 
     316             :     // 0xffff encoded with five bytes:
     317           1 :     ss.write("\xfe\xff\xff\x00\x00", 5);
     318           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
     319             : 
     320             :     // zero encoded with nine bytes:
     321           1 :     ss.write("\xff\x00\x00\x00\x00\x00\x00\x00\x00", 9);
     322           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
     323             : 
     324             :     // 0x01ffffff encoded with nine bytes:
     325           1 :     ss.write("\xff\xff\xff\xff\x01\x00\x00\x00\x00", 9);
     326           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
     327           1 : }
     328             : 
     329           2 : BOOST_AUTO_TEST_CASE(insert_delete)
     330             : {
     331             :     // Test inserting/deleting bytes.
     332           1 :     CDataStream ss(SER_DISK, 0);
     333           1 :     BOOST_CHECK_EQUAL(ss.size(), 0);
     334             : 
     335           1 :     ss.write("\x00\x01\x02\xff", 4);
     336           1 :     BOOST_CHECK_EQUAL(ss.size(), 4);
     337             : 
     338           1 :     char c = (char)11;
     339             : 
     340             :     // Inserting at beginning/end/middle:
     341           1 :     ss.insert(ss.begin(), c);
     342           1 :     BOOST_CHECK_EQUAL(ss.size(), 5);
     343           1 :     BOOST_CHECK_EQUAL(ss[0], c);
     344           1 :     BOOST_CHECK_EQUAL(ss[1], 0);
     345             : 
     346           1 :     ss.insert(ss.end(), c);
     347           1 :     BOOST_CHECK_EQUAL(ss.size(), 6);
     348           1 :     BOOST_CHECK_EQUAL(ss[4], (char)0xff);
     349           1 :     BOOST_CHECK_EQUAL(ss[5], c);
     350             : 
     351           1 :     ss.insert(ss.begin()+2, c);
     352           1 :     BOOST_CHECK_EQUAL(ss.size(), 7);
     353           1 :     BOOST_CHECK_EQUAL(ss[2], c);
     354             : 
     355             :     // Delete at beginning/end/middle
     356           1 :     ss.erase(ss.begin());
     357           1 :     BOOST_CHECK_EQUAL(ss.size(), 6);
     358           1 :     BOOST_CHECK_EQUAL(ss[0], 0);
     359             : 
     360           1 :     ss.erase(ss.begin()+ss.size()-1);
     361           1 :     BOOST_CHECK_EQUAL(ss.size(), 5);
     362           1 :     BOOST_CHECK_EQUAL(ss[4], (char)0xff);
     363             : 
     364           1 :     ss.erase(ss.begin()+1);
     365           1 :     BOOST_CHECK_EQUAL(ss.size(), 4);
     366           1 :     BOOST_CHECK_EQUAL(ss[0], 0);
     367           1 :     BOOST_CHECK_EQUAL(ss[1], 1);
     368           1 :     BOOST_CHECK_EQUAL(ss[2], 2);
     369           1 :     BOOST_CHECK_EQUAL(ss[3], (char)0xff);
     370             : 
     371             :     // Make sure GetAndClear does the right thing:
     372           2 :     CSerializeData d;
     373           1 :     ss.GetAndClear(d);
     374           1 :     BOOST_CHECK_EQUAL(ss.size(), 0);
     375           1 : }
     376             : 
     377           2 : BOOST_AUTO_TEST_CASE(class_methods)
     378             : {
     379           1 :     int intval(100);
     380           1 :     bool boolval(true);
     381           1 :     std::string stringval("testing");
     382           1 :     const char charstrval[16] = "testing charstr";
     383           2 :     CMutableTransaction txval;
     384           3 :     CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, txval);
     385           3 :     CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, txval);
     386           2 :     CSerializeMethodsTestSingle methodtest3;
     387           1 :     CSerializeMethodsTestMany methodtest4;
     388           2 :     CDataStream ss(SER_DISK, PROTOCOL_VERSION);
     389           2 :     BOOST_CHECK(methodtest1 == methodtest2);
     390           1 :     ss << methodtest1;
     391           1 :     ss >> methodtest4;
     392           1 :     ss << methodtest2;
     393           1 :     ss >> methodtest3;
     394           2 :     BOOST_CHECK(methodtest1 == methodtest2);
     395           2 :     BOOST_CHECK(methodtest2 == methodtest3);
     396           2 :     BOOST_CHECK(methodtest3 == methodtest4);
     397             : 
     398           2 :     CDataStream ss2(SER_DISK, PROTOCOL_VERSION, intval, boolval, stringval, charstrval, txval);
     399           1 :     ss2 >> methodtest3;
     400           2 :     BOOST_CHECK(methodtest3 == methodtest4);
     401           1 : }
     402             : 
     403             : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.14