github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_int_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gconv_test 8 9 import ( 10 "reflect" 11 "testing" 12 13 "github.com/gogf/gf/v2/container/gvar" 14 "github.com/gogf/gf/v2/test/gtest" 15 "github.com/gogf/gf/v2/util/gconv" 16 ) 17 18 var ( 19 intTestValue = 123 20 int8TestValue = int8(123) 21 int16TestValue = int16(123) 22 int32TestValue = int32(123) 23 int64TestValue = int64(123) 24 ) 25 26 var intTests = []struct { 27 value interface{} 28 expect int 29 expect8 int8 30 expect16 int16 31 expect32 int32 32 expect64 int64 33 }{ 34 {true, 1, 1, 1, 1, 1}, 35 {false, 0, 0, 0, 0, 0}, 36 37 {int(0), 0, 0, 0, 0, 0}, 38 {int(123), 123, 123, 123, 123, 123}, 39 {int8(123), 123, 123, 123, 123, 123}, 40 {int16(123), 123, 123, 123, 123, 123}, 41 {int32(123), 123, 123, 123, 123, 123}, 42 {int64(123), 123, 123, 123, 123, 123}, 43 44 {uint(0), 0, 0, 0, 0, 0}, 45 {uint(123), 123, 123, 123, 123, 123}, 46 {uint8(123), 123, 123, 123, 123, 123}, 47 {uint16(123), 123, 123, 123, 123, 123}, 48 {uint32(123), 123, 123, 123, 123, 123}, 49 {uint64(123), 123, 123, 123, 123, 123}, 50 51 {uintptr(0), 0, 0, 0, 0, 0}, 52 {uintptr(123), 123, 123, 123, 123, 123}, 53 54 {rune(0), 0, 0, 0, 0, 0}, 55 {rune(49), 49, 49, 49, 49, 49}, 56 57 {float32(123), 123, 123, 123, 123, 123}, 58 {float64(123.456), 123, 123, 123, 123, 123}, 59 60 {[]byte(""), 0, 0, 0, 0, 0}, 61 62 {"", 0, 0, 0, 0, 0}, 63 {"0", 0, 0, 0, 0, 0}, 64 {"1", 1, 1, 1, 1, 1}, 65 {"+1", 1, 1, 1, 1, 1}, 66 {"-1", -1, -1, -1, -1, -1}, 67 {"0xA", 10, 10, 10, 10, 10}, 68 {"-0xA", -10, -10, -10, -10, -10}, 69 {"0XA", 10, 10, 10, 10, 10}, 70 {"-0XA", -10, -10, -10, -10, -10}, 71 {"123.456", 123, 123, 123, 123, 123}, 72 {"true", 0, 0, 0, 0, 0}, 73 {"false", 0, 0, 0, 0, 0}, 74 {"on", 0, 0, 0, 0, 0}, 75 {"off", 0, 0, 0, 0, 0}, 76 {"NaN", 0, 0, 0, 0, 0}, 77 78 {complex(1, 2), 0, 0, 0, 0, 0}, 79 {complex(123.456, 789.123), 0, 0, 0, 0, 0}, 80 81 {[3]int{1, 2, 3}, 0, 0, 0, 0, 0}, 82 {[]int{1, 2, 3}, 0, 0, 0, 0, 0}, 83 84 {map[int]int{1: 1}, 0, 0, 0, 0, 0}, 85 {map[string]string{"Earth": "大西洋"}, 0, 0, 0, 0, 0}, 86 87 {struct{}{}, 0, 0, 0, 0, 0}, 88 {nil, 0, 0, 0, 0, 0}, 89 90 {(*int)(nil), 0, 0, 0, 0, 0}, 91 {(*int8)(nil), 0, 0, 0, 0, 0}, 92 {(*int16)(nil), 0, 0, 0, 0, 0}, 93 {(*int32)(nil), 0, 0, 0, 0, 0}, 94 {(*int64)(nil), 0, 0, 0, 0, 0}, 95 96 {gvar.New(123), 123, 123, 123, 123, 123}, 97 {gvar.New(123.456), 123, 123, 123, 123, 123}, 98 99 {&intTestValue, 123, 123, 123, 123, 123}, 100 {&int8TestValue, 123, 123, 123, 123, 123}, 101 {&int16TestValue, 123, 123, 123, 123, 123}, 102 {&int32TestValue, 123, 123, 123, 123, 123}, 103 {&int64TestValue, 123, 123, 123, 123, 123}, 104 105 {(myInt)(intTestValue), 123, 123, 123, 123, 123}, 106 {(myInt8)(int8TestValue), 123, 123, 123, 123, 123}, 107 {(myInt16)(int16TestValue), 123, 123, 123, 123, 123}, 108 {(myInt32)(int32TestValue), 123, 123, 123, 123, 123}, 109 {(myInt64)(int64TestValue), 123, 123, 123, 123, 123}, 110 111 {(*myInt)(&intTestValue), 123, 123, 123, 123, 123}, 112 {(*myInt8)(&int8TestValue), 123, 123, 123, 123, 123}, 113 {(*myInt16)(&int16TestValue), 123, 123, 123, 123, 123}, 114 {(*myInt32)(&int32TestValue), 123, 123, 123, 123, 123}, 115 {(*myInt64)(&int64TestValue), 123, 123, 123, 123, 123}, 116 117 {(*myInt)(nil), 0, 0, 0, 0, 0}, 118 {(*myInt8)(nil), 0, 0, 0, 0, 0}, 119 {(*myInt16)(nil), 0, 0, 0, 0, 0}, 120 {(*myInt32)(nil), 0, 0, 0, 0, 0}, 121 {(*myInt64)(nil), 0, 0, 0, 0, 0}, 122 } 123 124 func TestInt(t *testing.T) { 125 gtest.C(t, func(t *gtest.T) { 126 for _, test := range intTests { 127 t.AssertEQ(gconv.Int(test.value), test.expect) 128 } 129 }) 130 } 131 132 func TestInt8(t *testing.T) { 133 gtest.C(t, func(t *gtest.T) { 134 for _, test := range intTests { 135 t.AssertEQ(gconv.Int8(test.value), test.expect8) 136 } 137 }) 138 } 139 140 func TestInt16(t *testing.T) { 141 gtest.C(t, func(t *gtest.T) { 142 for _, test := range intTests { 143 t.AssertEQ(gconv.Int16(test.value), test.expect16) 144 } 145 }) 146 } 147 148 func TestInt32(t *testing.T) { 149 gtest.C(t, func(t *gtest.T) { 150 for _, test := range intTests { 151 t.AssertEQ(gconv.Int32(test.value), test.expect32) 152 } 153 }) 154 } 155 156 func TestInt64(t *testing.T) { 157 gtest.C(t, func(t *gtest.T) { 158 for _, test := range intTests { 159 t.AssertEQ(gconv.Int64(test.value), test.expect64) 160 } 161 }) 162 } 163 164 func TestInts(t *testing.T) { 165 gtest.C(t, func(t *gtest.T) { 166 for _, test := range intTests { 167 if test.value == nil { 168 t.AssertNil(gconv.Ints(test.value)) 169 continue 170 } 171 172 var ( 173 sliceType = reflect.SliceOf(reflect.TypeOf(test.value)) 174 ints = reflect.MakeSlice(sliceType, 0, 0) 175 expects = []int{ 176 test.expect, test.expect, 177 } 178 ) 179 ints = reflect.Append(ints, reflect.ValueOf(test.value)) 180 ints = reflect.Append(ints, reflect.ValueOf(test.value)) 181 182 t.AssertEQ(gconv.Ints(ints.Interface()), expects) 183 t.AssertEQ(gconv.SliceInt(ints.Interface()), expects) 184 } 185 }) 186 187 // Test for special types. 188 gtest.C(t, func(t *gtest.T) { 189 // string 190 t.AssertEQ(gconv.Ints(""), []int{}) 191 t.AssertEQ(gconv.Ints("123"), []int{123}) 192 193 // []int8 json 194 t.AssertEQ(gconv.Ints([]uint8(`{"Name":"Earth"}`)), 195 []int{123, 34, 78, 97, 109, 101, 34, 58, 34, 69, 97, 114, 116, 104, 34, 125}) 196 197 // []interface 198 t.AssertEQ(gconv.Ints([]interface{}{1, 2, 3}), []int{1, 2, 3}) 199 200 // gvar.Var 201 t.AssertEQ(gconv.Ints( 202 gvar.New([]int{1, 2, 3}), 203 ), []int{1, 2, 3}) 204 205 // array 206 t.AssertEQ(gconv.Ints("[1, 2]"), []int{1, 2}) 207 }) 208 } 209 210 func TestInt32s(t *testing.T) { 211 gtest.C(t, func(t *gtest.T) { 212 for _, test := range intTests { 213 if test.value == nil { 214 t.AssertNil(gconv.Int32s(test.value)) 215 continue 216 } 217 218 var ( 219 sliceType = reflect.SliceOf(reflect.TypeOf(test.value)) 220 int32s = reflect.MakeSlice(sliceType, 0, 0) 221 expects = []int32{ 222 test.expect32, test.expect32, 223 } 224 ) 225 int32s = reflect.Append(int32s, reflect.ValueOf(test.value)) 226 int32s = reflect.Append(int32s, reflect.ValueOf(test.value)) 227 228 t.AssertEQ(gconv.Int32s(int32s.Interface()), expects) 229 t.AssertEQ(gconv.SliceInt32(int32s.Interface()), expects) 230 } 231 }) 232 233 // Test for special types. 234 gtest.C(t, func(t *gtest.T) { 235 // string 236 t.AssertEQ(gconv.Int32s(""), []int32{}) 237 t.AssertEQ(gconv.Int32s("123"), []int32{123}) 238 239 // []int8 json 240 t.AssertEQ(gconv.Int32s([]uint8(`{"Name":"Earth"}"`)), 241 []int32{123, 34, 78, 97, 109, 101, 34, 58, 34, 69, 97, 114, 116, 104, 34, 125, 34}) 242 243 // []interface 244 t.AssertEQ(gconv.Int32s([]interface{}{1, 2, 3}), []int32{1, 2, 3}) 245 246 // gvar.Var 247 t.AssertEQ(gconv.Int32s( 248 gvar.New([]int32{1, 2, 3}), 249 ), []int32{1, 2, 3}) 250 }) 251 } 252 253 func TestInt64s(t *testing.T) { 254 gtest.C(t, func(t *gtest.T) { 255 for _, test := range intTests { 256 if test.value == nil { 257 t.AssertNil(gconv.Int64s(test.value)) 258 continue 259 } 260 261 var ( 262 sliceType = reflect.SliceOf(reflect.TypeOf(test.value)) 263 int64s = reflect.MakeSlice(sliceType, 0, 0) 264 expects = []int64{ 265 test.expect64, test.expect64, 266 } 267 ) 268 int64s = reflect.Append(int64s, reflect.ValueOf(test.value)) 269 int64s = reflect.Append(int64s, reflect.ValueOf(test.value)) 270 271 t.AssertEQ(gconv.Int64s(int64s.Interface()), expects) 272 t.AssertEQ(gconv.SliceInt64(int64s.Interface()), expects) 273 } 274 }) 275 276 // Test for special types. 277 gtest.C(t, func(t *gtest.T) { 278 // string 279 t.AssertEQ(gconv.Int64s(""), []int64{}) 280 t.AssertEQ(gconv.Int64s("123"), []int64{123}) 281 282 // []int8 json 283 t.AssertEQ(gconv.Int64s([]uint8(`{"Name":"Earth"}"`)), 284 []int64{123, 34, 78, 97, 109, 101, 34, 58, 34, 69, 97, 114, 116, 104, 34, 125, 34}) 285 286 // []interface 287 t.AssertEQ(gconv.Int64s([]interface{}{1, 2, 3}), []int64{1, 2, 3}) 288 289 // gvar.Var 290 t.AssertEQ(gconv.Int64s( 291 gvar.New([]int64{1, 2, 3}), 292 ), []int64{1, 2, 3}) 293 }) 294 }