jsoniter_float_test.go 5.9 KB

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