mirror of
https://github.com/golang/go
synced 2024-11-20 06:24: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 {
|
type setStringTest struct {
|
||||||
in, out string
|
in, out string
|
||||||
|
ok bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var setStringTests = []setStringTest{
|
var setStringTests = []setStringTest{
|
||||||
setStringTest{"0", "0"},
|
setStringTest{"0", "0", true},
|
||||||
setStringTest{"1", "1"},
|
setStringTest{"-0", "0", true},
|
||||||
setStringTest{"-1", "-1"},
|
setStringTest{"1", "1", true},
|
||||||
setStringTest{"2/4", "1/2"},
|
setStringTest{"-1", "-1", true},
|
||||||
setStringTest{".25", "1/4"},
|
setStringTest{in: "r", ok: false},
|
||||||
setStringTest{"-1/5", "-1/5"},
|
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) {
|
func TestRatSetString(t *testing.T) {
|
||||||
for i, test := range setStringTests {
|
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)
|
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 ratBinFun func(z, x, y *Rat) *Rat
|
||||||
type ratBinArg struct {
|
type ratBinArg struct {
|
||||||
x string
|
x, y, z string
|
||||||
y string
|
|
||||||
out string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRatBin(t *testing.T, f ratBinFun, a []ratBinArg) {
|
func testRatBin(t *testing.T, i int, name string, f ratBinFun, a ratBinArg) {
|
||||||
for i, test := range a {
|
x, _ := NewRat(0, 1).SetString(a.x)
|
||||||
x, _ := NewRat(0, 1).SetString(test.x)
|
y, _ := NewRat(0, 1).SetString(a.y)
|
||||||
y, _ := NewRat(0, 1).SetString(test.y)
|
z, _ := NewRat(0, 1).SetString(a.z)
|
||||||
expected, _ := NewRat(0, 1).SetString(test.out)
|
out := f(NewRat(0, 1), x, y)
|
||||||
out := f(NewRat(0, 1), x, y)
|
|
||||||
|
|
||||||
if out.Cmp(expected) != 0 {
|
if out.Cmp(z) != 0 {
|
||||||
t.Errorf("#%d got %s want %s", i, out, expected)
|
t.Errorf("%s #%d got %s want %s", name, i, out, z)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var ratAddTests = []ratBinArg{
|
type ratBinTest struct {
|
||||||
ratBinArg{"0", "0", "0"},
|
x, y string
|
||||||
ratBinArg{"0", "1", "1"},
|
sum, prod string
|
||||||
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"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRatAdd(t *testing.T) {
|
var ratBinTests = []ratBinTest{
|
||||||
testRatBin(t, (*Rat).Add, ratAddTests)
|
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{
|
arg = ratBinArg{test.y, test.x, test.sum}
|
||||||
ratBinArg{"0", "0", "0"},
|
testRatBin(t, i, "Add symmetric", (*Rat).Add, arg)
|
||||||
ratBinArg{"0", "1", "-1"},
|
|
||||||
ratBinArg{"-1", "0", "-1"},
|
arg = ratBinArg{test.sum, test.x, test.y}
|
||||||
ratBinArg{"-1", "1", "-2"},
|
testRatBin(t, i, "Sub", (*Rat).Sub, arg)
|
||||||
ratBinArg{"1", "1", "0"},
|
|
||||||
ratBinArg{"1/2", "1/3", "1/6"},
|
arg = ratBinArg{test.sum, test.y, test.x}
|
||||||
ratBinArg{"1/4", "1/3", "-1/12"},
|
testRatBin(t, i, "Sub symmetric", (*Rat).Sub, arg)
|
||||||
ratBinArg{"2/5", "-14/3", "76/15"},
|
|
||||||
ratBinArg{"4707/49292519774798173060", "-3367/70976135186689855734", "250026291202747299816479/1749296273614329067191168098769082663020"},
|
arg = ratBinArg{test.x, test.y, test.prod}
|
||||||
ratBinArg{"-27/133467566250814981", "-18/31750379913563777419", "-854857841473707320655/4237645934602118692642972629634714039"},
|
testRatBin(t, i, "Mul", (*Rat).Mul, arg)
|
||||||
ratBinArg{"27674141753240653/30123979153216", "-19948846211000086/637313996471", "618575745270541348005638912139/19198433543745179392300736"},
|
|
||||||
}
|
arg = ratBinArg{test.y, test.x, test.prod}
|
||||||
|
testRatBin(t, i, "Mul symmetric", (*Rat).Mul, arg)
|
||||||
func TestRatSub(t *testing.T) {
|
|
||||||
testRatBin(t, (*Rat).Sub, ratSubTests)
|
if test.x != "0" {
|
||||||
}
|
arg = ratBinArg{test.prod, test.x, test.y}
|
||||||
|
testRatBin(t, i, "Quo", (*Rat).Quo, arg)
|
||||||
|
}
|
||||||
var ratMulTests = []ratBinArg{
|
|
||||||
ratBinArg{"0", "0", "0"},
|
if test.y != "0" {
|
||||||
ratBinArg{"0", "1", "0"},
|
arg = ratBinArg{test.prod, test.y, test.x}
|
||||||
ratBinArg{"-1", "0", "0"},
|
testRatBin(t, i, "Quo symmetric", (*Rat).Quo, arg)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user