Commit 1fe33e3c authored by Robert Griesemer's avatar Robert Griesemer

math/big: ensure correct test input

There is a (theoretical, but possible) chance that the
random number values a, b used for TestDiv are 0 or 1,
in which case the test would fail.

This CL makes sure that a >= 1 and b >= 2 at all times.

Fixes #35523.

Change-Id: I6451feb94241249516a821cd0066e95a0c65b0ed
Reviewed-on: https://go-review.googlesource.com/c/go/+/206818
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 8c5dbba0
...@@ -192,10 +192,22 @@ func TestMulUnbalanced(t *testing.T) { ...@@ -192,10 +192,22 @@ func TestMulUnbalanced(t *testing.T) {
} }
} }
// rndNat returns a random nat value >= 0 of (usually) n words in length.
// In extremely unlikely cases it may be smaller than n words if the top-
// most words are 0.
func rndNat(n int) nat { func rndNat(n int) nat {
return nat(rndV(n)).norm() return nat(rndV(n)).norm()
} }
// rndNat1 is like rndNat but the result is guaranteed to be > 0.
func rndNat1(n int) nat {
x := nat(rndV(n)).norm()
if len(x) == 0 {
x.setWord(1)
}
return x
}
func BenchmarkMul(b *testing.B) { func BenchmarkMul(b *testing.B) {
mulx := rndNat(1e4) mulx := rndNat(1e4)
muly := rndNat(1e4) muly := rndNat(1e4)
...@@ -747,18 +759,22 @@ func TestNatDiv(t *testing.T) { ...@@ -747,18 +759,22 @@ func TestNatDiv(t *testing.T) {
} }
for _, i := range sizes { for _, i := range sizes {
for _, j := range sizes { for _, j := range sizes {
a := rndNat(i) a := rndNat1(i)
b := rndNat(j) b := rndNat1(j)
// the test requires b >= 2
if len(b) == 1 && b[0] == 1 {
b[0] = 2
}
x := nat(nil).mul(a, b) x := nat(nil).mul(a, b)
addVW(x, x, 1) addVW(x, x, 1)
var q, r nat var q, r nat
q, r = q.div(r, x, b) q, r = q.div(r, x, b)
if q.cmp(a) != 0 { if q.cmp(a) != 0 {
t.Fatal("wrong quotient", i, j) t.Fatalf("wrong quotient: got %s; want %s for %s/%s", q.utoa(10), a.utoa(10), x.utoa(10), b.utoa(10))
} }
if len(r) != 1 || r[0] != 1 { if len(r) != 1 || r[0] != 1 {
t.Fatal("wrong remainder") t.Fatalf("wrong remainder: got %s; want 1 for %s/%s", r.utoa(10), x.utoa(10), b.utoa(10))
} }
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment