-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_int32_test.go
152 lines (148 loc) · 3.85 KB
/
iterator_int32_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_ReadUint32(t *testing.T) {
t.Run("leading space", func(t *testing.T) {
withIterator(" 1", func(it *Iterator) {
u, err := it.ReadUint32()
require.NoError(t, err)
require.Equal(t, uint32(1), u)
})
})
t.Run("invalid first byte", func(t *testing.T) {
withIterator("", func(it *Iterator) {
_, err := it.ReadUint32()
require.Equal(t, io.EOF, err)
})
})
t.Run("zero", func(t *testing.T) {
withIterator("0", func(it *Iterator) {
i, err := it.ReadUint32()
require.NoError(t, err)
require.Equal(t, uint32(0), i)
})
})
t.Run("invalid first digit", func(t *testing.T) {
withIterator("-", func(it *Iterator) {
_, err := it.ReadUint32()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("early return", func(t *testing.T) {
withIterator("1a", func(it *Iterator) {
u, err := it.ReadUint32()
require.NoError(t, err)
require.Equal(t, uint32(1), u)
})
})
t.Run("overflow 1", func(t *testing.T) {
s := fmt.Sprintf("%d0", math.MaxUint32/10+1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadUint32()
require.IsType(t, IntOverflowError{}, err)
})
})
t.Run("overflow 2", func(t *testing.T) {
d := math.MaxUint32 / 10
m := math.MaxUint32 - d*10
s := fmt.Sprintf("%d%d", d, m+1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadUint32()
require.IsType(t, IntOverflowError{}, err)
})
})
t.Run("max uint32", func(t *testing.T) {
var m uint32 = math.MaxUint32
s := fmt.Sprint(m)
withIterator(s, func(it *Iterator) {
u, err := it.ReadUint32()
require.NoError(t, err)
require.Equal(t, m, u)
})
})
t.Run("reader", func(t *testing.T) {
withIterator("", func(it *Iterator) {
var m uint32 = math.MaxUint32
it.Reset(&stepByteReader{
b: fmt.Sprint(m),
})
u, err := it.ReadUint32()
require.NoError(t, err)
require.Equal(t, m, u)
})
})
}
func TestIterator_Int_ReadInt32(t *testing.T) {
t.Run("leading space", func(t *testing.T) {
withIterator(" -1", func(it *Iterator) {
i, err := it.ReadInt32()
require.NoError(t, err)
require.Equal(t, int32(-1), i)
})
})
t.Run("first byte error", func(t *testing.T) {
withIterator("", func(it *Iterator) {
_, err := it.ReadInt32()
require.Equal(t, io.EOF, err)
})
})
t.Run("negative error", func(t *testing.T) {
withIterator("-", func(it *Iterator) {
_, err := it.ReadInt32()
require.Equal(t, io.EOF, err)
})
})
t.Run("negative readUint32 error", func(t *testing.T) {
withIterator("-a", func(it *Iterator) {
_, err := it.ReadInt32()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("negative overflow", func(t *testing.T) {
s := fmt.Sprint(-math.MaxInt32 - 2)
withIterator(s, func(it *Iterator) {
_, err := it.ReadInt32()
e, ok := err.(IntOverflowError)
require.True(t, ok)
require.Equal(t, "int32", e.typ)
require.Equal(t, s, e.value)
})
})
t.Run("negative max", func(t *testing.T) {
s := fmt.Sprint(-math.MaxInt32 - 1)
withIterator(s, func(it *Iterator) {
i, err := it.ReadInt32()
require.NoError(t, err)
require.Equal(t, int32(-math.MaxInt32-1), i)
})
})
t.Run("positive readUint32 error", func(t *testing.T) {
withIterator("a", func(it *Iterator) {
_, err := it.ReadInt32()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("positive overflow", func(t *testing.T) {
s := fmt.Sprint(math.MaxInt32 + 1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadInt32()
e, ok := err.(IntOverflowError)
require.True(t, ok)
require.Equal(t, "int32", e.typ)
require.Equal(t, s, e.value)
})
})
t.Run("positive max", func(t *testing.T) {
s := fmt.Sprint(math.MaxInt32)
withIterator(s, func(it *Iterator) {
i, err := it.ReadInt32()
require.NoError(t, err)
require.Equal(t, int32(math.MaxInt32), i)
})
})
}