aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/big/rat_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/big/rat_test.go')
-rw-r--r--libgo/go/big/rat_test.go74
1 files changed, 62 insertions, 12 deletions
diff --git a/libgo/go/big/rat_test.go b/libgo/go/big/rat_test.go
index 8f42949..dbc5bb6 100644
--- a/libgo/go/big/rat_test.go
+++ b/libgo/go/big/rat_test.go
@@ -4,8 +4,12 @@
package big
-import "testing"
-
+import (
+ "bytes"
+ "fmt"
+ "gob"
+ "testing"
+)
var setStringTests = []struct {
in, out string
@@ -52,6 +56,27 @@ func TestRatSetString(t *testing.T) {
}
}
+func TestRatScan(t *testing.T) {
+ var buf bytes.Buffer
+ for i, test := range setStringTests {
+ x := new(Rat)
+ buf.Reset()
+ buf.WriteString(test.in)
+
+ _, err := fmt.Fscanf(&buf, "%v", x)
+ if err == nil != test.ok {
+ if test.ok {
+ t.Errorf("#%d error: %s", i, err.String())
+ } else {
+ t.Errorf("#%d expected error", i)
+ }
+ continue
+ }
+ if err == nil && x.RatString() != test.out {
+ t.Errorf("#%d got %s want %s", i, x.RatString(), test.out)
+ }
+ }
+}
var floatStringTests = []struct {
in string
@@ -59,12 +84,13 @@ var floatStringTests = []struct {
out string
}{
{"0", 0, "0"},
- {"0", 4, "0"},
+ {"0", 4, "0.0000"},
{"1", 0, "1"},
- {"1", 2, "1"},
+ {"1", 2, "1.00"},
{"-1", 0, "-1"},
{".25", 2, "0.25"},
{".25", 1, "0.3"},
+ {".25", 3, "0.250"},
{"-1/3", 3, "-0.333"},
{"-2/3", 4, "-0.6667"},
{"0.96", 1, "1.0"},
@@ -84,7 +110,6 @@ func TestFloatString(t *testing.T) {
}
}
-
func TestRatSign(t *testing.T) {
zero := NewRat(0, 1)
for _, a := range setStringTests {
@@ -98,7 +123,6 @@ func TestRatSign(t *testing.T) {
}
}
-
var ratCmpTests = []struct {
rat1, rat2 string
out int
@@ -126,7 +150,6 @@ func TestRatCmp(t *testing.T) {
}
}
-
func TestIsInt(t *testing.T) {
one := NewInt(1)
for _, a := range setStringTests {
@@ -140,7 +163,6 @@ func TestIsInt(t *testing.T) {
}
}
-
func TestRatAbs(t *testing.T) {
zero := NewRat(0, 1)
for _, a := range setStringTests {
@@ -158,7 +180,6 @@ func TestRatAbs(t *testing.T) {
}
}
-
type ratBinFun func(z, x, y *Rat) *Rat
type ratBinArg struct {
x, y, z string
@@ -175,7 +196,6 @@ func testRatBin(t *testing.T, i int, name string, f ratBinFun, a ratBinArg) {
}
}
-
var ratBinTests = []struct {
x, y string
sum, prod string
@@ -232,7 +252,6 @@ func TestRatBin(t *testing.T) {
}
}
-
func TestIssue820(t *testing.T) {
x := NewRat(3, 1)
y := NewRat(2, 1)
@@ -258,7 +277,6 @@ func TestIssue820(t *testing.T) {
}
}
-
var setFrac64Tests = []struct {
a, b int64
out string
@@ -280,3 +298,35 @@ func TestRatSetFrac64Rat(t *testing.T) {
}
}
}
+
+func TestRatGobEncoding(t *testing.T) {
+ var medium bytes.Buffer
+ enc := gob.NewEncoder(&medium)
+ dec := gob.NewDecoder(&medium)
+ for i, test := range gobEncodingTests {
+ for j := 0; j < 4; j++ {
+ medium.Reset() // empty buffer for each test case (in case of failures)
+ stest := test
+ if j&1 != 0 {
+ // negative numbers
+ stest = "-" + test
+ }
+ if j%2 != 0 {
+ // fractions
+ stest = stest + "." + test
+ }
+ var tx Rat
+ tx.SetString(stest)
+ if err := enc.Encode(&tx); err != nil {
+ t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err)
+ }
+ var rx Rat
+ if err := dec.Decode(&rx); err != nil {
+ t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err)
+ }
+ if rx.Cmp(&tx) != 0 {
+ t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx)
+ }
+ }
+ }
+}