From 91c0f006fc6f30d970d18ef19407e2a77a2e6acb Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 4 Feb 2015 16:31:59 -0800 Subject: [PATCH] math/big: more Float conversion tests Change-Id: Ia30886569141ca2e0321bea6ee1d5c9e0f79d6f9 Reviewed-on: https://go-review.googlesource.com/3941 Reviewed-by: Alan Donovan --- src/math/big/floatconv_test.go | 91 +++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/src/math/big/floatconv_test.go b/src/math/big/floatconv_test.go index 27ac6c877b..d3290dddd0 100644 --- a/src/math/big/floatconv_test.go +++ b/src/math/big/floatconv_test.go @@ -5,6 +5,7 @@ package big import ( + "io" "math" "strconv" "testing" @@ -167,7 +168,7 @@ func TestFloat64Format(t *testing.T) { {below1e23, 'e', 17, "9.99999999999999748e+22"}, {below1e23, 'f', 17, "99999999999999974834176.00000000000000000"}, - // {below1e23, 'g', 17, "9.9999999999999975e+22"}, + {below1e23, 'g', 17, "9.9999999999999975e+22"}, // {below1e23, 'e', -1, "9.999999999999997e+22"}, // {below1e23, 'f', -1, "99999999999999970000000"}, @@ -236,3 +237,91 @@ func TestFloat64Format(t *testing.T) { } } } + +func TestFloatFormat(t *testing.T) { + for _, test := range []struct { + x string + format byte + prec int + want string + }{ + {"0", 'f', 0, "0"}, + {"-0", 'f', 0, "-0"}, + {"1", 'f', 0, "1"}, + {"-1", 'f', 0, "-1"}, + + {"1.459", 'e', 0, "1e+00"}, + {"2.459", 'e', 1, "2.5e+00"}, + {"3.459", 'e', 2, "3.46e+00"}, + {"4.459", 'e', 3, "4.459e+00"}, + {"5.459", 'e', 4, "5.4590e+00"}, + + {"1.459", 'E', 0, "1E+00"}, + {"2.459", 'E', 1, "2.5E+00"}, + {"3.459", 'E', 2, "3.46E+00"}, + {"4.459", 'E', 3, "4.459E+00"}, + {"5.459", 'E', 4, "5.4590E+00"}, + + {"1.459", 'f', 0, "1"}, + {"2.459", 'f', 1, "2.5"}, + {"3.459", 'f', 2, "3.46"}, + {"4.459", 'f', 3, "4.459"}, + {"5.459", 'f', 4, "5.4590"}, + + {"1.459", 'g', 0, "1"}, + {"2.459", 'g', 1, "2"}, + {"3.459", 'g', 2, "3.5"}, + {"4.459", 'g', 3, "4.46"}, + {"5.459", 'g', 4, "5.459"}, + + {"1459", 'g', 0, "1e+03"}, + {"2459", 'g', 1, "2e+03"}, + {"3459", 'g', 2, "3.5e+03"}, + {"4459", 'g', 3, "4.46e+03"}, + {"5459", 'g', 4, "5459"}, + + {"1459", 'G', 0, "1E+03"}, + {"2459", 'G', 1, "2E+03"}, + {"3459", 'G', 2, "3.5E+03"}, + {"4459", 'G', 3, "4.46E+03"}, + {"5459", 'G', 4, "5459"}, + + {"3", 'e', 40, "3.0000000000000000000000000000000000000000e+00"}, + {"3", 'f', 40, "3.0000000000000000000000000000000000000000"}, + {"3", 'g', 40, "3"}, + + {"3e40", 'e', 40, "3.0000000000000000000000000000000000000000e+40"}, + {"3e40", 'f', 4, "30000000000000000000000000000000000000000.0000"}, + {"3e40", 'g', 40, "3e+40"}, + + // TODO(gri) need tests for actual large Floats + + // These depend on the selected mantissa length to match strconv.FormatFloat. + // Disabled for now. + // {"0", 'b', 0, "0"}, + // {"-0", 'b', 0, "-0"}, + // {"1.0", 'b', 0, "4503599627370496p-52"}, + // {"-1.0", 'b', 0, "-4503599627370496p-52"}, + // {"4503599627370496", 'b', 0, "4503599627370496p+0"}, + + {"0", 'p', 0, "0"}, + {"-0", 'p', 0, "-0"}, + {"1024.0", 'p', 0, "0x.8p11"}, + {"-1024.0", 'p', 0, "-0x.8p11"}, + + // unsupported format + {"3.14", 'x', 0, "%x"}, + } { + f, _, err := ParseFloat(test.x, 0, 1000, ToNearestEven) + // TODO(gri) should we return io.EOF at the end? + if err != nil && err != io.EOF { + t.Errorf("%v: %s", test, err) + continue + } + + got := f.Format(test.format, test.prec) + if got != test.want { + t.Errorf("%v: got %s; want %s", test, got, test.want) + } + } +} -- 2.50.0