-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_int_test.go
88 lines (86 loc) · 1.3 KB
/
encode_int_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package msgpack
import (
"bytes"
"testing"
)
func TestEncoder_encodeInt(t *testing.T) {
tests := []struct {
name string
v int
want []byte
wantErr bool
}{
{
"positive fixnum",
0,
[]byte{0x00},
false,
},
{
"negative fixnum",
-1,
[]byte{0xFF},
false,
},
{
"int8",
-128,
[]byte{0xD0, 0x80},
false,
},
{
"int16",
-32768,
[]byte{0xD1, 0x80, 0x00},
false,
},
{
"int32",
-2147483648,
[]byte{0xD2, 0x80, 0x00, 0x00, 0x00},
false,
},
{
"int64",
-9223372036854775808,
[]byte{0xD3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
false,
},
{
"uint8",
1<<8 - 1,
[]byte{0xCC, 0xFF},
false,
},
{
"uint16",
1 << 8,
[]byte{0xCD, 0x01, 0x00},
false,
},
{
"uint32",
1 << 16,
[]byte{0xCE, 0x00, 0x01, 0x00, 0x00},
false,
},
{
"uint64",
1 << 32,
[]byte{0xCF, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewEncoder(nil).encodeInt(tt.v)
if (err != nil) != tt.wantErr {
t.Errorf("encodeInt() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !bytes.Equal(got, tt.want) {
t.Errorf("encodeInt() got = [% X], want [% X]", got, tt.want)
}
})
}
}