1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-23 18:54:21 +02:00
json-iterator/jsoniter_float_test.go

207 lines
5.9 KiB
Go
Raw Normal View History

2017-07-02 15:20:18 +08:00
// +build go1.8
2016-12-01 23:25:29 +08:00
package jsoniter
import (
2017-06-06 23:27:00 +08:00
"bytes"
2016-12-01 23:25:29 +08:00
"encoding/json"
2016-12-08 10:02:18 +08:00
"fmt"
2017-01-07 23:06:48 +08:00
"strconv"
2017-06-06 23:27:00 +08:00
"testing"
2017-07-06 11:44:39 +08:00
"github.com/json-iterator/go/require"
2016-12-01 23:25:29 +08:00
)
2017-05-31 12:40:50 +08:00
func Test_read_big_float(t *testing.T) {
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := ParseString(ConfigDefault, `12.3`)
2017-05-31 12:40:50 +08:00
val := iter.ReadBigFloat()
val64, _ := val.Float64()
should.Equal(12.3, val64)
}
func Test_read_big_int(t *testing.T) {
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := ParseString(ConfigDefault, `92233720368547758079223372036854775807`)
2017-05-31 12:40:50 +08:00
val := iter.ReadBigInt()
should.NotNil(val)
should.Equal(`92233720368547758079223372036854775807`, val.String())
}
2017-01-15 18:08:49 +08:00
func Test_read_float(t *testing.T) {
2017-01-15 17:30:02 +08:00
inputs := []string{`1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`}
for _, input := range inputs {
// non-streaming
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := ParseString(ConfigDefault, input+",")
2017-01-15 17:30:02 +08:00
expected, err := strconv.ParseFloat(input, 32)
should.Nil(err)
should.Equal(float32(expected), iter.ReadFloat32())
})
2017-01-15 18:08:49 +08:00
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := ParseString(ConfigDefault, input+",")
2017-01-15 18:08:49 +08:00
expected, err := strconv.ParseFloat(input, 64)
should.Nil(err)
should.Equal(expected, iter.ReadFloat64())
})
2017-01-15 17:30:02 +08:00
// streaming
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := Parse(ConfigDefault, bytes.NewBufferString(input+","), 2)
2017-01-15 17:30:02 +08:00
expected, err := strconv.ParseFloat(input, 32)
should.Nil(err)
should.Equal(float32(expected), iter.ReadFloat32())
})
2017-01-15 18:08:49 +08:00
t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := Parse(ConfigDefault, bytes.NewBufferString(input+","), 2)
2017-01-15 18:08:49 +08:00
expected, err := strconv.ParseFloat(input, 64)
should.Nil(err)
should.Equal(expected, iter.ReadFloat64())
})
2016-12-06 10:21:47 +08:00
}
}
2017-01-22 23:38:55 +08:00
func Test_read_float_as_interface(t *testing.T) {
2017-01-21 16:09:38 +08:00
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := ParseString(ConfigDefault, `12.3`)
2017-01-21 16:09:38 +08:00
should.Equal(float64(12.3), iter.Read())
}
2017-01-26 16:33:16 +08:00
func Test_wrap_float(t *testing.T) {
should := require.New(t)
str, err := MarshalToString(WrapFloat64(12.3))
should.Nil(err)
should.Equal("12.3", str)
}
2017-01-07 23:06:48 +08:00
func Test_write_float32(t *testing.T) {
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
2017-06-06 23:27:00 +08:00
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
2017-01-07 23:06:48 +08:00
for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
2017-06-17 10:21:37 +08:00
stream := NewStream(ConfigDefault, buf, 4096)
stream.WriteFloat32Lossy(val)
2017-01-07 23:06:48 +08:00
stream.Flush()
should.Nil(stream.Error)
2017-07-02 15:11:36 +08:00
output, err := json.Marshal(val)
should.Nil(err)
should.Equal(string(output), buf.String())
2017-01-07 23:06:48 +08:00
})
2017-01-09 19:19:48 +08:00
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
2017-06-17 10:21:37 +08:00
stream := NewStream(ConfigDefault, buf, 4096)
2017-01-09 19:19:48 +08:00
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
2017-07-02 15:11:36 +08:00
output, err := json.Marshal(val)
should.Nil(err)
should.Equal(string(output), buf.String())
2017-01-09 19:19:48 +08:00
})
2017-01-07 23:06:48 +08:00
}
should := require.New(t)
buf := &bytes.Buffer{}
2017-06-17 10:21:37 +08:00
stream := NewStream(ConfigDefault, buf, 10)
2017-01-07 23:06:48 +08:00
stream.WriteRaw("abcdefg")
stream.WriteFloat32Lossy(1.123456)
2017-01-07 23:06:48 +08:00
stream.Flush()
should.Nil(stream.Error)
should.Equal("abcdefg1.123456", buf.String())
2017-07-06 11:44:39 +08:00
stream = NewStream(ConfigDefault, nil, 0)
stream.WriteFloat32(float32(0.0000001))
should.Equal("1e-07", string(stream.buf))
2017-01-07 23:06:48 +08:00
}
2017-01-07 23:32:54 +08:00
func Test_write_float64(t *testing.T) {
vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
2017-06-06 23:27:00 +08:00
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
2017-01-07 23:32:54 +08:00
for _, val := range vals {
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
2017-06-17 10:21:37 +08:00
stream := NewStream(ConfigDefault, buf, 4096)
stream.WriteFloat64Lossy(val)
2017-01-07 23:32:54 +08:00
stream.Flush()
should.Nil(stream.Error)
should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
})
2017-01-09 19:19:48 +08:00
t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
should := require.New(t)
buf := &bytes.Buffer{}
2017-06-17 10:21:37 +08:00
stream := NewStream(ConfigDefault, buf, 4096)
2017-01-09 19:19:48 +08:00
stream.WriteVal(val)
stream.Flush()
should.Nil(stream.Error)
should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
})
2017-01-07 23:32:54 +08:00
}
should := require.New(t)
buf := &bytes.Buffer{}
2017-06-17 10:21:37 +08:00
stream := NewStream(ConfigDefault, buf, 10)
2017-01-07 23:32:54 +08:00
stream.WriteRaw("abcdefg")
stream.WriteFloat64Lossy(1.123456)
2017-01-07 23:32:54 +08:00
stream.Flush()
should.Nil(stream.Error)
should.Equal("abcdefg1.123456", buf.String())
2017-07-06 11:44:39 +08:00
stream = NewStream(ConfigDefault, nil, 0)
stream.WriteFloat64(float64(0.0000001))
should.Equal("1e-07", string(stream.buf))
2017-01-07 23:32:54 +08:00
}
func Test_read_float64_cursor(t *testing.T) {
should := require.New(t)
2017-06-17 10:21:37 +08:00
iter := ParseString(ConfigDefault, "[1.23456789\n,2,3]")
should.True(iter.ReadArray())
should.Equal(1.23456789, iter.Read())
should.True(iter.ReadArray())
should.Equal(float64(2), iter.Read())
}
2017-06-07 21:34:56 +08:00
func Test_read_float_scientific(t *testing.T) {
should := require.New(t)
var obj interface{}
should.Nil(UnmarshalFromString(`1e1`, &obj))
should.Equal(float64(10), obj)
should.Nil(json.Unmarshal([]byte(`1e1`), &obj))
should.Equal(float64(10), obj)
should.Nil(UnmarshalFromString(`1.0e1`, &obj))
should.Equal(float64(10), obj)
should.Nil(json.Unmarshal([]byte(`1.0e1`), &obj))
should.Equal(float64(10), obj)
}
2017-06-20 07:51:38 +08:00
func Test_lossy_float_marshal(t *testing.T) {
should := require.New(t)
api := Config{MarshalFloatWith6Digits: true}.Froze()
output, err := api.MarshalToString(float64(0.1234567))
should.Nil(err)
should.Equal("0.123457", output)
output, err = api.MarshalToString(float32(0.1234567))
should.Nil(err)
should.Equal("0.123457", output)
}
2016-12-01 23:25:29 +08:00
func Benchmark_jsoniter_float(b *testing.B) {
2016-12-08 10:02:18 +08:00
b.ReportAllocs()
2017-01-15 17:30:02 +08:00
input := []byte(`1.1123,`)
2017-06-17 10:21:37 +08:00
iter := NewIterator(ConfigDefault)
2016-12-01 23:25:29 +08:00
for n := 0; n < b.N; n++ {
2017-01-15 17:30:02 +08:00
iter.ResetBytes(input)
2017-01-15 18:08:49 +08:00
iter.ReadFloat64()
2016-12-01 23:25:29 +08:00
}
}
func Benchmark_json_float(b *testing.B) {
for n := 0; n < b.N; n++ {
result := float64(0)
json.Unmarshal([]byte(`1.1`), &result)
}
}