Permalink
Browse files

addressing comments

  • Loading branch information...
janusheide committed Nov 8, 2017
1 parent 6281e13 commit a591f2ed5d3a2160ae8748561ee1e3c775afee5e
Showing with 39 additions and 15 deletions.
  1. +15 −12 src/endian/stream_reader.hpp
  2. +24 −3 test/src/test_stream_reader.cpp
@@ -47,8 +47,8 @@ class stream_reader : public stream
template<uint8_t Bytes, class ValueType>
void read_bytes(ValueType& value)
{
// Make sure there is enough data to read in the underlying buffer
assert(Bytes <= remaining_size());
assert(sizeof(ValueType) <= remaining_size() &&
"Reading over the end of the underlying buffer");
// Get the value at the current position
peek_bytes<Bytes, ValueType>(value);
@@ -73,10 +73,13 @@ class stream_reader : public stream
/// Reads a ValueType-sized integer from the stream and moves the read
/// position.
///
/// @return the value to be read
/// @return the read value
template<class ValueType>
ValueType read()
{
assert(sizeof(ValueType) <= remaining_size() &&
"Reading over the end of the underlying buffer");
ValueType value;
read(value);
return value;
@@ -92,8 +95,8 @@ class stream_reader : public stream
/// @param size The number of bytes to fill.
void read(uint8_t* data, uint64_t size)
{
// Make sure there is enough data to read in the underlying buffer
assert(size <= remaining_size());
assert(size <= remaining_size() &&
"Reading over the end of the underlying buffer");
// Copy the data from the buffer to the storage
std::copy_n(remaining_data(), (std::size_t)size, data);
@@ -110,9 +113,9 @@ class stream_reader : public stream
template<uint8_t Bytes, class ValueType>
void peek_bytes(ValueType& value, uint64_t offset=0) const
{
assert(remaining_size() >= offset);
// Make sure there is enough data to read in the underlying buffer
assert(Bytes <= remaining_size() - offset);
assert(remaining_size() >= offset && "Offset too large");
assert(sizeof(ValueType) <= remaining_size() - offset &&
"Reading over the end of the underlying buffer");
const uint8_t* data_position = remaining_data() + offset;
// Get the value at the current position
@@ -127,9 +130,10 @@ class stream_reader : public stream
template<class ValueType>
void peek(ValueType& value, uint64_t offset=0) const
{
assert(remaining_size() >= offset);
// Make sure there is enough data to read in the underlying buffer
assert(sizeof(ValueType) <= remaining_size() - offset);
assert(remaining_size() >= offset && "Offset too large");
assert(sizeof(ValueType) <= remaining_size() - offset &&
"Reading over the end of the underlying buffer");
peek_bytes<sizeof(ValueType), ValueType>(value, offset);
}
@@ -142,7 +146,6 @@ class stream_reader : public stream
ValueType peek(uint64_t offset=0) const
{
assert(remaining_size() >= offset && "Offset too large");
assert(sizeof(ValueType) <= remaining_size() - offset &&
"Reading over the end of the underlying buffer");
@@ -113,18 +113,39 @@ static void test_basic_api()
}
{
SCOPED_TRACE(testing::Message() << "peek");
SCOPED_TRACE(testing::Message() << "peek_read");
std::vector<uint8_t> buffer = { 1, 2 };
endian::stream_reader<EndianType> stream(buffer.data(), buffer.size());
uint8_t first_peek = 0;
uint8_t second_peek = 0;
uint8_t first_read = 0;
stream.peek(first_peek);
stream.peek(second_peek);
stream.read(first_read);
EXPECT_EQ(first_peek, second_peek);
EXPECT_EQ(first_peek, first_read);
uint8_t second_read = 0;
uint8_t third_peek = 0;
stream.peek(third_peek);
stream.read(second_read);
EXPECT_NE(first_peek, third_peek);
EXPECT_EQ(third_peek, second_read);
}
{
SCOPED_TRACE(testing::Message() << "peek_read_return_value");
std::vector<uint8_t> buffer = { 1, 2 };
endian::stream_reader<EndianType> stream(buffer.data(), buffer.size());
uint8_t first_peek = stream.template peek<uint8_t>();
uint8_t second_peek = stream.template peek<uint8_t>();
uint8_t first_read = stream.template read<uint8_t>();
EXPECT_EQ(first_peek, second_peek);
EXPECT_EQ(first_peek, first_read);
uint8_t third_peek = stream.template peek<uint8_t>();
uint8_t second_read = stream.template read<uint8_t>();
auto third_peek = stream.template peek<uint8_t>();
auto second_read = stream.template read<uint8_t>();
EXPECT_NE(first_peek, third_peek);
EXPECT_EQ(third_peek, second_read);
}

0 comments on commit a591f2e

Please sign in to comment.