blob: 2632e572a7c6212fa71ed94679b48dcc2eb42459 [file] [log] [blame]
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strconv_test
import (
"os";
"reflect";
. "strconv";
"testing";
)
type atoui64Test struct {
in string;
out uint64;
err os.Error;
}
var atoui64tests = []atoui64Test {
atoui64Test{"", 0, os.EINVAL},
atoui64Test{"0", 0, nil},
atoui64Test{"1", 1, nil},
atoui64Test{"12345", 12345, nil},
atoui64Test{"012345", 012345, nil},
atoui64Test{"0x12345", 0x12345, nil},
atoui64Test{"0X12345", 0x12345, nil},
atoui64Test{"12345x", 0, os.EINVAL},
atoui64Test{"98765432100", 98765432100, nil},
atoui64Test{"18446744073709551615", 1<<64-1, nil},
atoui64Test{"18446744073709551616", 1<<64-1, os.ERANGE},
atoui64Test{"18446744073709551620", 1<<64-1, os.ERANGE},
atoui64Test{"0xFFFFFFFFFFFFFFFF", 1<<64-1, nil},
atoui64Test{"0x10000000000000000", 1<<64-1, os.ERANGE},
atoui64Test{"01777777777777777777777", 1<<64-1, nil},
atoui64Test{"01777777777777777777778", 0, os.EINVAL},
atoui64Test{"02000000000000000000000", 1<<64-1, os.ERANGE},
atoui64Test{"0200000000000000000000", 1<<61, nil},
}
type atoi64Test struct {
in string;
out int64;
err os.Error;
}
var atoi64tests = []atoi64Test {
atoi64Test{"", 0, os.EINVAL},
atoi64Test{"0", 0, nil},
atoi64Test{"-0", 0, nil},
atoi64Test{"1", 1, nil},
atoi64Test{"-1", -1, nil},
atoi64Test{"12345", 12345, nil},
atoi64Test{"-12345", -12345, nil},
atoi64Test{"012345", 012345, nil},
atoi64Test{"-012345", -012345, nil},
atoi64Test{"0x12345", 0x12345, nil},
atoi64Test{"-0X12345", -0x12345, nil},
atoi64Test{"12345x", 0, os.EINVAL},
atoi64Test{"-12345x", 0, os.EINVAL},
atoi64Test{"98765432100", 98765432100, nil},
atoi64Test{"-98765432100", -98765432100, nil},
atoi64Test{"9223372036854775807", 1<<63-1, nil},
atoi64Test{"-9223372036854775807", -(1<<63-1), nil},
atoi64Test{"9223372036854775808", 1<<63-1, os.ERANGE},
atoi64Test{"-9223372036854775808", -1<<63, nil},
atoi64Test{"9223372036854775809", 1<<63-1, os.ERANGE},
atoi64Test{"-9223372036854775809", -1<<63, os.ERANGE},
}
type atoui32Test struct {
in string;
out uint32;
err os.Error;
}
var atoui32tests = []atoui32Test {
atoui32Test{"", 0, os.EINVAL},
atoui32Test{"0", 0, nil},
atoui32Test{"1", 1, nil},
atoui32Test{"12345", 12345, nil},
atoui32Test{"012345", 012345, nil},
atoui32Test{"0x12345", 0x12345, nil},
atoui32Test{"0X12345", 0x12345, nil},
atoui32Test{"12345x", 0, os.EINVAL},
atoui32Test{"987654321", 987654321, nil},
atoui32Test{"4294967295", 1<<32-1, nil},
atoui32Test{"4294967296", 1<<32-1, os.ERANGE},
}
type atoi32Test struct {
in string;
out int32;
err os.Error;
}
var atoi32tests = []atoi32Test {
atoi32Test{"", 0, os.EINVAL},
atoi32Test{"0", 0, nil},
atoi32Test{"-0", 0, nil},
atoi32Test{"1", 1, nil},
atoi32Test{"-1", -1, nil},
atoi32Test{"12345", 12345, nil},
atoi32Test{"-12345", -12345, nil},
atoi32Test{"012345", 012345, nil},
atoi32Test{"-012345", -012345, nil},
atoi32Test{"0x12345", 0x12345, nil},
atoi32Test{"-0X12345", -0x12345, nil},
atoi32Test{"12345x", 0, os.EINVAL},
atoi32Test{"-12345x", 0, os.EINVAL},
atoi32Test{"987654321", 987654321, nil},
atoi32Test{"-987654321", -987654321, nil},
atoi32Test{"2147483647", 1<<31-1, nil},
atoi32Test{"-2147483647", -(1<<31-1), nil},
atoi32Test{"2147483648", 1<<31-1, os.ERANGE},
atoi32Test{"-2147483648", -1<<31, nil},
atoi32Test{"2147483649", 1<<31-1, os.ERANGE},
atoi32Test{"-2147483649", -1<<31, os.ERANGE},
}
func init() {
// The atoi routines return NumErrors wrapping
// the error and the string. Convert the tables above.
for i := range atoui64tests {
test := &atoui64tests[i];
if test.err != nil {
test.err = &NumError{test.in, test.err}
}
}
for i := range atoi64tests {
test := &atoi64tests[i];
if test.err != nil {
test.err = &NumError{test.in, test.err}
}
}
for i := range atoui32tests {
test := &atoui32tests[i];
if test.err != nil {
test.err = &NumError{test.in, test.err}
}
}
for i := range atoi32tests {
test := &atoi32tests[i];
if test.err != nil {
test.err = &NumError{test.in, test.err}
}
}
}
func TestAtoui64(t *testing.T) {
for i := range atoui64tests {
test := &atoui64tests[i];
out, err := Atoui64(test.in);
if test.out != out || !reflect.DeepEqual(test.err, err) {
t.Errorf("Atoui64(%q) = %v, %v want %v, %v\n",
test.in, out, err, test.out, test.err);
}
}
}
func TestAtoi64(t *testing.T) {
for i := range atoi64tests {
test := &atoi64tests[i];
out, err := Atoi64(test.in);
if test.out != out || !reflect.DeepEqual(test.err, err) {
t.Errorf("Atoi64(%q) = %v, %v want %v, %v\n",
test.in, out, err, test.out, test.err);
}
}
}
func TestAtoui(t *testing.T) {
switch IntSize {
case 32:
for i := range atoui32tests {
test := &atoui32tests[i];
out, err := Atoui(test.in);
if test.out != uint32(out) || !reflect.DeepEqual(test.err, err) {
t.Errorf("Atoui(%q) = %v, %v want %v, %v\n",
test.in, out, err, test.out, test.err);
}
}
case 64:
for i := range atoui64tests {
test := &atoui64tests[i];
out, err := Atoui(test.in);
if test.out != uint64(out) || !reflect.DeepEqual(test.err, err) {
t.Errorf("Atoui(%q) = %v, %v want %v, %v\n",
test.in, out, err, test.out, test.err);
}
}
}
}
func TestAtoi(t *testing.T) {
switch IntSize {
case 32:
for i := range atoi32tests {
test := &atoi32tests[i];
out, err := Atoi(test.in);
if test.out != int32(out) || !reflect.DeepEqual(test.err, err) {
t.Errorf("Atoi(%q) = %v, %v want %v, %v\n",
test.in, out, err, test.out, test.err);
}
}
case 64:
for i := range atoi64tests {
test := &atoi64tests[i];
out, err := Atoi(test.in);
if test.out != int64(out) || !reflect.DeepEqual(test.err, err) {
t.Errorf("Atoi(%q) = %v, %v want %v, %v\n",
test.in, out, err, test.out, test.err);
}
}
}
}