Skip to content

Commit

Permalink
strconv: fix ParseComplex for strings with separators
Browse files Browse the repository at this point in the history
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 golang#38962.

Change-Id: Ifce84f362bb4ede559103f8d535556d3de9325f1
Reviewed-on: https://go-review.googlesource.com/c/go/+/233017
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
griesemer committed May 8, 2020
1 parent 26de581 commit 65126c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
39 changes: 23 additions & 16 deletions src/strconv/atoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -30,7 +31,6 @@ type atocTest struct {
}

func TestParseComplex(t *testing.T) {

tests := []atocTest{
// Clearly invalid
{"", 0, ErrSyntax},
Expand All @@ -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},
Expand All @@ -54,13 +55,15 @@ 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},
{"nan+nAni", complex(math.NaN(), math.NaN()), nil},
{"+NaN", 0, ErrSyntax},
{"-NaN", 0, ErrSyntax},
{"NaN-NaNi", 0, ErrSyntax},

// Infs
{"Inf", infp0, nil},
{"+inf", infp0, nil},
Expand All @@ -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},
Expand All @@ -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},
Expand All @@ -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},
Expand All @@ -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},
Expand Down Expand Up @@ -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)
}
})
}
}
2 changes: 1 addition & 1 deletion src/strconv/atof.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ loop:
exp = dp - ndMant
}

if underscores && !underscoreOK(s) {
if underscores && !underscoreOK(s[:i]) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion src/strconv/atof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 65126c5

Please sign in to comment.