mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:57:46 +00:00
AK: Remove the fallible constructor from FixedMemoryStream
This commit is contained in:
parent
8b2f23d016
commit
220fbcaa7e
31 changed files with 185 additions and 209 deletions
|
@ -14,18 +14,18 @@ TEST_CASE(single_byte)
|
|||
u32 output = {};
|
||||
i32 output_signed = {};
|
||||
u8 buf[] = { 0x00 };
|
||||
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { buf, sizeof(buf) }));
|
||||
FixedMemoryStream stream { ReadonlyBytes { buf, sizeof(buf) } };
|
||||
|
||||
// less than/eq 0b0011_1111, signed == unsigned == raw byte
|
||||
for (u8 i = 0u; i <= 0x3F; ++i) {
|
||||
buf[0] = i;
|
||||
|
||||
MUST(stream->seek(0));
|
||||
output = MUST(stream->read_value<LEB128<u32>>());
|
||||
MUST(stream.seek(0));
|
||||
output = MUST(stream.read_value<LEB128<u32>>());
|
||||
EXPECT_EQ(output, i);
|
||||
|
||||
MUST(stream->seek(0));
|
||||
output_signed = MUST(stream->read_value<LEB128<i32>>());
|
||||
MUST(stream.seek(0));
|
||||
output_signed = MUST(stream.read_value<LEB128<i32>>());
|
||||
EXPECT_EQ(output_signed, i);
|
||||
}
|
||||
|
||||
|
@ -33,23 +33,23 @@ TEST_CASE(single_byte)
|
|||
for (u8 i = 0x40u; i < 0x80; ++i) {
|
||||
buf[0] = i;
|
||||
|
||||
MUST(stream->seek(0));
|
||||
output = MUST(stream->read_value<LEB128<u32>>());
|
||||
MUST(stream.seek(0));
|
||||
output = MUST(stream.read_value<LEB128<u32>>());
|
||||
EXPECT_EQ(output, i);
|
||||
|
||||
MUST(stream->seek(0));
|
||||
output_signed = MUST(stream->read_value<LEB128<i32>>());
|
||||
MUST(stream.seek(0));
|
||||
output_signed = MUST(stream.read_value<LEB128<i32>>());
|
||||
EXPECT_EQ(output_signed, (i | (-1 & (~0x3F))));
|
||||
}
|
||||
// MSB set, but input too short
|
||||
for (u16 i = 0x80; i <= 0xFF; ++i) {
|
||||
buf[0] = static_cast<u8>(i);
|
||||
|
||||
MUST(stream->seek(0));
|
||||
EXPECT(stream->read_value<LEB128<u32>>().is_error());
|
||||
MUST(stream.seek(0));
|
||||
EXPECT(stream.read_value<LEB128<u32>>().is_error());
|
||||
|
||||
MUST(stream->seek(0));
|
||||
EXPECT(stream->read_value<LEB128<i32>>().is_error());
|
||||
MUST(stream.seek(0));
|
||||
EXPECT(stream.read_value<LEB128<i32>>().is_error());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,7 +58,7 @@ TEST_CASE(two_bytes)
|
|||
u32 output = {};
|
||||
i32 output_signed = {};
|
||||
u8 buf[] = { 0x00, 0x1 };
|
||||
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { buf, sizeof(buf) }));
|
||||
FixedMemoryStream stream { ReadonlyBytes { buf, sizeof(buf) } };
|
||||
|
||||
// Only test with first byte expecting more, otherwise equivalent to single byte case
|
||||
for (u16 i = 0x80; i <= 0xFF; ++i) {
|
||||
|
@ -68,12 +68,12 @@ TEST_CASE(two_bytes)
|
|||
for (u8 j = 0u; j <= 0x3F; ++j) {
|
||||
buf[1] = j;
|
||||
|
||||
MUST(stream->seek(0));
|
||||
output = MUST(stream->read_value<LEB128<u32>>());
|
||||
MUST(stream.seek(0));
|
||||
output = MUST(stream.read_value<LEB128<u32>>());
|
||||
EXPECT_EQ(output, (static_cast<u32>(j) << 7) + (i & 0x7F));
|
||||
|
||||
MUST(stream->seek(0));
|
||||
output_signed = MUST(stream->read_value<LEB128<i32>>());
|
||||
MUST(stream.seek(0));
|
||||
output_signed = MUST(stream.read_value<LEB128<i32>>());
|
||||
EXPECT_EQ(output_signed, (static_cast<i32>(j) << 7) + (i & 0x7F));
|
||||
}
|
||||
|
||||
|
@ -81,12 +81,12 @@ TEST_CASE(two_bytes)
|
|||
for (u8 j = 0x40u; j < 0x80; ++j) {
|
||||
buf[1] = j;
|
||||
|
||||
MUST(stream->seek(0));
|
||||
output = MUST(stream->read_value<LEB128<u32>>());
|
||||
MUST(stream.seek(0));
|
||||
output = MUST(stream.read_value<LEB128<u32>>());
|
||||
EXPECT_EQ(output, (static_cast<u32>(j) << 7) + (i & 0x7F));
|
||||
|
||||
MUST(stream->seek(0));
|
||||
output_signed = MUST(stream->read_value<LEB128<i32>>());
|
||||
MUST(stream.seek(0));
|
||||
output_signed = MUST(stream.read_value<LEB128<i32>>());
|
||||
EXPECT_EQ(output_signed, ((static_cast<i32>(j) << 7) + (i & 0x7F)) | (-1 & (~0x3FFF)));
|
||||
}
|
||||
|
||||
|
@ -94,11 +94,11 @@ TEST_CASE(two_bytes)
|
|||
for (u16 j = 0x80; j <= 0xFF; ++j) {
|
||||
buf[1] = static_cast<u8>(j);
|
||||
|
||||
MUST(stream->seek(0));
|
||||
EXPECT(stream->read_value<LEB128<u32>>().is_error());
|
||||
MUST(stream.seek(0));
|
||||
EXPECT(stream.read_value<LEB128<u32>>().is_error());
|
||||
|
||||
MUST(stream->seek(0));
|
||||
EXPECT(stream->read_value<LEB128<i32>>().is_error());
|
||||
MUST(stream.seek(0));
|
||||
EXPECT(stream.read_value<LEB128<i32>>().is_error());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -107,22 +107,22 @@ TEST_CASE(overflow_sizeof_output_unsigned)
|
|||
{
|
||||
u8 u32_max_plus_one[] = { 0x80, 0x80, 0x80, 0x80, 0x10 };
|
||||
{
|
||||
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { u32_max_plus_one, sizeof(u32_max_plus_one) }));
|
||||
EXPECT(stream->read_value<LEB128<u32>>().is_error());
|
||||
FixedMemoryStream stream { ReadonlyBytes { u32_max_plus_one, sizeof(u32_max_plus_one) } };
|
||||
EXPECT(stream.read_value<LEB128<u32>>().is_error());
|
||||
|
||||
MUST(stream->seek(0));
|
||||
u64 out64 = MUST(stream->read_value<LEB128<u64>>());
|
||||
MUST(stream.seek(0));
|
||||
u64 out64 = MUST(stream.read_value<LEB128<u64>>());
|
||||
EXPECT_EQ(out64, static_cast<u64>(NumericLimits<u32>::max()) + 1);
|
||||
}
|
||||
|
||||
u8 u32_max[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x0F };
|
||||
{
|
||||
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { u32_max, sizeof(u32_max) }));
|
||||
u32 out = MUST(stream->read_value<LEB128<u32>>());
|
||||
FixedMemoryStream stream { ReadonlyBytes { u32_max, sizeof(u32_max) } };
|
||||
u32 out = MUST(stream.read_value<LEB128<u32>>());
|
||||
EXPECT_EQ(out, NumericLimits<u32>::max());
|
||||
|
||||
MUST(stream->seek(0));
|
||||
u64 out64 = MUST(stream->read_value<LEB128<u64>>());
|
||||
MUST(stream.seek(0));
|
||||
u64 out64 = MUST(stream.read_value<LEB128<u64>>());
|
||||
EXPECT_EQ(out64, NumericLimits<u32>::max());
|
||||
}
|
||||
}
|
||||
|
@ -131,43 +131,43 @@ TEST_CASE(overflow_sizeof_output_signed)
|
|||
{
|
||||
u8 i32_max_plus_one[] = { 0x80, 0x80, 0x80, 0x80, 0x08 };
|
||||
{
|
||||
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_max_plus_one, sizeof(i32_max_plus_one) }));
|
||||
EXPECT(stream->read_value<LEB128<i32>>().is_error());
|
||||
FixedMemoryStream stream { ReadonlyBytes { i32_max_plus_one, sizeof(i32_max_plus_one) } };
|
||||
EXPECT(stream.read_value<LEB128<i32>>().is_error());
|
||||
|
||||
MUST(stream->seek(0));
|
||||
i64 out64 = MUST(stream->read_value<LEB128<i64>>());
|
||||
MUST(stream.seek(0));
|
||||
i64 out64 = MUST(stream.read_value<LEB128<i64>>());
|
||||
EXPECT_EQ(out64, static_cast<i64>(NumericLimits<i32>::max()) + 1);
|
||||
}
|
||||
|
||||
u8 i32_max[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x07 };
|
||||
{
|
||||
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_max, sizeof(i32_max) }));
|
||||
i32 out = MUST(stream->read_value<LEB128<i32>>());
|
||||
FixedMemoryStream stream { ReadonlyBytes { i32_max, sizeof(i32_max) } };
|
||||
i32 out = MUST(stream.read_value<LEB128<i32>>());
|
||||
EXPECT_EQ(out, NumericLimits<i32>::max());
|
||||
|
||||
MUST(stream->seek(0));
|
||||
i64 out64 = MUST(stream->read_value<LEB128<i64>>());
|
||||
MUST(stream.seek(0));
|
||||
i64 out64 = MUST(stream.read_value<LEB128<i64>>());
|
||||
EXPECT_EQ(out64, NumericLimits<i32>::max());
|
||||
}
|
||||
|
||||
u8 i32_min_minus_one[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x77 };
|
||||
{
|
||||
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_min_minus_one, sizeof(i32_min_minus_one) }));
|
||||
EXPECT(stream->read_value<LEB128<i32>>().is_error());
|
||||
FixedMemoryStream stream { ReadonlyBytes { i32_min_minus_one, sizeof(i32_min_minus_one) } };
|
||||
EXPECT(stream.read_value<LEB128<i32>>().is_error());
|
||||
|
||||
MUST(stream->seek(0));
|
||||
i64 out64 = MUST(stream->read_value<LEB128<i64>>());
|
||||
MUST(stream.seek(0));
|
||||
i64 out64 = MUST(stream.read_value<LEB128<i64>>());
|
||||
EXPECT_EQ(out64, static_cast<i64>(NumericLimits<i32>::min()) - 1);
|
||||
}
|
||||
|
||||
u8 i32_min[] = { 0x80, 0x80, 0x80, 0x80, 0x78 };
|
||||
{
|
||||
auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_min, sizeof(i32_min) }));
|
||||
i32 out = MUST(stream->read_value<LEB128<i32>>());
|
||||
FixedMemoryStream stream { ReadonlyBytes { i32_min, sizeof(i32_min) } };
|
||||
i32 out = MUST(stream.read_value<LEB128<i32>>());
|
||||
EXPECT_EQ(out, NumericLimits<i32>::min());
|
||||
|
||||
MUST(stream->seek(0));
|
||||
i64 out64 = MUST(stream->read_value<LEB128<i64>>());
|
||||
MUST(stream.seek(0));
|
||||
i64 out64 = MUST(stream.read_value<LEB128<i64>>());
|
||||
EXPECT_EQ(out64, NumericLimits<i32>::min());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue