-
Notifications
You must be signed in to change notification settings - Fork 2
/
u16_test.go
72 lines (58 loc) · 1.53 KB
/
u16_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
67
68
69
70
71
72
package goscale
import (
"bytes"
"io"
"math/big"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_EncodeU16(t *testing.T) {
var testExamples = []struct {
label string
input U16
expectation []byte
}{
{label: "uint16(127)", input: U16(127), expectation: []byte{0x7f, 0x00}},
{label: "uint16(42)", input: U16(42), expectation: []byte{0x2a, 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_DecodeU16(t *testing.T) {
var testExamples = []struct {
label string
input []byte
expectation U16
}{
{label: "(0x2a00)", input: []byte{0x2a, 0x00}, expectation: U16(42)},
}
for _, testExample := range testExamples {
t.Run(testExample.label, func(t *testing.T) {
buffer := &bytes.Buffer{}
buffer.Write(testExample.input)
result, err := DecodeU16(buffer)
assert.NoError(t, err)
assert.Equal(t, testExample.expectation, result)
})
}
}
func Test_DecodeU16_Empty(t *testing.T) {
buffer := &bytes.Buffer{}
result, err := DecodeU16(buffer)
assert.Equal(t, io.EOF, err)
assert.Equal(t, U16(0), result)
}
func Test_U16_ToBigInt(t *testing.T) {
n := U16(127)
nBigInt := n.ToBigInt()
expect, ok := new(big.Int).SetString("127", 10)
assert.True(t, ok)
assert.Equal(t, expect, nBigInt)
}