-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdec_int_test.go
72 lines (65 loc) · 1.32 KB
/
dec_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
package jx
import (
"testing"
"github.com/stretchr/testify/require"
)
func BenchmarkDecoder_Int(b *testing.B) {
data := []byte(`69315063`)
d := GetDecoder()
for i := 0; i < b.N; i++ {
d.ResetBytes(data)
if _, err := d.Int(); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkDecoder_Uint(b *testing.B) {
data := []byte(`69315063`)
d := GetDecoder()
for i := 0; i < b.N; i++ {
d.ResetBytes(data)
if _, err := d.UInt(); err != nil {
b.Fatal(err)
}
}
}
func TestDecoder_int_sizes(t *testing.T) {
data := []byte(`69315063`)
d := GetDecoder()
for _, size := range []int{32, 64} {
d.ResetBytes(data)
v, err := d.int(size)
require.NoError(t, err)
require.Equal(t, 69315063, v)
}
}
func TestDecoder_uint_sizes(t *testing.T) {
data := []byte(`69315063`)
d := GetDecoder()
for _, size := range []int{32, 64} {
d.ResetBytes(data)
v, err := d.uint(size)
require.NoError(t, err)
require.Equal(t, uint(69315063), v)
}
}
func TestDecoder_Int(t *testing.T) {
r := errReader{}
get := func() *Decoder {
return &Decoder{
buf: []byte{'1', '2'},
tail: 2,
reader: errReader{},
}
}
t.Run("32", func(t *testing.T) {
d := get()
_, err := d.Int32()
require.ErrorIs(t, err, r.Err())
})
t.Run("64", func(t *testing.T) {
d := get()
_, err := d.Int64()
require.ErrorIs(t, err, r.Err())
})
}