jsoniter_float_test.go 3.1 KB

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