]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: ensure correct test input
authorRobert Griesemer <gri@golang.org>
Tue, 12 Nov 2019 17:48:38 +0000 (09:48 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 12 Nov 2019 18:52:52 +0000 (18:52 +0000)
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: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/math/big/nat_test.go

index da34e95c1fac86488ccb4dc138b0223effa72e5c..cbbaf02771a2728d06f19f1a6c9106121ecfe097 100644 (file)
@@ -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 {
        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) {
        mulx := rndNat(1e4)
        muly := rndNat(1e4)
@@ -747,18 +759,22 @@ func TestNatDiv(t *testing.T) {
        }
        for _, i := range sizes {
                for _, j := range sizes {
-                       a := rndNat(i)
-                       b := rndNat(j)
+                       a := rndNat1(i)
+                       b := rndNat1(j)
+                       // the test requires b >= 2
+                       if len(b) == 1 && b[0] == 1 {
+                               b[0] = 2
+                       }
                        x := nat(nil).mul(a, b)
                        addVW(x, x, 1)
 
                        var q, r nat
                        q, r = q.div(r, x, b)
                        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 {
-                               t.Fatal("wrong remainder")
+                               t.Fatalf("wrong remainder: got %s; want 1 for %s/%s", r.utoa(10), x.utoa(10), b.utoa(10))
                        }
                }
        }