-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_int8_test.go
169 lines (165 loc) · 4.24 KB
/
iterator_int8_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package jzon
import (
"fmt"
"io"
"math"
"testing"
"github.com/stretchr/testify/require"
)
func TestIterator_Int_ReadUint8(t *testing.T) {
t.Run("leading space", func(t *testing.T) {
withIterator(" 1", func(it *Iterator) {
u, err := it.ReadUint8()
require.NoError(t, err)
require.Equal(t, uint8(1), u)
})
})
t.Run("invalid first byte", func(t *testing.T) {
withIterator("", func(it *Iterator) {
_, err := it.ReadUint8()
require.Equal(t, io.EOF, err)
})
})
t.Run("zero", func(t *testing.T) {
withIterator("0", func(it *Iterator) {
i, err := it.ReadUint8()
require.NoError(t, err)
require.Equal(t, uint8(0), i)
})
})
t.Run("invalid first digit", func(t *testing.T) {
withIterator("-", func(it *Iterator) {
_, err := it.ReadUint8()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("early return", func(t *testing.T) {
withIterator("1a", func(it *Iterator) {
u, err := it.ReadUint8()
require.NoError(t, err)
require.Equal(t, uint8(1), u)
})
})
t.Run("early return2", func(t *testing.T) {
withIterator("12a", func(it *Iterator) {
u, err := it.ReadUint8()
require.NoError(t, err)
require.Equal(t, uint8(12), u)
})
})
t.Run("overflow 1", func(t *testing.T) {
s := fmt.Sprintf("%d0", math.MaxUint8/10+1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadUint8()
require.IsType(t, IntOverflowError{}, err)
})
})
t.Run("overflow 2", func(t *testing.T) {
d := math.MaxUint8 / 10
m := math.MaxUint8 - d*10
s := fmt.Sprintf("%d%d", d, m+1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadUint8()
require.IsType(t, IntOverflowError{}, err)
})
})
t.Run("max uint8", func(t *testing.T) {
var m uint8 = math.MaxUint8
s := fmt.Sprint(m)
withIterator(s, func(it *Iterator) {
u, err := it.ReadUint8()
require.NoError(t, err)
require.Equal(t, m, u)
})
})
t.Run("reader", func(t *testing.T) {
withIterator("", func(it *Iterator) {
var m uint8 = math.MaxUint8
it.Reset(&stepByteReader{
b: fmt.Sprint(m),
})
u, err := it.ReadUint8()
require.NoError(t, err)
require.Equal(t, m, u)
})
})
t.Run("all values", func(t *testing.T) {
for i := 0; i <= math.MaxUint8; i++ {
s := fmt.Sprint(i)
withIterator(s, func(it *Iterator) {
j, err := it.ReadUint8()
require.NoError(t, err, "value %d", i)
require.Equal(t, uint8(i), j)
})
}
})
}
func TestIterator_Int_ReadInt8(t *testing.T) {
t.Run("leading space", func(t *testing.T) {
withIterator(" -1", func(it *Iterator) {
i, err := it.ReadInt8()
require.NoError(t, err)
require.Equal(t, int8(-1), i)
})
})
t.Run("first byte error", func(t *testing.T) {
withIterator("", func(it *Iterator) {
_, err := it.ReadInt8()
require.Equal(t, io.EOF, err)
})
})
t.Run("negative error", func(t *testing.T) {
withIterator("-", func(it *Iterator) {
_, err := it.ReadInt8()
require.Equal(t, io.EOF, err)
})
})
t.Run("negative readUint8 error", func(t *testing.T) {
withIterator("-a", func(it *Iterator) {
_, err := it.ReadInt8()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("negative overflow", func(t *testing.T) {
s := fmt.Sprint(-math.MaxInt8 - 2)
withIterator(s, func(it *Iterator) {
_, err := it.ReadInt8()
e, ok := err.(IntOverflowError)
require.True(t, ok)
require.Equal(t, "int8", e.typ)
require.Equal(t, s, e.value)
})
})
t.Run("negative max", func(t *testing.T) {
s := fmt.Sprint(-math.MaxInt8 - 1)
withIterator(s, func(it *Iterator) {
i, err := it.ReadInt8()
require.NoError(t, err)
require.Equal(t, int8(-math.MaxInt8-1), i)
})
})
t.Run("positive readUint8 error", func(t *testing.T) {
withIterator("a", func(it *Iterator) {
_, err := it.ReadInt8()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("positive overflow", func(t *testing.T) {
s := fmt.Sprint(math.MaxInt8 + 1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadInt8()
e, ok := err.(IntOverflowError)
require.True(t, ok)
require.Equal(t, "int8", e.typ)
require.Equal(t, s, e.value)
})
})
t.Run("positive max", func(t *testing.T) {
s := fmt.Sprint(math.MaxInt8)
withIterator(s, func(it *Iterator) {
i, err := it.ReadInt8()
require.NoError(t, err)
require.Equal(t, int8(math.MaxInt8), i)
})
})
}