]> Cypherpunks.ru repositories - gostls13.git/commitdiff
strconv: fix ParseComplex for strings with separators
authorRobert Griesemer <gri@golang.org>
Fri, 8 May 2020 19:13:11 +0000 (12:13 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 8 May 2020 20:34:23 +0000 (20:34 +0000)
The recently added function parseFloatPrefix tested the entire
string for correct placement of separators rather than just the
consumed part. The 4-char fix is in readFloat (atof.go:303).

Added more tests. Also added some white space for nicer
grouping of the test cases.

While at it, removed the need for calling testing.Run.

Fixes #38962.

Change-Id: Ifce84f362bb4ede559103f8d535556d3de9325f1
Reviewed-on: https://go-review.googlesource.com/c/go/+/233017
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/strconv/atoc_test.go
src/strconv/atof.go
src/strconv/atof_test.go

index 5c817a2e44b6f5d694e4336b9e608d76a7bffc67..3aa421dd0362e78c7d8917c2cdba251a1a1ddb30 100644 (file)
@@ -17,6 +17,7 @@ var (
        infm0 = complex(math.Inf(-1), 0)
        inf0p = complex(0, math.Inf(+1))
        inf0m = complex(0, math.Inf(-1))
+
        infpp = complex(math.Inf(+1), math.Inf(+1))
        infpm = complex(math.Inf(+1), math.Inf(-1))
        infmp = complex(math.Inf(-1), math.Inf(+1))
@@ -30,7 +31,6 @@ type atocTest struct {
 }
 
 func TestParseComplex(t *testing.T) {
-
        tests := []atocTest{
                // Clearly invalid
                {"", 0, ErrSyntax},
@@ -45,6 +45,7 @@ func TestParseComplex(t *testing.T) {
                {"3+", 0, ErrSyntax},
                {"3+5", 0, ErrSyntax},
                {"3+5+5i", 0, ErrSyntax},
+
                // Parentheses
                {"()", 0, ErrSyntax},
                {"(i)", 0, ErrSyntax},
@@ -54,6 +55,7 @@ func TestParseComplex(t *testing.T) {
                {"(1)+1i", 0, ErrSyntax},
                {"(3.0+5.5i", 0, ErrSyntax},
                {"3.0+5.5i)", 0, ErrSyntax},
+
                // NaNs
                {"NaN", complex(math.NaN(), 0), nil},
                {"NANi", complex(0, math.NaN()), nil},
@@ -61,6 +63,7 @@ func TestParseComplex(t *testing.T) {
                {"+NaN", 0, ErrSyntax},
                {"-NaN", 0, ErrSyntax},
                {"NaN-NaNi", 0, ErrSyntax},
+
                // Infs
                {"Inf", infp0, nil},
                {"+inf", infp0, nil},
@@ -74,6 +77,7 @@ func TestParseComplex(t *testing.T) {
                {"+Inf-Infi", infpm, nil},
                {"-Infinity+Infi", infmp, nil},
                {"inf-inf", 0, ErrSyntax},
+
                // Zeros
                {"0", 0, nil},
                {"0i", 0, nil},
@@ -88,6 +92,7 @@ func TestParseComplex(t *testing.T) {
                {"+0e-0+0e-0i", 0, nil},
                {"0e+0+0e+0i", 0, nil},
                {"-0e+0-0e+0i", 0, nil},
+
                // Regular non-zeroes
                {"0.1", 0.1, nil},
                {"0.1i", 0 + 0.1i, nil},
@@ -104,14 +109,17 @@ func TestParseComplex(t *testing.T) {
                {"+3e+3-3e+3i", 3e+3 - 3e+3i, nil},
                {"+3e+3+3e+3i", 3e+3 + 3e+3i, nil},
                {"+3e+3+3e+3i+", 0, ErrSyntax},
+
                // Separators
                {"0.1", 0.1, nil},
                {"0.1i", 0 + 0.1i, nil},
                {"0.1_2_3", 0.123, nil},
                {"+0x_3p3i", 0x3p3i, nil},
+               {"0_0+0x_0p0i", 0, nil},
                {"0x_10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
-               {"+0x_1_0.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
-               {"0x10.3p+8-0x_3p3i", 0x10.3p+8 - 0x3p3i, nil},
+               {"+0x_1_0.3p-8+0x_3_0p3i", 0x10.3p-8 + 0x30p3i, nil},
+               {"0x1_0.3p+8-0x_3p3i", 0x10.3p+8 - 0x3p3i, nil},
+
                // Hexadecimals
                {"0x10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
                {"+0x10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
@@ -125,6 +133,7 @@ func TestParseComplex(t *testing.T) {
                {"0x1e2", 0, ErrSyntax},
                {"1p2", 0, ErrSyntax},
                {"0x1e2i", 0, ErrSyntax},
+
                // ErrRange
                // next float64 - too large
                {"+0x1p1024", infp0, ErrRange},
@@ -177,19 +186,17 @@ func TestParseComplex(t *testing.T) {
                {"1e+4294967296+1e+4294967296i", infpp, ErrRange},
                {"1e+4294967296-1e+4294967296i", infpm, ErrRange},
        }
-       for _, tt := range tests {
-               tt := tt // for capture in Run closures below
-               if tt.err != nil {
-                       tt.err = &NumError{Func: "ParseComplex", Num: tt.in, Err: tt.err}
+       for i := range tests {
+               test := &tests[i]
+               if test.err != nil {
+                       test.err = &NumError{Func: "ParseComplex", Num: test.in, Err: test.err}
+               }
+               got, err := ParseComplex(test.in, 128)
+               if !reflect.DeepEqual(err, test.err) {
+                       t.Fatalf("ParseComplex(%q, 128) = %v, %v; want %v, %v", test.in, got, err, test.out, test.err)
+               }
+               if !(cmplx.IsNaN(test.out) && cmplx.IsNaN(got)) && got != test.out {
+                       t.Fatalf("ParseComplex(%q, 128) = %v, %v; want %v, %v", test.in, got, err, test.out, test.err)
                }
-               t.Run(tt.in, func(t *testing.T) {
-                       got, err := ParseComplex(tt.in, 128)
-                       if !reflect.DeepEqual(err, tt.err) {
-                               t.Fatalf("ParseComplex(%q, 128) = %v, %v want %v, %v", tt.in, got, err, tt.out, tt.err)
-                       }
-                       if !(cmplx.IsNaN(tt.out) && cmplx.IsNaN(got)) && got != tt.out {
-                               t.Fatalf("ParseComplex(%q, 128) = %v, %v want %v, %v", tt.in, got, err, tt.out, tt.err)
-                       }
-               })
        }
 }
index f20ae4af09b44f74a3994d1946e1c55fb068dca6..901f27afff6848c9db10358fcf58c2f23b80a8dc 100644 (file)
@@ -300,7 +300,7 @@ loop:
                exp = dp - ndMant
        }
 
-       if underscores && !underscoreOK(s) {
+       if underscores && !underscoreOK(s[:i]) {
                return
        }
 
index c30cb2e0feb902ce7432bae6c26c1f29be73c40f..545d989c41c2b80c783e33c1fee801548b1cdf0c 100644 (file)
@@ -480,7 +480,7 @@ func initAtofOnce() {
 }
 
 func TestParseFloatPrefix(t *testing.T) {
-       for i := 0; i < len(atoftests); i++ {
+       for i := range atoftests {
                test := &atoftests[i]
                if test.err != nil {
                        continue