mirror of
https://github.com/golang/go
synced 2024-11-20 06:34:40 -07:00
big: Rat test improvements
R=gri CC=golang-dev https://golang.org/cl/1270041
This commit is contained in:
parent
d14baee96e
commit
751fc425ee
@ -9,22 +9,33 @@ import "testing"
|
||||
|
||||
type setStringTest struct {
|
||||
in, out string
|
||||
ok bool
|
||||
}
|
||||
|
||||
var setStringTests = []setStringTest{
|
||||
setStringTest{"0", "0"},
|
||||
setStringTest{"1", "1"},
|
||||
setStringTest{"-1", "-1"},
|
||||
setStringTest{"2/4", "1/2"},
|
||||
setStringTest{".25", "1/4"},
|
||||
setStringTest{"-1/5", "-1/5"},
|
||||
setStringTest{"0", "0", true},
|
||||
setStringTest{"-0", "0", true},
|
||||
setStringTest{"1", "1", true},
|
||||
setStringTest{"-1", "-1", true},
|
||||
setStringTest{in: "r", ok: false},
|
||||
setStringTest{in: "a/b", ok: false},
|
||||
setStringTest{in: "a.b", ok: false},
|
||||
setStringTest{"-0.1", "-1/10", true},
|
||||
setStringTest{"-.1", "-1/10", true},
|
||||
setStringTest{"2/4", "1/2", true},
|
||||
setStringTest{".25", "1/4", true},
|
||||
setStringTest{"-1/5", "-1/5", true},
|
||||
setStringTest{"884243222337379604041632732738665534", "884243222337379604041632732738665534", true},
|
||||
setStringTest{"53/70893980658822810696", "53/70893980658822810696", true},
|
||||
setStringTest{"106/141787961317645621392", "53/70893980658822810696", true},
|
||||
setStringTest{"204211327800791583.81095", "4084226556015831676219/20000", true},
|
||||
}
|
||||
|
||||
func TestRatSetString(t *testing.T) {
|
||||
for i, test := range setStringTests {
|
||||
x, _ := new(Rat).SetString(test.in)
|
||||
x, ok := new(Rat).SetString(test.in)
|
||||
|
||||
if x.String() != test.out {
|
||||
if ok != test.ok || ok && x.String() != test.out {
|
||||
t.Errorf("#%d got %s want %s", i, x.String(), test.out)
|
||||
}
|
||||
}
|
||||
@ -92,94 +103,75 @@ func TestRatCmp(t *testing.T) {
|
||||
|
||||
type ratBinFun func(z, x, y *Rat) *Rat
|
||||
type ratBinArg struct {
|
||||
x string
|
||||
y string
|
||||
out string
|
||||
x, y, z string
|
||||
}
|
||||
|
||||
func testRatBin(t *testing.T, f ratBinFun, a []ratBinArg) {
|
||||
for i, test := range a {
|
||||
x, _ := NewRat(0, 1).SetString(test.x)
|
||||
y, _ := NewRat(0, 1).SetString(test.y)
|
||||
expected, _ := NewRat(0, 1).SetString(test.out)
|
||||
out := f(NewRat(0, 1), x, y)
|
||||
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)
|
||||
|
||||
if out.Cmp(expected) != 0 {
|
||||
t.Errorf("#%d got %s want %s", i, out, expected)
|
||||
}
|
||||
if out.Cmp(z) != 0 {
|
||||
t.Errorf("%s #%d got %s want %s", name, i, out, z)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var ratAddTests = []ratBinArg{
|
||||
ratBinArg{"0", "0", "0"},
|
||||
ratBinArg{"0", "1", "1"},
|
||||
ratBinArg{"-1", "0", "-1"},
|
||||
ratBinArg{"-1", "1", "0"},
|
||||
ratBinArg{"1", "1", "2"},
|
||||
ratBinArg{"1/2", "1/2", "1"},
|
||||
ratBinArg{"1/4", "1/3", "7/12"},
|
||||
ratBinArg{"2/5", "-14/3", "-64/15"},
|
||||
ratBinArg{"4707/49292519774798173060", "-3367/70976135186689855734", "84058377121001851123459/1749296273614329067191168098769082663020"},
|
||||
ratBinArg{"-61204110018146728334/3", "-31052192278051565633/2", "-215564796870448153567/6"},
|
||||
type ratBinTest struct {
|
||||
x, y string
|
||||
sum, prod string
|
||||
}
|
||||
|
||||
func TestRatAdd(t *testing.T) {
|
||||
testRatBin(t, (*Rat).Add, ratAddTests)
|
||||
var ratBinTests = []ratBinTest{
|
||||
ratBinTest{"0", "0", "0", "0"},
|
||||
ratBinTest{"0", "1", "1", "0"},
|
||||
ratBinTest{"-1", "0", "-1", "0"},
|
||||
ratBinTest{"-1", "1", "0", "-1"},
|
||||
ratBinTest{"1", "1", "2", "1"},
|
||||
ratBinTest{"1/2", "1/2", "1", "1/4"},
|
||||
ratBinTest{"1/4", "1/3", "7/12", "1/12"},
|
||||
ratBinTest{"2/5", "-14/3", "-64/15", "-28/15"},
|
||||
ratBinTest{"4707/49292519774798173060", "-3367/70976135186689855734", "84058377121001851123459/1749296273614329067191168098769082663020", "-1760941/388732505247628681598037355282018369560"},
|
||||
ratBinTest{"-61204110018146728334/3", "-31052192278051565633/2", "-215564796870448153567/6", "950260896245257153059642991192710872711/3"},
|
||||
ratBinTest{"-854857841473707320655/4237645934602118692642972629634714039", "-18/31750379913563777419", "-27/133467566250814981", "15387441146526731771790/134546868362786310073779084329032722548987800600710485341"},
|
||||
ratBinTest{"618575745270541348005638912139/19198433543745179392300736", "-19948846211000086/637313996471", "27674141753240653/30123979153216", "-6169936206128396568797607742807090270137721977/6117715203873571641674006593837351328"},
|
||||
ratBinTest{"-3/26206484091896184128", "5/2848423294177090248", "15310893822118706237/9330894968229805033368778458685147968", "-5/24882386581946146755650075889827061248"},
|
||||
ratBinTest{"26946729/330400702820", "41563965/225583428284", "1238218672302860271/4658307703098666660055", "224002580204097/14906584649915733312176"},
|
||||
ratBinTest{"-8259900599013409474/7", "-84829337473700364773/56707961321161574960", "-468402123685491748914621885145127724451/396955729248131024720", "350340947706464153265156004876107029701/198477864624065512360"},
|
||||
ratBinTest{"575775209696864/1320203974639986246357", "29/712593081308", "410331716733912717985762465/940768218243776489278275419794956", "808/45524274987585732633"},
|
||||
ratBinTest{"1786597389946320496771/2066653520653241", "6269770/1992362624741777", "3559549865190272133656109052308126637/4117523232840525481453983149257", "8967230/3296219033"},
|
||||
ratBinTest{"-36459180403360509753/32150500941194292113930", "9381566963714/9633539", "301622077145533298008420642898530153/309723104686531919656937098270", "-3784609207827/3426986245"},
|
||||
}
|
||||
|
||||
func TestRatBin(t *testing.T) {
|
||||
for i, test := range ratBinTests {
|
||||
arg := ratBinArg{test.x, test.y, test.sum}
|
||||
testRatBin(t, i, "Add", (*Rat).Add, arg)
|
||||
|
||||
var ratSubTests = []ratBinArg{
|
||||
ratBinArg{"0", "0", "0"},
|
||||
ratBinArg{"0", "1", "-1"},
|
||||
ratBinArg{"-1", "0", "-1"},
|
||||
ratBinArg{"-1", "1", "-2"},
|
||||
ratBinArg{"1", "1", "0"},
|
||||
ratBinArg{"1/2", "1/3", "1/6"},
|
||||
ratBinArg{"1/4", "1/3", "-1/12"},
|
||||
ratBinArg{"2/5", "-14/3", "76/15"},
|
||||
ratBinArg{"4707/49292519774798173060", "-3367/70976135186689855734", "250026291202747299816479/1749296273614329067191168098769082663020"},
|
||||
ratBinArg{"-27/133467566250814981", "-18/31750379913563777419", "-854857841473707320655/4237645934602118692642972629634714039"},
|
||||
ratBinArg{"27674141753240653/30123979153216", "-19948846211000086/637313996471", "618575745270541348005638912139/19198433543745179392300736"},
|
||||
}
|
||||
|
||||
func TestRatSub(t *testing.T) {
|
||||
testRatBin(t, (*Rat).Sub, ratSubTests)
|
||||
}
|
||||
|
||||
|
||||
var ratMulTests = []ratBinArg{
|
||||
ratBinArg{"0", "0", "0"},
|
||||
ratBinArg{"0", "1", "0"},
|
||||
ratBinArg{"-1", "0", "0"},
|
||||
ratBinArg{"-1", "1", "-1"},
|
||||
ratBinArg{"1", "1", "1"},
|
||||
ratBinArg{"1/2", "1/2", "1/4"},
|
||||
ratBinArg{"1/4", "1/3", "1/12"},
|
||||
ratBinArg{"2/5", "-14/3", "-28/15"},
|
||||
ratBinArg{"-3/26206484091896184128", "5/2848423294177090248", "-5/24882386581946146755650075889827061248"},
|
||||
ratBinArg{"26946729/330400702820", "41563965/225583428284", "224002580204097/14906584649915733312176"},
|
||||
ratBinArg{"-8259900599013409474/7", "-84829337473700364773/56707961321161574960", "350340947706464153265156004876107029701/198477864624065512360"},
|
||||
}
|
||||
|
||||
func TestRatMul(t *testing.T) {
|
||||
testRatBin(t, (*Rat).Mul, ratMulTests)
|
||||
}
|
||||
|
||||
|
||||
var ratQuoTests = []ratBinArg{
|
||||
ratBinArg{"0", "1", "0"},
|
||||
ratBinArg{"0", "-1", "0"},
|
||||
ratBinArg{"-1", "1", "-1"},
|
||||
ratBinArg{"1", "1", "1"},
|
||||
ratBinArg{"1/2", "1/2", "1"},
|
||||
ratBinArg{"1/4", "1/3", "3/4"},
|
||||
ratBinArg{"2/5", "-14/3", "-3/35"},
|
||||
ratBinArg{"808/45524274987585732633", "29/712593081308", "575775209696864/1320203974639986246357"},
|
||||
ratBinArg{"8967230/3296219033", "6269770/1992362624741777", "1786597389946320496771/2066653520653241"},
|
||||
ratBinArg{"-3784609207827/3426986245", "9381566963714/9633539", "-36459180403360509753/32150500941194292113930"},
|
||||
}
|
||||
|
||||
func TestRatQuo(t *testing.T) {
|
||||
testRatBin(t, (*Rat).Quo, ratQuoTests)
|
||||
arg = ratBinArg{test.y, test.x, test.sum}
|
||||
testRatBin(t, i, "Add symmetric", (*Rat).Add, arg)
|
||||
|
||||
arg = ratBinArg{test.sum, test.x, test.y}
|
||||
testRatBin(t, i, "Sub", (*Rat).Sub, arg)
|
||||
|
||||
arg = ratBinArg{test.sum, test.y, test.x}
|
||||
testRatBin(t, i, "Sub symmetric", (*Rat).Sub, arg)
|
||||
|
||||
arg = ratBinArg{test.x, test.y, test.prod}
|
||||
testRatBin(t, i, "Mul", (*Rat).Mul, arg)
|
||||
|
||||
arg = ratBinArg{test.y, test.x, test.prod}
|
||||
testRatBin(t, i, "Mul symmetric", (*Rat).Mul, arg)
|
||||
|
||||
if test.x != "0" {
|
||||
arg = ratBinArg{test.prod, test.x, test.y}
|
||||
testRatBin(t, i, "Quo", (*Rat).Quo, arg)
|
||||
}
|
||||
|
||||
if test.y != "0" {
|
||||
arg = ratBinArg{test.prod, test.y, test.x}
|
||||
testRatBin(t, i, "Quo symmetric", (*Rat).Quo, arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user