github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_uint_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 uintTestValue = uint(123) 20 uint8TestValue = uint8(123) 21 uint16TestValue = uint16(123) 22 uint32TestValue = uint32(123) 23 uint64TestValue = uint64(123) 24 ) 25 26 var uintTests = []struct { 27 value interface{} 28 expect uint 29 expect8 uint8 30 expect16 uint16 31 expect32 uint32 32 expect64 uint64 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 {"0xA", 10, 10, 10, 10, 10}, 67 {"0XA", 10, 10, 10, 10, 10}, 68 {"123.456", 123, 123, 123, 123, 123}, 69 {"true", 0, 0, 0, 0, 0}, 70 {"false", 0, 0, 0, 0, 0}, 71 {"on", 0, 0, 0, 0, 0}, 72 {"off", 0, 0, 0, 0, 0}, 73 {"NaN", 0, 0, 0, 0, 0}, 74 75 {complex(1, 2), 0, 0, 0, 0, 0}, 76 {complex(123.456, 789.123), 0, 0, 0, 0, 0}, 77 78 {[3]int{1, 2, 3}, 0, 0, 0, 0, 0}, 79 {[]int{1, 2, 3}, 0, 0, 0, 0, 0}, 80 81 {map[int]int{1: 1}, 0, 0, 0, 0, 0}, 82 {map[string]string{"Earth": "珠穆朗玛峰"}, 0, 0, 0, 0, 0}, 83 84 {struct{}{}, 0, 0, 0, 0, 0}, 85 {nil, 0, 0, 0, 0, 0}, 86 87 {(*uint)(nil), 0, 0, 0, 0, 0}, 88 {(*uint8)(nil), 0, 0, 0, 0, 0}, 89 {(*uint16)(nil), 0, 0, 0, 0, 0}, 90 {(*uint32)(nil), 0, 0, 0, 0, 0}, 91 {(*uint64)(nil), 0, 0, 0, 0, 0}, 92 93 {gvar.New(123), 123, 123, 123, 123, 123}, 94 {gvar.New(123.456), 123, 123, 123, 123, 123}, 95 96 {&uintTestValue, 123, 123, 123, 123, 123}, 97 {&uint8TestValue, 123, 123, 123, 123, 123}, 98 {&uint16TestValue, 123, 123, 123, 123, 123}, 99 {&uint32TestValue, 123, 123, 123, 123, 123}, 100 {&uint64TestValue, 123, 123, 123, 123, 123}, 101 102 {(myUint)(uintTestValue), 123, 123, 123, 123, 123}, 103 {(myUint8)(uint8TestValue), 123, 123, 123, 123, 123}, 104 {(myUint16)(uint16TestValue), 123, 123, 123, 123, 123}, 105 {(myUint32)(uint32TestValue), 123, 123, 123, 123, 123}, 106 {(myUint64)(uint64TestValue), 123, 123, 123, 123, 123}, 107 108 {(*myUint)(&uintTestValue), 123, 123, 123, 123, 123}, 109 {(*myUint8)(&uint8TestValue), 123, 123, 123, 123, 123}, 110 {(*myUint16)(&uint16TestValue), 123, 123, 123, 123, 123}, 111 {(*myUint32)(&uint32TestValue), 123, 123, 123, 123, 123}, 112 {(*myUint64)(&uint64TestValue), 123, 123, 123, 123, 123}, 113 114 {(*myUint)(nil), 0, 0, 0, 0, 0}, 115 {(*myUint8)(nil), 0, 0, 0, 0, 0}, 116 {(*myUint16)(nil), 0, 0, 0, 0, 0}, 117 {(*myUint32)(nil), 0, 0, 0, 0, 0}, 118 {(*myUint64)(nil), 0, 0, 0, 0, 0}, 119 } 120 121 func TestUint(t *testing.T) { 122 gtest.C(t, func(t *gtest.T) { 123 for _, test := range uintTests { 124 t.AssertEQ(gconv.Uint(test.value), test.expect) 125 } 126 }) 127 } 128 129 func TestUint8(t *testing.T) { 130 gtest.C(t, func(t *gtest.T) { 131 for _, test := range uintTests { 132 t.AssertEQ(gconv.Uint8(test.value), test.expect8) 133 } 134 }) 135 } 136 137 func TestUint16(t *testing.T) { 138 gtest.C(t, func(t *gtest.T) { 139 for _, test := range uintTests { 140 t.AssertEQ(gconv.Uint16(test.value), test.expect16) 141 } 142 }) 143 } 144 145 func TestUint32(t *testing.T) { 146 gtest.C(t, func(t *gtest.T) { 147 for _, test := range uintTests { 148 t.AssertEQ(gconv.Uint32(test.value), test.expect32) 149 } 150 }) 151 } 152 153 func TestUint64(t *testing.T) { 154 gtest.C(t, func(t *gtest.T) { 155 for _, test := range uintTests { 156 t.AssertEQ(gconv.Uint64(test.value), test.expect64) 157 } 158 }) 159 } 160 161 func TestUints(t *testing.T) { 162 gtest.C(t, func(t *gtest.T) { 163 for _, test := range uintTests { 164 if test.value == nil { 165 t.AssertNil(gconv.Uints(test.value)) 166 continue 167 } 168 169 var ( 170 sliceType = reflect.SliceOf(reflect.TypeOf(test.value)) 171 uints = reflect.MakeSlice(sliceType, 0, 0) 172 expects = []uint{ 173 test.expect, test.expect, 174 } 175 ) 176 uints = reflect.Append(uints, reflect.ValueOf(test.value)) 177 uints = reflect.Append(uints, reflect.ValueOf(test.value)) 178 179 t.AssertEQ(gconv.Uints(uints.Interface()), expects) 180 t.AssertEQ(gconv.SliceUint(uints.Interface()), expects) 181 } 182 }) 183 184 // Test for special types. 185 gtest.C(t, func(t *gtest.T) { 186 // string 187 t.AssertEQ(gconv.Uints(""), []uint{}) 188 t.AssertEQ(gconv.Uints("123"), []uint{123}) 189 190 // []int8 json 191 t.AssertEQ(gconv.Uints([]uint8(`{"Name":"Earth"}`)), 192 []uint{123, 34, 78, 97, 109, 101, 34, 58, 34, 69, 97, 114, 116, 104, 34, 125}) 193 194 // []interface 195 t.AssertEQ(gconv.Uints([]interface{}{1, 2, 3}), []uint{1, 2, 3}) 196 197 // gvar.Var 198 t.AssertEQ(gconv.Uints( 199 gvar.New([]uint{1, 2, 3}), 200 ), []uint{1, 2, 3}) 201 202 // array 203 t.AssertEQ(gconv.Uints("[1, 2]"), []uint{1, 2}) 204 }) 205 } 206 207 func TestUint32s(t *testing.T) { 208 gtest.C(t, func(t *gtest.T) { 209 for _, test := range uintTests { 210 if test.value == nil { 211 t.AssertNil(gconv.Uint32s(test.value)) 212 continue 213 } 214 215 var ( 216 sliceType = reflect.SliceOf(reflect.TypeOf(test.value)) 217 uint32s = reflect.MakeSlice(sliceType, 0, 0) 218 expects = []uint32{ 219 test.expect32, test.expect32, 220 } 221 ) 222 uint32s = reflect.Append(uint32s, reflect.ValueOf(test.value)) 223 uint32s = reflect.Append(uint32s, reflect.ValueOf(test.value)) 224 225 t.AssertEQ(gconv.Uint32s(uint32s.Interface()), expects) 226 t.AssertEQ(gconv.SliceUint32(uint32s.Interface()), expects) 227 } 228 }) 229 230 // Test for special types. 231 gtest.C(t, func(t *gtest.T) { 232 // string 233 t.AssertEQ(gconv.Uint32s(""), []uint32{}) 234 t.AssertEQ(gconv.Uint32s("123"), []uint32{123}) 235 236 // []int8 json 237 t.AssertEQ(gconv.Uint32s([]uint8(`{"Name":"Earth"}"`)), 238 []uint32{123, 34, 78, 97, 109, 101, 34, 58, 34, 69, 97, 114, 116, 104, 34, 125, 34}) 239 240 // []interface 241 t.AssertEQ(gconv.Uint32s([]interface{}{1, 2, 3}), []uint32{1, 2, 3}) 242 243 // gvar.Var 244 t.AssertEQ(gconv.Uint32s( 245 gvar.New([]uint32{1, 2, 3}), 246 ), []uint32{1, 2, 3}) 247 }) 248 } 249 250 func TestUint64s(t *testing.T) { 251 gtest.C(t, func(t *gtest.T) { 252 for _, test := range uintTests { 253 if test.value == nil { 254 t.AssertNil(gconv.Uint64s(test.value)) 255 continue 256 } 257 258 var ( 259 sliceType = reflect.SliceOf(reflect.TypeOf(test.value)) 260 uint64s = reflect.MakeSlice(sliceType, 0, 0) 261 expects = []uint64{ 262 test.expect64, test.expect64, 263 } 264 ) 265 uint64s = reflect.Append(uint64s, reflect.ValueOf(test.value)) 266 uint64s = reflect.Append(uint64s, reflect.ValueOf(test.value)) 267 268 t.AssertEQ(gconv.Uint64s(uint64s.Interface()), expects) 269 t.AssertEQ(gconv.SliceUint64(uint64s.Interface()), expects) 270 } 271 }) 272 273 // Test for special types. 274 gtest.C(t, func(t *gtest.T) { 275 // string 276 t.AssertEQ(gconv.Uint64s(""), []uint64{}) 277 t.AssertEQ(gconv.Uint64s("123"), []uint64{123}) 278 279 // []int8 json 280 t.AssertEQ(gconv.Uint64s([]uint8(`{"Name":"Earth"}"`)), 281 []uint64{123, 34, 78, 97, 109, 101, 34, 58, 34, 69, 97, 114, 116, 104, 34, 125, 34}) 282 283 // []interface 284 t.AssertEQ(gconv.Uint64s([]interface{}{1, 2, 3}), []uint64{1, 2, 3}) 285 286 // gvar.Var 287 t.AssertEQ(gconv.Uint64s( 288 gvar.New([]uint64{1, 2, 3}), 289 ), []uint64{1, 2, 3}) 290 }) 291 }