32 lines
1.2 KiB
C++
32 lines
1.2 KiB
C++
#include <Void/Binary/Reader.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <array>
|
|
|
|
Void::Testing::Test binary_reader_checks_bounds_and_endian()
|
|
{
|
|
const std::array bytes{std::byte{0x12}, std::byte{0x34}, std::byte{0x56}};
|
|
Void::Binary::Reader reader{bytes};
|
|
auto value = reader.read<std::uint16_t>(std::endian::big);
|
|
Void::Testing::require(value.has_value());
|
|
Void::Testing::expect_eq(*value, static_cast<std::uint16_t>(0x1234));
|
|
Void::Testing::expect_eq(reader.position(), static_cast<std::size_t>(2));
|
|
auto overflow = reader.read<std::uint16_t>(std::endian::big);
|
|
Void::Testing::expect(!overflow.has_value());
|
|
Void::Testing::expect_eq(reader.position(), static_cast<std::size_t>(2));
|
|
}
|
|
|
|
Void::Testing::Test binary_reader_subreader_is_bounded()
|
|
{
|
|
const std::array bytes{std::byte{'a'}, std::byte{'b'}, std::byte{'c'}};
|
|
Void::Binary::Reader reader{bytes};
|
|
auto child = reader.subreader(2);
|
|
Void::Testing::require(child.has_value());
|
|
auto value = child->readString(2);
|
|
Void::Testing::require(value.has_value());
|
|
Void::Testing::expect_eq(*value, std::string{"ab"});
|
|
Void::Testing::expect(!child->readBytes(1).has_value());
|
|
Void::Testing::expect_eq(reader.remaining(), static_cast<std::size_t>(1));
|
|
}
|