2010-12-03 05:34:57 +01: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_test
|
|
|
|
|
|
|
|
import (
|
2013-07-16 08:54:42 +02:00
|
|
|
"errors"
|
2018-01-09 02:23:08 +01:00
|
|
|
"fmt"
|
2010-12-03 05:34:57 +01:00
|
|
|
"reflect"
|
|
|
|
. "strconv"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
type parseUint64Test struct {
|
2010-12-03 05:34:57 +01:00
|
|
|
in string
|
|
|
|
out uint64
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
var parseUint64Tests = []parseUint64Test{
|
2011-12-02 20:34:41 +01:00
|
|
|
{"", 0, ErrSyntax},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"0", 0, nil},
|
|
|
|
{"1", 1, nil},
|
|
|
|
{"12345", 12345, nil},
|
|
|
|
{"012345", 12345, nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"12345x", 0, ErrSyntax},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"98765432100", 98765432100, nil},
|
|
|
|
{"18446744073709551615", 1<<64 - 1, nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"18446744073709551616", 1<<64 - 1, ErrRange},
|
|
|
|
{"18446744073709551620", 1<<64 - 1, ErrRange},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
type parseUint64BaseTest struct {
|
|
|
|
in string
|
|
|
|
base int
|
|
|
|
out uint64
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
var parseUint64BaseTests = []parseUint64BaseTest{
|
|
|
|
{"", 0, 0, ErrSyntax},
|
|
|
|
{"0", 0, 0, nil},
|
|
|
|
{"0x", 0, 0, ErrSyntax},
|
|
|
|
{"0X", 0, 0, ErrSyntax},
|
|
|
|
{"1", 0, 1, nil},
|
|
|
|
{"12345", 0, 12345, nil},
|
|
|
|
{"012345", 0, 012345, nil},
|
|
|
|
{"0x12345", 0, 0x12345, nil},
|
|
|
|
{"0X12345", 0, 0x12345, nil},
|
|
|
|
{"12345x", 0, 0, ErrSyntax},
|
|
|
|
{"0xabcdefg123", 0, 0, ErrSyntax},
|
|
|
|
{"123456789abc", 0, 0, ErrSyntax},
|
|
|
|
{"98765432100", 0, 98765432100, nil},
|
|
|
|
{"18446744073709551615", 0, 1<<64 - 1, nil},
|
|
|
|
{"18446744073709551616", 0, 1<<64 - 1, ErrRange},
|
|
|
|
{"18446744073709551620", 0, 1<<64 - 1, ErrRange},
|
|
|
|
{"0xFFFFFFFFFFFFFFFF", 0, 1<<64 - 1, nil},
|
|
|
|
{"0x10000000000000000", 0, 1<<64 - 1, ErrRange},
|
|
|
|
{"01777777777777777777777", 0, 1<<64 - 1, nil},
|
|
|
|
{"01777777777777777777778", 0, 0, ErrSyntax},
|
|
|
|
{"02000000000000000000000", 0, 1<<64 - 1, ErrRange},
|
|
|
|
{"0200000000000000000000", 0, 1 << 61, nil},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
type parseInt64Test struct {
|
2010-12-03 05:34:57 +01:00
|
|
|
in string
|
|
|
|
out int64
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
var parseInt64Tests = []parseInt64Test{
|
2011-12-02 20:34:41 +01:00
|
|
|
{"", 0, ErrSyntax},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"0", 0, nil},
|
|
|
|
{"-0", 0, nil},
|
|
|
|
{"1", 1, nil},
|
|
|
|
{"-1", -1, nil},
|
|
|
|
{"12345", 12345, nil},
|
|
|
|
{"-12345", -12345, nil},
|
|
|
|
{"012345", 12345, nil},
|
|
|
|
{"-012345", -12345, nil},
|
|
|
|
{"98765432100", 98765432100, nil},
|
|
|
|
{"-98765432100", -98765432100, nil},
|
|
|
|
{"9223372036854775807", 1<<63 - 1, nil},
|
|
|
|
{"-9223372036854775807", -(1<<63 - 1), nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"9223372036854775808", 1<<63 - 1, ErrRange},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"-9223372036854775808", -1 << 63, nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"9223372036854775809", 1<<63 - 1, ErrRange},
|
|
|
|
{"-9223372036854775809", -1 << 63, ErrRange},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
type parseInt64BaseTest struct {
|
2015-10-31 01:59:47 +01:00
|
|
|
in string
|
|
|
|
base int
|
|
|
|
out int64
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
var parseInt64BaseTests = []parseInt64BaseTest{
|
2015-10-31 01:59:47 +01:00
|
|
|
{"", 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},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
type parseUint32Test struct {
|
2010-12-03 05:34:57 +01:00
|
|
|
in string
|
|
|
|
out uint32
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
var parseUint32Tests = []parseUint32Test{
|
2011-12-02 20:34:41 +01:00
|
|
|
{"", 0, ErrSyntax},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"0", 0, nil},
|
|
|
|
{"1", 1, nil},
|
|
|
|
{"12345", 12345, nil},
|
|
|
|
{"012345", 12345, nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"12345x", 0, ErrSyntax},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"987654321", 987654321, nil},
|
|
|
|
{"4294967295", 1<<32 - 1, nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"4294967296", 1<<32 - 1, ErrRange},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
type parseInt32Test struct {
|
2010-12-03 05:34:57 +01:00
|
|
|
in string
|
|
|
|
out int32
|
2011-12-03 03:17:34 +01:00
|
|
|
err error
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
var parseInt32Tests = []parseInt32Test{
|
2011-12-02 20:34:41 +01:00
|
|
|
{"", 0, ErrSyntax},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"0", 0, nil},
|
|
|
|
{"-0", 0, nil},
|
|
|
|
{"1", 1, nil},
|
|
|
|
{"-1", -1, nil},
|
|
|
|
{"12345", 12345, nil},
|
|
|
|
{"-12345", -12345, nil},
|
|
|
|
{"012345", 12345, nil},
|
|
|
|
{"-012345", -12345, nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"12345x", 0, ErrSyntax},
|
|
|
|
{"-12345x", 0, ErrSyntax},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"987654321", 987654321, nil},
|
|
|
|
{"-987654321", -987654321, nil},
|
|
|
|
{"2147483647", 1<<31 - 1, nil},
|
|
|
|
{"-2147483647", -(1<<31 - 1), nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"2147483648", 1<<31 - 1, ErrRange},
|
2010-12-03 05:34:57 +01:00
|
|
|
{"-2147483648", -1 << 31, nil},
|
2011-12-02 20:34:41 +01:00
|
|
|
{"2147483649", 1<<31 - 1, ErrRange},
|
|
|
|
{"-2147483649", -1 << 31, ErrRange},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
type numErrorTest struct {
|
|
|
|
num, want string
|
|
|
|
}
|
|
|
|
|
|
|
|
var numErrorTests = []numErrorTest{
|
|
|
|
{"0", `strconv.ParseFloat: parsing "0": failed`},
|
|
|
|
{"`", "strconv.ParseFloat: parsing \"`\": failed"},
|
|
|
|
{"1\x00.2", `strconv.ParseFloat: parsing "1\x00.2": failed`},
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func init() {
|
2018-01-09 02:23:08 +01:00
|
|
|
// The parse routines return NumErrors wrapping
|
2016-07-22 20:15:38 +02:00
|
|
|
// the error and the string. Convert the tables above.
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseUint64Tests {
|
|
|
|
test := &parseUint64Tests[i]
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.err != nil {
|
2012-01-12 02:31:45 +01:00
|
|
|
test.err = &NumError{"ParseUint", test.in, test.err}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseUint64BaseTests {
|
|
|
|
test := &parseUint64BaseTests[i]
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.err != nil {
|
2012-01-12 02:31:45 +01:00
|
|
|
test.err = &NumError{"ParseUint", test.in, test.err}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseInt64Tests {
|
|
|
|
test := &parseInt64Tests[i]
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.err != nil {
|
2012-01-12 02:31:45 +01:00
|
|
|
test.err = &NumError{"ParseInt", test.in, test.err}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseInt64BaseTests {
|
|
|
|
test := &parseInt64BaseTests[i]
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.err != nil {
|
2012-01-12 02:31:45 +01:00
|
|
|
test.err = &NumError{"ParseInt", test.in, test.err}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseUint32Tests {
|
|
|
|
test := &parseUint32Tests[i]
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.err != nil {
|
2012-01-12 02:31:45 +01:00
|
|
|
test.err = &NumError{"ParseUint", test.in, test.err}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseInt32Tests {
|
|
|
|
test := &parseInt32Tests[i]
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.err != nil {
|
2012-01-12 02:31:45 +01:00
|
|
|
test.err = &NumError{"ParseInt", test.in, test.err}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func TestParseUint32(t *testing.T) {
|
|
|
|
for i := range parseUint32Tests {
|
|
|
|
test := &parseUint32Tests[i]
|
|
|
|
out, err := ParseUint(test.in, 10, 32)
|
|
|
|
if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) {
|
|
|
|
t.Errorf("ParseUint(%q, 10, 32) = %v, %v want %v, %v",
|
|
|
|
test.in, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
func TestParseUint64(t *testing.T) {
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseUint64Tests {
|
|
|
|
test := &parseUint64Tests[i]
|
2011-12-14 16:41:54 +01:00
|
|
|
out, err := ParseUint(test.in, 10, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
2018-01-09 02:23:08 +01:00
|
|
|
t.Errorf("ParseUint(%q, 10, 64) = %v, %v want %v, %v",
|
2010-12-03 05:34:57 +01:00
|
|
|
test.in, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
func TestParseUint64Base(t *testing.T) {
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseUint64BaseTests {
|
|
|
|
test := &parseUint64BaseTests[i]
|
|
|
|
out, err := ParseUint(test.in, test.base, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
2018-01-09 02:23:08 +01:00
|
|
|
t.Errorf("ParseUint(%q, %v, 64) = %v, %v want %v, %v",
|
|
|
|
test.in, test.base, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseInt32(t *testing.T) {
|
|
|
|
for i := range parseInt32Tests {
|
|
|
|
test := &parseInt32Tests[i]
|
|
|
|
out, err := ParseInt(test.in, 10, 32)
|
|
|
|
if int64(test.out) != out || !reflect.DeepEqual(test.err, err) {
|
|
|
|
t.Errorf("ParseInt(%q, 10 ,32) = %v, %v want %v, %v",
|
2010-12-03 05:34:57 +01:00
|
|
|
test.in, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
func TestParseInt64(t *testing.T) {
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseInt64Tests {
|
|
|
|
test := &parseInt64Tests[i]
|
2011-12-14 16:41:54 +01:00
|
|
|
out, err := ParseInt(test.in, 10, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
2018-01-09 02:23:08 +01:00
|
|
|
t.Errorf("ParseInt(%q, 10, 64) = %v, %v want %v, %v",
|
2010-12-03 05:34:57 +01:00
|
|
|
test.in, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
func TestParseInt64Base(t *testing.T) {
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseInt64BaseTests {
|
|
|
|
test := &parseInt64BaseTests[i]
|
2015-10-31 01:59:47 +01:00
|
|
|
out, err := ParseInt(test.in, test.base, 64)
|
2010-12-03 05:34:57 +01:00
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
2018-01-09 02:23:08 +01:00
|
|
|
t.Errorf("ParseInt(%q, %v, 64) = %v, %v want %v, %v",
|
|
|
|
test.in, test.base, out, err, test.out, test.err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
func TestParseUint(t *testing.T) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch IntSize {
|
|
|
|
case 32:
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseUint32Tests {
|
|
|
|
test := &parseUint32Tests[i]
|
2011-12-14 16:41:54 +01:00
|
|
|
out, err := ParseUint(test.in, 10, 0)
|
2018-01-09 02:23:08 +01:00
|
|
|
if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) {
|
|
|
|
t.Errorf("ParseUint(%q, 10, 0) = %v, %v want %v, %v",
|
2010-12-03 05:34:57 +01:00
|
|
|
test.in, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 64:
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseUint64Tests {
|
|
|
|
test := &parseUint64Tests[i]
|
2011-12-14 16:41:54 +01:00
|
|
|
out, err := ParseUint(test.in, 10, 0)
|
2018-01-09 02:23:08 +01:00
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
|
|
|
t.Errorf("ParseUint(%q, 10, 0) = %v, %v want %v, %v",
|
2010-12-03 05:34:57 +01:00
|
|
|
test.in, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
func TestParseInt(t *testing.T) {
|
2010-12-03 05:34:57 +01:00
|
|
|
switch IntSize {
|
|
|
|
case 32:
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseInt32Tests {
|
|
|
|
test := &parseInt32Tests[i]
|
2011-12-14 16:41:54 +01:00
|
|
|
out, err := ParseInt(test.in, 10, 0)
|
2018-01-09 02:23:08 +01:00
|
|
|
if int64(test.out) != out || !reflect.DeepEqual(test.err, err) {
|
|
|
|
t.Errorf("ParseInt(%q, 10, 0) = %v, %v want %v, %v",
|
2010-12-03 05:34:57 +01:00
|
|
|
test.in, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 64:
|
2018-01-09 02:23:08 +01:00
|
|
|
for i := range parseInt64Tests {
|
|
|
|
test := &parseInt64Tests[i]
|
2011-12-14 16:41:54 +01:00
|
|
|
out, err := ParseInt(test.in, 10, 0)
|
2018-01-09 02:23:08 +01:00
|
|
|
if test.out != out || !reflect.DeepEqual(test.err, err) {
|
|
|
|
t.Errorf("ParseInt(%q, 10, 0) = %v, %v want %v, %v",
|
2010-12-03 05:34:57 +01:00
|
|
|
test.in, out, err, test.out, test.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func TestAtoi(t *testing.T) {
|
|
|
|
switch IntSize {
|
|
|
|
case 32:
|
|
|
|
for i := range parseInt32Tests {
|
|
|
|
test := &parseInt32Tests[i]
|
|
|
|
out, err := Atoi(test.in)
|
|
|
|
var testErr error
|
|
|
|
if test.err != nil {
|
|
|
|
testErr = &NumError{"Atoi", test.in, test.err.(*NumError).Err}
|
|
|
|
}
|
|
|
|
if int(test.out) != out || !reflect.DeepEqual(testErr, err) {
|
|
|
|
t.Errorf("Atoi(%q) = %v, %v want %v, %v",
|
|
|
|
test.in, out, err, test.out, testErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 64:
|
|
|
|
for i := range parseInt64Tests {
|
|
|
|
test := &parseInt64Tests[i]
|
|
|
|
out, err := Atoi(test.in)
|
|
|
|
var testErr error
|
|
|
|
if test.err != nil {
|
|
|
|
testErr = &NumError{"Atoi", test.in, test.err.(*NumError).Err}
|
|
|
|
}
|
|
|
|
if test.out != int64(out) || !reflect.DeepEqual(testErr, err) {
|
|
|
|
t.Errorf("Atoi(%q) = %v, %v want %v, %v",
|
|
|
|
test.in, out, err, test.out, testErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func bitSizeErrStub(name string, bitSize int) error {
|
|
|
|
return BitSizeError(name, "0", bitSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func baseErrStub(name string, base int) error {
|
|
|
|
return BaseError(name, "0", base)
|
|
|
|
}
|
|
|
|
|
|
|
|
func noErrStub(name string, arg int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type parseErrorTest struct {
|
|
|
|
arg int
|
|
|
|
errStub func(name string, arg int) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var parseBitSizeTests = []parseErrorTest{
|
|
|
|
{-1, bitSizeErrStub},
|
|
|
|
{0, noErrStub},
|
|
|
|
{64, noErrStub},
|
|
|
|
{65, bitSizeErrStub},
|
|
|
|
}
|
|
|
|
|
|
|
|
var parseBaseTests = []parseErrorTest{
|
|
|
|
{-1, baseErrStub},
|
|
|
|
{0, noErrStub},
|
|
|
|
{1, baseErrStub},
|
|
|
|
{2, noErrStub},
|
|
|
|
{36, noErrStub},
|
|
|
|
{37, baseErrStub},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseIntBitSize(t *testing.T) {
|
|
|
|
for i := range parseBitSizeTests {
|
|
|
|
test := &parseBitSizeTests[i]
|
|
|
|
testErr := test.errStub("ParseInt", test.arg)
|
|
|
|
_, err := ParseInt("0", 0, test.arg)
|
|
|
|
if !reflect.DeepEqual(testErr, err) {
|
|
|
|
t.Errorf("ParseInt(\"0\", 0, %v) = 0, %v want 0, %v",
|
|
|
|
test.arg, err, testErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseUintBitSize(t *testing.T) {
|
|
|
|
for i := range parseBitSizeTests {
|
|
|
|
test := &parseBitSizeTests[i]
|
|
|
|
testErr := test.errStub("ParseUint", test.arg)
|
|
|
|
_, err := ParseUint("0", 0, test.arg)
|
|
|
|
if !reflect.DeepEqual(testErr, err) {
|
|
|
|
t.Errorf("ParseUint(\"0\", 0, %v) = 0, %v want 0, %v",
|
|
|
|
test.arg, err, testErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseIntBase(t *testing.T) {
|
|
|
|
for i := range parseBaseTests {
|
|
|
|
test := &parseBaseTests[i]
|
|
|
|
testErr := test.errStub("ParseInt", test.arg)
|
|
|
|
_, err := ParseInt("0", test.arg, 0)
|
|
|
|
if !reflect.DeepEqual(testErr, err) {
|
|
|
|
t.Errorf("ParseInt(\"0\", %v, 0) = 0, %v want 0, %v",
|
|
|
|
test.arg, err, testErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseUintBase(t *testing.T) {
|
|
|
|
for i := range parseBaseTests {
|
|
|
|
test := &parseBaseTests[i]
|
|
|
|
testErr := test.errStub("ParseUint", test.arg)
|
|
|
|
_, err := ParseUint("0", test.arg, 0)
|
|
|
|
if !reflect.DeepEqual(testErr, err) {
|
|
|
|
t.Errorf("ParseUint(\"0\", %v, 0) = 0, %v want 0, %v",
|
|
|
|
test.arg, err, testErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 08:54:42 +02:00
|
|
|
func TestNumError(t *testing.T) {
|
|
|
|
for _, test := range numErrorTests {
|
|
|
|
err := &NumError{
|
|
|
|
Func: "ParseFloat",
|
|
|
|
Num: test.num,
|
|
|
|
Err: errors.New("failed"),
|
|
|
|
}
|
|
|
|
if got := err.Error(); got != test.want {
|
|
|
|
t.Errorf(`(&NumError{"ParseFloat", %q, "failed"}).Error() = %v, want %v`, test.num, got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func BenchmarkParseInt(b *testing.B) {
|
|
|
|
b.Run("Pos", func(b *testing.B) {
|
|
|
|
benchmarkParseInt(b, 1)
|
|
|
|
})
|
|
|
|
b.Run("Neg", func(b *testing.B) {
|
|
|
|
benchmarkParseInt(b, -1)
|
|
|
|
})
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
type benchCase struct {
|
|
|
|
name string
|
|
|
|
num int64
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func benchmarkParseInt(b *testing.B, neg int) {
|
|
|
|
cases := []benchCase{
|
|
|
|
{"7bit", 1<<7 - 1},
|
|
|
|
{"26bit", 1<<26 - 1},
|
|
|
|
{"31bit", 1<<31 - 1},
|
|
|
|
{"56bit", 1<<56 - 1},
|
|
|
|
{"63bit", 1<<63 - 1},
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
for _, cs := range cases {
|
|
|
|
b.Run(cs.name, func(b *testing.B) {
|
|
|
|
s := fmt.Sprintf("%d", cs.num*int64(neg))
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
out, _ := ParseInt(s, 10, 64)
|
|
|
|
BenchSink += int(out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkAtoi(b *testing.B) {
|
|
|
|
b.Run("Pos", func(b *testing.B) {
|
|
|
|
benchmarkAtoi(b, 1)
|
|
|
|
})
|
|
|
|
b.Run("Neg", func(b *testing.B) {
|
|
|
|
benchmarkAtoi(b, -1)
|
|
|
|
})
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func benchmarkAtoi(b *testing.B, neg int) {
|
|
|
|
cases := []benchCase{
|
|
|
|
{"7bit", 1<<7 - 1},
|
|
|
|
{"26bit", 1<<26 - 1},
|
|
|
|
{"31bit", 1<<31 - 1},
|
|
|
|
}
|
|
|
|
if IntSize == 64 {
|
|
|
|
cases = append(cases, []benchCase{
|
|
|
|
{"56bit", 1<<56 - 1},
|
|
|
|
{"63bit", 1<<63 - 1},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
for _, cs := range cases {
|
|
|
|
b.Run(cs.name, func(b *testing.B) {
|
|
|
|
s := fmt.Sprintf("%d", cs.num*int64(neg))
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
out, _ := Atoi(s)
|
|
|
|
BenchSink += out
|
|
|
|
}
|
|
|
|
})
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|