jsoniter_float_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(`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(`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(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(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(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(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(`12.3`)
  62. should.Equal(float64(12.3), iter.Read())
  63. }
  64. func Test_read_float_as_any(t *testing.T) {
  65. should := require.New(t)
  66. any, err := UnmarshalAnyFromString("12.3")
  67. should.Nil(err)
  68. should.Equal(float64(12.3), any.ToFloat64())
  69. should.Equal("12.3", any.ToString())
  70. should.True(any.ToBool())
  71. }
  72. func Test_wrap_float(t *testing.T) {
  73. should := require.New(t)
  74. str, err := MarshalToString(WrapFloat64(12.3))
  75. should.Nil(err)
  76. should.Equal("12.3", str)
  77. }
  78. func Test_write_float32(t *testing.T) {
  79. vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
  80. -0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
  81. for _, val := range vals {
  82. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  83. should := require.New(t)
  84. buf := &bytes.Buffer{}
  85. stream := NewStream(buf, 4096)
  86. stream.WriteFloat32Lossy(val)
  87. stream.Flush()
  88. should.Nil(stream.Error)
  89. should.Equal(strconv.FormatFloat(float64(val), 'f', -1, 32), buf.String())
  90. })
  91. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  92. should := require.New(t)
  93. buf := &bytes.Buffer{}
  94. stream := NewStream(buf, 4096)
  95. stream.WriteVal(val)
  96. stream.Flush()
  97. should.Nil(stream.Error)
  98. should.Equal(strconv.FormatFloat(float64(val), 'f', -1, 32), buf.String())
  99. })
  100. }
  101. should := require.New(t)
  102. buf := &bytes.Buffer{}
  103. stream := NewStream(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. }
  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(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(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(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. }
  142. func Test_read_float64_cursor(t *testing.T) {
  143. should := require.New(t)
  144. iter := ParseString("[1.23456789\n,2,3]")
  145. should.True(iter.ReadArray())
  146. should.Equal(1.23456789, iter.Read())
  147. should.True(iter.ReadArray())
  148. should.Equal(float64(2), iter.Read())
  149. }
  150. func Test_read_float_scientific(t *testing.T) {
  151. should := require.New(t)
  152. var obj interface{}
  153. should.Nil(UnmarshalFromString(`1e1`, &obj))
  154. should.Equal(float64(10), obj)
  155. should.Nil(json.Unmarshal([]byte(`1e1`), &obj))
  156. should.Equal(float64(10), obj)
  157. should.Nil(UnmarshalFromString(`1.0e1`, &obj))
  158. should.Equal(float64(10), obj)
  159. should.Nil(json.Unmarshal([]byte(`1.0e1`), &obj))
  160. should.Equal(float64(10), obj)
  161. }
  162. func Benchmark_jsoniter_float(b *testing.B) {
  163. b.ReportAllocs()
  164. input := []byte(`1.1123,`)
  165. iter := NewIterator()
  166. for n := 0; n < b.N; n++ {
  167. iter.ResetBytes(input)
  168. iter.ReadFloat64()
  169. }
  170. }
  171. func Benchmark_json_float(b *testing.B) {
  172. for n := 0; n < b.N; n++ {
  173. result := float64(0)
  174. json.Unmarshal([]byte(`1.1`), &result)
  175. }
  176. }