forked from mhr3/jsoniter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter_float_test.go
156 lines (137 loc) · 3.72 KB
/
iter_float_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
package jsoniter
import (
"math"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestReadNumber(t *testing.T) {
input := []byte(`{"num":1234567890}`)
iter := ParseBytes(ConfigDefault, input)
key, _ := iter.ReadObject()
require.Equal(t, "num", key)
num := iter.ReadNumber()
require.Equal(t, "1234567890", num.String())
n, _ := num.Int64()
require.EqualValues(t, 1234567890, n)
}
func TestParseNumber(t *testing.T) {
testCases := map[string]interface{}{
"-1": -1,
"0": 0,
"400": uint(400),
"1234567890": 1234567890,
"0.0125": float32(0.0125),
"-64.5": float32(-64.5),
"-0.00625": float64(-0.00625),
"12.3e8": float64(12.3e8),
"18446744073709551616": float64(math.MaxUint64 + 1),
"-9223372036854775808": int64(math.MinInt64),
"9223372036854775807": int64(math.MaxInt64),
"18446744073709551615": uint64(math.MaxUint64),
}
iter := NewIterator(ConfigDefault)
for input, expected := range testCases {
iter.ResetBytes([]byte(input))
iter.Error = nil
switch val := expected.(type) {
case int:
require.Equal(t, val, iter.ReadInt())
case int64:
require.Equal(t, val, iter.ReadInt64())
case uint:
require.Equal(t, val, iter.ReadUint())
case uint64:
require.Equal(t, val, iter.ReadUint64())
case float64:
require.Equal(t, val, iter.ReadFloat64())
case float32:
require.Equal(t, val, iter.ReadFloat32())
}
}
}
func BenchmarkAllocs(b *testing.B) {
type tcPair struct {
Value interface{}
Slice []byte
}
testCases := map[string]*tcPair{
"0.0125": {Value: float32(0.0125)},
"-64.5": {Value: float32(-64.5)},
"-0.00625": {Value: float64(-0.00625)},
"12.3e8": {Value: float64(12.3e8)},
"18446744073709551616": {Value: float64(18446744073709551616)},
}
for k, v := range testCases {
v.Slice = []byte(k)
}
iter := NewIterator(ConfigDefault)
for i := 0; i < b.N; i++ {
for _, expected := range testCases {
iter.ResetBytes(expected.Slice)
iter.Error = nil
switch val := expected.Value.(type) {
case int:
if iter.ReadInt() != val {
b.Fatal("mismatch @", val)
}
case int64:
if iter.ReadInt64() != val {
b.Fatal("mismatch @", val)
}
case uint:
if iter.ReadUint() != val {
b.Fatal("mismatch @", val)
}
case uint64:
if iter.ReadUint64() != val {
b.Fatal("mismatch @", val)
}
case float64:
if iter.ReadFloat64() != val {
b.Fatal("mismatch @", val)
}
case float32:
if iter.ReadFloat32() != val {
b.Fatal("mismatch @", val)
}
}
}
}
}
func BenchmarkNumberAllocs(b *testing.B) {
type tcPair struct {
Value interface{}
Slice []byte
}
testCases := map[string]*tcPair{
"0.0125": {Value: float32(0.0125)},
"-64.5": {Value: float32(-64.5)},
"-0.00625": {Value: float64(-0.00625)},
"12.3e8": {Value: float64(12.3e8)},
"18446744073709551616": {Value: float64(18446744073709551616)},
}
for k, v := range testCases {
v.Slice = []byte(k)
}
iter := NewIterator(ConfigDefault)
for i := 0; i < b.N; i++ {
for _, expected := range testCases {
iter.ResetBytes(expected.Slice)
iter.Error = nil
scratch := [24]byte{}
switch val := expected.Value.(type) {
case float64:
buf := iter.ReadNumberAsSlice(scratch[:0])
if res, _ := strconv.ParseFloat(string(buf), 64); res != val {
b.Fatal("mismatch @", val, "!=", res)
}
case float32:
buf := iter.ReadNumberAsSlice(scratch[:0])
if res, _ := strconv.ParseFloat(string(buf), 64); float32(res) != val {
b.Fatal("mismatch @", val, "!=", res)
}
}
}
}
}