-
Notifications
You must be signed in to change notification settings - Fork 2
/
i32_test.go
66 lines (53 loc) · 1.63 KB
/
i32_test.go
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
package goscale
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_EncodeI32(t *testing.T) {
var testExamples = []struct {
label string
input I32
expectation []byte
}{
{label: "int32(-128)", input: -128, expectation: []byte{0x80, 0xff, 0xff, 0xff}},
{label: "int32(16777215)", input: 16777215, expectation: []byte{0xff, 0xff, 0xff, 0x00}},
{label: "int32(127)", input: 127, expectation: []byte{0x7f, 0x00, 0x00, 0x00}},
}
for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
buffer := &bytes.Buffer{}
err := testExample.input.Encode(buffer)
assert.NoError(t, err)
assert.Equal(t, testExample.expectation, buffer.Bytes())
assert.Equal(t, testExample.expectation, testExample.input.Bytes())
})
}
}
func Test_DecodeI32(t *testing.T) {
var testExamples = []struct {
label string
input []byte
expectation I32
}{
{label: "(0x80ffffff)", input: []byte{0x80, 0xff, 0xff, 0xff}, expectation: -128},
{label: "(0xffffff00)", input: []byte{0xff, 0xff, 0xff, 0x00}, expectation: 16777215},
{label: "(0x7f000000)", input: []byte{0x7f, 0x00, 0x00, 0x00}, expectation: 127},
}
for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
buffer := &bytes.Buffer{}
buffer.Write(testExample.input)
result, err := DecodeI32(buffer)
assert.NoError(t, err)
assert.Equal(t, testExample.expectation, result)
})
}
}
func Test_DecodeI32_Empty(t *testing.T) {
buffer := &bytes.Buffer{}
result, err := DecodeI32(buffer)
assert.Equal(t, io.EOF, err)
assert.Equal(t, I32(0), result)
}