| 1 |
package leb128 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"math" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/tetratelabs/wazero/internal/testing/require" |
| 10 |
) |
| 11 |
|
| 12 |
func TestEncode_DecodeInt32(t *testing.T) { |
| 13 |
for _, c := range []struct { |
| 14 |
input int32 |
| 15 |
expected []byte |
| 16 |
}{ |
| 17 |
{input: -165675008, expected: []byte{0x80, 0x80, 0x80, 0xb1, 0x7f}}, |
| 18 |
{input: -624485, expected: []byte{0x9b, 0xf1, 0x59}}, |
| 19 |
{input: -16256, expected: []byte{0x80, 0x81, 0x7f}}, |
| 20 |
{input: -4, expected: []byte{0x7c}}, |
| 21 |
{input: -1, expected: []byte{0x7f}}, |
| 22 |
{input: 0, expected: []byte{0x00}}, |
| 23 |
{input: 1, expected: []byte{0x01}}, |
| 24 |
{input: 4, expected: []byte{0x04}}, |
| 25 |
{input: 16256, expected: []byte{0x80, 0xff, 0x0}}, |
| 26 |
{input: 624485, expected: []byte{0xe5, 0x8e, 0x26}}, |
| 27 |
{input: 165675008, expected: []byte{0x80, 0x80, 0x80, 0xcf, 0x0}}, |
| 28 |
{input: int32(math.MaxInt32), expected: []byte{0xff, 0xff, 0xff, 0xff, 0x7}}, |
| 29 |
} { |
| 30 |
require.Equal(t, c.expected, EncodeInt32(c.input)) |
| 31 |
decoded, _, err := LoadInt32(c.expected) |
| 32 |
require.NoError(t, err) |
| 33 |
require.Equal(t, c.input, decoded) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
func TestEncode_DecodeInt64(t *testing.T) { |
| 38 |
for _, c := range []struct { |
| 39 |
input int64 |
| 40 |
expected []byte |
| 41 |
}{ |
| 42 |
{input: -math.MaxInt32, expected: []byte{0x81, 0x80, 0x80, 0x80, 0x78}}, |
| 43 |
{input: -165675008, expected: []byte{0x80, 0x80, 0x80, 0xb1, 0x7f}}, |
| 44 |
{input: -624485, expected: []byte{0x9b, 0xf1, 0x59}}, |
| 45 |
{input: -16256, expected: []byte{0x80, 0x81, 0x7f}}, |
| 46 |
{input: -4, expected: []byte{0x7c}}, |
| 47 |
{input: -1, expected: []byte{0x7f}}, |
| 48 |
{input: 0, expected: []byte{0x00}}, |
| 49 |
{input: 1, expected: []byte{0x01}}, |
| 50 |
{input: 4, expected: []byte{0x04}}, |
| 51 |
{input: 16256, expected: []byte{0x80, 0xff, 0x0}}, |
| 52 |
{input: 624485, expected: []byte{0xe5, 0x8e, 0x26}}, |
| 53 |
{input: 165675008, expected: []byte{0x80, 0x80, 0x80, 0xcf, 0x0}}, |
| 54 |
{input: math.MaxInt32, expected: []byte{0xff, 0xff, 0xff, 0xff, 0x7}}, |
| 55 |
{input: math.MaxInt64, expected: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0}}, |
| 56 |
} { |
| 57 |
require.Equal(t, c.expected, EncodeInt64(c.input)) |
| 58 |
decoded, _, err := LoadInt64(c.expected) |
| 59 |
require.NoError(t, err) |
| 60 |
require.Equal(t, c.input, decoded) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestEncodeUint32(t *testing.T) { |
| 65 |
for _, c := range []struct { |
| 66 |
input uint32 |
| 67 |
expected []byte |
| 68 |
}{ |
| 69 |
{input: 0, expected: []byte{0x00}}, |
| 70 |
{input: 1, expected: []byte{0x01}}, |
| 71 |
{input: 4, expected: []byte{0x04}}, |
| 72 |
{input: 16256, expected: []byte{0x80, 0x7f}}, |
| 73 |
{input: 624485, expected: []byte{0xe5, 0x8e, 0x26}}, |
| 74 |
{input: 165675008, expected: []byte{0x80, 0x80, 0x80, 0x4f}}, |
| 75 |
{input: uint32(math.MaxUint32), expected: []byte{0xff, 0xff, 0xff, 0xff, 0xf}}, |
| 76 |
} { |
| 77 |
require.Equal(t, c.expected, EncodeUint32(c.input)) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestEncodeUint64(t *testing.T) { |
| 82 |
for _, c := range []struct { |
| 83 |
input uint64 |
| 84 |
expected []byte |
| 85 |
}{ |
| 86 |
{input: 0, expected: []byte{0x00}}, |
| 87 |
{input: 1, expected: []byte{0x01}}, |
| 88 |
{input: 4, expected: []byte{0x04}}, |
| 89 |
{input: 16256, expected: []byte{0x80, 0x7f}}, |
| 90 |
{input: 624485, expected: []byte{0xe5, 0x8e, 0x26}}, |
| 91 |
{input: 165675008, expected: []byte{0x80, 0x80, 0x80, 0x4f}}, |
| 92 |
{input: math.MaxUint32, expected: []byte{0xff, 0xff, 0xff, 0xff, 0xf}}, |
| 93 |
{input: math.MaxUint64, expected: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1}}, |
| 94 |
} { |
| 95 |
require.Equal(t, c.expected, EncodeUint64(c.input)) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func TestDecodeUint32(t *testing.T) { |
| 100 |
for _, c := range []struct { |
| 101 |
bytes []byte |
| 102 |
exp uint32 |
| 103 |
expErr bool |
| 104 |
}{ |
| 105 |
{bytes: []byte{0xff, 0xff, 0xff, 0xff, 0xf}, exp: 0xffffffff}, |
| 106 |
{bytes: []byte{0x00}, exp: 0}, |
| 107 |
{bytes: []byte{0x04}, exp: 4}, |
| 108 |
{bytes: []byte{0x01}, exp: 1}, |
| 109 |
{bytes: []byte{0x80, 0}, exp: 0}, |
| 110 |
{bytes: []byte{0x80, 0x7f}, exp: 16256}, |
| 111 |
{bytes: []byte{0xe5, 0x8e, 0x26}, exp: 624485}, |
| 112 |
{bytes: []byte{0x80, 0x80, 0x80, 0x4f}, exp: 165675008}, |
| 113 |
{bytes: []byte{0xff, 0xff, 0xff, 0xff, 0xf}, exp: math.MaxUint32}, |
| 114 |
{bytes: []byte{0x83, 0x80, 0x80, 0x80, 0x80, 0x00}, expErr: true}, |
| 115 |
{bytes: []byte{0x82, 0x80, 0x80, 0x80, 0x70}, expErr: true}, |
| 116 |
{bytes: []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x00}, expErr: true}, |
| 117 |
} { |
| 118 |
actual, num, err := LoadUint32(c.bytes) |
| 119 |
if c.expErr { |
| 120 |
require.Error(t, err) |
| 121 |
} else { |
| 122 |
require.NoError(t, err) |
| 123 |
require.Equal(t, c.exp, actual) |
| 124 |
require.Equal(t, uint64(len(c.bytes)), num) |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func TestDecodeUint64(t *testing.T) { |
| 130 |
for _, c := range []struct { |
| 131 |
bytes []byte |
| 132 |
exp uint64 |
| 133 |
expErr bool |
| 134 |
}{ |
| 135 |
{bytes: []byte{0x04}, exp: 4}, |
| 136 |
{bytes: []byte{0x80, 0x7f}, exp: 16256}, |
| 137 |
{bytes: []byte{0xe5, 0x8e, 0x26}, exp: 624485}, |
| 138 |
{bytes: []byte{0x80, 0x80, 0x80, 0x4f}, exp: 165675008}, |
| 139 |
{bytes: []byte{0xff, 0xff, 0xff, 0xff, 0xf}, exp: math.MaxUint32}, |
| 140 |
{bytes: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1}, exp: math.MaxUint64}, |
| 141 |
{bytes: []byte{0x89, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x71}, expErr: true}, |
| 142 |
} { |
| 143 |
actual, num, err := LoadUint64(c.bytes) |
| 144 |
if c.expErr { |
| 145 |
require.Error(t, err) |
| 146 |
} else { |
| 147 |
require.NoError(t, err) |
| 148 |
require.Equal(t, c.exp, actual) |
| 149 |
require.Equal(t, uint64(len(c.bytes)), num) |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
func TestDecodeInt32(t *testing.T) { |
| 155 |
for i, c := range []struct { |
| 156 |
bytes []byte |
| 157 |
exp int32 |
| 158 |
expErr bool |
| 159 |
}{ |
| 160 |
{bytes: []byte{0x13}, exp: 19}, |
| 161 |
{bytes: []byte{0x00}, exp: 0}, |
| 162 |
{bytes: []byte{0x04}, exp: 4}, |
| 163 |
{bytes: []byte{0xFF, 0x00}, exp: 127}, |
| 164 |
{bytes: []byte{0x81, 0x01}, exp: 129}, |
| 165 |
{bytes: []byte{0x7f}, exp: -1}, |
| 166 |
{bytes: []byte{0x81, 0x7f}, exp: -127}, |
| 167 |
{bytes: []byte{0xFF, 0x7e}, exp: -129}, |
| 168 |
{bytes: []byte{0xff, 0xff, 0xff, 0xff, 0x0f}, expErr: true}, |
| 169 |
{bytes: []byte{0xff, 0xff, 0xff, 0xff, 0x4f}, expErr: true}, |
| 170 |
{bytes: []byte{0x80, 0x80, 0x80, 0x80, 0x70}, expErr: true}, |
| 171 |
} { |
| 172 |
actual, num, err := LoadInt32(c.bytes) |
| 173 |
if c.expErr { |
| 174 |
require.Error(t, err, fmt.Sprintf("%d-th got value %d", i, actual)) |
| 175 |
} else { |
| 176 |
require.NoError(t, err, i) |
| 177 |
require.Equal(t, c.exp, actual, i) |
| 178 |
require.Equal(t, uint64(len(c.bytes)), num, i) |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func TestDecodeInt33AsInt64(t *testing.T) { |
| 184 |
for _, c := range []struct { |
| 185 |
bytes []byte |
| 186 |
exp int64 |
| 187 |
}{ |
| 188 |
{bytes: []byte{0x00}, exp: 0}, |
| 189 |
{bytes: []byte{0x04}, exp: 4}, |
| 190 |
{bytes: []byte{0x40}, exp: -64}, |
| 191 |
{bytes: []byte{0x7f}, exp: -1}, |
| 192 |
{bytes: []byte{0x7e}, exp: -2}, |
| 193 |
{bytes: []byte{0x7d}, exp: -3}, |
| 194 |
{bytes: []byte{0x7c}, exp: -4}, |
| 195 |
{bytes: []byte{0xFF, 0x00}, exp: 127}, |
| 196 |
{bytes: []byte{0x81, 0x01}, exp: 129}, |
| 197 |
{bytes: []byte{0x7f}, exp: -1}, |
| 198 |
{bytes: []byte{0x81, 0x7f}, exp: -127}, |
| 199 |
{bytes: []byte{0xFF, 0x7e}, exp: -129}, |
| 200 |
} { |
| 201 |
actual, num, err := DecodeInt33AsInt64(bytes.NewReader(c.bytes)) |
| 202 |
require.NoError(t, err) |
| 203 |
require.Equal(t, c.exp, actual) |
| 204 |
require.Equal(t, uint64(len(c.bytes)), num) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
func TestDecodeInt64(t *testing.T) { |
| 209 |
for _, c := range []struct { |
| 210 |
bytes []byte |
| 211 |
exp int64 |
| 212 |
}{ |
| 213 |
{bytes: []byte{0x00}, exp: 0}, |
| 214 |
{bytes: []byte{0x04}, exp: 4}, |
| 215 |
{bytes: []byte{0xFF, 0x00}, exp: 127}, |
| 216 |
{bytes: []byte{0x81, 0x01}, exp: 129}, |
| 217 |
{bytes: []byte{0x7f}, exp: -1}, |
| 218 |
{bytes: []byte{0x81, 0x7f}, exp: -127}, |
| 219 |
{bytes: []byte{0xFF, 0x7e}, exp: -129}, |
| 220 |
{ |
| 221 |
bytes: []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f}, |
| 222 |
exp: -9223372036854775808, |
| 223 |
}, |
| 224 |
} { |
| 225 |
actual, num, err := LoadInt64(c.bytes) |
| 226 |
require.NoError(t, err) |
| 227 |
require.Equal(t, c.exp, actual) |
| 228 |
require.Equal(t, uint64(len(c.bytes)), num) |
| 229 |
} |
| 230 |
} |