...
1 package misc_tests
2
3 import (
4 "encoding/json"
5 "math"
6 "testing"
7
8 "github.com/json-iterator/go"
9 "github.com/stretchr/testify/require"
10 )
11
12 func Test_read_big_float(t *testing.T) {
13 should := require.New(t)
14 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `12.3`)
15 val := iter.ReadBigFloat()
16 val64, _ := val.Float64()
17 should.Equal(12.3, val64)
18 }
19
20 func Test_read_big_int(t *testing.T) {
21 should := require.New(t)
22 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `92233720368547758079223372036854775807`)
23 val := iter.ReadBigInt()
24 should.NotNil(val)
25 should.Equal(`92233720368547758079223372036854775807`, val.String())
26 }
27
28 func Test_read_float_as_interface(t *testing.T) {
29 should := require.New(t)
30 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `12.3`)
31 should.Equal(float64(12.3), iter.Read())
32 }
33
34 func Test_wrap_float(t *testing.T) {
35 should := require.New(t)
36 str, err := jsoniter.MarshalToString(jsoniter.WrapFloat64(12.3))
37 should.Nil(err)
38 should.Equal("12.3", str)
39 }
40
41 func Test_read_float64_cursor(t *testing.T) {
42 should := require.New(t)
43 iter := jsoniter.ParseString(jsoniter.ConfigDefault, "[1.23456789\n,2,3]")
44 should.True(iter.ReadArray())
45 should.Equal(1.23456789, iter.Read())
46 should.True(iter.ReadArray())
47 should.Equal(float64(2), iter.Read())
48 }
49
50 func Test_read_float_scientific(t *testing.T) {
51 should := require.New(t)
52 var obj interface{}
53 should.NoError(jsoniter.UnmarshalFromString(`1e1`, &obj))
54 should.Equal(float64(10), obj)
55 should.NoError(json.Unmarshal([]byte(`1e1`), &obj))
56 should.Equal(float64(10), obj)
57 should.NoError(jsoniter.UnmarshalFromString(`1.0e1`, &obj))
58 should.Equal(float64(10), obj)
59 should.NoError(json.Unmarshal([]byte(`1.0e1`), &obj))
60 should.Equal(float64(10), obj)
61 }
62
63 func Test_lossy_float_marshal(t *testing.T) {
64 should := require.New(t)
65 api := jsoniter.Config{MarshalFloatWith6Digits: true}.Froze()
66 output, err := api.MarshalToString(float64(0.1234567))
67 should.Nil(err)
68 should.Equal("0.123457", output)
69 output, err = api.MarshalToString(float32(0.1234567))
70 should.Nil(err)
71 should.Equal("0.123457", output)
72 }
73
74 func Test_read_number(t *testing.T) {
75 should := require.New(t)
76 iter := jsoniter.ParseString(jsoniter.ConfigDefault, `92233720368547758079223372036854775807`)
77 val := iter.ReadNumber()
78 should.Equal(`92233720368547758079223372036854775807`, string(val))
79 }
80
81 func Test_encode_inf(t *testing.T) {
82 should := require.New(t)
83 _, err := json.Marshal(math.Inf(1))
84 should.Error(err)
85 _, err = jsoniter.Marshal(float32(math.Inf(1)))
86 should.Error(err)
87 _, err = jsoniter.Marshal(math.Inf(-1))
88 should.Error(err)
89 }
90
91 func Test_encode_nan(t *testing.T) {
92 should := require.New(t)
93 _, err := json.Marshal(math.NaN())
94 should.Error(err)
95 _, err = jsoniter.Marshal(float32(math.NaN()))
96 should.Error(err)
97 _, err = jsoniter.Marshal(math.NaN())
98 should.Error(err)
99 }
100
101 func Benchmark_jsoniter_float(b *testing.B) {
102 b.ReportAllocs()
103 input := []byte(`1.1123,`)
104 iter := jsoniter.NewIterator(jsoniter.ConfigDefault)
105 for n := 0; n < b.N; n++ {
106 iter.ResetBytes(input)
107 iter.ReadFloat64()
108 }
109 }
110
111 func Benchmark_json_float(b *testing.B) {
112 for n := 0; n < b.N; n++ {
113 result := float64(0)
114 json.Unmarshal([]byte(`1.1`), &result)
115 }
116 }
117
View as plain text