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