Skip to content

Commit

Permalink
Added skip() to byte_stream_writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwellbelove committed May 17, 2022
1 parent 645296b commit 7bb39b5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
22 changes: 22 additions & 0 deletions include/etl/byte_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,28 @@ namespace etl
return success;
}

//***************************************************************************
/// Skip n items of T, up to the maximum space available.
/// Returns <b>true</b> if the skip was possible.
/// Returns <b>false</b> if the full skip size was not possible.
//***************************************************************************
template <typename T>
bool skip(size_t n)
{
size_t maximum = available<T>();

if (n < maximum)
{
pcurrent += (n * sizeof(T));
return true;
}
else
{
pcurrent += (maximum * sizeof(T));
return false;
}
}

//***************************************************************************
/// Sets the index back to the position in the stream. Default = 0.
//***************************************************************************
Expand Down
52 changes: 52 additions & 0 deletions test/test_byte_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,58 @@ namespace
}
}

//*************************************************************************
TEST(write_and_skip_int32_t)
{
// Tests assume big endian.
std::array<char, sizeof(int32_t) * 4> storage = { char(0xF0), char(0xF1), char(0xF2), char(0xF3),
char(0xF4), char(0xF5), char(0xF6), char(0xF7),
char(0xF8), char(0xF9), char(0xFA), char(0xFB),
char(0xFC), char(0xFD), char(0xFE), char(0xFF) };

std::array<char, sizeof(int32_t) * 4> compare_data = { char(0x01), char(0x02), char(0x03), char(0x04),
char(0xF4), char(0xF5), char(0xF6), char(0xF7),
char(0xF8), char(0xF9), char(0xFA), char(0xFB),
char(0x05), char(0x06), char(0x07), char(0x08) };

etl::byte_stream_writer byte_stream(storage.data(), storage.size(), etl::endian::big);

CHECK(byte_stream.write(int32_t(0x01020304)));
CHECK(byte_stream.skip<int32_t>(2));
CHECK(byte_stream.write(int32_t(0x05060708)));
CHECK(!byte_stream.skip<int32_t>(1));

for (size_t i = 0U; i < storage.size(); ++i)
{
CHECK_EQUAL(int(compare_data[i]), int(storage[i]));
}
}

//*************************************************************************
TEST(read_and_skip_int32_t)
{
// Tests assume big endian.
std::array<char, sizeof(int32_t) * 4> storage = { char(0x01), char(0x02), char(0x03), char(0x04),
char(0xF4), char(0xF5), char(0xF6), char(0xF7),
char(0xF8), char(0xF9), char(0xFA), char(0xFB),
char(0x05), char(0x06), char(0x07), char(0x08) };

std::array<etl::optional<int32_t>, 4> compare = { int32_t(0x01020304), int32_t(0xF4F5F6F7), int32_t(0xF8F9FAFB), int32_t(0x05060708) };
std::array<etl::optional<int32_t>, 4> result = { int32_t(0xF0F1F2F3), int32_t(0xF4F5F6F7), int32_t(0xF8F9FAFB), int32_t(0xFCFDFEFF) };

etl::byte_stream_reader byte_stream(storage.data(), storage.size(), etl::endian::big);

CHECK(result[0] = byte_stream.read<int32_t>());
CHECK(byte_stream.skip<int32_t>(2));
CHECK(result[3] = byte_stream.read<int32_t>());
CHECK(!byte_stream.skip<int32_t>(2));

for (size_t i = 0U; i < result.size(); ++i)
{
CHECK_EQUAL(compare[i].value(), result[i].value());
}
}

//*************************************************************************
TEST(write_read_bool)
{
Expand Down

0 comments on commit 7bb39b5

Please sign in to comment.