Source file src/internal/runtime/strconv/atoi_test.go

     1  // Copyright 2025 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  	"internal/runtime/strconv"
     9  	"testing"
    10  )
    11  
    12  const intSize = 32 << (^uint(0) >> 63)
    13  
    14  type atoi64Test struct {
    15  	in  string
    16  	out int64
    17  	ok  bool
    18  }
    19  
    20  var atoi64tests = []atoi64Test{
    21  	{"", 0, false},
    22  	{"0", 0, true},
    23  	{"-0", 0, true},
    24  	{"1", 1, true},
    25  	{"-1", -1, true},
    26  	{"12345", 12345, true},
    27  	{"-12345", -12345, true},
    28  	{"012345", 12345, true},
    29  	{"-012345", -12345, true},
    30  	{"12345x", 0, false},
    31  	{"-12345x", 0, false},
    32  	{"98765432100", 98765432100, true},
    33  	{"-98765432100", -98765432100, true},
    34  	{"20496382327982653440", 0, false},
    35  	{"-20496382327982653440", 0, false},
    36  	{"9223372036854775807", 1<<63 - 1, true},
    37  	{"-9223372036854775807", -(1<<63 - 1), true},
    38  	{"9223372036854775808", 0, false},
    39  	{"-9223372036854775808", -1 << 63, true},
    40  	{"9223372036854775809", 0, false},
    41  	{"-9223372036854775809", 0, false},
    42  }
    43  
    44  func TestAtoi(t *testing.T) {
    45  	switch intSize {
    46  	case 32:
    47  		for i := range atoi32tests {
    48  			test := &atoi32tests[i]
    49  			out, ok := strconv.Atoi(test.in)
    50  			if test.out != int32(out) || test.ok != ok {
    51  				t.Errorf("Atoi(%q) = (%v, %v) want (%v, %v)",
    52  					test.in, out, ok, test.out, test.ok)
    53  			}
    54  		}
    55  	case 64:
    56  		for i := range atoi64tests {
    57  			test := &atoi64tests[i]
    58  			out, ok := strconv.Atoi(test.in)
    59  			if test.out != int64(out) || test.ok != ok {
    60  				t.Errorf("Atoi(%q) = (%v, %v) want (%v, %v)",
    61  					test.in, out, ok, test.out, test.ok)
    62  			}
    63  		}
    64  	}
    65  }
    66  
    67  type atoi32Test struct {
    68  	in  string
    69  	out int32
    70  	ok  bool
    71  }
    72  
    73  var atoi32tests = []atoi32Test{
    74  	{"", 0, false},
    75  	{"0", 0, true},
    76  	{"-0", 0, true},
    77  	{"1", 1, true},
    78  	{"-1", -1, true},
    79  	{"12345", 12345, true},
    80  	{"-12345", -12345, true},
    81  	{"012345", 12345, true},
    82  	{"-012345", -12345, true},
    83  	{"12345x", 0, false},
    84  	{"-12345x", 0, false},
    85  	{"987654321", 987654321, true},
    86  	{"-987654321", -987654321, true},
    87  	{"2147483647", 1<<31 - 1, true},
    88  	{"-2147483647", -(1<<31 - 1), true},
    89  	{"2147483648", 0, false},
    90  	{"-2147483648", -1 << 31, true},
    91  	{"2147483649", 0, false},
    92  	{"-2147483649", 0, false},
    93  }
    94  
    95  func TestAtoi32(t *testing.T) {
    96  	for i := range atoi32tests {
    97  		test := &atoi32tests[i]
    98  		out, ok := strconv.Atoi32(test.in)
    99  		if test.out != out || test.ok != ok {
   100  			t.Errorf("Atoi32(%q) = (%v, %v) want (%v, %v)",
   101  				test.in, out, ok, test.out, test.ok)
   102  		}
   103  	}
   104  }
   105  
   106  

View as plain text