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