amuz.es/src/go/misc@v1.0.1/strutil/strconv_test.go (about) 1 package strutil 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestFormatIntToBytesReversed(t *testing.T) { 9 type args struct { 10 number int 11 } 12 tests := []struct { 13 name string 14 args args 15 wantOut []byte 16 }{ 17 { 18 "simple", 19 args{1234567890}, 20 []byte("0987654321"), 21 }, 22 { 23 "-simple", 24 args{-1234567890}, 25 []byte("0987654321-"), 26 }, 27 { 28 "0", 29 args{0}, 30 []byte("0"), 31 }, 32 } 33 for _, tt := range tests { 34 t.Run(tt.name, func(t *testing.T) { 35 if gotOut := FormatIntToBytesReversed(tt.args.number); !reflect.DeepEqual(gotOut, tt.wantOut) { 36 t.Errorf("FormatIntToBytesReversed() = %v, want %v", string(gotOut), string(tt.wantOut)) 37 } 38 }) 39 } 40 } 41 func TestFormatIntToBytes(t *testing.T) { 42 type args struct { 43 number int 44 } 45 tests := []struct { 46 name string 47 args args 48 wantOut []byte 49 }{ 50 { 51 "simple", 52 args{1234567890}, 53 []byte("1234567890"), 54 }, 55 { 56 "-simple", 57 args{-1234567890}, 58 []byte("-1234567890"), 59 }, 60 { 61 "0", 62 args{0}, 63 []byte("0"), 64 }, 65 } 66 for _, tt := range tests { 67 t.Run(tt.name, func(t *testing.T) { 68 if gotOut := FormatIntToBytes(tt.args.number); !reflect.DeepEqual(gotOut, tt.wantOut) { 69 t.Errorf("FormatIntToBytes() = %v, want %v", string(gotOut), string(tt.wantOut)) 70 } 71 }) 72 } 73 } 74 75 func TestFormatIntToStringReversed(t *testing.T) { 76 type args struct { 77 number int 78 } 79 tests := []struct { 80 name string 81 args args 82 wantOut string 83 }{ 84 { 85 "simple", 86 args{1234567890}, 87 "0987654321", 88 }, 89 { 90 "-simple", 91 args{-1234567890}, 92 "0987654321-", 93 }, 94 { 95 "0", 96 args{0}, 97 "0", 98 }, 99 } 100 for _, tt := range tests { 101 t.Run(tt.name, func(t *testing.T) { 102 if gotOut := FormatIntToStringReversed(tt.args.number); gotOut != tt.wantOut { 103 t.Errorf("FormatIntToStringReversed() = %v, want %v", gotOut, tt.wantOut) 104 } 105 }) 106 } 107 } 108 109 func TestFormatIntToString(t *testing.T) { 110 type args struct { 111 number int 112 } 113 tests := []struct { 114 name string 115 args args 116 wantOut string 117 }{ 118 { 119 "simple", 120 args{1234567890}, 121 "1234567890", 122 }, 123 { 124 "simple", 125 args{-1234567890}, 126 "-1234567890", 127 }, 128 { 129 "0", 130 args{0}, 131 "0", 132 }, 133 } 134 for _, tt := range tests { 135 t.Run(tt.name, func(t *testing.T) { 136 if gotOut := FormatIntToString(tt.args.number); gotOut != tt.wantOut { 137 t.Errorf("FormatIntToString() = %v, want %v", gotOut, tt.wantOut) 138 } 139 }) 140 } 141 } 142 143 func TestParseBytesToInt(t *testing.T) { 144 type args struct { 145 in []byte 146 } 147 tests := []struct { 148 name string 149 args args 150 wantNumber int 151 }{ 152 { 153 "simple", 154 args{[]byte("1234567890")}, 155 1234567890, 156 }, 157 { 158 "simple", 159 args{[]byte("-1234567890")}, 160 -1234567890, 161 }, 162 { 163 "0", 164 args{[]byte("0")}, 165 0, 166 }, 167 } 168 for _, tt := range tests { 169 t.Run(tt.name, func(t *testing.T) { 170 if gotNumber := ParseBytesToInt(tt.args.in); gotNumber != tt.wantNumber { 171 t.Errorf("ParseBytesToInt() = %v, want %v", gotNumber, tt.wantNumber) 172 } 173 }) 174 } 175 } 176 177 func TestParseStringToInt(t *testing.T) { 178 type args struct { 179 in string 180 } 181 tests := []struct { 182 name string 183 args args 184 wantNumber int 185 }{ 186 { 187 "simple", 188 args{"1234567890"}, 189 1234567890, 190 }, 191 { 192 "-simple", 193 args{"-1234567890"}, 194 -1234567890, 195 }, 196 { 197 "0", 198 args{"0"}, 199 0, 200 }, 201 } 202 for _, tt := range tests { 203 t.Run(tt.name, func(t *testing.T) { 204 if gotNumber := ParseStringToInt(tt.args.in); gotNumber != tt.wantNumber { 205 t.Errorf("ParseStringToInt() = %v, want %v", gotNumber, tt.wantNumber) 206 } 207 }) 208 } 209 } 210 211 func TestFormatUnsignedIntToStringReversed(t *testing.T) { 212 type args struct { 213 sign bool 214 number uint 215 } 216 tests := []struct { 217 name string 218 args args 219 wantOut string 220 }{ 221 { 222 "simple", 223 args{false, 1234567890}, 224 "0987654321", 225 }, 226 { 227 "-simple", 228 args{true, 1234567890}, 229 "0987654321-", 230 }, 231 { 232 "0", 233 args{false, 0}, 234 "0", 235 }, 236 { 237 "-0", 238 args{true, 0}, 239 "0", 240 }, 241 } 242 for _, tt := range tests { 243 t.Run(tt.name, func(t *testing.T) { 244 if gotOut := FormatUnsignedIntToStringReversed(tt.args.sign, tt.args.number); gotOut != tt.wantOut { 245 t.Errorf("FormatUnsignedIntToStringReversed() = %v, want %v", gotOut, tt.wantOut) 246 } 247 }) 248 } 249 } 250 251 func TestFormatUnsignedIntToString(t *testing.T) { 252 type args struct { 253 sign bool 254 number uint 255 } 256 tests := []struct { 257 name string 258 args args 259 wantOut string 260 }{ 261 { 262 "simple", 263 args{false, 1234567890}, 264 "1234567890", 265 }, 266 { 267 "-simple", 268 args{true, 1234567890}, 269 "-1234567890", 270 }, 271 { 272 "0", 273 args{false, 0}, 274 "0", 275 }, 276 { 277 "-0", 278 args{true, 0}, 279 "0", 280 }, 281 } 282 for _, tt := range tests { 283 t.Run(tt.name, func(t *testing.T) { 284 if gotOut := FormatUnsignedIntToString(tt.args.sign, tt.args.number); gotOut != tt.wantOut { 285 t.Errorf("FormatUnsignedIntToString() = %v, want %v", gotOut, tt.wantOut) 286 } 287 }) 288 } 289 } 290 291 func TestFormatUnsignedIntToBytesReversed(t *testing.T) { 292 type args struct { 293 sign bool 294 number uint 295 } 296 tests := []struct { 297 name string 298 args args 299 wantOut []byte 300 }{ 301 { 302 "simple", 303 args{false, 1234567890}, 304 []byte("0987654321"), 305 }, 306 { 307 "-simple", 308 args{true, 1234567890}, 309 []byte("0987654321-"), 310 }, 311 { 312 "0", 313 args{false, 0}, 314 []byte("0"), 315 }, 316 { 317 "0", 318 args{true, 0}, 319 []byte("0"), 320 }, 321 } 322 for _, tt := range tests { 323 t.Run(tt.name, func(t *testing.T) { 324 if gotOut := FormatUnsignedIntToBytesReversed(tt.args.sign, tt.args.number); !reflect.DeepEqual(gotOut, tt.wantOut) { 325 t.Errorf("FormatUnsignedIntToBytesReversed() = %v, want %v", gotOut, tt.wantOut) 326 } 327 }) 328 } 329 } 330 331 func TestFormatUnsignedIntToBytes(t *testing.T) { 332 type args struct { 333 sign bool 334 number uint 335 } 336 tests := []struct { 337 name string 338 args args 339 wantOut []byte 340 }{{ 341 "simple", 342 args{false, 1234567890}, 343 []byte("1234567890"), 344 }, 345 { 346 "-simple", 347 args{true, 1234567890}, 348 []byte("-1234567890"), 349 }, 350 { 351 "0", 352 args{false, 0}, 353 []byte("0"), 354 }, 355 { 356 "-0", 357 args{true, 0}, 358 []byte("0"), 359 }, 360 } 361 for _, tt := range tests { 362 t.Run(tt.name, func(t *testing.T) { 363 if gotOut := FormatUnsignedIntToBytes(tt.args.sign, tt.args.number); !reflect.DeepEqual(gotOut, tt.wantOut) { 364 t.Errorf("FormatUnsignedIntToBytes() = %v, want %v", gotOut, tt.wantOut) 365 } 366 }) 367 } 368 }