diff options
Diffstat (limited to 'libgo/go/big/rat_test.go')
-rw-r--r-- | libgo/go/big/rat_test.go | 124 |
1 files changed, 104 insertions, 20 deletions
diff --git a/libgo/go/big/rat_test.go b/libgo/go/big/rat_test.go index a2b9055..a95e5fe 100644 --- a/libgo/go/big/rat_test.go +++ b/libgo/go/big/rat_test.go @@ -11,6 +11,46 @@ import ( "testing" ) +func TestZeroRat(t *testing.T) { + var x, y, z Rat + y.SetFrac64(0, 42) + + if x.Cmp(&y) != 0 { + t.Errorf("x and y should be both equal and zero") + } + + if s := x.String(); s != "0/1" { + t.Errorf("got x = %s, want 0/1", s) + } + + if s := x.RatString(); s != "0" { + t.Errorf("got x = %s, want 0", s) + } + + z.Add(&x, &y) + if s := z.RatString(); s != "0" { + t.Errorf("got x+y = %s, want 0", s) + } + + z.Sub(&x, &y) + if s := z.RatString(); s != "0" { + t.Errorf("got x-y = %s, want 0", s) + } + + z.Mul(&x, &y) + if s := z.RatString(); s != "0" { + t.Errorf("got x*y = %s, want 0", s) + } + + // check for division by zero + defer func() { + if s := recover(); s == nil || s.(string) != "division by zero" { + panic(s) + } + }() + z.Quo(&x, &y) +} + var setStringTests = []struct { in, out string ok bool @@ -50,8 +90,14 @@ func TestRatSetString(t *testing.T) { for i, test := range setStringTests { x, ok := new(Rat).SetString(test.in) - if ok != test.ok || ok && x.RatString() != test.out { - t.Errorf("#%d got %s want %s", i, x.RatString(), test.out) + if ok { + if !test.ok { + t.Errorf("#%d SetString(%q) expected failure", i, test.in) + } else if x.RatString() != test.out { + t.Errorf("#%d SetString(%q) got %s want %s", i, test.in, x.RatString(), test.out) + } + } else if x != nil { + t.Errorf("#%d SetString(%q) got %p want nil", i, test.in, x) } } } @@ -113,8 +159,10 @@ func TestFloatString(t *testing.T) { func TestRatSign(t *testing.T) { zero := NewRat(0, 1) for _, a := range setStringTests { - var x Rat - x.SetString(a.in) + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } s := x.Sign() e := x.Cmp(zero) if s != e { @@ -153,29 +201,65 @@ func TestRatCmp(t *testing.T) { func TestIsInt(t *testing.T) { one := NewInt(1) for _, a := range setStringTests { - var x Rat - x.SetString(a.in) + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } i := x.IsInt() e := x.Denom().Cmp(one) == 0 if i != e { - t.Errorf("got %v; want %v for z = %v", i, e, &x) + t.Errorf("got IsInt(%v) == %v; want %v", x, i, e) } } } func TestRatAbs(t *testing.T) { - zero := NewRat(0, 1) + zero := new(Rat) for _, a := range setStringTests { - var z Rat - z.SetString(a.in) - var e Rat - e.Set(&z) + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } + e := new(Rat).Set(x) if e.Cmp(zero) < 0 { - e.Sub(zero, &e) + e.Sub(zero, e) + } + z := new(Rat).Abs(x) + if z.Cmp(e) != 0 { + t.Errorf("got Abs(%v) = %v; want %v", x, z, e) + } + } +} + +func TestRatNeg(t *testing.T) { + zero := new(Rat) + for _, a := range setStringTests { + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } + e := new(Rat).Sub(zero, x) + z := new(Rat).Neg(x) + if z.Cmp(e) != 0 { + t.Errorf("got Neg(%v) = %v; want %v", x, z, e) + } + } +} + +func TestRatInv(t *testing.T) { + zero := new(Rat) + for _, a := range setStringTests { + x, ok := new(Rat).SetString(a.in) + if !ok { + continue + } + if x.Cmp(zero) == 0 { + continue // avoid division by zero } - z.Abs(&z) - if z.Cmp(&e) != 0 { - t.Errorf("got z = %v; want %v", &z, &e) + e := new(Rat).SetFrac(x.Denom(), x.Num()) + z := new(Rat).Inv(x) + if z.Cmp(e) != 0 { + t.Errorf("got Inv(%v) = %v; want %v", x, z, e) } } } @@ -186,10 +270,10 @@ type ratBinArg struct { } func testRatBin(t *testing.T, i int, name string, f ratBinFun, a ratBinArg) { - x, _ := NewRat(0, 1).SetString(a.x) - y, _ := NewRat(0, 1).SetString(a.y) - z, _ := NewRat(0, 1).SetString(a.z) - out := f(NewRat(0, 1), x, y) + x, _ := new(Rat).SetString(a.x) + y, _ := new(Rat).SetString(a.y) + z, _ := new(Rat).SetString(a.z) + out := f(new(Rat), x, y) if out.Cmp(z) != 0 { t.Errorf("%s #%d got %s want %s", name, i, out, z) |