diff options
author | Robert Griesemer <gri@golang.org> | 2015-05-28 17:38:05 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2015-05-29 17:11:43 +0000 |
commit | a63b1806aa7484d744e79cd2f6d8e3bf73c4092c (patch) | |
tree | 4a03914612fb69dfd9725860d9cd3f9128c1fb7b /src/math/big/floatconv_test.go | |
parent | 9b3d9230aac0e8433add721a67f22ad6c27267ed (diff) | |
download | go-git-a63b1806aa7484d744e79cd2f6d8e3bf73c4092c.tar.gz |
math/big: remove (*Float).Scan, ScanFloat; more robust (*Float).Parse
- (*Float).Scan conflicted with fmt.Scanner.Scan; it was also only used
internally. Removed it, as well as the companion ScanFloat function.
- (*Float).Parse (and thus ParseFloat) can now also parse infinities.
As a result, more code could be simplified.
- Fixed a bug in rounding (round may implicitly be called for infinite
values). Found via existing test cases, after simplifying some code.
- Added more test cases.
Fixes issue #10938.
Change-Id: I1df97821654f034965ba8b82b272e52e6dc427f1
Reviewed-on: https://go-review.googlesource.com/10498
Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/floatconv_test.go')
-rw-r--r-- | src/math/big/floatconv_test.go | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/math/big/floatconv_test.go b/src/math/big/floatconv_test.go index fffcd70ce6..656d28c975 100644 --- a/src/math/big/floatconv_test.go +++ b/src/math/big/floatconv_test.go @@ -11,9 +11,12 @@ import ( ) func TestFloatSetFloat64String(t *testing.T) { + inf := math.Inf(0) + nan := math.NaN() + for _, test := range []struct { s string - x float64 + x float64 // NaNs represent invalid inputs }{ // basics {"0", 0}, @@ -45,6 +48,25 @@ func TestFloatSetFloat64String(t *testing.T) { {"1.E+10", 1e10}, {"+1E-10", 1e-10}, + // infinities + {"Inf", inf}, + {"+Inf", inf}, + {"-Inf", -inf}, + {"inf", inf}, + {"+inf", inf}, + {"-inf", -inf}, + + // invalid numbers + {"", nan}, + {"-", nan}, + {"0x", nan}, + {"0e", nan}, + {"1.2ef", nan}, + {"2..3", nan}, + {"123..", nan}, + {"infinity", nan}, + {"foobar", nan}, + // misc decimal values {"3.14159265", 3.14159265}, {"-687436.79457e-245", -687436.79457e-245}, @@ -96,8 +118,16 @@ func TestFloatSetFloat64String(t *testing.T) { var x Float x.SetPrec(53) _, ok := x.SetString(test.s) + if math.IsNaN(test.x) { + // test.s is invalid + if ok { + t.Errorf("%s: want parse error", test.s) + } + continue + } + // test.s is valid if !ok { - t.Errorf("%s: parse error", test.s) + t.Errorf("%s: got parse error", test.s) continue } f, _ := x.Float64() |