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.
|
|
|
|
|
2009-08-12 14:18:37 -06:00
|
|
|
package strconv_test
|
2009-02-16 21:44:21 -07:00
|
|
|
|
2008-11-18 18:12:14 -07:00
|
|
|
import (
|
2009-11-05 00:20:49 -07:00
|
|
|
"os";
|
|
|
|
"reflect";
|
|
|
|
. "strconv";
|
|
|
|
"testing";
|
2008-11-18 18:12:14 -07:00
|
|
|
)
|
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atoui64Test struct {
|
2009-10-07 12:55:06 -06:00
|
|
|
in string;
|
|
|
|
out uint64;
|
|
|
|
err os.Error;
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
var atoui64tests = []atoui64Test{
|
2009-10-12 00:46:11 -06:00
|
|
|
atoui64Test{"", 0, os.EINVAL},
|
|
|
|
atoui64Test{"0", 0, nil},
|
|
|
|
atoui64Test{"1", 1, nil},
|
|
|
|
atoui64Test{"12345", 12345, nil},
|
|
|
|
atoui64Test{"012345", 12345, 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},
|
|
|
|
}
|
|
|
|
|
|
|
|
var btoui64tests = []atoui64Test{
|
2009-03-03 09:39:12 -07:00
|
|
|
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},
|
2009-10-07 12:55:06 -06:00
|
|
|
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},
|
2009-03-03 09:39:12 -07:00
|
|
|
atoui64Test{"01777777777777777777778", 0, os.EINVAL},
|
2009-10-07 12:55:06 -06:00
|
|
|
atoui64Test{"02000000000000000000000", 1<<64 - 1, os.ERANGE},
|
2009-11-09 22:23:52 -07:00
|
|
|
atoui64Test{"0200000000000000000000", 1 << 61, nil},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-11-18 18:12:14 -07:00
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atoi64Test struct {
|
2009-10-07 12:55:06 -06:00
|
|
|
in string;
|
|
|
|
out int64;
|
|
|
|
err os.Error;
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
var atoi64tests = []atoi64Test{
|
2009-10-12 00:46:11 -06:00
|
|
|
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", 12345, nil},
|
|
|
|
atoi64Test{"-012345", -12345, nil},
|
|
|
|
atoi64Test{"98765432100", 98765432100, nil},
|
|
|
|
atoi64Test{"-98765432100", -98765432100, nil},
|
|
|
|
atoi64Test{"9223372036854775807", 1<<63 - 1, nil},
|
2009-11-20 12:45:05 -07:00
|
|
|
atoi64Test{"-9223372036854775807", -(1<<63 - 1), nil},
|
2009-10-12 00:46:11 -06:00
|
|
|
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},
|
|
|
|
}
|
|
|
|
|
|
|
|
var btoi64tests = []atoi64Test{
|
2009-03-03 09:39:12 -07:00
|
|
|
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},
|
2009-10-07 12:55:06 -06:00
|
|
|
atoi64Test{"9223372036854775807", 1<<63 - 1, nil},
|
2009-11-20 12:45:05 -07:00
|
|
|
atoi64Test{"-9223372036854775807", -(1<<63 - 1), nil},
|
2009-10-07 12:55:06 -06:00
|
|
|
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},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-11-18 18:12:14 -07:00
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atoui32Test struct {
|
2009-10-07 12:55:06 -06:00
|
|
|
in string;
|
|
|
|
out uint32;
|
|
|
|
err os.Error;
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
var atoui32tests = []atoui32Test{
|
2009-03-03 09:39:12 -07:00
|
|
|
atoui32Test{"", 0, os.EINVAL},
|
|
|
|
atoui32Test{"0", 0, nil},
|
|
|
|
atoui32Test{"1", 1, nil},
|
|
|
|
atoui32Test{"12345", 12345, nil},
|
2009-10-12 00:46:11 -06:00
|
|
|
atoui32Test{"012345", 12345, nil},
|
2009-03-03 09:39:12 -07:00
|
|
|
atoui32Test{"12345x", 0, os.EINVAL},
|
|
|
|
atoui32Test{"987654321", 987654321, nil},
|
2009-10-07 12:55:06 -06:00
|
|
|
atoui32Test{"4294967295", 1<<32 - 1, nil},
|
|
|
|
atoui32Test{"4294967296", 1<<32 - 1, os.ERANGE},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-11-18 18:12:14 -07:00
|
|
|
|
2009-01-15 18:22:17 -07:00
|
|
|
type atoi32Test struct {
|
2009-10-07 12:55:06 -06:00
|
|
|
in string;
|
|
|
|
out int32;
|
|
|
|
err os.Error;
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
|
2009-10-07 12:55:06 -06:00
|
|
|
var atoi32tests = []atoi32Test{
|
2009-03-03 09:39:12 -07:00
|
|
|
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},
|
2009-10-12 00:46:11 -06:00
|
|
|
atoi32Test{"012345", 12345, nil},
|
|
|
|
atoi32Test{"-012345", -12345, nil},
|
2009-03-03 09:39:12 -07:00
|
|
|
atoi32Test{"12345x", 0, os.EINVAL},
|
|
|
|
atoi32Test{"-12345x", 0, os.EINVAL},
|
|
|
|
atoi32Test{"987654321", 987654321, nil},
|
|
|
|
atoi32Test{"-987654321", -987654321, nil},
|
2009-10-07 12:55:06 -06:00
|
|
|
atoi32Test{"2147483647", 1<<31 - 1, nil},
|
2009-11-20 12:45:05 -07:00
|
|
|
atoi32Test{"-2147483647", -(1<<31 - 1), nil},
|
2009-10-07 12:55:06 -06:00
|
|
|
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},
|
2009-03-03 09:39:12 -07:00
|
|
|
}
|
2008-11-18 18:12:14 -07:00
|
|
|
|
2009-06-25 21:24:55 -06:00
|
|
|
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 {
|
2009-11-09 13:07:39 -07:00
|
|
|
test.err = &NumError{test.in, test.err}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
}
|
2009-10-12 00:46:11 -06:00
|
|
|
for i := range btoui64tests {
|
|
|
|
test := &btoui64tests[i];
|
|
|
|
if test.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
test.err = &NumError{test.in, test.err}
|
2009-10-12 00:46:11 -06:00
|
|
|
}
|
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
for i := range atoi64tests {
|
|
|
|
test := &atoi64tests[i];
|
|
|
|
if test.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
test.err = &NumError{test.in, test.err}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
}
|
2009-10-12 00:46:11 -06:00
|
|
|
for i := range btoi64tests {
|
|
|
|
test := &btoi64tests[i];
|
|
|
|
if test.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
test.err = &NumError{test.in, test.err}
|
2009-10-12 00:46:11 -06:00
|
|
|
}
|
|
|
|
}
|
2009-06-25 21:24:55 -06:00
|
|
|
for i := range atoui32tests {
|
|
|
|
test := &atoui32tests[i];
|
|
|
|
if test.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
test.err = &NumError{test.in, test.err}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := range atoi32tests {
|
|
|
|
test := &atoi32tests[i];
|
|
|
|
if test.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
test.err = &NumError{test.in, test.err}
|
2009-06-25 21:24:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestAtoui64(t *testing.T) {
|
2009-06-25 21:24:55 -06:00
|
|
|
for i := range atoui64tests {
|
2009-01-15 17:16:42 -07:00
|
|
|
test := &atoui64tests[i];
|
2009-08-12 14:18:37 -06:00
|
|
|
out, err := Atoui64(test.in);
|
2009-06-25 21:24:55 -06:00
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Atoui64(%q) = %v, %v want %v, %v\n",
|
2009-11-09 13:07:39 -07:00
|
|
|
test.in, out, err, test.out, test.err)
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 00:46:11 -06:00
|
|
|
func TestBtoui64(t *testing.T) {
|
|
|
|
for i := range btoui64tests {
|
|
|
|
test := &btoui64tests[i];
|
|
|
|
out, err := Btoui64(test.in, 0);
|
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
|
|
|
t.Errorf("Btoui64(%q) = %v, %v want %v, %v\n",
|
2009-11-09 13:07:39 -07:00
|
|
|
test.in, out, err, test.out, test.err)
|
2009-10-12 00:46:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestAtoi64(t *testing.T) {
|
2009-06-25 21:24:55 -06:00
|
|
|
for i := range atoi64tests {
|
|
|
|
test := &atoi64tests[i];
|
2009-08-12 14:18:37 -06:00
|
|
|
out, err := Atoi64(test.in);
|
2009-06-25 21:24:55 -06:00
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Atoi64(%q) = %v, %v want %v, %v\n",
|
2009-11-09 13:07:39 -07:00
|
|
|
test.in, out, err, test.out, test.err)
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 00:46:11 -06:00
|
|
|
func TestBtoi64(t *testing.T) {
|
|
|
|
for i := range btoi64tests {
|
|
|
|
test := &btoi64tests[i];
|
|
|
|
out, err := Btoi64(test.in, 0);
|
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
|
|
|
t.Errorf("Btoi64(%q) = %v, %v want %v, %v\n",
|
2009-11-09 13:07:39 -07:00
|
|
|
test.in, out, err, test.out, test.err)
|
2009-10-12 00:46:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestAtoui(t *testing.T) {
|
2009-08-12 14:18:37 -06:00
|
|
|
switch IntSize {
|
2008-11-18 18:12:14 -07:00
|
|
|
case 32:
|
2009-06-25 21:24:55 -06:00
|
|
|
for i := range atoui32tests {
|
2009-01-15 17:16:42 -07:00
|
|
|
test := &atoui32tests[i];
|
2009-08-12 14:18:37 -06:00
|
|
|
out, err := Atoui(test.in);
|
2009-06-25 21:24:55 -06:00
|
|
|
if test.out != uint32(out) || !reflect.DeepEqual(test.err, err) {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Atoui(%q) = %v, %v want %v, %v\n",
|
2009-11-09 13:07:39 -07:00
|
|
|
test.in, out, err, test.out, test.err)
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case 64:
|
2009-06-25 21:24:55 -06:00
|
|
|
for i := range atoui64tests {
|
2009-01-15 17:16:42 -07:00
|
|
|
test := &atoui64tests[i];
|
2009-08-12 14:18:37 -06:00
|
|
|
out, err := Atoui(test.in);
|
2009-06-25 21:24:55 -06:00
|
|
|
if test.out != uint64(out) || !reflect.DeepEqual(test.err, err) {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Atoui(%q) = %v, %v want %v, %v\n",
|
2009-11-09 13:07:39 -07:00
|
|
|
test.in, out, err, test.out, test.err)
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestAtoi(t *testing.T) {
|
2009-08-12 14:18:37 -06:00
|
|
|
switch IntSize {
|
2008-11-18 18:12:14 -07:00
|
|
|
case 32:
|
2009-06-25 21:24:55 -06:00
|
|
|
for i := range atoi32tests {
|
2009-01-15 17:16:42 -07:00
|
|
|
test := &atoi32tests[i];
|
2009-08-12 14:18:37 -06:00
|
|
|
out, err := Atoi(test.in);
|
2009-06-25 21:24:55 -06:00
|
|
|
if test.out != int32(out) || !reflect.DeepEqual(test.err, err) {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Atoi(%q) = %v, %v want %v, %v\n",
|
2009-11-09 13:07:39 -07:00
|
|
|
test.in, out, err, test.out, test.err)
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case 64:
|
2009-06-25 21:24:55 -06:00
|
|
|
for i := range atoi64tests {
|
|
|
|
test := &atoi64tests[i];
|
2009-08-12 14:18:37 -06:00
|
|
|
out, err := Atoi(test.in);
|
2009-06-25 21:24:55 -06:00
|
|
|
if test.out != int64(out) || !reflect.DeepEqual(test.err, err) {
|
2009-08-12 14:18:37 -06:00
|
|
|
t.Errorf("Atoi(%q) = %v, %v want %v, %v\n",
|
2009-11-09 13:07:39 -07:00
|
|
|
test.in, out, err, test.out, test.err)
|
2008-11-18 18:12:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|