Source file src/strconv/atoi_test.go

Documentation: strconv

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package strconv_test
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"reflect"
    11  	. "strconv"
    12  	"testing"
    13  )
    14  
    15  type parseUint64Test struct {
    16  	in  string
    17  	out uint64
    18  	err error
    19  }
    20  
    21  var parseUint64Tests = []parseUint64Test{
    22  	{"", 0, ErrSyntax},
    23  	{"0", 0, nil},
    24  	{"1", 1, nil},
    25  	{"12345", 12345, nil},
    26  	{"012345", 12345, nil},
    27  	{"12345x", 0, ErrSyntax},
    28  	{"98765432100", 98765432100, nil},
    29  	{"18446744073709551615", 1<<64 - 1, nil},
    30  	{"18446744073709551616", 1<<64 - 1, ErrRange},
    31  	{"18446744073709551620", 1<<64 - 1, ErrRange},
    32  	{"1_2_3_4_5", 0, ErrSyntax}, // base=10 so no underscores allowed
    33  	{"_12345", 0, ErrSyntax},
    34  	{"1__2345", 0, ErrSyntax},
    35  	{"12345_", 0, ErrSyntax},
    36  }
    37  
    38  type parseUint64BaseTest struct {
    39  	in   string
    40  	base int
    41  	out  uint64
    42  	err  error
    43  }
    44  
    45  var parseUint64BaseTests = []parseUint64BaseTest{
    46  	{"", 0, 0, ErrSyntax},
    47  	{"0", 0, 0, nil},
    48  	{"0x", 0, 0, ErrSyntax},
    49  	{"0X", 0, 0, ErrSyntax},
    50  	{"1", 0, 1, nil},
    51  	{"12345", 0, 12345, nil},
    52  	{"012345", 0, 012345, nil},
    53  	{"0x12345", 0, 0x12345, nil},
    54  	{"0X12345", 0, 0x12345, nil},
    55  	{"12345x", 0, 0, ErrSyntax},
    56  	{"0xabcdefg123", 0, 0, ErrSyntax},
    57  	{"123456789abc", 0, 0, ErrSyntax},
    58  	{"98765432100", 0, 98765432100, nil},
    59  	{"18446744073709551615", 0, 1<<64 - 1, nil},
    60  	{"18446744073709551616", 0, 1<<64 - 1, ErrRange},
    61  	{"18446744073709551620", 0, 1<<64 - 1, ErrRange},
    62  	{"0xFFFFFFFFFFFFFFFF", 0, 1<<64 - 1, nil},
    63  	{"0x10000000000000000", 0, 1<<64 - 1, ErrRange},
    64  	{"01777777777777777777777", 0, 1<<64 - 1, nil},
    65  	{"01777777777777777777778", 0, 0, ErrSyntax},
    66  	{"02000000000000000000000", 0, 1<<64 - 1, ErrRange},
    67  	{"0200000000000000000000", 0, 1 << 61, nil},
    68  	{"0b", 0, 0, ErrSyntax},
    69  	{"0B", 0, 0, ErrSyntax},
    70  	{"0b101", 0, 5, nil},
    71  	{"0B101", 0, 5, nil},
    72  	{"0o", 0, 0, ErrSyntax},
    73  	{"0O", 0, 0, ErrSyntax},
    74  	{"0o377", 0, 255, nil},
    75  	{"0O377", 0, 255, nil},
    76  
    77  	// underscores allowed with base == 0 only
    78  	{"1_2_3_4_5", 0, 12345, nil}, // base 0 => 10
    79  	{"_12345", 0, 0, ErrSyntax},
    80  	{"1__2345", 0, 0, ErrSyntax},
    81  	{"12345_", 0, 0, ErrSyntax},
    82  
    83  	{"1_2_3_4_5", 10, 0, ErrSyntax}, // base 10
    84  	{"_12345", 10, 0, ErrSyntax},
    85  	{"1__2345", 10, 0, ErrSyntax},
    86  	{"12345_", 10, 0, ErrSyntax},
    87  
    88  	{"0x_1_2_3_4_5", 0, 0x12345, nil}, // base 0 => 16
    89  	{"_0x12345", 0, 0, ErrSyntax},
    90  	{"0x__12345", 0, 0, ErrSyntax},
    91  	{"0x1__2345", 0, 0, ErrSyntax},
    92  	{"0x1234__5", 0, 0, ErrSyntax},
    93  	{"0x12345_", 0, 0, ErrSyntax},
    94  
    95  	{"1_2_3_4_5", 16, 0, ErrSyntax}, // base 16
    96  	{"_12345", 16, 0, ErrSyntax},
    97  	{"1__2345", 16, 0, ErrSyntax},
    98  	{"1234__5", 16, 0, ErrSyntax},
    99  	{"12345_", 16, 0, ErrSyntax},
   100  
   101  	{"0_1_2_3_4_5", 0, 012345, nil}, // base 0 => 8 (0377)
   102  	{"_012345", 0, 0, ErrSyntax},
   103  	{"0__12345", 0, 0, ErrSyntax},
   104  	{"01234__5", 0, 0, ErrSyntax},
   105  	{"012345_", 0, 0, ErrSyntax},
   106  
   107  	{"0o_1_2_3_4_5", 0, 012345, nil}, // base 0 => 8 (0o377)
   108  	{"_0o12345", 0, 0, ErrSyntax},
   109  	{"0o__12345", 0, 0, ErrSyntax},
   110  	{"0o1234__5", 0, 0, ErrSyntax},
   111  	{"0o12345_", 0, 0, ErrSyntax},
   112  
   113  	{"0_1_2_3_4_5", 8, 0, ErrSyntax}, // base 8
   114  	{"_012345", 8, 0, ErrSyntax},
   115  	{"0__12345", 8, 0, ErrSyntax},
   116  	{"01234__5", 8, 0, ErrSyntax},
   117  	{"012345_", 8, 0, ErrSyntax},
   118  
   119  	{"0b_1_0_1", 0, 5, nil}, // base 0 => 2 (0b101)
   120  	{"_0b101", 0, 0, ErrSyntax},
   121  	{"0b__101", 0, 0, ErrSyntax},
   122  	{"0b1__01", 0, 0, ErrSyntax},
   123  	{"0b10__1", 0, 0, ErrSyntax},
   124  	{"0b101_", 0, 0, ErrSyntax},
   125  
   126  	{"1_0_1", 2, 0, ErrSyntax}, // base 2
   127  	{"_101", 2, 0, ErrSyntax},
   128  	{"1_01", 2, 0, ErrSyntax},
   129  	{"10_1", 2, 0, ErrSyntax},
   130  	{"101_", 2, 0, ErrSyntax},
   131  }
   132  
   133  type parseInt64Test struct {
   134  	in  string
   135  	out int64
   136  	err error
   137  }
   138  
   139  var parseInt64Tests = []parseInt64Test{
   140  	{"", 0, ErrSyntax},
   141  	{"0", 0, nil},
   142  	{"-0", 0, nil},
   143  	{"1", 1, nil},
   144  	{"-1", -1, nil},
   145  	{"12345", 12345, nil},
   146  	{"-12345", -12345, nil},
   147  	{"012345", 12345, nil},
   148  	{"-012345", -12345, nil},
   149  	{"98765432100", 98765432100, nil},
   150  	{"-98765432100", -98765432100, nil},
   151  	{"9223372036854775807", 1<<63 - 1, nil},
   152  	{"-9223372036854775807", -(1<<63 - 1), nil},
   153  	{"9223372036854775808", 1<<63 - 1, ErrRange},
   154  	{"-9223372036854775808", -1 << 63, nil},
   155  	{"9223372036854775809", 1<<63 - 1, ErrRange},
   156  	{"-9223372036854775809", -1 << 63, ErrRange},
   157  	{"-1_2_3_4_5", 0, ErrSyntax}, // base=10 so no underscores allowed
   158  	{"-_12345", 0, ErrSyntax},
   159  	{"_12345", 0, ErrSyntax},
   160  	{"1__2345", 0, ErrSyntax},
   161  	{"12345_", 0, ErrSyntax},
   162  }
   163  
   164  type parseInt64BaseTest struct {
   165  	in   string
   166  	base int
   167  	out  int64
   168  	err  error
   169  }
   170  
   171  var parseInt64BaseTests = []parseInt64BaseTest{
   172  	{"", 0, 0, ErrSyntax},
   173  	{"0", 0, 0, nil},
   174  	{"-0", 0, 0, nil},
   175  	{"1", 0, 1, nil},
   176  	{"-1", 0, -1, nil},
   177  	{"12345", 0, 12345, nil},
   178  	{"-12345", 0, -12345, nil},
   179  	{"012345", 0, 012345, nil},
   180  	{"-012345", 0, -012345, nil},
   181  	{"0x12345", 0, 0x12345, nil},
   182  	{"-0X12345", 0, -0x12345, nil},
   183  	{"12345x", 0, 0, ErrSyntax},
   184  	{"-12345x", 0, 0, ErrSyntax},
   185  	{"98765432100", 0, 98765432100, nil},
   186  	{"-98765432100", 0, -98765432100, nil},
   187  	{"9223372036854775807", 0, 1<<63 - 1, nil},
   188  	{"-9223372036854775807", 0, -(1<<63 - 1), nil},
   189  	{"9223372036854775808", 0, 1<<63 - 1, ErrRange},
   190  	{"-9223372036854775808", 0, -1 << 63, nil},
   191  	{"9223372036854775809", 0, 1<<63 - 1, ErrRange},
   192  	{"-9223372036854775809", 0, -1 << 63, ErrRange},
   193  
   194  	// other bases
   195  	{"g", 17, 16, nil},
   196  	{"10", 25, 25, nil},
   197  	{"holycow", 35, (((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, nil},
   198  	{"holycow", 36, (((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, nil},
   199  
   200  	// base 2
   201  	{"0", 2, 0, nil},
   202  	{"-1", 2, -1, nil},
   203  	{"1010", 2, 10, nil},
   204  	{"1000000000000000", 2, 1 << 15, nil},
   205  	{"111111111111111111111111111111111111111111111111111111111111111", 2, 1<<63 - 1, nil},
   206  	{"1000000000000000000000000000000000000000000000000000000000000000", 2, 1<<63 - 1, ErrRange},
   207  	{"-1000000000000000000000000000000000000000000000000000000000000000", 2, -1 << 63, nil},
   208  	{"-1000000000000000000000000000000000000000000000000000000000000001", 2, -1 << 63, ErrRange},
   209  
   210  	// base 8
   211  	{"-10", 8, -8, nil},
   212  	{"57635436545", 8, 057635436545, nil},
   213  	{"100000000", 8, 1 << 24, nil},
   214  
   215  	// base 16
   216  	{"10", 16, 16, nil},
   217  	{"-123456789abcdef", 16, -0x123456789abcdef, nil},
   218  	{"7fffffffffffffff", 16, 1<<63 - 1, nil},
   219  
   220  	// underscores
   221  	{"-0x_1_2_3_4_5", 0, -0x12345, nil},
   222  	{"0x_1_2_3_4_5", 0, 0x12345, nil},
   223  	{"-_0x12345", 0, 0, ErrSyntax},
   224  	{"_-0x12345", 0, 0, ErrSyntax},
   225  	{"_0x12345", 0, 0, ErrSyntax},
   226  	{"0x__12345", 0, 0, ErrSyntax},
   227  	{"0x1__2345", 0, 0, ErrSyntax},
   228  	{"0x1234__5", 0, 0, ErrSyntax},
   229  	{"0x12345_", 0, 0, ErrSyntax},
   230  
   231  	{"-0_1_2_3_4_5", 0, -012345, nil}, // octal
   232  	{"0_1_2_3_4_5", 0, 012345, nil},   // octal
   233  	{"-_012345", 0, 0, ErrSyntax},
   234  	{"_-012345", 0, 0, ErrSyntax},
   235  	{"_012345", 0, 0, ErrSyntax},
   236  	{"0__12345", 0, 0, ErrSyntax},
   237  	{"01234__5", 0, 0, ErrSyntax},
   238  	{"012345_", 0, 0, ErrSyntax},
   239  }
   240  
   241  type parseUint32Test struct {
   242  	in  string
   243  	out uint32
   244  	err error
   245  }
   246  
   247  var parseUint32Tests = []parseUint32Test{
   248  	{"", 0, ErrSyntax},
   249  	{"0", 0, nil},
   250  	{"1", 1, nil},
   251  	{"12345", 12345, nil},
   252  	{"012345", 12345, nil},
   253  	{"12345x", 0, ErrSyntax},
   254  	{"987654321", 987654321, nil},
   255  	{"4294967295", 1<<32 - 1, nil},
   256  	{"4294967296", 1<<32 - 1, ErrRange},
   257  	{"1_2_3_4_5", 0, ErrSyntax}, // base=10 so no underscores allowed
   258  	{"_12345", 0, ErrSyntax},
   259  	{"_12345", 0, ErrSyntax},
   260  	{"1__2345", 0, ErrSyntax},
   261  	{"12345_", 0, ErrSyntax},
   262  }
   263  
   264  type parseInt32Test struct {
   265  	in  string
   266  	out int32
   267  	err error
   268  }
   269  
   270  var parseInt32Tests = []parseInt32Test{
   271  	{"", 0, ErrSyntax},
   272  	{"0", 0, nil},
   273  	{"-0", 0, nil},
   274  	{"1", 1, nil},
   275  	{"-1", -1, nil},
   276  	{"12345", 12345, nil},
   277  	{"-12345", -12345, nil},
   278  	{"012345", 12345, nil},
   279  	{"-012345", -12345, nil},
   280  	{"12345x", 0, ErrSyntax},
   281  	{"-12345x", 0, ErrSyntax},
   282  	{"987654321", 987654321, nil},
   283  	{"-987654321", -987654321, nil},
   284  	{"2147483647", 1<<31 - 1, nil},
   285  	{"-2147483647", -(1<<31 - 1), nil},
   286  	{"2147483648", 1<<31 - 1, ErrRange},
   287  	{"-2147483648", -1 << 31, nil},
   288  	{"2147483649", 1<<31 - 1, ErrRange},
   289  	{"-2147483649", -1 << 31, ErrRange},
   290  	{"-1_2_3_4_5", 0, ErrSyntax}, // base=10 so no underscores allowed
   291  	{"-_12345", 0, ErrSyntax},
   292  	{"_12345", 0, ErrSyntax},
   293  	{"1__2345", 0, ErrSyntax},
   294  	{"12345_", 0, ErrSyntax},
   295  }
   296  
   297  type numErrorTest struct {
   298  	num, want string
   299  }
   300  
   301  var numErrorTests = []numErrorTest{
   302  	{"0", `strconv.ParseFloat: parsing "0": failed`},
   303  	{"`", "strconv.ParseFloat: parsing \"`\": failed"},
   304  	{"1\x00.2", `strconv.ParseFloat: parsing "1\x00.2": failed`},
   305  }
   306  
   307  func init() {
   308  	// The parse routines return NumErrors wrapping
   309  	// the error and the string. Convert the tables above.
   310  	for i := range parseUint64Tests {
   311  		test := &parseUint64Tests[i]
   312  		if test.err != nil {
   313  			test.err = &NumError{"ParseUint", test.in, test.err}
   314  		}
   315  	}
   316  	for i := range parseUint64BaseTests {
   317  		test := &parseUint64BaseTests[i]
   318  		if test.err != nil {
   319  			test.err = &NumError{"ParseUint", test.in, test.err}
   320  		}
   321  	}
   322  	for i := range parseInt64Tests {
   323  		test := &parseInt64Tests[i]
   324  		if test.err != nil {
   325  			test.err = &NumError{"ParseInt", test.in, test.err}
   326  		}
   327  	}
   328  	for i := range parseInt64BaseTests {
   329  		test := &parseInt64BaseTests[i]
   330  		if test.err != nil {
   331  			test.err = &NumError{"ParseInt", test.in, test.err}
   332  		}
   333  	}
   334  	for i := range parseUint32Tests {
   335  		test := &parseUint32Tests[i]
   336  		if test.err != nil {
   337  			test.err = &NumError{"ParseUint", test.in, test.err}
   338  		}
   339  	}
   340  	for i := range parseInt32Tests {
   341  		test := &parseInt32Tests[i]
   342  		if test.err != nil {
   343  			test.err = &NumError{"ParseInt", test.in, test.err}
   344  		}
   345  	}
   346  }
   347  
   348  func TestParseUint32(t *testing.T) {
   349  	for i := range parseUint32Tests {
   350  		test := &parseUint32Tests[i]
   351  		out, err := ParseUint(test.in, 10, 32)
   352  		if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) {
   353  			t.Errorf("ParseUint(%q, 10, 32) = %v, %v want %v, %v",
   354  				test.in, out, err, test.out, test.err)
   355  		}
   356  	}
   357  }
   358  
   359  func TestParseUint64(t *testing.T) {
   360  	for i := range parseUint64Tests {
   361  		test := &parseUint64Tests[i]
   362  		out, err := ParseUint(test.in, 10, 64)
   363  		if test.out != out || !reflect.DeepEqual(test.err, err) {
   364  			t.Errorf("ParseUint(%q, 10, 64) = %v, %v want %v, %v",
   365  				test.in, out, err, test.out, test.err)
   366  		}
   367  	}
   368  }
   369  
   370  func TestParseUint64Base(t *testing.T) {
   371  	for i := range parseUint64BaseTests {
   372  		test := &parseUint64BaseTests[i]
   373  		out, err := ParseUint(test.in, test.base, 64)
   374  		if test.out != out || !reflect.DeepEqual(test.err, err) {
   375  			t.Errorf("ParseUint(%q, %v, 64) = %v, %v want %v, %v",
   376  				test.in, test.base, out, err, test.out, test.err)
   377  		}
   378  	}
   379  }
   380  
   381  func TestParseInt32(t *testing.T) {
   382  	for i := range parseInt32Tests {
   383  		test := &parseInt32Tests[i]
   384  		out, err := ParseInt(test.in, 10, 32)
   385  		if int64(test.out) != out || !reflect.DeepEqual(test.err, err) {
   386  			t.Errorf("ParseInt(%q, 10 ,32) = %v, %v want %v, %v",
   387  				test.in, out, err, test.out, test.err)
   388  		}
   389  	}
   390  }
   391  
   392  func TestParseInt64(t *testing.T) {
   393  	for i := range parseInt64Tests {
   394  		test := &parseInt64Tests[i]
   395  		out, err := ParseInt(test.in, 10, 64)
   396  		if test.out != out || !reflect.DeepEqual(test.err, err) {
   397  			t.Errorf("ParseInt(%q, 10, 64) = %v, %v want %v, %v",
   398  				test.in, out, err, test.out, test.err)
   399  		}
   400  	}
   401  }
   402  
   403  func TestParseInt64Base(t *testing.T) {
   404  	for i := range parseInt64BaseTests {
   405  		test := &parseInt64BaseTests[i]
   406  		out, err := ParseInt(test.in, test.base, 64)
   407  		if test.out != out || !reflect.DeepEqual(test.err, err) {
   408  			t.Errorf("ParseInt(%q, %v, 64) = %v, %v want %v, %v",
   409  				test.in, test.base, out, err, test.out, test.err)
   410  		}
   411  	}
   412  }
   413  
   414  func TestParseUint(t *testing.T) {
   415  	switch IntSize {
   416  	case 32:
   417  		for i := range parseUint32Tests {
   418  			test := &parseUint32Tests[i]
   419  			out, err := ParseUint(test.in, 10, 0)
   420  			if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) {
   421  				t.Errorf("ParseUint(%q, 10, 0) = %v, %v want %v, %v",
   422  					test.in, out, err, test.out, test.err)
   423  			}
   424  		}
   425  	case 64:
   426  		for i := range parseUint64Tests {
   427  			test := &parseUint64Tests[i]
   428  			out, err := ParseUint(test.in, 10, 0)
   429  			if test.out != out || !reflect.DeepEqual(test.err, err) {
   430  				t.Errorf("ParseUint(%q, 10, 0) = %v, %v want %v, %v",
   431  					test.in, out, err, test.out, test.err)
   432  			}
   433  		}
   434  	}
   435  }
   436  
   437  func TestParseInt(t *testing.T) {
   438  	switch IntSize {
   439  	case 32:
   440  		for i := range parseInt32Tests {
   441  			test := &parseInt32Tests[i]
   442  			out, err := ParseInt(test.in, 10, 0)
   443  			if int64(test.out) != out || !reflect.DeepEqual(test.err, err) {
   444  				t.Errorf("ParseInt(%q, 10, 0) = %v, %v want %v, %v",
   445  					test.in, out, err, test.out, test.err)
   446  			}
   447  		}
   448  	case 64:
   449  		for i := range parseInt64Tests {
   450  			test := &parseInt64Tests[i]
   451  			out, err := ParseInt(test.in, 10, 0)
   452  			if test.out != out || !reflect.DeepEqual(test.err, err) {
   453  				t.Errorf("ParseInt(%q, 10, 0) = %v, %v want %v, %v",
   454  					test.in, out, err, test.out, test.err)
   455  			}
   456  		}
   457  	}
   458  }
   459  
   460  func TestAtoi(t *testing.T) {
   461  	switch IntSize {
   462  	case 32:
   463  		for i := range parseInt32Tests {
   464  			test := &parseInt32Tests[i]
   465  			out, err := Atoi(test.in)
   466  			var testErr error
   467  			if test.err != nil {
   468  				testErr = &NumError{"Atoi", test.in, test.err.(*NumError).Err}
   469  			}
   470  			if int(test.out) != out || !reflect.DeepEqual(testErr, err) {
   471  				t.Errorf("Atoi(%q) = %v, %v want %v, %v",
   472  					test.in, out, err, test.out, testErr)
   473  			}
   474  		}
   475  	case 64:
   476  		for i := range parseInt64Tests {
   477  			test := &parseInt64Tests[i]
   478  			out, err := Atoi(test.in)
   479  			var testErr error
   480  			if test.err != nil {
   481  				testErr = &NumError{"Atoi", test.in, test.err.(*NumError).Err}
   482  			}
   483  			if test.out != int64(out) || !reflect.DeepEqual(testErr, err) {
   484  				t.Errorf("Atoi(%q) = %v, %v want %v, %v",
   485  					test.in, out, err, test.out, testErr)
   486  			}
   487  		}
   488  	}
   489  }
   490  
   491  func bitSizeErrStub(name string, bitSize int) error {
   492  	return BitSizeError(name, "0", bitSize)
   493  }
   494  
   495  func baseErrStub(name string, base int) error {
   496  	return BaseError(name, "0", base)
   497  }
   498  
   499  func noErrStub(name string, arg int) error {
   500  	return nil
   501  }
   502  
   503  type parseErrorTest struct {
   504  	arg     int
   505  	errStub func(name string, arg int) error
   506  }
   507  
   508  var parseBitSizeTests = []parseErrorTest{
   509  	{-1, bitSizeErrStub},
   510  	{0, noErrStub},
   511  	{64, noErrStub},
   512  	{65, bitSizeErrStub},
   513  }
   514  
   515  var parseBaseTests = []parseErrorTest{
   516  	{-1, baseErrStub},
   517  	{0, noErrStub},
   518  	{1, baseErrStub},
   519  	{2, noErrStub},
   520  	{36, noErrStub},
   521  	{37, baseErrStub},
   522  }
   523  
   524  func equalError(a, b error) bool {
   525  	if a == nil {
   526  		return b == nil
   527  	}
   528  	if b == nil {
   529  		return a == nil
   530  	}
   531  	return a.Error() == b.Error()
   532  }
   533  
   534  func TestParseIntBitSize(t *testing.T) {
   535  	for i := range parseBitSizeTests {
   536  		test := &parseBitSizeTests[i]
   537  		testErr := test.errStub("ParseInt", test.arg)
   538  		_, err := ParseInt("0", 0, test.arg)
   539  		if !equalError(testErr, err) {
   540  			t.Errorf("ParseInt(\"0\", 0, %v) = 0, %v want 0, %v",
   541  				test.arg, err, testErr)
   542  		}
   543  	}
   544  }
   545  
   546  func TestParseUintBitSize(t *testing.T) {
   547  	for i := range parseBitSizeTests {
   548  		test := &parseBitSizeTests[i]
   549  		testErr := test.errStub("ParseUint", test.arg)
   550  		_, err := ParseUint("0", 0, test.arg)
   551  		if !equalError(testErr, err) {
   552  			t.Errorf("ParseUint(\"0\", 0, %v) = 0, %v want 0, %v",
   553  				test.arg, err, testErr)
   554  		}
   555  	}
   556  }
   557  
   558  func TestParseIntBase(t *testing.T) {
   559  	for i := range parseBaseTests {
   560  		test := &parseBaseTests[i]
   561  		testErr := test.errStub("ParseInt", test.arg)
   562  		_, err := ParseInt("0", test.arg, 0)
   563  		if !equalError(testErr, err) {
   564  			t.Errorf("ParseInt(\"0\", %v, 0) = 0, %v want 0, %v",
   565  				test.arg, err, testErr)
   566  		}
   567  	}
   568  }
   569  
   570  func TestParseUintBase(t *testing.T) {
   571  	for i := range parseBaseTests {
   572  		test := &parseBaseTests[i]
   573  		testErr := test.errStub("ParseUint", test.arg)
   574  		_, err := ParseUint("0", test.arg, 0)
   575  		if !equalError(testErr, err) {
   576  			t.Errorf("ParseUint(\"0\", %v, 0) = 0, %v want 0, %v",
   577  				test.arg, err, testErr)
   578  		}
   579  	}
   580  }
   581  
   582  func TestNumError(t *testing.T) {
   583  	for _, test := range numErrorTests {
   584  		err := &NumError{
   585  			Func: "ParseFloat",
   586  			Num:  test.num,
   587  			Err:  errors.New("failed"),
   588  		}
   589  		if got := err.Error(); got != test.want {
   590  			t.Errorf(`(&NumError{"ParseFloat", %q, "failed"}).Error() = %v, want %v`, test.num, got, test.want)
   591  		}
   592  	}
   593  }
   594  
   595  func BenchmarkParseInt(b *testing.B) {
   596  	b.Run("Pos", func(b *testing.B) {
   597  		benchmarkParseInt(b, 1)
   598  	})
   599  	b.Run("Neg", func(b *testing.B) {
   600  		benchmarkParseInt(b, -1)
   601  	})
   602  }
   603  
   604  type benchCase struct {
   605  	name string
   606  	num  int64
   607  }
   608  
   609  func benchmarkParseInt(b *testing.B, neg int) {
   610  	cases := []benchCase{
   611  		{"7bit", 1<<7 - 1},
   612  		{"26bit", 1<<26 - 1},
   613  		{"31bit", 1<<31 - 1},
   614  		{"56bit", 1<<56 - 1},
   615  		{"63bit", 1<<63 - 1},
   616  	}
   617  	for _, cs := range cases {
   618  		b.Run(cs.name, func(b *testing.B) {
   619  			s := fmt.Sprintf("%d", cs.num*int64(neg))
   620  			for i := 0; i < b.N; i++ {
   621  				out, _ := ParseInt(s, 10, 64)
   622  				BenchSink += int(out)
   623  			}
   624  		})
   625  	}
   626  }
   627  
   628  func BenchmarkAtoi(b *testing.B) {
   629  	b.Run("Pos", func(b *testing.B) {
   630  		benchmarkAtoi(b, 1)
   631  	})
   632  	b.Run("Neg", func(b *testing.B) {
   633  		benchmarkAtoi(b, -1)
   634  	})
   635  }
   636  
   637  func benchmarkAtoi(b *testing.B, neg int) {
   638  	cases := []benchCase{
   639  		{"7bit", 1<<7 - 1},
   640  		{"26bit", 1<<26 - 1},
   641  		{"31bit", 1<<31 - 1},
   642  	}
   643  	if IntSize == 64 {
   644  		cases = append(cases, []benchCase{
   645  			{"56bit", 1<<56 - 1},
   646  			{"63bit", 1<<63 - 1},
   647  		}...)
   648  	}
   649  	for _, cs := range cases {
   650  		b.Run(cs.name, func(b *testing.B) {
   651  			s := fmt.Sprintf("%d", cs.num*int64(neg))
   652  			for i := 0; i < b.N; i++ {
   653  				out, _ := Atoi(s)
   654  				BenchSink += out
   655  			}
   656  		})
   657  	}
   658  }
   659  

View as plain text