Skip to content

Commit 87f2d9e

Browse files
Jim Posenjimpo
authored andcommitted
streams: Unit test for VectorReader class.
1 parent 947133d commit 87f2d9e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/test/streams_tests.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,51 @@ BOOST_AUTO_TEST_CASE(streams_vector_writer)
6868
vch.clear();
6969
}
7070

71+
BOOST_AUTO_TEST_CASE(streams_vector_reader)
72+
{
73+
std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
74+
75+
VectorReader reader(SER_NETWORK, INIT_PROTO_VERSION, vch, 0);
76+
BOOST_CHECK_EQUAL(reader.size(), 6);
77+
BOOST_CHECK(!reader.empty());
78+
79+
// Read a single byte as an unsigned char.
80+
unsigned char a;
81+
reader >> a;
82+
BOOST_CHECK_EQUAL(a, 1);
83+
BOOST_CHECK_EQUAL(reader.size(), 5);
84+
BOOST_CHECK(!reader.empty());
85+
86+
// Read a single byte as a signed char.
87+
signed char b;
88+
reader >> b;
89+
BOOST_CHECK_EQUAL(b, -1);
90+
BOOST_CHECK_EQUAL(reader.size(), 4);
91+
BOOST_CHECK(!reader.empty());
92+
93+
// Read a 4 bytes as an unsigned int.
94+
unsigned int c;
95+
reader >> c;
96+
BOOST_CHECK_EQUAL(c, 100992003); // 3,4,5,6 in little-endian base-256
97+
BOOST_CHECK_EQUAL(reader.size(), 0);
98+
BOOST_CHECK(reader.empty());
99+
100+
// Reading after end of byte vector throws an error.
101+
signed int d;
102+
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
103+
104+
// Read a 4 bytes as a signed int from the beginning of the buffer.
105+
reader.seek(-6);
106+
reader >> d;
107+
BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
108+
BOOST_CHECK_EQUAL(reader.size(), 2);
109+
BOOST_CHECK(!reader.empty());
110+
111+
// Reading after end of byte vector throws an error even if the reader is
112+
// not totally empty.
113+
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
114+
}
115+
71116
BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
72117
{
73118
std::vector<char> in;

0 commit comments

Comments
 (0)