jsoniter_float_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // +build go1.8
  2. package jsoniter
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "strconv"
  8. "testing"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func Test_read_big_float(t *testing.T) {
  12. should := require.New(t)
  13. iter := ParseString(ConfigDefault, `12.3`)
  14. val := iter.ReadBigFloat()
  15. val64, _ := val.Float64()
  16. should.Equal(12.3, val64)
  17. }
  18. func Test_read_big_int(t *testing.T) {
  19. should := require.New(t)
  20. iter := ParseString(ConfigDefault, `92233720368547758079223372036854775807`)
  21. val := iter.ReadBigInt()
  22. should.NotNil(val)
  23. should.Equal(`92233720368547758079223372036854775807`, val.String())
  24. }
  25. func Test_read_float(t *testing.T) {
  26. inputs := []string{
  27. `1.1`, `1000`, `9223372036854775807`, `12.3`, `-12.3`, `720368.54775807`, `720368.547758075`,
  28. `1e1`, `1e+1`, `1e-1`, `1E1`, `1E+1`, `1E-1`, `-1e1`, `-1e+1`, `-1e-1`,
  29. }
  30. for _, input := range inputs {
  31. // non-streaming
  32. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  33. should := require.New(t)
  34. iter := ParseString(ConfigDefault, input+",")
  35. expected, err := strconv.ParseFloat(input, 32)
  36. should.Nil(err)
  37. should.Equal(float32(expected), iter.ReadFloat32())
  38. })
  39. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  40. should := require.New(t)
  41. iter := ParseString(ConfigDefault, input+",")
  42. expected, err := strconv.ParseFloat(input, 64)
  43. should.Nil(err)
  44. should.Equal(expected, iter.ReadFloat64())
  45. })
  46. // streaming
  47. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  48. should := require.New(t)
  49. iter := Parse(ConfigDefault, bytes.NewBufferString(input+","), 2)
  50. expected, err := strconv.ParseFloat(input, 32)
  51. should.Nil(err)
  52. should.Equal(float32(expected), iter.ReadFloat32())
  53. })
  54. t.Run(fmt.Sprintf("%v", input), func(t *testing.T) {
  55. should := require.New(t)
  56. iter := Parse(ConfigDefault, bytes.NewBufferString(input+","), 2)
  57. expected, err := strconv.ParseFloat(input, 64)
  58. should.Nil(err)
  59. should.Equal(expected, iter.ReadFloat64())
  60. })
  61. }
  62. }
  63. func Test_read_float_as_interface(t *testing.T) {
  64. should := require.New(t)
  65. iter := ParseString(ConfigDefault, `12.3`)
  66. should.Equal(float64(12.3), iter.Read())
  67. }
  68. func Test_wrap_float(t *testing.T) {
  69. should := require.New(t)
  70. str, err := MarshalToString(WrapFloat64(12.3))
  71. should.Nil(err)
  72. should.Equal("12.3", str)
  73. }
  74. func Test_write_float32(t *testing.T) {
  75. vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
  76. -0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
  77. for _, val := range vals {
  78. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  79. should := require.New(t)
  80. buf := &bytes.Buffer{}
  81. stream := NewStream(ConfigDefault, buf, 4096)
  82. stream.WriteFloat32Lossy(val)
  83. stream.Flush()
  84. should.Nil(stream.Error)
  85. output, err := json.Marshal(val)
  86. should.Nil(err)
  87. should.Equal(string(output), buf.String())
  88. })
  89. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  90. should := require.New(t)
  91. buf := &bytes.Buffer{}
  92. stream := NewStream(ConfigDefault, buf, 4096)
  93. stream.WriteVal(val)
  94. stream.Flush()
  95. should.Nil(stream.Error)
  96. output, err := json.Marshal(val)
  97. should.Nil(err)
  98. should.Equal(string(output), buf.String())
  99. })
  100. }
  101. should := require.New(t)
  102. buf := &bytes.Buffer{}
  103. stream := NewStream(ConfigDefault, buf, 10)
  104. stream.WriteRaw("abcdefg")
  105. stream.WriteFloat32Lossy(1.123456)
  106. stream.Flush()
  107. should.Nil(stream.Error)
  108. should.Equal("abcdefg1.123456", buf.String())
  109. stream = NewStream(ConfigDefault, nil, 0)
  110. stream.WriteFloat32(float32(0.0000001))
  111. should.Equal("1e-07", string(stream.buf))
  112. }
  113. func Test_write_float64(t *testing.T) {
  114. vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
  115. -0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
  116. for _, val := range vals {
  117. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  118. should := require.New(t)
  119. buf := &bytes.Buffer{}
  120. stream := NewStream(ConfigDefault, buf, 4096)
  121. stream.WriteFloat64Lossy(val)
  122. stream.Flush()
  123. should.Nil(stream.Error)
  124. should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
  125. })
  126. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  127. should := require.New(t)
  128. buf := &bytes.Buffer{}
  129. stream := NewStream(ConfigDefault, buf, 4096)
  130. stream.WriteVal(val)
  131. stream.Flush()
  132. should.Nil(stream.Error)
  133. should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
  134. })
  135. }
  136. should := require.New(t)
  137. buf := &bytes.Buffer{}
  138. stream := NewStream(ConfigDefault, buf, 10)
  139. stream.WriteRaw("abcdefg")
  140. stream.WriteFloat64Lossy(1.123456)
  141. stream.Flush()
  142. should.Nil(stream.Error)
  143. should.Equal("abcdefg1.123456", buf.String())
  144. stream = NewStream(ConfigDefault, nil, 0)
  145. stream.WriteFloat64(float64(0.0000001))
  146. should.Equal("1e-07", string(stream.buf))
  147. }
  148. func Test_read_float64_cursor(t *testing.T) {
  149. should := require.New(t)
  150. iter := ParseString(ConfigDefault, "[1.23456789\n,2,3]")
  151. should.True(iter.ReadArray())
  152. should.Equal(1.23456789, iter.Read())
  153. should.True(iter.ReadArray())
  154. should.Equal(float64(2), iter.Read())
  155. }
  156. func Test_read_float_scientific(t *testing.T) {
  157. should := require.New(t)
  158. var obj interface{}
  159. should.Nil(UnmarshalFromString(`1e1`, &obj))
  160. should.Equal(float64(10), obj)
  161. should.Nil(json.Unmarshal([]byte(`1e1`), &obj))
  162. should.Equal(float64(10), obj)
  163. should.Nil(UnmarshalFromString(`1.0e1`, &obj))
  164. should.Equal(float64(10), obj)
  165. should.Nil(json.Unmarshal([]byte(`1.0e1`), &obj))
  166. should.Equal(float64(10), obj)
  167. }
  168. func Test_lossy_float_marshal(t *testing.T) {
  169. should := require.New(t)
  170. api := Config{MarshalFloatWith6Digits: true}.Froze()
  171. output, err := api.MarshalToString(float64(0.1234567))
  172. should.Nil(err)
  173. should.Equal("0.123457", output)
  174. output, err = api.MarshalToString(float32(0.1234567))
  175. should.Nil(err)
  176. should.Equal("0.123457", output)
  177. }
  178. func Benchmark_jsoniter_float(b *testing.B) {
  179. b.ReportAllocs()
  180. input := []byte(`1.1123,`)
  181. iter := NewIterator(ConfigDefault)
  182. for n := 0; n < b.N; n++ {
  183. iter.ResetBytes(input)
  184. iter.ReadFloat64()
  185. }
  186. }
  187. func Benchmark_json_float(b *testing.B) {
  188. for n := 0; n < b.N; n++ {
  189. result := float64(0)
  190. json.Unmarshal([]byte(`1.1`), &result)
  191. }
  192. }