github.com/andeya/ameda@v1.5.3/atoi62_test.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ameda_test 6 7 import ( 8 "errors" 9 "fmt" 10 "reflect" 11 "strconv" 12 "testing" 13 14 . "github.com/andeya/ameda" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 type parseUint64Test struct { 19 in string 20 out uint64 21 err error 22 } 23 24 var parseUint64Tests = []parseUint64Test{ 25 {"", 0, strconv.ErrSyntax}, 26 {"0", 0, nil}, 27 {"1", 1, nil}, 28 {"12345", 12345, nil}, 29 {"012345", 12345, nil}, 30 {"12345x", 0, strconv.ErrSyntax}, 31 {"98765432100", 98765432100, nil}, 32 {"18446744073709551615", 1<<64 - 1, nil}, 33 {"18446744073709551616", 1<<64 - 1, strconv.ErrRange}, 34 {"18446744073709551620", 1<<64 - 1, strconv.ErrRange}, 35 {"1_2_3_4_5", 0, strconv.ErrSyntax}, // base=10 so no underscores allowed 36 {"_12345", 0, strconv.ErrSyntax}, 37 {"1__2345", 0, strconv.ErrSyntax}, 38 {"12345_", 0, strconv.ErrSyntax}, 39 } 40 41 type parseUint64BaseTest struct { 42 in string 43 base int 44 out uint64 45 err error 46 } 47 48 var parseUint64BaseTests = []parseUint64BaseTest{ 49 {"", 0, 0, strconv.ErrSyntax}, 50 {"0", 0, 0, nil}, 51 {"0x", 0, 0, strconv.ErrSyntax}, 52 {"0X", 0, 0, strconv.ErrSyntax}, 53 {"1", 0, 1, nil}, 54 {"12345", 0, 12345, nil}, 55 {"012345", 0, 012345, nil}, 56 {"0x12345", 0, 0x12345, nil}, 57 {"0X12345", 0, 0x12345, nil}, 58 {"12345x", 0, 0, strconv.ErrSyntax}, 59 {"0xabcdefg123", 0, 0, strconv.ErrSyntax}, 60 {"123456789abc", 0, 0, strconv.ErrSyntax}, 61 {"98765432100", 0, 98765432100, nil}, 62 {"18446744073709551615", 0, 1<<64 - 1, nil}, 63 {"18446744073709551616", 0, 1<<64 - 1, strconv.ErrRange}, 64 {"18446744073709551620", 0, 1<<64 - 1, strconv.ErrRange}, 65 {"0xFFFFFFFFFFFFFFFF", 0, 1<<64 - 1, nil}, 66 {"0x10000000000000000", 0, 1<<64 - 1, strconv.ErrRange}, 67 {"01777777777777777777777", 0, 1<<64 - 1, nil}, 68 {"01777777777777777777778", 0, 0, strconv.ErrSyntax}, 69 {"02000000000000000000000", 0, 1<<64 - 1, strconv.ErrRange}, 70 {"0200000000000000000000", 0, 1 << 61, nil}, 71 {"0b", 0, 0, strconv.ErrSyntax}, 72 {"0B", 0, 0, strconv.ErrSyntax}, 73 {"0b101", 0, 5, nil}, 74 {"0B101", 0, 5, nil}, 75 {"0o", 0, 0, strconv.ErrSyntax}, 76 {"0O", 0, 0, strconv.ErrSyntax}, 77 {"0o377", 0, 255, nil}, 78 {"0O377", 0, 255, nil}, 79 80 // underscores allowed with base == 0 only 81 {"1_2_3_4_5", 0, 12345, nil}, // base 0 => 10 82 {"_12345", 0, 0, strconv.ErrSyntax}, 83 {"1__2345", 0, 0, strconv.ErrSyntax}, 84 {"12345_", 0, 0, strconv.ErrSyntax}, 85 86 {"1_2_3_4_5", 10, 0, strconv.ErrSyntax}, // base 10 87 {"_12345", 10, 0, strconv.ErrSyntax}, 88 {"1__2345", 10, 0, strconv.ErrSyntax}, 89 {"12345_", 10, 0, strconv.ErrSyntax}, 90 91 {"0x_1_2_3_4_5", 0, 0x12345, nil}, // base 0 => 16 92 {"_0x12345", 0, 0, strconv.ErrSyntax}, 93 {"0x__12345", 0, 0, strconv.ErrSyntax}, 94 {"0x1__2345", 0, 0, strconv.ErrSyntax}, 95 {"0x1234__5", 0, 0, strconv.ErrSyntax}, 96 {"0x12345_", 0, 0, strconv.ErrSyntax}, 97 98 {"1_2_3_4_5", 16, 0, strconv.ErrSyntax}, // base 16 99 {"_12345", 16, 0, strconv.ErrSyntax}, 100 {"1__2345", 16, 0, strconv.ErrSyntax}, 101 {"1234__5", 16, 0, strconv.ErrSyntax}, 102 {"12345_", 16, 0, strconv.ErrSyntax}, 103 104 {"0_1_2_3_4_5", 0, 012345, nil}, // base 0 => 8 (0377) 105 {"_012345", 0, 0, strconv.ErrSyntax}, 106 {"0__12345", 0, 0, strconv.ErrSyntax}, 107 {"01234__5", 0, 0, strconv.ErrSyntax}, 108 {"012345_", 0, 0, strconv.ErrSyntax}, 109 110 {"0o_1_2_3_4_5", 0, 012345, nil}, // base 0 => 8 (0o377) 111 {"_0o12345", 0, 0, strconv.ErrSyntax}, 112 {"0o__12345", 0, 0, strconv.ErrSyntax}, 113 {"0o1234__5", 0, 0, strconv.ErrSyntax}, 114 {"0o12345_", 0, 0, strconv.ErrSyntax}, 115 116 {"0_1_2_3_4_5", 8, 0, strconv.ErrSyntax}, // base 8 117 {"_012345", 8, 0, strconv.ErrSyntax}, 118 {"0__12345", 8, 0, strconv.ErrSyntax}, 119 {"01234__5", 8, 0, strconv.ErrSyntax}, 120 {"012345_", 8, 0, strconv.ErrSyntax}, 121 122 {"0b_1_0_1", 0, 5, nil}, // base 0 => 2 (0b101) 123 {"_0b101", 0, 0, strconv.ErrSyntax}, 124 {"0b__101", 0, 0, strconv.ErrSyntax}, 125 {"0b1__01", 0, 0, strconv.ErrSyntax}, 126 {"0b10__1", 0, 0, strconv.ErrSyntax}, 127 {"0b101_", 0, 0, strconv.ErrSyntax}, 128 129 {"1_0_1", 2, 0, strconv.ErrSyntax}, // base 2 130 {"_101", 2, 0, strconv.ErrSyntax}, 131 {"1_01", 2, 0, strconv.ErrSyntax}, 132 {"10_1", 2, 0, strconv.ErrSyntax}, 133 {"101_", 2, 0, strconv.ErrSyntax}, 134 } 135 136 type parseInt64Test struct { 137 in string 138 out int64 139 err error 140 } 141 142 var parseInt64Tests = []parseInt64Test{ 143 {"", 0, strconv.ErrSyntax}, 144 {"0", 0, nil}, 145 {"-0", 0, nil}, 146 {"1", 1, nil}, 147 {"-1", -1, nil}, 148 {"12345", 12345, nil}, 149 {"-12345", -12345, nil}, 150 {"012345", 12345, nil}, 151 {"-012345", -12345, nil}, 152 {"98765432100", 98765432100, nil}, 153 {"-98765432100", -98765432100, nil}, 154 {"9223372036854775807", 1<<63 - 1, nil}, 155 {"-9223372036854775807", -(1<<63 - 1), nil}, 156 {"9223372036854775808", 1<<63 - 1, strconv.ErrRange}, 157 {"-9223372036854775808", -1 << 63, nil}, 158 {"9223372036854775809", 1<<63 - 1, strconv.ErrRange}, 159 {"-9223372036854775809", -1 << 63, strconv.ErrRange}, 160 {"-1_2_3_4_5", 0, strconv.ErrSyntax}, // base=10 so no underscores allowed 161 {"-_12345", 0, strconv.ErrSyntax}, 162 {"_12345", 0, strconv.ErrSyntax}, 163 {"1__2345", 0, strconv.ErrSyntax}, 164 {"12345_", 0, strconv.ErrSyntax}, 165 } 166 167 type parseInt64BaseTest struct { 168 in string 169 base int 170 out int64 171 err error 172 } 173 174 var parseInt64BaseTests = []parseInt64BaseTest{ 175 {"", 0, 0, strconv.ErrSyntax}, 176 {"0", 0, 0, nil}, 177 {"-0", 0, 0, nil}, 178 {"1", 0, 1, nil}, 179 {"-1", 0, -1, nil}, 180 {"12345", 0, 12345, nil}, 181 {"-12345", 0, -12345, nil}, 182 {"012345", 0, 012345, nil}, 183 {"-012345", 0, -012345, nil}, 184 {"0x12345", 0, 0x12345, nil}, 185 {"-0X12345", 0, -0x12345, nil}, 186 {"12345x", 0, 0, strconv.ErrSyntax}, 187 {"-12345x", 0, 0, strconv.ErrSyntax}, 188 {"98765432100", 0, 98765432100, nil}, 189 {"-98765432100", 0, -98765432100, nil}, 190 {"9223372036854775807", 0, 1<<63 - 1, nil}, 191 {"-9223372036854775807", 0, -(1<<63 - 1), nil}, 192 {"9223372036854775808", 0, 1<<63 - 1, strconv.ErrRange}, 193 {"-9223372036854775808", 0, -1 << 63, nil}, 194 {"9223372036854775809", 0, 1<<63 - 1, strconv.ErrRange}, 195 {"-9223372036854775809", 0, -1 << 63, strconv.ErrRange}, 196 197 // other bases 198 {"g", 17, 16, nil}, 199 {"10", 25, 25, nil}, 200 {"holycow", 35, (((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, nil}, 201 {"holycow", 36, (((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, nil}, 202 203 // base 2 204 {"0", 2, 0, nil}, 205 {"-1", 2, -1, nil}, 206 {"1010", 2, 10, nil}, 207 {"1000000000000000", 2, 1 << 15, nil}, 208 {"111111111111111111111111111111111111111111111111111111111111111", 2, 1<<63 - 1, nil}, 209 {"1000000000000000000000000000000000000000000000000000000000000000", 2, 1<<63 - 1, strconv.ErrRange}, 210 {"-1000000000000000000000000000000000000000000000000000000000000000", 2, -1 << 63, nil}, 211 {"-1000000000000000000000000000000000000000000000000000000000000001", 2, -1 << 63, strconv.ErrRange}, 212 213 // base 8 214 {"-10", 8, -8, nil}, 215 {"57635436545", 8, 057635436545, nil}, 216 {"100000000", 8, 1 << 24, nil}, 217 218 // base 16 219 {"10", 16, 16, nil}, 220 {"-123456789abcdef", 16, -0x123456789abcdef, nil}, 221 {"7fffffffffffffff", 16, 1<<63 - 1, nil}, 222 223 // underscores 224 {"-0x_1_2_3_4_5", 0, -0x12345, nil}, 225 {"0x_1_2_3_4_5", 0, 0x12345, nil}, 226 {"-_0x12345", 0, 0, strconv.ErrSyntax}, 227 {"_-0x12345", 0, 0, strconv.ErrSyntax}, 228 {"_0x12345", 0, 0, strconv.ErrSyntax}, 229 {"0x__12345", 0, 0, strconv.ErrSyntax}, 230 {"0x1__2345", 0, 0, strconv.ErrSyntax}, 231 {"0x1234__5", 0, 0, strconv.ErrSyntax}, 232 {"0x12345_", 0, 0, strconv.ErrSyntax}, 233 234 {"-0_1_2_3_4_5", 0, -012345, nil}, // octal 235 {"0_1_2_3_4_5", 0, 012345, nil}, // octal 236 {"-_012345", 0, 0, strconv.ErrSyntax}, 237 {"_-012345", 0, 0, strconv.ErrSyntax}, 238 {"_012345", 0, 0, strconv.ErrSyntax}, 239 {"0__12345", 0, 0, strconv.ErrSyntax}, 240 {"01234__5", 0, 0, strconv.ErrSyntax}, 241 {"012345_", 0, 0, strconv.ErrSyntax}, 242 } 243 244 type parseUint32Test struct { 245 in string 246 out uint32 247 err error 248 } 249 250 var parseUint32Tests = []parseUint32Test{ 251 {"", 0, strconv.ErrSyntax}, 252 {"0", 0, nil}, 253 {"1", 1, nil}, 254 {"12345", 12345, nil}, 255 {"012345", 12345, nil}, 256 {"12345x", 0, strconv.ErrSyntax}, 257 {"987654321", 987654321, nil}, 258 {"4294967295", 1<<32 - 1, nil}, 259 {"4294967296", 1<<32 - 1, strconv.ErrRange}, 260 {"1_2_3_4_5", 0, strconv.ErrSyntax}, // base=10 so no underscores allowed 261 {"_12345", 0, strconv.ErrSyntax}, 262 {"_12345", 0, strconv.ErrSyntax}, 263 {"1__2345", 0, strconv.ErrSyntax}, 264 {"12345_", 0, strconv.ErrSyntax}, 265 } 266 267 type parseInt32Test struct { 268 in string 269 out int32 270 err error 271 } 272 273 var parseInt32Tests = []parseInt32Test{ 274 {"", 0, strconv.ErrSyntax}, 275 {"0", 0, nil}, 276 {"-0", 0, nil}, 277 {"1", 1, nil}, 278 {"-1", -1, nil}, 279 {"12345", 12345, nil}, 280 {"-12345", -12345, nil}, 281 {"012345", 12345, nil}, 282 {"-012345", -12345, nil}, 283 {"12345x", 0, strconv.ErrSyntax}, 284 {"-12345x", 0, strconv.ErrSyntax}, 285 {"987654321", 987654321, nil}, 286 {"-987654321", -987654321, nil}, 287 {"2147483647", 1<<31 - 1, nil}, 288 {"-2147483647", -(1<<31 - 1), nil}, 289 {"2147483648", 1<<31 - 1, strconv.ErrRange}, 290 {"-2147483648", -1 << 31, nil}, 291 {"2147483649", 1<<31 - 1, strconv.ErrRange}, 292 {"-2147483649", -1 << 31, strconv.ErrRange}, 293 {"-1_2_3_4_5", 0, strconv.ErrSyntax}, // base=10 so no underscores allowed 294 {"-_12345", 0, strconv.ErrSyntax}, 295 {"_12345", 0, strconv.ErrSyntax}, 296 {"1__2345", 0, strconv.ErrSyntax}, 297 {"12345_", 0, strconv.ErrSyntax}, 298 } 299 300 type numErrorTest struct { 301 num, want string 302 } 303 304 var numErrorTests = []numErrorTest{ 305 {"0", `strconv.ParseFloat: parsing "0": failed`}, 306 {"`", "strconv.ParseFloat: parsing \"`\": failed"}, 307 {"1\x00.2", `strconv.ParseFloat: parsing "1\x00.2": failed`}, 308 } 309 310 func init() { 311 // The parse routines return strconv.NumErrors wrapping 312 // the error and the string. Convert the tables above. 313 for i := range parseUint64Tests { 314 test := &parseUint64Tests[i] 315 if test.err != nil { 316 test.err = &strconv.NumError{"ParseUint", test.in, test.err} 317 } 318 } 319 for i := range parseUint64BaseTests { 320 test := &parseUint64BaseTests[i] 321 if test.err != nil { 322 test.err = &strconv.NumError{"ParseUint", test.in, test.err} 323 } 324 } 325 for i := range parseInt64Tests { 326 test := &parseInt64Tests[i] 327 if test.err != nil { 328 test.err = &strconv.NumError{"ParseInt", test.in, test.err} 329 } 330 } 331 for i := range parseInt64BaseTests { 332 test := &parseInt64BaseTests[i] 333 if test.err != nil { 334 test.err = &strconv.NumError{"ParseInt", test.in, test.err} 335 } 336 } 337 for i := range parseUint32Tests { 338 test := &parseUint32Tests[i] 339 if test.err != nil { 340 test.err = &strconv.NumError{"ParseUint", test.in, test.err} 341 } 342 } 343 for i := range parseInt32Tests { 344 test := &parseInt32Tests[i] 345 if test.err != nil { 346 test.err = &strconv.NumError{"ParseInt", test.in, test.err} 347 } 348 } 349 } 350 351 func TestParseUint32(t *testing.T) { 352 for i := range parseUint32Tests { 353 test := &parseUint32Tests[i] 354 out, err := ParseUint(test.in, 10, 32) 355 if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) { 356 t.Errorf("ParseUint(%q, 10, 32) = %v, %v want %v, %v", 357 test.in, out, err, test.out, test.err) 358 } 359 } 360 } 361 362 func TestParseUint64(t *testing.T) { 363 for i := range parseUint64Tests { 364 test := &parseUint64Tests[i] 365 out, err := ParseUint(test.in, 10, 64) 366 if test.out != out || !reflect.DeepEqual(test.err, err) { 367 t.Errorf("ParseUint(%q, 10, 64) = %v, %v want %v, %v", 368 test.in, out, err, test.out, test.err) 369 } 370 } 371 } 372 373 func TestParseUint64Base(t *testing.T) { 374 for i := range parseUint64BaseTests { 375 test := &parseUint64BaseTests[i] 376 out, err := ParseUint(test.in, test.base, 64) 377 if test.out != out || !reflect.DeepEqual(test.err, err) { 378 t.Errorf("ParseUint(%q, %v, 64) = %v, %v want %v, %v", 379 test.in, test.base, out, err, test.out, test.err) 380 } 381 } 382 } 383 384 func TestParseInt32(t *testing.T) { 385 for i := range parseInt32Tests { 386 test := &parseInt32Tests[i] 387 out, err := ParseInt(test.in, 10, 32) 388 if int64(test.out) != out || !reflect.DeepEqual(test.err, err) { 389 t.Errorf("ParseInt(%q, 10 ,32) = %v, %v want %v, %v", 390 test.in, out, err, test.out, test.err) 391 } 392 } 393 } 394 395 func TestParseInt64(t *testing.T) { 396 for i := range parseInt64Tests { 397 test := &parseInt64Tests[i] 398 out, err := ParseInt(test.in, 10, 64) 399 if test.out != out || !reflect.DeepEqual(test.err, err) { 400 t.Errorf("ParseInt(%q, 10, 64) = %v, %v want %v, %v", 401 test.in, out, err, test.out, test.err) 402 } 403 } 404 } 405 406 func TestParseInt64Base(t *testing.T) { 407 for i := range parseInt64BaseTests { 408 test := &parseInt64BaseTests[i] 409 out, err := ParseInt(test.in, test.base, 64) 410 if test.out != out || !reflect.DeepEqual(test.err, err) { 411 t.Errorf("ParseInt(%q, %v, 64) = %v, %v want %v, %v", 412 test.in, test.base, out, err, test.out, test.err) 413 } 414 } 415 } 416 417 func TestParseUint(t *testing.T) { 418 switch strconv.IntSize { 419 case 32: 420 for i := range parseUint32Tests { 421 test := &parseUint32Tests[i] 422 out, err := ParseUint(test.in, 10, 0) 423 if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) { 424 t.Errorf("ParseUint(%q, 10, 0) = %v, %v want %v, %v", 425 test.in, out, err, test.out, test.err) 426 } 427 } 428 case 64: 429 for i := range parseUint64Tests { 430 test := &parseUint64Tests[i] 431 out, err := ParseUint(test.in, 10, 0) 432 if test.out != out || !reflect.DeepEqual(test.err, err) { 433 t.Errorf("ParseUint(%q, 10, 0) = %v, %v want %v, %v", 434 test.in, out, err, test.out, test.err) 435 } 436 } 437 } 438 } 439 440 func TestParseInt(t *testing.T) { 441 switch strconv.IntSize { 442 case 32: 443 for i := range parseInt32Tests { 444 test := &parseInt32Tests[i] 445 out, err := ParseInt(test.in, 10, 0) 446 if int64(test.out) != out || !reflect.DeepEqual(test.err, err) { 447 t.Errorf("ParseInt(%q, 10, 0) = %v, %v want %v, %v", 448 test.in, out, err, test.out, test.err) 449 } 450 } 451 case 64: 452 for i := range parseInt64Tests { 453 test := &parseInt64Tests[i] 454 out, err := ParseInt(test.in, 10, 0) 455 if test.out != out || !reflect.DeepEqual(test.err, err) { 456 t.Errorf("ParseInt(%q, 10, 0) = %v, %v want %v, %v", 457 test.in, out, err, test.out, test.err) 458 } 459 } 460 } 461 } 462 463 func TestAtoi(t *testing.T) { 464 switch strconv.IntSize { 465 case 32: 466 for i := range parseInt32Tests { 467 test := &parseInt32Tests[i] 468 out, err := Atoi(test.in) 469 var testErr error 470 if test.err != nil { 471 testErr = &strconv.NumError{"Atoi", test.in, test.err.(*strconv.NumError).Err} 472 } 473 if int(test.out) != out || !reflect.DeepEqual(testErr, err) { 474 t.Errorf("Atoi(%q) = %v, %v want %v, %v", 475 test.in, out, err, test.out, testErr) 476 } 477 } 478 case 64: 479 for i := range parseInt64Tests { 480 test := &parseInt64Tests[i] 481 out, err := Atoi(test.in) 482 var testErr error 483 if test.err != nil { 484 testErr = &strconv.NumError{"Atoi", test.in, test.err.(*strconv.NumError).Err} 485 } 486 if test.out != int64(out) || !reflect.DeepEqual(testErr, err) { 487 t.Errorf("Atoi(%q) = %v, %v want %v, %v", 488 test.in, out, err, test.out, testErr) 489 } 490 } 491 } 492 } 493 494 func bitSizeErrStub(name string, bitSize int) error { 495 return BitSizeError(name, "0", bitSize) 496 } 497 498 func baseErrStub(name string, base int) error { 499 return BaseError(name, "0", base) 500 } 501 502 func noErrStub(name string, arg int) error { 503 return nil 504 } 505 506 type parseErrorTest struct { 507 arg int 508 errStub func(name string, arg int) error 509 } 510 511 var parseBitSizeTests = []parseErrorTest{ 512 {-1, bitSizeErrStub}, 513 {0, noErrStub}, 514 {64, noErrStub}, 515 {65, bitSizeErrStub}, 516 } 517 518 var parseBaseTests = []parseErrorTest{ 519 {-1, baseErrStub}, 520 {0, noErrStub}, 521 {1, baseErrStub}, 522 {2, noErrStub}, 523 {36, noErrStub}, 524 {62, noErrStub}, 525 {63, baseErrStub}, 526 } 527 528 func equalError(a, b error) bool { 529 if a == nil { 530 return b == nil 531 } 532 if b == nil { 533 return a == nil 534 } 535 return a.Error() == b.Error() 536 } 537 538 func TestParseIntBitSize(t *testing.T) { 539 for i := range parseBitSizeTests { 540 test := &parseBitSizeTests[i] 541 testErr := test.errStub("ParseInt", test.arg) 542 _, err := ParseInt("0", 0, test.arg) 543 if !equalError(testErr, err) { 544 t.Errorf("ParseInt(\"0\", 0, %v) = 0, %v want 0, %v", 545 test.arg, err, testErr) 546 } 547 } 548 } 549 550 func TestParseUintBitSize(t *testing.T) { 551 for i := range parseBitSizeTests { 552 test := &parseBitSizeTests[i] 553 testErr := test.errStub("ParseUint", test.arg) 554 _, err := ParseUint("0", 0, test.arg) 555 if !equalError(testErr, err) { 556 t.Errorf("ParseUint(\"0\", 0, %v) = 0, %v want 0, %v", 557 test.arg, err, testErr) 558 } 559 } 560 } 561 562 func TestParseIntBase(t *testing.T) { 563 for i := range parseBaseTests { 564 test := &parseBaseTests[i] 565 testErr := test.errStub("ParseInt", test.arg) 566 _, err := ParseInt("0", test.arg, 0) 567 if !equalError(testErr, err) { 568 t.Errorf("ParseInt(\"0\", %v, 0) = 0, %v want 0, %v", 569 test.arg, err, testErr) 570 } 571 } 572 } 573 574 func TestParseUintBase(t *testing.T) { 575 for i := range parseBaseTests { 576 test := &parseBaseTests[i] 577 testErr := test.errStub("ParseUint", test.arg) 578 _, err := ParseUint("0", test.arg, 0) 579 if !equalError(testErr, err) { 580 t.Errorf("ParseUint(\"0\", %v, 0) = 0, %v want 0, %v", 581 test.arg, err, testErr) 582 } 583 } 584 } 585 586 func TestNumError(t *testing.T) { 587 for _, test := range numErrorTests { 588 err := &strconv.NumError{ 589 Func: "ParseFloat", 590 Num: test.num, 591 Err: errors.New("failed"), 592 } 593 if got := err.Error(); got != test.want { 594 t.Errorf(`(&strconv.NumError{"ParseFloat", %q, "failed"}).Error() = %v, want %v`, test.num, got, test.want) 595 } 596 } 597 } 598 599 func BenchmarkParseInt(b *testing.B) { 600 b.Run("Pos", func(b *testing.B) { 601 benchmarkParseInt(b, 1) 602 }) 603 b.Run("Neg", func(b *testing.B) { 604 benchmarkParseInt(b, -1) 605 }) 606 } 607 608 type benchCase struct { 609 name string 610 num int64 611 } 612 613 func benchmarkParseInt(b *testing.B, neg int) { 614 cases := []benchCase{ 615 {"7bit", 1<<7 - 1}, 616 {"26bit", 1<<26 - 1}, 617 {"31bit", 1<<31 - 1}, 618 {"56bit", 1<<56 - 1}, 619 {"63bit", 1<<63 - 1}, 620 } 621 for _, cs := range cases { 622 b.Run(cs.name, func(b *testing.B) { 623 s := fmt.Sprintf("%d", cs.num*int64(neg)) 624 for i := 0; i < b.N; i++ { 625 out, _ := ParseInt(s, 10, 64) 626 BenchSink += int(out) 627 } 628 }) 629 } 630 } 631 632 func BenchmarkAtoi(b *testing.B) { 633 b.Run("Pos", func(b *testing.B) { 634 benchmarkAtoi(b, 1) 635 }) 636 b.Run("Neg", func(b *testing.B) { 637 benchmarkAtoi(b, -1) 638 }) 639 } 640 641 func benchmarkAtoi(b *testing.B, neg int) { 642 cases := []benchCase{ 643 {"7bit", 1<<7 - 1}, 644 {"26bit", 1<<26 - 1}, 645 {"31bit", 1<<31 - 1}, 646 } 647 if strconv.IntSize == 64 { 648 cases = append(cases, []benchCase{ 649 {"56bit", 1<<56 - 1}, 650 {"63bit", 1<<63 - 1}, 651 }...) 652 } 653 for _, cs := range cases { 654 b.Run(cs.name, func(b *testing.B) { 655 s := fmt.Sprintf("%d", cs.num*int64(neg)) 656 for i := 0; i < b.N; i++ { 657 out, _ := Atoi(s) 658 BenchSink += out 659 } 660 }) 661 } 662 } 663 664 func TestAtoi62(t *testing.T) { 665 i, err := ParseUint("aZl8N0y58M7", 62, 64) 666 assert.NoError(t, err) 667 assert.Equal(t, uint64(9223372036854775807), i) 668 }