summaryrefslogtreecommitdiff
path: root/Tests/AK/TestLEB128.cpp
blob: f16af9bf0965f06b3d0471f6fe8071ed61bd1871 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/LEB128.h>
#include <AK/MemoryStream.h>
#include <AK/NumericLimits.h>
#include <LibTest/TestCase.h>

TEST_CASE(single_byte)
{
    u32 output = {};
    i32 output_signed = {};
    u8 buf[] = { 0x00 };
    auto stream = MUST(FixedMemoryStream::construct(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));
        EXPECT(!LEB128::read_unsigned(*stream, output).is_error());
        EXPECT_EQ(output, i);

        MUST(stream->seek(0));
        EXPECT(!LEB128::read_signed(*stream, output_signed).is_error());
        EXPECT_EQ(output_signed, i);
    }

    // 0b0100_0000 to 0b0111_1111 unsigned == byte, signed = {{ 26'b(-1), 6'b(byte) }}
    for (u8 i = 0x40u; i < 0x80; ++i) {
        buf[0] = i;

        MUST(stream->seek(0));
        EXPECT(!LEB128::read_unsigned(*stream, output).is_error());
        EXPECT_EQ(output, i);

        MUST(stream->seek(0));
        EXPECT(!LEB128::read_signed(*stream, output_signed).is_error());
        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(LEB128::read_unsigned(*stream, output).is_error());

        MUST(stream->seek(0));
        EXPECT(LEB128::read_signed(*stream, output_signed).is_error());
    }
}

TEST_CASE(two_bytes)
{
    u32 output = {};
    i32 output_signed = {};
    u8 buf[] = { 0x00, 0x1 };
    auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { buf, sizeof(buf) }));

    // Only test with first byte expecting more, otherwise equivalent to single byte case
    for (u16 i = 0x80; i <= 0xFF; ++i) {
        buf[0] = static_cast<u8>(i);

        // less than/eq 0b0011_1111: signed == unsigned == (j << 7) + (7 MSB of i)
        for (u8 j = 0u; j <= 0x3F; ++j) {
            buf[1] = j;

            MUST(stream->seek(0));
            EXPECT(!LEB128::read_unsigned(*stream, output).is_error());
            EXPECT_EQ(output, (static_cast<u32>(j) << 7) + (i & 0x7F));

            MUST(stream->seek(0));
            EXPECT(!LEB128::read_signed(*stream, output_signed).is_error());
            EXPECT_EQ(output_signed, (static_cast<i32>(j) << 7) + (i & 0x7F));
        }

        // 0b0100_0000 to 0b0111_1111: unsigned == (j << 7) + (7 MSB of i), signed == {{ 19'b(-1), 6'b(j), 7'b(i) }}
        for (u8 j = 0x40u; j < 0x80; ++j) {
            buf[1] = j;

            MUST(stream->seek(0));
            EXPECT(!LEB128::read_unsigned(*stream, output).is_error());
            EXPECT_EQ(output, (static_cast<u32>(j) << 7) + (i & 0x7F));

            MUST(stream->seek(0));
            EXPECT(!LEB128::read_signed(*stream, output_signed).is_error());
            EXPECT_EQ(output_signed, ((static_cast<i32>(j) << 7) + (i & 0x7F)) | (-1 & (~0x3FFF)));
        }

        // MSB set on last byte, but input too short
        for (u16 j = 0x80; j <= 0xFF; ++j) {
            buf[1] = static_cast<u8>(j);

            MUST(stream->seek(0));
            EXPECT(LEB128::read_unsigned(*stream, output).is_error());

            MUST(stream->seek(0));
            EXPECT(LEB128::read_signed(*stream, output_signed).is_error());
        }
    }
}

TEST_CASE(overflow_sizeof_output_unsigned)
{
    u8 u32_max_plus_one[] = { 0x80, 0x80, 0x80, 0x80, 0x10 };
    {
        u32 out = 0;
        auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { u32_max_plus_one, sizeof(u32_max_plus_one) }));
        EXPECT(LEB128::read_unsigned(*stream, out).is_error());
        EXPECT_EQ(out, 0u);

        u64 out64 = 0;
        MUST(stream->seek(0));
        EXPECT(!LEB128::read_unsigned(*stream, out64).is_error());
        EXPECT_EQ(out64, static_cast<u64>(NumericLimits<u32>::max()) + 1);
    }

    u8 u32_max[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x0F };
    {
        u32 out = 0;
        auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { u32_max, sizeof(u32_max) }));
        EXPECT(!LEB128::read_unsigned(*stream, out).is_error());
        EXPECT_EQ(out, NumericLimits<u32>::max());

        u64 out64 = 0;
        MUST(stream->seek(0));
        EXPECT(!LEB128::read_unsigned(*stream, out64).is_error());
        EXPECT_EQ(out64, NumericLimits<u32>::max());
    }
}

TEST_CASE(overflow_sizeof_output_signed)
{
    u8 i32_max_plus_one[] = { 0x80, 0x80, 0x80, 0x80, 0x08 };
    {
        i32 out = 0;
        auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_max_plus_one, sizeof(i32_max_plus_one) }));
        EXPECT(LEB128::read_signed(*stream, out).is_error());
        EXPECT_EQ(out, 0);

        i64 out64 = 0;
        MUST(stream->seek(0));
        EXPECT(!LEB128::read_signed(*stream, out64).is_error());
        EXPECT_EQ(out64, static_cast<i64>(NumericLimits<i32>::max()) + 1);
    }

    u8 i32_max[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x07 };
    {
        i32 out = 0;
        auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_max, sizeof(i32_max) }));
        EXPECT(!LEB128::read_signed(*stream, out).is_error());
        EXPECT_EQ(out, NumericLimits<i32>::max());

        i64 out64 = 0;
        MUST(stream->seek(0));
        EXPECT(!LEB128::read_signed(*stream, out64).is_error());
        EXPECT_EQ(out64, NumericLimits<i32>::max());
    }

    u8 i32_min_minus_one[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0x77 };
    {
        i32 out = 0;
        auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_min_minus_one, sizeof(i32_min_minus_one) }));
        EXPECT(LEB128::read_signed(*stream, out).is_error());
        EXPECT_EQ(out, 0);

        i64 out64 = 0;
        MUST(stream->seek(0));
        EXPECT(!LEB128::read_signed(*stream, out64).is_error());
        EXPECT_EQ(out64, static_cast<i64>(NumericLimits<i32>::min()) - 1);
    }

    u8 i32_min[] = { 0x80, 0x80, 0x80, 0x80, 0x78 };
    {
        i32 out = 0;
        auto stream = MUST(FixedMemoryStream::construct(ReadonlyBytes { i32_min, sizeof(i32_min) }));
        EXPECT(!LEB128::read_signed(*stream, out).is_error());
        EXPECT_EQ(out, NumericLimits<i32>::min());

        i64 out64 = 0;
        MUST(stream->seek(0));
        EXPECT(!LEB128::read_signed(*stream, out64).is_error());
        EXPECT_EQ(out64, NumericLimits<i32>::min());
    }
}