-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_int64_test.go
160 lines (156 loc) · 4.08 KB
/
iterator_int64_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
package jzon
import (
"fmt"
"io"
"math"
"testing"
"github.com/stretchr/testify/require"
)
func TestIterator_Int_ReadUint64(t *testing.T) {
t.Run("leading space", func(t *testing.T) {
withIterator(" 1", func(it *Iterator) {
u, err := it.ReadUint64()
require.NoError(t, err)
require.Equal(t, uint64(1), u)
})
})
t.Run("invalid first byte", func(t *testing.T) {
withIterator("", func(it *Iterator) {
_, err := it.ReadUint64()
require.Equal(t, io.EOF, err)
})
})
t.Run("zero", func(t *testing.T) {
withIterator("0", func(it *Iterator) {
i, err := it.ReadUint64()
require.NoError(t, err)
require.Equal(t, uint64(0), i)
})
})
t.Run("invalid first digit", func(t *testing.T) {
withIterator("-", func(it *Iterator) {
_, err := it.ReadUint64()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("early return", func(t *testing.T) {
withIterator("1a", func(it *Iterator) {
u, err := it.ReadUint64()
require.NoError(t, err)
require.Equal(t, uint64(1), u)
})
})
t.Run("overflow 1", func(t *testing.T) {
s := fmt.Sprintf("%d0", math.MaxUint64/10+1)
withIterator(s, func(it *Iterator) {
_, err := it.ReadUint64()
require.IsType(t, IntOverflowError{}, err)
})
})
t.Run("overflow 2", func(t *testing.T) {
d := uint64(math.MaxUint64) / 10
m := uint64(math.MaxUint64) - d*10 + 1
require.Less(t, m, uint64(10))
s := fmt.Sprintf("%d%d", d, m)
withIterator(s, func(it *Iterator) {
_, err := it.ReadUint64()
require.IsType(t, IntOverflowError{}, err)
})
})
t.Run("max uint64", func(t *testing.T) {
var m uint64 = math.MaxUint64
s := fmt.Sprint(m)
withIterator(s, func(it *Iterator) {
u, err := it.ReadUint64()
require.NoError(t, err)
require.Equal(t, m, u)
})
})
t.Run("reader", func(t *testing.T) {
withIterator("", func(it *Iterator) {
var m uint64 = math.MaxUint64
it.Reset(&stepByteReader{
b: fmt.Sprint(m),
})
u, err := it.ReadUint64()
require.NoError(t, err)
require.Equal(t, m, u)
})
})
}
func TestIterator_Int_ReadInt64(t *testing.T) {
t.Run("leading space", func(t *testing.T) {
withIterator(" -1", func(it *Iterator) {
i, err := it.ReadInt64()
require.NoError(t, err)
require.Equal(t, int64(-1), i)
})
})
t.Run("first byte error", func(t *testing.T) {
withIterator("", func(it *Iterator) {
_, err := it.ReadInt64()
require.Equal(t, io.EOF, err)
})
})
t.Run("negative error", func(t *testing.T) {
withIterator("-", func(it *Iterator) {
_, err := it.ReadInt64()
require.Equal(t, io.EOF, err)
})
})
t.Run("negative readUint64 error", func(t *testing.T) {
withIterator("-a", func(it *Iterator) {
_, err := it.ReadInt64()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("negative overflow", func(t *testing.T) {
d := int64(math.MaxInt64) / 10
m := int64(math.MaxInt64) - d*10 + 2
require.Less(t, m, int64(10))
s := fmt.Sprintf("-%d%d", d, m)
withIterator(s, func(it *Iterator) {
_, err := it.ReadInt64()
e, ok := err.(IntOverflowError)
require.True(t, ok)
require.Equal(t, "int64", e.typ)
require.Equal(t, s, e.value)
})
})
t.Run("negative max", func(t *testing.T) {
v := int64(-math.MaxInt64 - 1)
s := fmt.Sprint(v)
withIterator(s, func(it *Iterator) {
i, err := it.ReadInt64()
require.NoError(t, err)
require.Equal(t, v, i)
})
})
t.Run("positive readUint64 error", func(t *testing.T) {
withIterator("a", func(it *Iterator) {
_, err := it.ReadInt64()
require.IsType(t, InvalidDigitError{}, err)
})
})
t.Run("positive overflow", func(t *testing.T) {
d := int64(math.MaxInt64 / 10)
m := int64(math.MaxInt64-d*10) + 1
require.Less(t, m, int64(10))
s := fmt.Sprintf("%d%d", d, m)
withIterator(s, func(it *Iterator) {
_, err := it.ReadInt64()
e, ok := err.(IntOverflowError)
require.True(t, ok)
require.Equal(t, "int64", e.typ)
require.Equal(t, s, e.value)
})
})
t.Run("positive max", func(t *testing.T) {
s := fmt.Sprint(math.MaxInt64)
withIterator(s, func(it *Iterator) {
i, err := it.ReadInt64()
require.NoError(t, err)
require.Equal(t, int64(math.MaxInt64), i)
})
})
}