1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-15 22:50:24 +02:00

optimize read float64

This commit is contained in:
Tao Wen
2017-01-15 18:08:49 +08:00
parent 5b2a731963
commit c3c57d61b5
2 changed files with 90 additions and 38 deletions

View File

@ -9,23 +9,7 @@ import (
"strconv"
)
func Test_float64_0(t *testing.T) {
iter := ParseString(`0`)
val := iter.ReadFloat64()
if val != 0 {
t.Fatal(val)
}
}
func Test_float64_1_dot_1(t *testing.T) {
iter := ParseString(`1.1`)
val := iter.ReadFloat64()
if val != 1.1 {
t.Fatal(val)
}
}
func Test_read_float32(t *testing.T) {
func Test_read_float(t *testing.T) {
inputs := []string{`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`}
for _, input := range inputs {
// non-streaming
@ -36,6 +20,13 @@ func Test_read_float32(t *testing.T) {
should.Nil(err)
should.Equal(float32(expected), iter.ReadFloat32())
})
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := ParseString(input + ",")
expected, err := strconv.ParseFloat(input, 64)
should.Nil(err)
should.Equal(expected, iter.ReadFloat64())
})
// streaming
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
@ -44,6 +35,13 @@ func Test_read_float32(t *testing.T) {
should.Nil(err)
should.Equal(float32(expected), iter.ReadFloat32())
})
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
iter := Parse(bytes.NewBufferString(input + ","), 2)
expected, err := strconv.ParseFloat(input, 64)
should.Nil(err)
should.Equal(expected, iter.ReadFloat64())
})
}
}
@ -119,7 +117,7 @@ func Benchmark_jsoniter_float(b *testing.B) {
iter := NewIterator()
for n := 0; n < b.N; n++ {
iter.ResetBytes(input)
iter.ReadFloat32()
iter.ReadFloat64()
}
}