aboutsummaryrefslogtreecommitdiff
path: root/src/math/big/floatconv_test.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2015-01-30 14:53:53 -0800
committerRobert Griesemer <gri@golang.org>2015-01-30 23:03:06 +0000
commitf17cd880895863e2c13396296819d0dce81fa43f (patch)
tree388b07704de7a148d687a0ffcf2f53d2e4ec188d /src/math/big/floatconv_test.go
parent20a96a1f68cfe608b8066f3ee1b0db28d1d3e4e0 (diff)
downloadgo-f17cd880895863e2c13396296819d0dce81fa43f.tar.xz
math/big: split float conversion routines and tests into separate files
No other functional changes. Change-Id: I7e0bb7452c6a265535297ec7ce6a629f1aff695c Reviewed-on: https://go-review.googlesource.com/3674 Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/floatconv_test.go')
-rw-r--r--src/math/big/floatconv_test.go111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/math/big/floatconv_test.go b/src/math/big/floatconv_test.go
new file mode 100644
index 0000000000..83ea1d6057
--- /dev/null
+++ b/src/math/big/floatconv_test.go
@@ -0,0 +1,111 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package big
+
+import (
+ "strconv"
+ "testing"
+)
+
+var floatSetFloat64StringTests = []struct {
+ s string
+ x float64
+}{
+ {"0", 0},
+ {"-0", -0},
+ {"+0", 0},
+ {"1", 1},
+ {"-1", -1},
+ {"+1", 1},
+ {"1.234", 1.234},
+ {"-1.234", -1.234},
+ {"+1.234", 1.234},
+ {".1", 0.1},
+ {"1.", 1},
+ {"+1.", 1},
+
+ {"0e100", 0},
+ {"-0e+100", 0},
+ {"+0e-100", 0},
+ {"0E100", 0},
+ {"-0E+100", 0},
+ {"+0E-100", 0},
+ {"0p100", 0},
+ {"-0p+100", 0},
+ {"+0p-100", 0},
+
+ {"1.e10", 1e10},
+ {"1e+10", 1e10},
+ {"+1e-10", 1e-10},
+ {"1E10", 1e10},
+ {"1.E+10", 1e10},
+ {"+1E-10", 1e-10},
+ {"1p10", 1 << 10},
+ {"1p+10", 1 << 10},
+ {"+1.p-10", 1.0 / (1 << 10)},
+
+ {"-687436.79457e-245", -687436.79457e-245},
+ {"-687436.79457E245", -687436.79457e245},
+ {"1024.p-12", 0.25},
+ {"-1.p10", -1024},
+ {"0.25p2", 1},
+
+ {".0000000000000000000000000000000000000001", 1e-40},
+ {"+10000000000000000000000000000000000000000e-0", 1e40},
+}
+
+func TestFloatSetFloat64String(t *testing.T) {
+ for _, test := range floatSetFloat64StringTests {
+ var x Float
+ x.prec = 53 // TODO(gri) find better solution
+ _, ok := x.SetString(test.s)
+ if !ok {
+ t.Errorf("%s: parse error", test.s)
+ continue
+ }
+ f, _ := x.Float64()
+ want := new(Float).SetFloat64(test.x)
+ if x.Cmp(want) != 0 {
+ t.Errorf("%s: got %s (%v); want %v", test.s, &x, f, test.x)
+ }
+ }
+}
+
+func TestFloatFormat(t *testing.T) {
+ for _, test := range []struct {
+ x string
+ format byte
+ prec int
+ want string
+ }{
+ {"0", 'b', 0, "0"},
+ {"-0", 'b', 0, "-0"},
+ {"1.0", 'b', 0, "4503599627370496p1"},
+ {"-1.0", 'b', 0, "-4503599627370496p1"},
+
+ {"0", 'p', 0, "0"},
+ {"-0", 'p', 0, "-0"},
+ {"1024.0", 'p', 0, "0x.8p11"},
+ {"-1024.0", 'p', 0, "-0x.8p11"},
+ } {
+ f64, err := strconv.ParseFloat(test.x, 64)
+ if err != nil {
+ t.Error(err)
+ continue
+ }
+ f := new(Float).SetFloat64(f64)
+ got := f.Format(test.format, test.prec)
+ if got != test.want {
+ t.Errorf("%v: got %s", test, got)
+ }
+ if test.format == 'b' || test.format == 'p' {
+ continue // 'b', 'p' format not supported or different in strconv.Format
+ }
+ want := strconv.FormatFloat(f64, test.format, test.prec, 64)
+ if got != want {
+ t.Errorf("%v: got %s; want %s", test, got, want)
+ }
+ }
+}