2008-11-18 18:12:14 -07:00
|
|
|
// 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
|
|
|
|
import (
|
|
|
|
"os";
|
|
|
|
"fmt";
|
2008-11-19 15:38:05 -07:00
|
|
|
"strconv";
|
|
|
|
"testing"
|
2008-11-18 18:12:14 -07:00
|
|
|
)
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atoui64Test struct {
|
2008-11-18 18:12:14 -07:00
|
|
|
in string;
|
|
|
|
out uint64;
|
|
|
|
err *os.Error;
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
var atoui64tests = []atoui64Test {
|
|
|
|
atoui64Test{ "", 0, os.EINVAL },
|
|
|
|
atoui64Test{ "0", 0, nil },
|
|
|
|
atoui64Test{ "1", 1, nil },
|
|
|
|
atoui64Test{ "12345", 12345, nil },
|
|
|
|
atoui64Test{ "012345", 0, os.EINVAL },
|
|
|
|
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 },
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atoi64Test struct {
|
2008-11-18 18:12:14 -07:00
|
|
|
in string;
|
|
|
|
out int64;
|
|
|
|
err *os.Error;
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
var atoi64test = []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", 0, os.EINVAL },
|
|
|
|
atoi64Test{ "-012345", 0, os.EINVAL },
|
|
|
|
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 },
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atoui32Test struct {
|
2008-11-18 18:12:14 -07:00
|
|
|
in string;
|
|
|
|
out uint32;
|
|
|
|
err *os.Error;
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
var atoui32tests = []atoui32Test {
|
|
|
|
atoui32Test{ "", 0, os.EINVAL },
|
|
|
|
atoui32Test{ "0", 0, nil },
|
|
|
|
atoui32Test{ "1", 1, nil },
|
|
|
|
atoui32Test{ "12345", 12345, nil },
|
|
|
|
atoui32Test{ "012345", 0, os.EINVAL },
|
|
|
|
atoui32Test{ "12345x", 0, os.EINVAL },
|
|
|
|
atoui32Test{ "987654321", 987654321, nil },
|
|
|
|
atoui32Test{ "4294967295", 1<<32-1, nil },
|
|
|
|
atoui32Test{ "4294967296", 1<<32-1, os.ERANGE },
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atoi32Test struct {
|
2008-11-18 18:12:14 -07:00
|
|
|
in string;
|
|
|
|
out int32;
|
|
|
|
err *os.Error;
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
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", 0, os.EINVAL },
|
|
|
|
atoi32Test{ "-012345", 0, os.EINVAL },
|
|
|
|
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 },
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
|
2008-11-19 15:38:05 -07:00
|
|
|
export func TestAtoui64(t *testing.T) {
|
2009-01-15 17:16:42 -07:00
|
|
|
for i := 0; i < len(atoui64tests); i++ {
|
|
|
|
test := &atoui64tests[i];
|
2009-01-15 18:22:17 -07:00
|
|
|
out, err := strconv.Atoui64(test.in);
|
2008-11-19 15:38:05 -07:00
|
|
|
if test.out != out || test.err != err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Atoui64(%v) = %v, %v want %v, %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, out, err, test.out, test.err);
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 15:38:05 -07:00
|
|
|
export func TestAtoi64(t *testing.T) {
|
2009-01-15 17:16:42 -07:00
|
|
|
for i := 0; i < len(atoi64test); i++ {
|
|
|
|
test := &atoi64test[i];
|
2009-01-15 18:22:17 -07:00
|
|
|
out, err := strconv.Atoi64(test.in);
|
2008-11-19 15:38:05 -07:00
|
|
|
if test.out != out || test.err != err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Atoi64(%v) = %v, %v want %v, %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, out, err, test.out, test.err);
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 15:38:05 -07:00
|
|
|
export func TestAtoui(t *testing.T) {
|
2009-01-15 18:22:17 -07:00
|
|
|
switch intsize {
|
2008-11-18 18:12:14 -07:00
|
|
|
case 32:
|
2009-01-15 17:16:42 -07:00
|
|
|
for i := 0; i < len(atoui32tests); i++ {
|
|
|
|
test := &atoui32tests[i];
|
2009-01-15 18:22:17 -07:00
|
|
|
out, err := strconv.Atoui(test.in);
|
2008-11-19 15:38:05 -07:00
|
|
|
if test.out != uint32(out) || test.err != err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Atoui(%v) = %v, %v want %v, %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, out, err, test.out, test.err);
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case 64:
|
2009-01-15 17:16:42 -07:00
|
|
|
for i := 0; i < len(atoui64tests); i++ {
|
|
|
|
test := &atoui64tests[i];
|
2009-01-15 18:22:17 -07:00
|
|
|
out, err := strconv.Atoui(test.in);
|
2008-11-19 15:38:05 -07:00
|
|
|
if test.out != uint64(out) || test.err != err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Atoui(%v) = %v, %v want %v, %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, out, err, test.out, test.err);
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 15:38:05 -07:00
|
|
|
export func TestAtoi(t *testing.T) {
|
2009-01-15 18:22:17 -07:00
|
|
|
switch intsize {
|
2008-11-18 18:12:14 -07:00
|
|
|
case 32:
|
2009-01-15 17:16:42 -07:00
|
|
|
for i := 0; i < len(atoi32tests); i++ {
|
|
|
|
test := &atoi32tests[i];
|
2009-01-15 18:22:17 -07:00
|
|
|
out, err := strconv.Atoi(test.in);
|
2008-11-19 15:38:05 -07:00
|
|
|
if test.out != int32(out) || test.err != err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Atoi(%v) = %v, %v want %v, %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, out, err, test.out, test.err);
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case 64:
|
2009-01-15 17:16:42 -07:00
|
|
|
for i := 0; i < len(atoi64test); i++ {
|
|
|
|
test := &atoi64test[i];
|
2009-01-15 18:22:17 -07:00
|
|
|
out, err := strconv.Atoi(test.in);
|
2008-11-19 15:38:05 -07:00
|
|
|
if test.out != int64(out) || test.err != err {
|
2009-01-15 18:22:17 -07:00
|
|
|
t.Errorf("strconv.Atoi(%v) = %v, %v want %v, %v\n",
|
2008-11-19 15:38:05 -07:00
|
|
|
test.in, out, err, test.out, test.err);
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|