Skip to content

Commit 9b622dc

Browse files
Jim Posenjimpo
authored andcommitted
streams: Unit tests for BitStreamReader and BitStreamWriter.
1 parent fe943f9 commit 9b622dc

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/test/streams_tests.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,41 @@ BOOST_AUTO_TEST_CASE(streams_vector_reader)
113113
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
114114
}
115115

116+
BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
117+
{
118+
CDataStream data(SER_NETWORK, INIT_PROTO_VERSION);
119+
120+
BitStreamWriter<CDataStream> bit_writer(data);
121+
bit_writer.Write(0, 1);
122+
bit_writer.Write(2, 2);
123+
bit_writer.Write(6, 3);
124+
bit_writer.Write(11, 4);
125+
bit_writer.Write(1, 5);
126+
bit_writer.Write(32, 6);
127+
bit_writer.Write(7, 7);
128+
bit_writer.Write(30497, 16);
129+
bit_writer.Flush();
130+
131+
CDataStream data_copy(data);
132+
uint32_t serialized_int1;
133+
data >> serialized_int1;
134+
BOOST_CHECK_EQUAL(serialized_int1, (uint32_t)0x7700C35A); // NOTE: Serialized as LE
135+
uint16_t serialized_int2;
136+
data >> serialized_int2;
137+
BOOST_CHECK_EQUAL(serialized_int2, (uint16_t)0x1072); // NOTE: Serialized as LE
138+
139+
BitStreamReader<CDataStream> bit_reader(data_copy);
140+
BOOST_CHECK_EQUAL(bit_reader.Read(1), 0);
141+
BOOST_CHECK_EQUAL(bit_reader.Read(2), 2);
142+
BOOST_CHECK_EQUAL(bit_reader.Read(3), 6);
143+
BOOST_CHECK_EQUAL(bit_reader.Read(4), 11);
144+
BOOST_CHECK_EQUAL(bit_reader.Read(5), 1);
145+
BOOST_CHECK_EQUAL(bit_reader.Read(6), 32);
146+
BOOST_CHECK_EQUAL(bit_reader.Read(7), 7);
147+
BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497);
148+
BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure);
149+
}
150+
116151
BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
117152
{
118153
std::vector<char> in;

0 commit comments

Comments
 (0)