Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Janus Heide committed Nov 8, 2017
1 parent 6281e13 commit a591f2e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
27 changes: 15 additions & 12 deletions src/endian/stream_reader.hpp
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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);
}

Expand All @@ -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");

Expand Down
27 changes: 24 additions & 3 deletions test/src/test_stream_reader.cpp
Expand Up @@ -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);
}
Expand Down

0 comments on commit a591f2e

Please sign in to comment.