-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_int16_test.go
152 lines (148 loc) · 3.85 KB
/
iterator_int16_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package jzon
import (
"fmt"
"io"
"math"
"testing"
"github.com/stretchr/testify/require"
)
func TestIterator_Int_ReadUint16(t *testing.T) {
t.Run("leading space", func(t *testing.T) {
withIterator(" 1", func(it *Iterator) {
u, err := it.ReadUint16()
require.NoError(t, err)
require.Equal(t, uint16(1), u)
})
})
t.Run("invalid first byte", func(t *testing.T) {
withIterator("", func(it *Iterator) {
_, err := it.ReadUint16()
require.Equal(t, io.EOF, err)
})
})
t.Run("zero", func(t *testing.T) {
withIterator("0", func(it *Iterator) {
i, err := it.ReadUint16()
require.NoError(t, err)
require.Equal(t, uint16(0), i)
})
})
t.Run("invalid first digit", func(t *testing.T) {
withIterator("-", func(it *Iterator) {
_, err := it.ReadUint16()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("early return", func(t *testing.T) {
withIterator("1a", func(it *Iterator) {
u, err := it.ReadUint16()
require.NoError(t, err)
require.Equal(t, uint16(1), u)
})
})
t.Run("overflow 1", func(t *testing.T) {
s := fmt.Sprintf("%d0", math.MaxUint16/10+1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadUint16()
require.IsType(t, IntOverflowError{}, err)
})
})
t.Run("overflow 2", func(t *testing.T) {
d := math.MaxUint16 / 10
m := math.MaxUint16 - d*10
s := fmt.Sprintf("%d%d", d, m+1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadUint16()
require.IsType(t, IntOverflowError{}, err)
})
})
t.Run("max uint16", func(t *testing.T) {
var m uint16 = math.MaxUint16
s := fmt.Sprint(m)
withIterator(s, func(it *Iterator) {
u, err := it.ReadUint16()
require.NoError(t, err)
require.Equal(t, m, u)
})
})
t.Run("reader", func(t *testing.T) {
withIterator("", func(it *Iterator) {
var m uint16 = math.MaxUint16
it.Reset(&stepByteReader{
b: fmt.Sprint(m),
})
u, err := it.ReadUint16()
require.NoError(t, err)
require.Equal(t, m, u)
})
})
}
func TestIterator_Int_ReadInt16(t *testing.T) {
t.Run("leading space", func(t *testing.T) {
withIterator(" -1", func(it *Iterator) {
i, err := it.ReadInt16()
require.NoError(t, err)
require.Equal(t, int16(-1), i)
})
})
t.Run("first byte error", func(t *testing.T) {
withIterator("", func(it *Iterator) {
_, err := it.ReadInt16()
require.Equal(t, io.EOF, err)
})
})
t.Run("negative error", func(t *testing.T) {
withIterator("-", func(it *Iterator) {
_, err := it.ReadInt16()
require.Equal(t, io.EOF, err)
})
})
t.Run("negative readUint16 error", func(t *testing.T) {
withIterator("-a", func(it *Iterator) {
_, err := it.ReadInt16()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("negative overflow", func(t *testing.T) {
s := fmt.Sprint(-math.MaxInt16 - 2)
withIterator(s, func(it *Iterator) {
_, err := it.ReadInt16()
e, ok := err.(IntOverflowError)
require.True(t, ok)
require.Equal(t, "int16", e.typ)
require.Equal(t, s, e.value)
})
})
t.Run("negative max", func(t *testing.T) {
s := fmt.Sprint(-math.MaxInt16 - 1)
withIterator(s, func(it *Iterator) {
i, err := it.ReadInt16()
require.NoError(t, err)
require.Equal(t, int16(-math.MaxInt16-1), i)
})
})
t.Run("positive readUint16 error", func(t *testing.T) {
withIterator("a", func(it *Iterator) {
_, err := it.ReadInt16()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("positive overflow", func(t *testing.T) {
s := fmt.Sprint(math.MaxInt16 + 1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadInt16()
e, ok := err.(IntOverflowError)
require.True(t, ok)
require.Equal(t, "int16", e.typ)
require.Equal(t, s, e.value)
})
})
t.Run("positive max", func(t *testing.T) {
s := fmt.Sprint(math.MaxInt16)
withIterator(s, func(it *Iterator) {
i, err := it.ReadInt16()
require.NoError(t, err)
require.Equal(t, int16(math.MaxInt16), i)
})
})
}