mirror of
https://github.com/golang/go
synced 2024-11-05 15:26:15 -07:00
strconv: add atoi tests for uncommon bases and syntax errors
Edge cases like base 2 and 36 conversions are now covered. Many tests are mirrored from the itoa tests. Added more test cases for syntax errors. Change-Id: Iad8b2fb4854f898c2bfa18cdeb0cb4a758fcfc2e Reviewed-on: https://go-review.googlesource.com/2463 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
d00024bd60
commit
878fa886a6
@ -33,12 +33,16 @@ var atoui64tests = []atoui64Test{
|
||||
var btoui64tests = []atoui64Test{
|
||||
{"", 0, ErrSyntax},
|
||||
{"0", 0, nil},
|
||||
{"0x", 0, ErrSyntax},
|
||||
{"0X", 0, ErrSyntax},
|
||||
{"1", 1, nil},
|
||||
{"12345", 12345, nil},
|
||||
{"012345", 012345, nil},
|
||||
{"0x12345", 0x12345, nil},
|
||||
{"0X12345", 0x12345, nil},
|
||||
{"12345x", 0, ErrSyntax},
|
||||
{"0xabcdefg123", 0, ErrSyntax},
|
||||
{"123456789abc", 0, ErrSyntax},
|
||||
{"98765432100", 98765432100, nil},
|
||||
{"18446744073709551615", 1<<64 - 1, nil},
|
||||
{"18446744073709551616", 1<<64 - 1, ErrRange},
|
||||
@ -77,28 +81,61 @@ var atoi64tests = []atoi64Test{
|
||||
{"-9223372036854775809", -1 << 63, ErrRange},
|
||||
}
|
||||
|
||||
var btoi64tests = []atoi64Test{
|
||||
{"", 0, ErrSyntax},
|
||||
{"0", 0, nil},
|
||||
{"-0", 0, nil},
|
||||
{"1", 1, nil},
|
||||
{"-1", -1, nil},
|
||||
{"12345", 12345, nil},
|
||||
{"-12345", -12345, nil},
|
||||
{"012345", 012345, nil},
|
||||
{"-012345", -012345, nil},
|
||||
{"0x12345", 0x12345, nil},
|
||||
{"-0X12345", -0x12345, nil},
|
||||
{"12345x", 0, ErrSyntax},
|
||||
{"-12345x", 0, ErrSyntax},
|
||||
{"98765432100", 98765432100, nil},
|
||||
{"-98765432100", -98765432100, nil},
|
||||
{"9223372036854775807", 1<<63 - 1, nil},
|
||||
{"-9223372036854775807", -(1<<63 - 1), nil},
|
||||
{"9223372036854775808", 1<<63 - 1, ErrRange},
|
||||
{"-9223372036854775808", -1 << 63, nil},
|
||||
{"9223372036854775809", 1<<63 - 1, ErrRange},
|
||||
{"-9223372036854775809", -1 << 63, ErrRange},
|
||||
type btoi64Test struct {
|
||||
in string
|
||||
base int
|
||||
out int64
|
||||
err error
|
||||
}
|
||||
|
||||
var btoi64tests = []btoi64Test{
|
||||
{"", 0, 0, ErrSyntax},
|
||||
{"0", 0, 0, nil},
|
||||
{"-0", 0, 0, nil},
|
||||
{"1", 0, 1, nil},
|
||||
{"-1", 0, -1, nil},
|
||||
{"12345", 0, 12345, nil},
|
||||
{"-12345", 0, -12345, nil},
|
||||
{"012345", 0, 012345, nil},
|
||||
{"-012345", 0, -012345, nil},
|
||||
{"0x12345", 0, 0x12345, nil},
|
||||
{"-0X12345", 0, -0x12345, nil},
|
||||
{"12345x", 0, 0, ErrSyntax},
|
||||
{"-12345x", 0, 0, ErrSyntax},
|
||||
{"98765432100", 0, 98765432100, nil},
|
||||
{"-98765432100", 0, -98765432100, nil},
|
||||
{"9223372036854775807", 0, 1<<63 - 1, nil},
|
||||
{"-9223372036854775807", 0, -(1<<63 - 1), nil},
|
||||
{"9223372036854775808", 0, 1<<63 - 1, ErrRange},
|
||||
{"-9223372036854775808", 0, -1 << 63, nil},
|
||||
{"9223372036854775809", 0, 1<<63 - 1, ErrRange},
|
||||
{"-9223372036854775809", 0, -1 << 63, ErrRange},
|
||||
|
||||
// other bases
|
||||
{"g", 17, 16, nil},
|
||||
{"10", 25, 25, nil},
|
||||
{"holycow", 35, (((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, nil},
|
||||
{"holycow", 36, (((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, nil},
|
||||
|
||||
// base 2
|
||||
{"0", 2, 0, nil},
|
||||
{"-1", 2, -1, nil},
|
||||
{"1010", 2, 10, nil},
|
||||
{"1000000000000000", 2, 1 << 15, nil},
|
||||
{"111111111111111111111111111111111111111111111111111111111111111", 2, 1<<63 - 1, nil},
|
||||
{"1000000000000000000000000000000000000000000000000000000000000000", 2, 1<<63 - 1, ErrRange},
|
||||
{"-1000000000000000000000000000000000000000000000000000000000000000", 2, -1 << 63, nil},
|
||||
{"-1000000000000000000000000000000000000000000000000000000000000001", 2, -1 << 63, ErrRange},
|
||||
|
||||
// base 8
|
||||
{"-10", 8, -8, nil},
|
||||
{"57635436545", 8, 057635436545, nil},
|
||||
{"100000000", 8, 1 << 24, nil},
|
||||
|
||||
// base 16
|
||||
{"10", 16, 16, nil},
|
||||
{"-123456789abcdef", 16, -0x123456789abcdef, nil},
|
||||
{"7fffffffffffffff", 16, 1<<63 - 1, nil},
|
||||
}
|
||||
|
||||
type atoui32Test struct {
|
||||
@ -234,7 +271,7 @@ func TestParseInt64(t *testing.T) {
|
||||
func TestParseInt64Base(t *testing.T) {
|
||||
for i := range btoi64tests {
|
||||
test := &btoi64tests[i]
|
||||
out, err := ParseInt(test.in, 0, 64)
|
||||
out, err := ParseInt(test.in, test.base, 64)
|
||||
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
||||
t.Errorf("ParseInt(%q) = %v, %v want %v, %v",
|
||||
test.in, out, err, test.out, test.err)
|
||||
|
Loading…
Reference in New Issue
Block a user