jsoniter_any_int_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package any_tests
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/json-iterator/go"
  6. "github.com/stretchr/testify/require"
  7. )
  8. var intConvertMap = map[string]int{
  9. "null": 0,
  10. "321.1": 321,
  11. "-321.1": -321,
  12. `"1.1"`: 1,
  13. `"-321.1"`: -321,
  14. "0.0": 0,
  15. "0": 0,
  16. `"0"`: 0,
  17. `"0.0"`: 0,
  18. "-1.1": -1,
  19. "true": 1,
  20. "false": 0,
  21. `"true"`: 0,
  22. `"false"`: 0,
  23. `"true123"`: 0,
  24. `"123true"`: 123,
  25. `"-123true"`: -123,
  26. `"1.2332e6"`: 1,
  27. `""`: 0,
  28. "+": 0,
  29. "-": 0,
  30. "[]": 0,
  31. "[1,2]": 1,
  32. `["1","2"]`: 1,
  33. // object in php cannot convert to int
  34. "{}": 0,
  35. }
  36. func Test_read_any_to_int(t *testing.T) {
  37. should := require.New(t)
  38. // int
  39. for k, v := range intConvertMap {
  40. any := jsoniter.Get([]byte(k))
  41. should.Equal(v, any.ToInt(), fmt.Sprintf("origin val %v", k))
  42. }
  43. // int32
  44. for k, v := range intConvertMap {
  45. any := jsoniter.Get([]byte(k))
  46. should.Equal(int32(v), any.ToInt32(), fmt.Sprintf("original val is %v", k))
  47. }
  48. // int64
  49. for k, v := range intConvertMap {
  50. any := jsoniter.Get([]byte(k))
  51. should.Equal(int64(v), any.ToInt64(), fmt.Sprintf("original val is %v", k))
  52. }
  53. }
  54. var uintConvertMap = map[string]int{
  55. "null": 0,
  56. "321.1": 321,
  57. `"1.1"`: 1,
  58. `"-123.1"`: 0,
  59. "0.0": 0,
  60. "0": 0,
  61. `"0"`: 0,
  62. `"0.0"`: 0,
  63. `"00.0"`: 0,
  64. "true": 1,
  65. "false": 0,
  66. `"true"`: 0,
  67. `"false"`: 0,
  68. `"true123"`: 0,
  69. `"+1"`: 1,
  70. `"123true"`: 123,
  71. `"-123true"`: 0,
  72. `"1.2332e6"`: 1,
  73. `""`: 0,
  74. "+": 0,
  75. "-": 0,
  76. ".": 0,
  77. "[]": 0,
  78. "[1,2]": 1,
  79. "{}": 0,
  80. "{1,2}": 0,
  81. "-1.1": 0,
  82. "-321.1": 0,
  83. }
  84. func Test_read_any_to_uint(t *testing.T) {
  85. should := require.New(t)
  86. for k, v := range uintConvertMap {
  87. any := jsoniter.Get([]byte(k))
  88. should.Equal(uint64(v), any.ToUint64(), fmt.Sprintf("origin val %v", k))
  89. }
  90. for k, v := range uintConvertMap {
  91. any := jsoniter.Get([]byte(k))
  92. should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k))
  93. }
  94. for k, v := range uintConvertMap {
  95. any := jsoniter.Get([]byte(k))
  96. should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k))
  97. }
  98. }
  99. func Test_read_int64_to_any(t *testing.T) {
  100. should := require.New(t)
  101. any := jsoniter.WrapInt64(12345)
  102. should.Equal(12345, any.ToInt())
  103. should.Equal(int32(12345), any.ToInt32())
  104. should.Equal(int64(12345), any.ToInt64())
  105. should.Equal(uint(12345), any.ToUint())
  106. should.Equal(uint32(12345), any.ToUint32())
  107. should.Equal(uint64(12345), any.ToUint64())
  108. should.Equal(float32(12345), any.ToFloat32())
  109. should.Equal(float64(12345), any.ToFloat64())
  110. should.Equal("12345", any.ToString())
  111. should.Equal(true, any.ToBool())
  112. should.Equal(any.ValueType(), jsoniter.NumberValue)
  113. stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
  114. any.WriteTo(stream)
  115. should.Equal("12345", string(stream.Buffer()))
  116. }
  117. func Test_read_int32_to_any(t *testing.T) {
  118. should := require.New(t)
  119. any := jsoniter.WrapInt32(12345)
  120. should.Equal(12345, any.ToInt())
  121. should.Equal(int32(12345), any.ToInt32())
  122. should.Equal(int64(12345), any.ToInt64())
  123. should.Equal(uint(12345), any.ToUint())
  124. should.Equal(uint32(12345), any.ToUint32())
  125. should.Equal(uint64(12345), any.ToUint64())
  126. should.Equal(float32(12345), any.ToFloat32())
  127. should.Equal(float64(12345), any.ToFloat64())
  128. should.Equal("12345", any.ToString())
  129. should.Equal(true, any.ToBool())
  130. should.Equal(any.ValueType(), jsoniter.NumberValue)
  131. stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
  132. any.WriteTo(stream)
  133. should.Equal("12345", string(stream.Buffer()))
  134. }
  135. func Test_read_uint32_to_any(t *testing.T) {
  136. should := require.New(t)
  137. any := jsoniter.WrapUint32(12345)
  138. should.Equal(12345, any.ToInt())
  139. should.Equal(int32(12345), any.ToInt32())
  140. should.Equal(int64(12345), any.ToInt64())
  141. should.Equal(uint(12345), any.ToUint())
  142. should.Equal(uint32(12345), any.ToUint32())
  143. should.Equal(uint64(12345), any.ToUint64())
  144. should.Equal(float32(12345), any.ToFloat32())
  145. should.Equal(float64(12345), any.ToFloat64())
  146. should.Equal("12345", any.ToString())
  147. should.Equal(true, any.ToBool())
  148. should.Equal(any.ValueType(), jsoniter.NumberValue)
  149. stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
  150. any.WriteTo(stream)
  151. should.Equal("12345", string(stream.Buffer()))
  152. }
  153. func Test_read_uint64_to_any(t *testing.T) {
  154. should := require.New(t)
  155. any := jsoniter.WrapUint64(12345)
  156. should.Equal(12345, any.ToInt())
  157. should.Equal(int32(12345), any.ToInt32())
  158. should.Equal(int64(12345), any.ToInt64())
  159. should.Equal(uint(12345), any.ToUint())
  160. should.Equal(uint32(12345), any.ToUint32())
  161. should.Equal(uint64(12345), any.ToUint64())
  162. should.Equal(float32(12345), any.ToFloat32())
  163. should.Equal(float64(12345), any.ToFloat64())
  164. should.Equal("12345", any.ToString())
  165. should.Equal(true, any.ToBool())
  166. should.Equal(any.ValueType(), jsoniter.NumberValue)
  167. stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
  168. any.WriteTo(stream)
  169. should.Equal("12345", string(stream.Buffer()))
  170. stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
  171. stream.WriteUint(uint(123))
  172. should.Equal("123", string(stream.Buffer()))
  173. }
  174. func Test_int_lazy_any_get(t *testing.T) {
  175. should := require.New(t)
  176. any := jsoniter.Get([]byte("1234"))
  177. // panic!!
  178. //should.Equal(any.LastError(), io.EOF)
  179. should.Equal(jsoniter.InvalidValue, any.Get(1, "2").ValueType())
  180. }