-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdec_num_test.go
146 lines (131 loc) · 3.01 KB
/
dec_num_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
package jx
import (
"fmt"
"io"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func testDecoderNum(t *testing.T, num func(*Decoder) (Num, error)) {
t.Run("Cases", func(t *testing.T) {
runTestCases(t, testNumbers, func(t *testing.T, d *Decoder) error {
if _, err := num(d); err != nil {
return err
}
if err := d.Skip(); err != nil {
if err != io.EOF && err != io.ErrUnexpectedEOF {
return err
}
}
return nil
})
})
testNum := func(inputs []string, cb func(input string, t *testing.T, d *Decoder)) func(t *testing.T) {
return func(t *testing.T) {
for i, input := range inputs {
input := input
t.Run(fmt.Sprintf("Test%d", i+1), testBufferReader(input, func(t *testing.T, d *Decoder) {
cb(input, t, d)
}))
}
}
}
t.Run("StrEscape", testNum([]string{
`"\u0030"`, // hex 30 = dec 48 = '0'
`"\u002d\u0031\u0030\u0030"`, // "-100", but escaped
}, func(input string, t *testing.T, d *Decoder) {
_, err := num(d)
require.NoErrorf(t, err, "input: %q", input)
}))
t.Run("Positive", testNum([]string{
`100`,
`100.0`,
`-100.0`,
`-100`,
`"-100"`,
`"-100.0"`,
}, func(input string, t *testing.T, d *Decoder) {
v, err := num(d)
require.NoErrorf(t, err, "input: %q", input)
require.Equalf(t, input, v.String(), "input: %q", input)
}))
t.Run("Negative", testNum([]string{
`1.00.0`,
`"-100`,
`""`,
`"-100.0.0"`,
"false",
`"false"`,
}, func(input string, t *testing.T, d *Decoder) {
_, err := num(d)
require.Errorf(t, err, "input: %q", input)
}))
}
func TestDecoder_Num(t *testing.T) {
testDecoderNum(t, (*Decoder).Num)
}
func TestDecoder_NumAppend(t *testing.T) {
testDecoderNum(t, func(d *Decoder) (Num, error) {
return d.NumAppend(nil)
})
}
func BenchmarkDecoder_Num(b *testing.B) {
number := strconv.FormatInt(1234567890421, 10)
// escapeHex escapes the number as a string in \uXXXX format.
escapeHex := func(number string) string {
var b strings.Builder
b.WriteByte('"')
for _, c := range []byte(number) {
b.WriteString("\\u00")
b.WriteString(strconv.FormatInt(int64(c), 16))
}
b.WriteByte('"')
return b.String()
}
for _, bt := range []struct {
name string
input string
}{
{`Number`, number},
{`String`, strconv.Quote(number)},
{`EscapedString`, escapeHex(number)},
} {
bt := bt
b.Run(bt.name, func(b *testing.B) {
b.Run("Buffer", func(b *testing.B) {
var (
input = []byte(bt.input)
d = DecodeBytes(input)
err error
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
d.ResetBytes(input)
_, err = d.Num()
}
if err != nil {
b.Fatal(err, bt.input)
}
})
b.Run("Reader", func(b *testing.B) {
var (
r = strings.NewReader(bt.input)
d = Decode(r, 512)
err error
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Reset(bt.input)
d.Reset(r)
_, err = d.Num()
}
if err != nil {
b.Fatal(err, bt.input)
}
})
})
}
}