aboutsummaryrefslogtreecommitdiff
path: root/src/math/big/float_test.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2015-03-13 17:24:30 -0700
committerRobert Griesemer <gri@golang.org>2015-03-14 00:48:53 +0000
commit23fd374bf22aa2eea9c07076061ef8cfbc6cf3d7 (patch)
tree13107ece1e2026dd83e99fceea9e0da36054dabb /src/math/big/float_test.go
parentb10021644145d7148ced74194b0d668a4428cc15 (diff)
downloadgo-23fd374bf22aa2eea9c07076061ef8cfbc6cf3d7.tar.xz
math/big: wrap Float.Cmp result in struct to prevent wrong use
Float.Cmp used to return a value < 0, 0, or > 0 depending on how arguments x, y compared against each other. With the possibility of NaNs, the result was changed into an Accuracy (to include Undef). Consequently, Float.Cmp results could still be compared for (in-) equality with 0, but comparing if < 0 or > 0 would provide the wrong answer w/o any obvious notice by the compiler. This change wraps Float.Cmp results into a struct and accessors are used to access the desired result. This prevents incorrect use. Change-Id: I34e6a6c1859251ec99b5cf953e82542025ace56f Reviewed-on: https://go-review.googlesource.com/7526 Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/math/big/float_test.go')
-rw-r--r--src/math/big/float_test.go20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/math/big/float_test.go b/src/math/big/float_test.go
index 86b1c6f7a1..683809bf56 100644
--- a/src/math/big/float_test.go
+++ b/src/math/big/float_test.go
@@ -207,7 +207,7 @@ func feq(x, y *Float) bool {
if x.IsNaN() || y.IsNaN() {
return x.IsNaN() && y.IsNaN()
}
- return x.Cmp(y) == 0 && x.IsNeg() == y.IsNeg()
+ return x.Cmp(y).Eql() && x.IsNeg() == y.IsNeg()
}
func TestFloatMantExp(t *testing.T) {
@@ -918,7 +918,7 @@ func TestFloatRat(t *testing.T) {
// inverse conversion
if res != nil {
got := new(Float).SetPrec(64).SetRat(res)
- if got.Cmp(x) != 0 {
+ if got.Cmp(x).Neq() {
t.Errorf("%s: got %s; want %s", test.x, got, x)
}
}
@@ -995,7 +995,7 @@ func TestFloatInc(t *testing.T) {
for i := 0; i < n; i++ {
x.Add(&x, &one)
}
- if x.Cmp(new(Float).SetInt64(n)) != 0 {
+ if x.Cmp(new(Float).SetInt64(n)).Neq() {
t.Errorf("prec = %d: got %s; want %d", prec, &x, n)
}
}
@@ -1036,14 +1036,14 @@ func TestFloatAdd(t *testing.T) {
got := new(Float).SetPrec(prec).SetMode(mode)
got.Add(x, y)
want := zbits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t+ %s %v\n\t= %s\n\twant %s",
i, prec, mode, x, xbits, y, ybits, got, want)
}
got.Sub(z, x)
want = ybits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t- %s %v\n\t= %s\n\twant %s",
i, prec, mode, z, zbits, x, xbits, got, want)
}
@@ -1137,7 +1137,7 @@ func TestFloatMul(t *testing.T) {
got := new(Float).SetPrec(prec).SetMode(mode)
got.Mul(x, y)
want := zbits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t* %s %v\n\t= %s\n\twant %s",
i, prec, mode, x, xbits, y, ybits, got, want)
}
@@ -1147,7 +1147,7 @@ func TestFloatMul(t *testing.T) {
}
got.Quo(z, x)
want = ybits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s %v\n\t/ %s %v\n\t= %s\n\twant %s",
i, prec, mode, z, zbits, x, xbits, got, want)
}
@@ -1230,7 +1230,7 @@ func TestIssue6866(t *testing.T) {
p.Mul(p, psix)
z2.Sub(two, p)
- if z1.Cmp(z2) != 0 {
+ if z1.Cmp(z2).Neq() {
t.Fatalf("prec %d: got z1 = %s != z2 = %s; want z1 == z2\n", prec, z1, z2)
}
if z1.Sign() != 0 {
@@ -1281,7 +1281,7 @@ func TestFloatQuo(t *testing.T) {
prec := uint(preci + d)
got := new(Float).SetPrec(prec).SetMode(mode).Quo(x, y)
want := bits.round(prec, mode)
- if got.Cmp(want) != 0 {
+ if got.Cmp(want).Neq() {
t.Errorf("i = %d, prec = %d, %s:\n\t %s\n\t/ %s\n\t= %s\n\twant %s",
i, prec, mode, x, y, got, want)
}
@@ -1462,7 +1462,7 @@ func TestFloatCmpSpecialValues(t *testing.T) {
}
for _, y := range args {
yy.SetFloat64(y)
- got := xx.Cmp(yy)
+ got := xx.Cmp(yy).Acc()
want := Undef
switch {
case x < y: