jsoniter_any_int_test.go 5.0 KB

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