diff --git a/include/etl/byte_stream.h b/include/etl/byte_stream.h index 2b75e82a0..bf93eb5ea 100644 --- a/include/etl/byte_stream.h +++ b/include/etl/byte_stream.h @@ -220,6 +220,28 @@ namespace etl return success; } + //*************************************************************************** + /// Skip n items of T, up to the maximum space available. + /// Returns true if the skip was possible. + /// Returns false if the full skip size was not possible. + //*************************************************************************** + template + bool skip(size_t n) + { + size_t maximum = available(); + + 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. //*************************************************************************** diff --git a/test/test_byte_stream.cpp b/test/test_byte_stream.cpp index 799f42952..57b6439a0 100644 --- a/test/test_byte_stream.cpp +++ b/test/test_byte_stream.cpp @@ -400,6 +400,58 @@ namespace } } + //************************************************************************* + TEST(write_and_skip_int32_t) + { + // Tests assume big endian. + std::array 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 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(2)); + CHECK(byte_stream.write(int32_t(0x05060708))); + CHECK(!byte_stream.skip(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 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, 4> compare = { int32_t(0x01020304), int32_t(0xF4F5F6F7), int32_t(0xF8F9FAFB), int32_t(0x05060708) }; + std::array, 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()); + CHECK(byte_stream.skip(2)); + CHECK(result[3] = byte_stream.read()); + CHECK(!byte_stream.skip(2)); + + for (size_t i = 0U; i < result.size(); ++i) + { + CHECK_EQUAL(compare[i].value(), result[i].value()); + } + } + //************************************************************************* TEST(write_read_bool) {