jsoniter_float_test.go 5.7 KB

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