Changeset View
Changeset View
Standalone View
Standalone View
src/test/streams_tests.cpp
Show First 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(streams_vector_writer) { | ||||
BOOST_CHECK((vch == std::vector<uint8_t>{{8, 8, 1, 3, 4, 5, 6, 2}})); | BOOST_CHECK((vch == std::vector<uint8_t>{{8, 8, 1, 3, 4, 5, 6, 2}})); | ||||
vch.clear(); | vch.clear(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(streams_vector_reader) { | BOOST_AUTO_TEST_CASE(streams_vector_reader) { | ||||
std::vector<uint8_t> vch = {1, 255, 3, 4, 5, 6}; | std::vector<uint8_t> vch = {1, 255, 3, 4, 5, 6}; | ||||
VectorReader reader(SER_NETWORK, INIT_PROTO_VERSION, vch, 0); | VectorReader reader(SER_NETWORK, INIT_PROTO_VERSION, vch, 0); | ||||
BOOST_CHECK_EQUAL(reader.size(), 6); | BOOST_CHECK_EQUAL(reader.size(), 6U); | ||||
BOOST_CHECK(!reader.empty()); | BOOST_CHECK(!reader.empty()); | ||||
// Read a single byte as an uint8_t. | // Read a single byte as an uint8_t. | ||||
uint8_t a; | uint8_t a; | ||||
reader >> a; | reader >> a; | ||||
BOOST_CHECK_EQUAL(a, 1); | BOOST_CHECK_EQUAL(a, 1); | ||||
BOOST_CHECK_EQUAL(reader.size(), 5); | BOOST_CHECK_EQUAL(reader.size(), 5U); | ||||
BOOST_CHECK(!reader.empty()); | BOOST_CHECK(!reader.empty()); | ||||
// Read a single byte as a (signed) int8_t. | // Read a single byte as a (signed) int8_t. | ||||
int8_t b; | int8_t b; | ||||
reader >> b; | reader >> b; | ||||
BOOST_CHECK_EQUAL(b, -1); | BOOST_CHECK_EQUAL(b, -1); | ||||
BOOST_CHECK_EQUAL(reader.size(), 4); | BOOST_CHECK_EQUAL(reader.size(), 4U); | ||||
BOOST_CHECK(!reader.empty()); | BOOST_CHECK(!reader.empty()); | ||||
// Read a 4 bytes as an unsigned uint32_t. | // Read a 4 bytes as an unsigned uint32_t. | ||||
uint32_t c; | uint32_t c; | ||||
reader >> c; | reader >> c; | ||||
// 100992003 = 3,4,5,6 in little-endian base-256 | // 100992003 = 3,4,5,6 in little-endian base-256 | ||||
BOOST_CHECK_EQUAL(c, 100992003); | BOOST_CHECK_EQUAL(c, 100992003); | ||||
BOOST_CHECK_EQUAL(reader.size(), 0); | BOOST_CHECK_EQUAL(reader.size(), 0U); | ||||
BOOST_CHECK(reader.empty()); | BOOST_CHECK(reader.empty()); | ||||
// Reading after end of byte vector throws an error. | // Reading after end of byte vector throws an error. | ||||
int32_t d; | int32_t d; | ||||
BOOST_CHECK_THROW(reader >> d, std::ios_base::failure); | BOOST_CHECK_THROW(reader >> d, std::ios_base::failure); | ||||
// Read a 4 bytes as a (signed) int32_t from the beginning of the buffer. | // Read a 4 bytes as a (signed) int32_t from the beginning of the buffer. | ||||
VectorReader new_reader(SER_NETWORK, INIT_PROTO_VERSION, vch, 0); | VectorReader new_reader(SER_NETWORK, INIT_PROTO_VERSION, vch, 0); | ||||
new_reader >> d; | new_reader >> d; | ||||
// 67370753 = 1,255,3,4 in little-endian base-256 | // 67370753 = 1,255,3,4 in little-endian base-256 | ||||
BOOST_CHECK_EQUAL(d, 67370753); | BOOST_CHECK_EQUAL(d, 67370753); | ||||
BOOST_CHECK_EQUAL(new_reader.size(), 2); | BOOST_CHECK_EQUAL(new_reader.size(), 2U); | ||||
BOOST_CHECK(!new_reader.empty()); | BOOST_CHECK(!new_reader.empty()); | ||||
// Reading after end of byte vector throws an error even if the reader is | // Reading after end of byte vector throws an error even if the reader is | ||||
// not totally empty. | // not totally empty. | ||||
BOOST_CHECK_THROW(new_reader >> d, std::ios_base::failure); | BOOST_CHECK_THROW(new_reader >> d, std::ios_base::failure); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(bitstream_reader_writer) { | BOOST_AUTO_TEST_CASE(bitstream_reader_writer) { | ||||
Show All 16 Lines | BOOST_AUTO_TEST_CASE(bitstream_reader_writer) { | ||||
// NOTE: Serialized as LE | // NOTE: Serialized as LE | ||||
BOOST_CHECK_EQUAL(serialized_int1, (uint32_t)0x7700C35A); | BOOST_CHECK_EQUAL(serialized_int1, (uint32_t)0x7700C35A); | ||||
uint16_t serialized_int2; | uint16_t serialized_int2; | ||||
data >> serialized_int2; | data >> serialized_int2; | ||||
// NOTE: Serialized as LE | // NOTE: Serialized as LE | ||||
BOOST_CHECK_EQUAL(serialized_int2, (uint16_t)0x1072); | BOOST_CHECK_EQUAL(serialized_int2, (uint16_t)0x1072); | ||||
BitStreamReader<CDataStream> bit_reader(data_copy); | BitStreamReader<CDataStream> bit_reader(data_copy); | ||||
BOOST_CHECK_EQUAL(bit_reader.Read(1), 0); | BOOST_CHECK_EQUAL(bit_reader.Read(1), 0U); | ||||
BOOST_CHECK_EQUAL(bit_reader.Read(2), 2); | BOOST_CHECK_EQUAL(bit_reader.Read(2), 2U); | ||||
BOOST_CHECK_EQUAL(bit_reader.Read(3), 6); | BOOST_CHECK_EQUAL(bit_reader.Read(3), 6U); | ||||
BOOST_CHECK_EQUAL(bit_reader.Read(4), 11); | BOOST_CHECK_EQUAL(bit_reader.Read(4), 11U); | ||||
BOOST_CHECK_EQUAL(bit_reader.Read(5), 1); | BOOST_CHECK_EQUAL(bit_reader.Read(5), 1U); | ||||
BOOST_CHECK_EQUAL(bit_reader.Read(6), 32); | BOOST_CHECK_EQUAL(bit_reader.Read(6), 32U); | ||||
BOOST_CHECK_EQUAL(bit_reader.Read(7), 7); | BOOST_CHECK_EQUAL(bit_reader.Read(7), 7U); | ||||
BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497); | BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497U); | ||||
BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure); | BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(streams_serializedata_xor) { | BOOST_AUTO_TEST_CASE(streams_serializedata_xor) { | ||||
std::vector<char> in; | std::vector<char> in; | ||||
std::vector<char> expected_xor; | std::vector<char> expected_xor; | ||||
std::vector<uint8_t> key; | std::vector<uint8_t> key; | ||||
CDataStream ds(in, 0, 0); | CDataStream ds(in, 0, 0); | ||||
▲ Show 20 Lines • Show All 90 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(streams_buffered_file) { | ||||
uint8_t i; | uint8_t i; | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK_EQUAL(i, 0); | BOOST_CHECK_EQUAL(i, 0); | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK_EQUAL(i, 1); | BOOST_CHECK_EQUAL(i, 1); | ||||
// After reading bytes 0 and 1, we're positioned at 2. | // After reading bytes 0 and 1, we're positioned at 2. | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 2); | BOOST_CHECK_EQUAL(bf.GetPos(), 2U); | ||||
// Rewind to offset 0, ok (within the 10 byte window). | // Rewind to offset 0, ok (within the 10 byte window). | ||||
BOOST_CHECK(bf.SetPos(0)); | BOOST_CHECK(bf.SetPos(0)); | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK_EQUAL(i, 0); | BOOST_CHECK_EQUAL(i, 0); | ||||
// We can go forward to where we've been, but beyond may fail. | // We can go forward to where we've been, but beyond may fail. | ||||
BOOST_CHECK(bf.SetPos(2)); | BOOST_CHECK(bf.SetPos(2)); | ||||
Show All 10 Lines | try { | ||||
BOOST_CHECK(false); | BOOST_CHECK(false); | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
BOOST_CHECK(strstr(e.what(), "Read attempted past buffer limit") != | BOOST_CHECK(strstr(e.what(), "Read attempted past buffer limit") != | ||||
nullptr); | nullptr); | ||||
} | } | ||||
// The default argument removes the limit completely. | // The default argument removes the limit completely. | ||||
BOOST_CHECK(bf.SetLimit()); | BOOST_CHECK(bf.SetLimit()); | ||||
// The read position should still be at 3 (no change). | // The read position should still be at 3 (no change). | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 3); | BOOST_CHECK_EQUAL(bf.GetPos(), 3U); | ||||
// Read from current offset, 3, forward until position 10. | // Read from current offset, 3, forward until position 10. | ||||
for (uint8_t j = 3; j < 10; ++j) { | for (uint8_t j = 3; j < 10; ++j) { | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK_EQUAL(i, j); | BOOST_CHECK_EQUAL(i, j); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 10); | BOOST_CHECK_EQUAL(bf.GetPos(), 10U); | ||||
// We're guaranteed (just barely) to be able to rewind to zero. | // We're guaranteed (just barely) to be able to rewind to zero. | ||||
BOOST_CHECK(bf.SetPos(0)); | BOOST_CHECK(bf.SetPos(0)); | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 0); | BOOST_CHECK_EQUAL(bf.GetPos(), 0U); | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK_EQUAL(i, 0); | BOOST_CHECK_EQUAL(i, 0); | ||||
// We can set the position forward again up to the farthest | // We can set the position forward again up to the farthest | ||||
// into the stream we've been, but no farther. (Attempting | // into the stream we've been, but no farther. (Attempting | ||||
// to go farther may succeed, but it's not guaranteed.) | // to go farther may succeed, but it's not guaranteed.) | ||||
BOOST_CHECK(bf.SetPos(10)); | BOOST_CHECK(bf.SetPos(10)); | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK_EQUAL(i, 10); | BOOST_CHECK_EQUAL(i, 10); | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 11); | BOOST_CHECK_EQUAL(bf.GetPos(), 11U); | ||||
// Now it's only guaranteed that we can rewind to offset 1 | // Now it's only guaranteed that we can rewind to offset 1 | ||||
// (current read position, 11, minus rewind amount, 10). | // (current read position, 11, minus rewind amount, 10). | ||||
BOOST_CHECK(bf.SetPos(1)); | BOOST_CHECK(bf.SetPos(1)); | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 1); | BOOST_CHECK_EQUAL(bf.GetPos(), 1U); | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK_EQUAL(i, 1); | BOOST_CHECK_EQUAL(i, 1); | ||||
// We can stream into large variables, even larger than | // We can stream into large variables, even larger than | ||||
// the buffer size. | // the buffer size. | ||||
BOOST_CHECK(bf.SetPos(11)); | BOOST_CHECK(bf.SetPos(11)); | ||||
{ | { | ||||
uint8_t a[40 - 11]; | uint8_t a[40 - 11]; | ||||
bf >> a; | bf >> a; | ||||
for (uint8_t j = 0; j < sizeof(a); ++j) { | for (uint8_t j = 0; j < sizeof(a); ++j) { | ||||
BOOST_CHECK_EQUAL(a[j], 11 + j); | BOOST_CHECK_EQUAL(a[j], 11 + j); | ||||
} | } | ||||
} | } | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 40); | BOOST_CHECK_EQUAL(bf.GetPos(), 40U); | ||||
// We've read the entire file, the next read should throw. | // We've read the entire file, the next read should throw. | ||||
try { | try { | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK(false); | BOOST_CHECK(false); | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
BOOST_CHECK(strstr(e.what(), "CBufferedFile::Fill: end of file") != | BOOST_CHECK(strstr(e.what(), "CBufferedFile::Fill: end of file") != | ||||
nullptr); | nullptr); | ||||
} | } | ||||
// Attempting to read beyond the end sets the EOF indicator. | // Attempting to read beyond the end sets the EOF indicator. | ||||
BOOST_CHECK(bf.eof()); | BOOST_CHECK(bf.eof()); | ||||
// Still at offset 40, we can go back 10, to 30. | // Still at offset 40, we can go back 10, to 30. | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 40); | BOOST_CHECK_EQUAL(bf.GetPos(), 40U); | ||||
BOOST_CHECK(bf.SetPos(30)); | BOOST_CHECK(bf.SetPos(30)); | ||||
bf >> i; | bf >> i; | ||||
BOOST_CHECK_EQUAL(i, 30); | BOOST_CHECK_EQUAL(i, 30); | ||||
BOOST_CHECK_EQUAL(bf.GetPos(), 31); | BOOST_CHECK_EQUAL(bf.GetPos(), 31U); | ||||
// We're too far to rewind to position zero. | // We're too far to rewind to position zero. | ||||
BOOST_CHECK(!bf.SetPos(0)); | BOOST_CHECK(!bf.SetPos(0)); | ||||
// But we should now be positioned at least as far back as allowed | // But we should now be positioned at least as far back as allowed | ||||
// by the rewind window (relative to our farthest read position, 40). | // by the rewind window (relative to our farthest read position, 40). | ||||
BOOST_CHECK(bf.GetPos() <= 30); | BOOST_CHECK(bf.GetPos() <= 30); | ||||
// We can explicitly close the file, or the destructor will do it. | // We can explicitly close the file, or the destructor will do it. | ||||
▲ Show 20 Lines • Show All 121 Lines • Show Last 20 Lines |