-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfloat_test.go
186 lines (170 loc) · 4.44 KB
/
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package jx
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
// epsilon to compare floats.
const epsilon = 1e-6
func Test_read_big_float(t *testing.T) {
should := require.New(t)
r := DecodeStr(`12.3`)
val, err := r.BigFloat()
should.NoError(err)
val64, _ := val.Float64()
should.Equal(12.3, val64)
}
func Test_read_big_int(t *testing.T) {
should := require.New(t)
iter := DecodeStr(`92233720368547758079223372036854775807`)
val, err := iter.BigInt()
should.NoError(err)
should.NotNil(val)
should.Equal(`92233720368547758079223372036854775807`, val.String())
}
func Test_encode_inf(t *testing.T) {
should := require.New(t)
_, err := json.Marshal(math.Inf(1))
should.Error(err)
_, err = json.Marshal(float32(math.Inf(1)))
should.Error(err)
_, err = json.Marshal(math.Inf(-1))
should.Error(err)
}
func Test_encode_nan(t *testing.T) {
should := require.New(t)
_, err := json.Marshal(math.NaN())
should.Error(err)
_, err = json.Marshal(float32(math.NaN()))
should.Error(err)
_, err = json.Marshal(math.NaN())
should.Error(err)
}
func Test_read_float(t *testing.T) {
inputs := []string{
`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`,
`1e1`, `1e+1`, `1e-1`, `1E1`, `1E+1`, `1E-1`, `-1e1`, `-1e+1`, `-1e-1`,
}
for _, input := range inputs {
// non-streaming
t.Run(input, func(t *testing.T) {
should := require.New(t)
r := DecodeStr(input + ",")
expected, err := strconv.ParseFloat(input, 32)
should.NoError(err)
got, err := r.Float32()
should.NoError(err)
should.Equal(float32(expected), got)
})
t.Run(input, func(t *testing.T) {
should := require.New(t)
r := DecodeStr(input + ",")
expected, err := strconv.ParseFloat(input, 64)
should.NoError(err)
got, err := r.Float64()
should.NoError(err)
should.Equal(expected, got)
})
t.Run(input, func(t *testing.T) {
should := require.New(t)
iter := Decode(bytes.NewBufferString(input+","), 2)
expected, err := strconv.ParseFloat(input, 32)
should.NoError(err)
got, err := iter.Float32()
should.NoError(err)
should.Equal(float32(expected), got)
})
t.Run(input, func(t *testing.T) {
should := require.New(t)
iter := Decode(bytes.NewBufferString(input+","), 2)
val := float64(0)
err := json.Unmarshal([]byte(input), &val)
should.NoError(err)
got, err := iter.Float64()
should.NoError(err)
should.Equal(val, got)
})
}
}
func Test_write_float32(t *testing.T) {
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
w := GetEncoder()
w.Float32(val)
output, err := json.Marshal(val)
should.Nil(err)
should.Equal(output, w.Bytes())
})
}
should := require.New(t)
e := GetEncoder()
e.Float32(float32(0.0000001))
should.Equal("1e-7", string(e.Bytes()))
}
func Test_write_float64(t *testing.T) {
vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
e := GetEncoder()
e.Float64(val)
s := strconv.FormatFloat(val, 'f', -1, 64)
should.Equal(s, string(e.Bytes()))
})
}
should := require.New(t)
e := GetEncoder()
e.Float64(0.0000001)
should.Equal("1e-7", e.String())
}
func TestDecoder_FloatEOF(t *testing.T) {
d := GetDecoder()
_, err := d.Float32()
require.ErrorIs(t, err, io.ErrUnexpectedEOF)
}
func TestDecoder_FloatLeadingDot(t *testing.T) {
v := `.0`
_, err := DecodeStr(v).Float32()
require.Error(t, err)
_, err = DecodeStr(v).Float64()
require.Error(t, err)
}
func TestDecoder_FloatReaderErr(t *testing.T) {
getDecoder := func() *Decoder {
d := Decode(errReader{}, -1)
d.tail = 1
d.buf = []byte{'-'}
return d
}
_, err := getDecoder().Float32()
require.Error(t, err)
_, err = getDecoder().Float64()
require.Error(t, err)
}
func TestEncoder_FloatNanInf(t *testing.T) {
for _, f := range []float64{
math.NaN(),
math.Inf(-1),
math.Inf(1),
} {
var e Encoder
e.ArrStart()
e.Float64(f)
e.Float32(float32(f))
e.ArrEnd()
d := DecodeBytes(e.Bytes())
requireElem(t, d)
require.NoError(t, d.Null())
requireElem(t, d)
require.NoError(t, d.Null())
}
}