diff options
author | Jim Posen <jimpo@coinbase.com> | 2018-04-17 02:19:36 -0700 |
---|---|---|
committer | Jim Posen <jim.posen@gmail.com> | 2018-08-25 10:02:37 -0700 |
commit | 9b622dc72279b027c59d6541cddff53800fc689b (patch) | |
tree | 71dcd4e1a76ff09ccb19c5461453ca4654bc1ca0 /src/test | |
parent | fe943f99bf0a2bbb12e30bc4803c0337e3c95b93 (diff) |
streams: Unit tests for BitStreamReader and BitStreamWriter.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/streams_tests.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 592ebcbd0a..26cf74830d 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -113,6 +113,41 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader) BOOST_CHECK_THROW(reader >> d, std::ios_base::failure); } +BOOST_AUTO_TEST_CASE(bitstream_reader_writer) +{ + CDataStream data(SER_NETWORK, INIT_PROTO_VERSION); + + BitStreamWriter<CDataStream> bit_writer(data); + bit_writer.Write(0, 1); + bit_writer.Write(2, 2); + bit_writer.Write(6, 3); + bit_writer.Write(11, 4); + bit_writer.Write(1, 5); + bit_writer.Write(32, 6); + bit_writer.Write(7, 7); + bit_writer.Write(30497, 16); + bit_writer.Flush(); + + CDataStream data_copy(data); + uint32_t serialized_int1; + data >> serialized_int1; + BOOST_CHECK_EQUAL(serialized_int1, (uint32_t)0x7700C35A); // NOTE: Serialized as LE + uint16_t serialized_int2; + data >> serialized_int2; + BOOST_CHECK_EQUAL(serialized_int2, (uint16_t)0x1072); // NOTE: Serialized as LE + + BitStreamReader<CDataStream> bit_reader(data_copy); + BOOST_CHECK_EQUAL(bit_reader.Read(1), 0); + BOOST_CHECK_EQUAL(bit_reader.Read(2), 2); + BOOST_CHECK_EQUAL(bit_reader.Read(3), 6); + BOOST_CHECK_EQUAL(bit_reader.Read(4), 11); + BOOST_CHECK_EQUAL(bit_reader.Read(5), 1); + BOOST_CHECK_EQUAL(bit_reader.Read(6), 32); + BOOST_CHECK_EQUAL(bit_reader.Read(7), 7); + BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497); + BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure); +} + BOOST_AUTO_TEST_CASE(streams_serializedata_xor) { std::vector<char> in; |