Skip to content

Commit

Permalink
streams: Unit test for VectorReader class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Posen authored and jimpo committed Aug 25, 2018
1 parent 947133d commit 87f2d9e
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/test/streams_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,51 @@ BOOST_AUTO_TEST_CASE(streams_vector_writer)
vch.clear();
}

BOOST_AUTO_TEST_CASE(streams_vector_reader)
{
std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};

VectorReader reader(SER_NETWORK, INIT_PROTO_VERSION, vch, 0);
BOOST_CHECK_EQUAL(reader.size(), 6);
BOOST_CHECK(!reader.empty());

// Read a single byte as an unsigned char.
unsigned char a;
reader >> a;
BOOST_CHECK_EQUAL(a, 1);
BOOST_CHECK_EQUAL(reader.size(), 5);
BOOST_CHECK(!reader.empty());

// Read a single byte as a signed char.
signed char b;
reader >> b;
BOOST_CHECK_EQUAL(b, -1);
BOOST_CHECK_EQUAL(reader.size(), 4);
BOOST_CHECK(!reader.empty());

// Read a 4 bytes as an unsigned int.
unsigned int c;
reader >> c;
BOOST_CHECK_EQUAL(c, 100992003); // 3,4,5,6 in little-endian base-256
BOOST_CHECK_EQUAL(reader.size(), 0);
BOOST_CHECK(reader.empty());

// Reading after end of byte vector throws an error.
signed int d;
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);

// Read a 4 bytes as a signed int from the beginning of the buffer.
reader.seek(-6);
reader >> d;
BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
BOOST_CHECK_EQUAL(reader.size(), 2);
BOOST_CHECK(!reader.empty());

// Reading after end of byte vector throws an error even if the reader is
// not totally empty.
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
}

BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
{
std::vector<char> in;
Expand Down

0 comments on commit 87f2d9e

Please sign in to comment.