jsoniter_float_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // +build go1.8
  2. package jsoniter
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/json-iterator/go/require"
  8. "strconv"
  9. "testing"
  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. }
  107. func Test_write_float64(t *testing.T) {
  108. vals := []float64{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
  109. -0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
  110. for _, val := range vals {
  111. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  112. should := require.New(t)
  113. buf := &bytes.Buffer{}
  114. stream := NewStream(ConfigDefault, buf, 4096)
  115. stream.WriteFloat64Lossy(val)
  116. stream.Flush()
  117. should.Nil(stream.Error)
  118. should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
  119. })
  120. t.Run(fmt.Sprintf("%v", val), func(t *testing.T) {
  121. should := require.New(t)
  122. buf := &bytes.Buffer{}
  123. stream := NewStream(ConfigDefault, buf, 4096)
  124. stream.WriteVal(val)
  125. stream.Flush()
  126. should.Nil(stream.Error)
  127. should.Equal(strconv.FormatFloat(val, 'f', -1, 64), buf.String())
  128. })
  129. }
  130. should := require.New(t)
  131. buf := &bytes.Buffer{}
  132. stream := NewStream(ConfigDefault, buf, 10)
  133. stream.WriteRaw("abcdefg")
  134. stream.WriteFloat64Lossy(1.123456)
  135. stream.Flush()
  136. should.Nil(stream.Error)
  137. should.Equal("abcdefg1.123456", buf.String())
  138. }
  139. func Test_read_float64_cursor(t *testing.T) {
  140. should := require.New(t)
  141. iter := ParseString(ConfigDefault, "[1.23456789\n,2,3]")
  142. should.True(iter.ReadArray())
  143. should.Equal(1.23456789, iter.Read())
  144. should.True(iter.ReadArray())
  145. should.Equal(float64(2), iter.Read())
  146. }
  147. func Test_read_float_scientific(t *testing.T) {
  148. should := require.New(t)
  149. var obj interface{}
  150. should.Nil(UnmarshalFromString(`1e1`, &obj))
  151. should.Equal(float64(10), obj)
  152. should.Nil(json.Unmarshal([]byte(`1e1`), &obj))
  153. should.Equal(float64(10), obj)
  154. should.Nil(UnmarshalFromString(`1.0e1`, &obj))
  155. should.Equal(float64(10), obj)
  156. should.Nil(json.Unmarshal([]byte(`1.0e1`), &obj))
  157. should.Equal(float64(10), obj)
  158. }
  159. func Test_lossy_float_marshal(t *testing.T) {
  160. should := require.New(t)
  161. api := Config{MarshalFloatWith6Digits: true}.Froze()
  162. output, err := api.MarshalToString(float64(0.1234567))
  163. should.Nil(err)
  164. should.Equal("0.123457", output)
  165. output, err = api.MarshalToString(float32(0.1234567))
  166. should.Nil(err)
  167. should.Equal("0.123457", output)
  168. }
  169. func Benchmark_jsoniter_float(b *testing.B) {
  170. b.ReportAllocs()
  171. input := []byte(`1.1123,`)
  172. iter := NewIterator(ConfigDefault)
  173. for n := 0; n < b.N; n++ {
  174. iter.ResetBytes(input)
  175. iter.ReadFloat64()
  176. }
  177. }
  178. func Benchmark_json_float(b *testing.B) {
  179. for n := 0; n < b.N; n++ {
  180. result := float64(0)
  181. json.Unmarshal([]byte(`1.1`), &result)
  182. }
  183. }