jsoniter_any_int_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }
  112. func Test_read_int32_to_any(t *testing.T) {
  113. should := require.New(t)
  114. any := WrapInt32(12345)
  115. should.Equal(12345, any.ToInt())
  116. should.Equal(int32(12345), any.ToInt32())
  117. should.Equal(int64(12345), any.ToInt64())
  118. should.Equal(uint(12345), any.ToUint())
  119. should.Equal(uint32(12345), any.ToUint32())
  120. should.Equal(uint64(12345), any.ToUint64())
  121. should.Equal(float32(12345), any.ToFloat32())
  122. should.Equal(float64(12345), any.ToFloat64())
  123. should.Equal("12345", any.ToString())
  124. should.Equal(true, any.ToBool())
  125. }
  126. func Test_read_uint32_to_any(t *testing.T) {
  127. should := require.New(t)
  128. any := WrapUint32(12345)
  129. should.Equal(12345, any.ToInt())
  130. should.Equal(int32(12345), any.ToInt32())
  131. should.Equal(int64(12345), any.ToInt64())
  132. should.Equal(uint(12345), any.ToUint())
  133. should.Equal(uint32(12345), any.ToUint32())
  134. should.Equal(uint64(12345), any.ToUint64())
  135. should.Equal(float32(12345), any.ToFloat32())
  136. should.Equal(float64(12345), any.ToFloat64())
  137. should.Equal("12345", any.ToString())
  138. should.Equal(true, any.ToBool())
  139. should.Equal(any.ValueType(), Number)
  140. }
  141. func Test_read_uint64_to_any(t *testing.T) {
  142. should := require.New(t)
  143. any := WrapUint64(12345)
  144. should.Equal(12345, any.ToInt())
  145. should.Equal(int32(12345), any.ToInt32())
  146. should.Equal(int64(12345), any.ToInt64())
  147. should.Equal(uint(12345), any.ToUint())
  148. should.Equal(uint32(12345), any.ToUint32())
  149. should.Equal(uint64(12345), any.ToUint64())
  150. should.Equal(float32(12345), any.ToFloat32())
  151. should.Equal(float64(12345), any.ToFloat64())
  152. should.Equal("12345", any.ToString())
  153. should.Equal(true, any.ToBool())
  154. should.Equal(any.ValueType(), Number)
  155. }
  156. func Test_int_lazy_any_get(t *testing.T) {
  157. should := require.New(t)
  158. any := Get([]byte("1234"))
  159. should.Equal(Invalid, any.Get(1, "2").ValueType())
  160. }