]> Cypherpunks repositories - gostls13.git/commitdiff
make bignum_test a test
authorRuss Cox <rsc@golang.org>
Mon, 24 Nov 2008 20:32:31 +0000 (12:32 -0800)
committerRuss Cox <rsc@golang.org>
Mon, 24 Nov 2008 20:32:31 +0000 (12:32 -0800)
R=gri
DELTA=967  (468 added, 499 deleted, 0 changed)
OCL=19906
CL=19912

src/lib/bignum_test.go [moved from test/bignum_test.go with 55% similarity]

similarity index 55%
rename from test/bignum_test.go
rename to src/lib/bignum_test.go
index 6ef65fa5342eb45d6ff17dccae58e90ff8a1c016..98fc7aed7deab3e0fa1954641843d10a8c7257fd 100644 (file)
@@ -2,12 +2,13 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// $G $D/$F.go && $L $F.$A && ./$A.out
+package bignum_test
 
-package main
-
-import Big "bignum"
-import Fmt "fmt"
+import (
+       bignum "bignum";
+       fmt "fmt";
+       testing "testing";
+)
 
 const (
        sa = "991";
@@ -18,92 +19,83 @@ const (
        sp = "170141183460469231731687303715884105727";  // prime
 )
 
-
-func NatFromString(s string, base uint, slen *int) *Big.Natural {
-       x, dummy := Big.NatFromString(s, base, slen);
+func NatFromString(s string, base uint, slen *int) *bignum.Natural {
+       x, dummy := bignum.NatFromString(s, base, slen);
        return x;
 }
 
 
-func IntFromString(s string, base uint, slen *int) *Big.Integer {
-       x, dummy := Big.IntFromString(s, base, slen);
+func IntFromString(s string, base uint, slen *int) *bignum.Integer {
+       x, dummy := bignum.IntFromString(s, base, slen);
        return x;
 }
 
 
-func RatFromString(s string, base uint, slen *int) *Big.Rational {
-       x, dummy := Big.RatFromString(s, base, slen);
+func RatFromString(s string, base uint, slen *int) *bignum.Rational {
+       x, dummy := bignum.RatFromString(s, base, slen);
        return x;
 }
 
 
 var (
-       nat_zero = Big.Nat(0);
-       nat_one = Big.Nat(1);
-       nat_two = Big.Nat(2);
+       nat_zero = bignum.Nat(0);
+       nat_one = bignum.Nat(1);
+       nat_two = bignum.Nat(2);
 
        a = NatFromString(sa, 10, nil);
        b = NatFromString(sb, 10, nil);
        c = NatFromString(sc, 10, nil);
        p = NatFromString(sp, 10, nil);
 
-       int_zero = Big.Int(0);
-       int_one = Big.Int(1);
-       int_two = Big.Int(2);
+       int_zero = bignum.Int(0);
+       int_one = bignum.Int(1);
+       int_two = bignum.Int(2);
 
        ip = IntFromString(sp, 10, nil);
 
-       rat_zero = Big.Rat(0, 1);
-       rat_half = Big.Rat(1, 2);
-       rat_one = Big.Rat(1, 1);
-       rat_two = Big.Rat(2, 1);
+       rat_zero = bignum.Rat(0, 1);
+       rat_half = bignum.Rat(1, 2);
+       rat_one = bignum.Rat(1, 1);
+       rat_two = bignum.Rat(2, 1);
 )
 
 
 var test_msg string;
+var tester *testing.T;
+
 func TEST(n uint, b bool) {
        if !b {
-               println("TEST failed: ", test_msg, "(", n, ")");
-               panic();
+               tester.Fatalf("TEST failed: %s (%d)", test_msg, n);
        }
 }
 
 
-func NAT_EQ(n uint, x, y *Big.Natural) {
+func NAT_EQ(n uint, x, y *bignum.Natural) {
        if x.Cmp(y) != 0 {
-               println("TEST failed:", test_msg, "(", n, ")");
-               println("x =", x.String());
-               println("y =", y.String());
-               panic();
+               tester.Fatalf("TEST failed: %s (%d)\nx = %v\ny = %v", test_msg, n, x, y);
        }
 }
 
 
-func INT_EQ(n uint, x, y *Big.Integer) {
+func INT_EQ(n uint, x, y *bignum.Integer) {
        if x.Cmp(y) != 0 {
-               println("TEST failed:", test_msg, "(", n, ")");
-               println("x =", x.String());
-               println("y =", y.String());
-               panic();
+               tester.Fatalf("TEST failed: %s (%d)\nx = %v\ny = %v", test_msg, n, x, y);
        }
 }
 
 
-func RAT_EQ(n uint, x, y *Big.Rational) {
+func RAT_EQ(n uint, x, y *bignum.Rational) {
        if x.Cmp(y) != 0 {
-               println("TEST failed:", test_msg, "(", n, ")");
-               println("x =", x.String());
-               println("y =", y.String());
-               panic();
+               tester.Fatalf("TEST failed: %s (%d)\nx = %v\ny = %v", test_msg, n, x, y);
        }
 }
 
-
-func NatConv() {
+export func TestNatConv(t *testing.T) {
+       tester = t;
        test_msg = "NatConvA";
-       NAT_EQ(0, a, Big.Nat(991));
-       NAT_EQ(1, b, Big.Fact(20));
-       NAT_EQ(2, c, Big.Fact(100));
+       NAT_EQ(0, a, bignum.Nat(991));
+       NAT_EQ(1, b, bignum.Fact(20));
+       NAT_EQ(2, c, bignum.Fact(100));
        TEST(3, a.String() == sa);
        TEST(4, b.String() == sb);
        TEST(5, c.String() == sc);
@@ -111,43 +103,45 @@ func NatConv() {
        test_msg = "NatConvB";
        var slen int;
        NAT_EQ(0, NatFromString("0", 0, nil), nat_zero);
-       NAT_EQ(1, NatFromString("123", 0, nil), Big.Nat(123));
-       NAT_EQ(2, NatFromString("077", 0, nil), Big.Nat(7*8 + 7));
-       NAT_EQ(3, NatFromString("0x1f", 0, nil), Big.Nat(1*16 + 15));
-       NAT_EQ(4, NatFromString("0x1fg", 0, &slen), Big.Nat(1*16 + 15));
+       NAT_EQ(1, NatFromString("123", 0, nil), bignum.Nat(123));
+       NAT_EQ(2, NatFromString("077", 0, nil), bignum.Nat(7*8 + 7));
+       NAT_EQ(3, NatFromString("0x1f", 0, nil), bignum.Nat(1*16 + 15));
+       NAT_EQ(4, NatFromString("0x1fg", 0, &slen), bignum.Nat(1*16 + 15));
        TEST(4, slen == 4);
 
        test_msg = "NatConvC";
-       t := c.Mul(c);
+       tmp := c.Mul(c);
        for base := uint(2); base <= 16; base++ {
-               NAT_EQ(base, NatFromString(t.ToString(base), base, nil), t);
+               NAT_EQ(base, NatFromString(tmp.ToString(base), base, nil), tmp);
        }
 
        test_msg = "NatConvD";
-       x := Big.Nat(100);
-       y, b := Big.NatFromString(Fmt.sprintf("%b", x), 2, nil);
+       x := bignum.Nat(100);
+       y, b := bignum.NatFromString(fmt.sprintf("%b", x), 2, nil);
        NAT_EQ(0, y, x);
 }
 
 
-func IntConv() {
+export func TestIntConv(t *testing.T) {
+       tester = t;
        test_msg = "IntConv";
        var slen int;
        INT_EQ(0, IntFromString("0", 0, nil), int_zero);
        INT_EQ(1, IntFromString("-0", 0, nil), int_zero);
-       INT_EQ(2, IntFromString("123", 0, nil), Big.Int(123));
-       INT_EQ(3, IntFromString("-123", 0, nil), Big.Int(-123));
-       INT_EQ(4, IntFromString("077", 0, nil), Big.Int(7*8 + 7));
-       INT_EQ(5, IntFromString("-077", 0, nil), Big.Int(-(7*8 + 7)));
-       INT_EQ(6, IntFromString("0x1f", 0, nil), Big.Int(1*16 + 15));
-       INT_EQ(7, IntFromString("-0x1f", 0, nil), Big.Int(-(1*16 + 15)));
-       INT_EQ(8, IntFromString("0x1fg", 0, &slen), Big.Int(1*16 + 15));
-       INT_EQ(9, IntFromString("-0x1fg", 0, &slen), Big.Int(-(1*16 + 15)));
+       INT_EQ(2, IntFromString("123", 0, nil), bignum.Int(123));
+       INT_EQ(3, IntFromString("-123", 0, nil), bignum.Int(-123));
+       INT_EQ(4, IntFromString("077", 0, nil), bignum.Int(7*8 + 7));
+       INT_EQ(5, IntFromString("-077", 0, nil), bignum.Int(-(7*8 + 7)));
+       INT_EQ(6, IntFromString("0x1f", 0, nil), bignum.Int(1*16 + 15));
+       INT_EQ(7, IntFromString("-0x1f", 0, nil), bignum.Int(-(1*16 + 15)));
+       INT_EQ(8, IntFromString("0x1fg", 0, &slen), bignum.Int(1*16 + 15));
+       INT_EQ(9, IntFromString("-0x1fg", 0, &slen), bignum.Int(-(1*16 + 15)));
        TEST(10, slen == 5);
 }
 
 
-func RatConv() {
+export func TestRatConv(t *testing.T) {
+       tester = t;
        test_msg = "RatConv";
        var slen int;
        RAT_EQ(0, RatFromString("0", 0, nil), rat_zero);
@@ -156,74 +150,64 @@ func RatConv() {
        RAT_EQ(3, RatFromString("0x14/10", 0, &slen), rat_two);
        TEST(4, slen == 7);
        RAT_EQ(5, RatFromString("0.", 0, nil), rat_zero);
-       RAT_EQ(6, RatFromString("0.001f", 10, nil), Big.Rat(1, 1000));
-       RAT_EQ(7, RatFromString("10101.0101", 2, nil), Big.Rat(0x155, 1<<4));
-       RAT_EQ(8, RatFromString("-0003.145926", 10, &slen), Big.Rat(-3145926, 1000000));
+       RAT_EQ(6, RatFromString("0.001f", 10, nil), bignum.Rat(1, 1000));
+       RAT_EQ(7, RatFromString("10101.0101", 2, nil), bignum.Rat(0x155, 1<<4));
+       RAT_EQ(8, RatFromString("-0003.145926", 10, &slen), bignum.Rat(-3145926, 1000000));
        TEST(9, slen == 12);
 }
 
 
-func Add(x, y *Big.Natural) *Big.Natural {
+func Add(x, y *bignum.Natural) *bignum.Natural {
        z1 := x.Add(y);
        z2 := y.Add(x);
        if z1.Cmp(z2) != 0 {
-               println("addition not symmetric");
-               println("x =", x.String());
-               println("y =", y.String());
-               panic();
+               tester.Fatalf("addition not symmetric:\n\tx = %v\n\ty = %t", x, y);
        }
        return z1;
 }
 
 
-func Sum(n uint, scale *Big.Natural) *Big.Natural {
+func Sum(n uint, scale *bignum.Natural) *bignum.Natural {
        s := nat_zero;
        for ; n > 0; n-- {
-               s = Add(s, Big.Nat(n).Mul(scale));
+               s = Add(s, bignum.Nat(n).Mul(scale));
        }
        return s;
 }
 
 
-func NatAdd() {
+export func TestNatAdd(t *testing.T) {
+       tester = t;
        test_msg = "NatAddA";
        NAT_EQ(0, Add(nat_zero, nat_zero), nat_zero);
        NAT_EQ(1, Add(nat_zero, c), c);
 
        test_msg = "NatAddB";
        for i := uint(0); i < 100; i++ {
-               t := Big.Nat(i);
+               t := bignum.Nat(i);
                NAT_EQ(i, Sum(i, c), t.Mul(t).Add(t).Shr(1).Mul(c));
        }
 }
 
 
-func Mul(x, y *Big.Natural) *Big.Natural {
+func Mul(x, y *bignum.Natural) *bignum.Natural {
        z1 := x.Mul(y);
        z2 := y.Mul(x);
        if z1.Cmp(z2) != 0 {
-               println("multiplication not symmetric");
-               println("x =", x.String());
-               println("y =", y.String());
-               panic();
+               tester.Fatalf("multiplication not symmetric:\n\tx = %v\n\ty = %t", x, y);
        }
        if !x.IsZero() && z1.Div(x).Cmp(y) != 0 {
-               println("multiplication/division not inverse (A)");
-               println("x =", x.String());
-               println("y =", y.String());
-               panic();
+               tester.Fatalf("multiplication/division not inverse (A):\n\tx = %v\n\ty = %t", x, y);
        }
        if !y.IsZero() && z1.Div(y).Cmp(x) != 0 {
-               println("multiplication/division not inverse (B)");
-               println("x =", x.String());
-               println("y =", y.String());
-               panic();
+               tester.Fatalf("multiplication/division not inverse (B):\n\tx = %v\n\ty = %t", x, y);
        }
        return z1;
 }
 
 
-func NatSub() {
+export func TestNatSub(t *testing.T) {
+       tester = t;
        test_msg = "NatSubA";
        NAT_EQ(0, nat_zero.Sub(nat_zero), nat_zero);
        NAT_EQ(1, c.Sub(nat_zero), c);
@@ -232,21 +216,22 @@ func NatSub() {
        for i := uint(0); i < 100; i++ {
                t := Sum(i, c);
                for j := uint(0); j <= i; j++ {
-                       t = t.Sub(Mul(Big.Nat(j), c));
+                       t = t.Sub(Mul(bignum.Nat(j), c));
                }
                NAT_EQ(i, t, nat_zero);
        }
 }
 
 
-func NatMul() {
+export func TestNatMul(t *testing.T) {
+       tester = t;
        test_msg = "NatMulA";
        NAT_EQ(0, Mul(c, nat_zero), nat_zero);
        NAT_EQ(1, Mul(c, nat_one), c);
 
        test_msg = "NatMulB";
-       NAT_EQ(0, b.Mul(Big.MulRange(0, 100)), nat_zero);
-       NAT_EQ(1, b.Mul(Big.MulRange(21, 100)), c);
+       NAT_EQ(0, b.Mul(bignum.MulRange(0, 100)), nat_zero);
+       NAT_EQ(1, b.Mul(bignum.MulRange(21, 100)), c);
 
        test_msg = "NatMulC";
        const n = 100;
@@ -257,24 +242,26 @@ func NatMul() {
 }
 
 
-func NatDiv() {
+export func TestNatDiv(t *testing.T) {
+       tester = t;
        test_msg = "NatDivA";
        NAT_EQ(0, c.Div(nat_one), c);
-       NAT_EQ(1, c.Div(Big.Nat(100)), Big.Fact(99));
+       NAT_EQ(1, c.Div(bignum.Nat(100)), bignum.Fact(99));
        NAT_EQ(2, b.Div(c), nat_zero);
        NAT_EQ(4, nat_one.Shl(100).Div(nat_one.Shl(90)), nat_one.Shl(10));
-       NAT_EQ(5, c.Div(b), Big.MulRange(21, 100));
+       NAT_EQ(5, c.Div(b), bignum.MulRange(21, 100));
 
        test_msg = "NatDivB";
        const n = 100;
-       p := Big.Fact(n);
+       p := bignum.Fact(n);
        for i := uint(0); i < n; i++ {
-               NAT_EQ(i, p.Div(Big.MulRange(1, i)), Big.MulRange(i+1, n));
+               NAT_EQ(i, p.Div(bignum.MulRange(1, i)), bignum.MulRange(i+1, n));
        }
 }
 
 
-func IntQuoRem() {
+export func TestIntQuoRem(t *testing.T) {
+       tester = t;
        test_msg = "IntQuoRem";
        type T struct { x, y, q, r int };
        a := []T{
@@ -289,8 +276,8 @@ func IntQuoRem() {
        };
        for i := uint(0); i < len(a); i++ {
                e := &a[i];
-               x, y := Big.Int(e.x).Mul(ip), Big.Int(e.y).Mul(ip);
-               q, r := Big.Int(e.q), Big.Int(e.r).Mul(ip);
+               x, y := bignum.Int(e.x).Mul(ip), bignum.Int(e.y).Mul(ip);
+               q, r := bignum.Int(e.q), bignum.Int(e.r).Mul(ip);
                qq, rr := x.QuoRem(y);
                INT_EQ(4*i+0, x.Quo(y), q);
                INT_EQ(4*i+1, x.Rem(y), r);
@@ -300,7 +287,8 @@ func IntQuoRem() {
 }
 
 
-func IntDivMod() {
+export func TestIntDivMod(t *testing.T) {
+       tester = t;
        test_msg = "IntDivMod";
        type T struct { x, y, q, r int };
        a := []T{
@@ -315,8 +303,8 @@ func IntDivMod() {
        };
        for i := uint(0); i < len(a); i++ {
                e := &a[i];
-               x, y := Big.Int(e.x).Mul(ip), Big.Int(e.y).Mul(ip);
-               q, r := Big.Int(e.q), Big.Int(e.r).Mul(ip);
+               x, y := bignum.Int(e.x).Mul(ip), bignum.Int(e.y).Mul(ip);
+               q, r := bignum.Int(e.q), bignum.Int(e.r).Mul(ip);
                qq, rr := x.DivMod(y);
                INT_EQ(4*i+0, x.Div(y), q);
                INT_EQ(4*i+1, x.Mod(y), r);
@@ -326,7 +314,8 @@ func IntDivMod() {
 }
 
 
-func NatMod() {
+export func TestNatMod(t *testing.T) {
+       tester = t;
        test_msg = "NatModA";
        for i := uint(0); ; i++ {
                d := nat_one.Shl(i);
@@ -341,7 +330,8 @@ func NatMod() {
 }
 
 
-func NatShift() {
+export func TestNatShift(t *testing.T) {
+       tester = t;
        test_msg = "NatShift1L";
        TEST(0, b.Shl(0).Cmp(b) == 0);
        TEST(1, c.Shl(1).Cmp(c) > 0);
@@ -358,7 +348,7 @@ func NatShift() {
        test_msg = "NatShift3L";
        {       const m = 3;
                p := b;
-               f := Big.Nat(1<<m);
+               f := bignum.Nat(1<<m);
                for i := uint(0); i < 100; i++ {
                        NAT_EQ(i, b.Shl(i*m), p);
                        p = Mul(p, f);
@@ -375,7 +365,8 @@ func NatShift() {
 }
 
 
-func IntShift() {
+export func TestIntShift(t *testing.T) {
+       tester = t;
        test_msg = "IntShift1L";
        TEST(0, ip.Shl(0).Cmp(ip) == 0);
        TEST(1, ip.Shl(1).Cmp(ip) > 0);
@@ -392,7 +383,7 @@ func IntShift() {
        test_msg = "IntShift3L";
        {       const m = 3;
                p := ip;
-               f := Big.Int(1<<m);
+               f := bignum.Int(1<<m);
                for i := uint(0); i < 100; i++ {
                        INT_EQ(i, ip.Shl(i*m), p);
                        p = p.Mul(f);
@@ -408,12 +399,13 @@ func IntShift() {
        }
 
        test_msg = "IntShift4R";
-       //INT_EQ(0, Big.Int(-43).Shr(1), Big.Int(-43 >> 1));
-       //INT_EQ(1, ip.Neg().Shr(10), ip.Neg().Div(Big.Int(1).Shl(10)));
+       //INT_EQ(0, bignum.Int(-43).Shr(1), bignum.Int(-43 >> 1));
+       //INT_EQ(1, ip.Neg().Shr(10), ip.Neg().Div(bignum.Int(1).Shl(10)));
 }
 
 
-func NatCmp() {
+export func TestNatCmp(t *testing.T) {
+       tester = t;
        test_msg = "NatCmp";
        TEST(0, a.Cmp(a) == 0);
        TEST(1, a.Cmp(b) < 0);
@@ -425,12 +417,13 @@ func NatCmp() {
 }
 
 
-func NatLog2() {
+export func TestNatLog2(t *testing.T) {
+       tester = t;
        test_msg = "NatLog2A";
        TEST(0, nat_one.Log2() == 0);
        TEST(1, nat_two.Log2() == 1);
-       TEST(2, Big.Nat(3).Log2() == 1);
-       TEST(3, Big.Nat(4).Log2() == 2);
+       TEST(2, bignum.Nat(3).Log2() == 1);
+       TEST(3, bignum.Nat(4).Log2() == 2);
        
        test_msg = "NatLog2B";
        for i := uint(0); i < 100; i++ {
@@ -439,14 +432,16 @@ func NatLog2() {
 }
 
 
-func NatGcd() {
+export func TestNatGcd(t *testing.T) {
+       tester = t;
        test_msg = "NatGcdA";
-       f := Big.Nat(99991);
-       NAT_EQ(0, b.Mul(f).Gcd(c.Mul(f)), Big.MulRange(1, 20).Mul(f));
+       f := bignum.Nat(99991);
+       NAT_EQ(0, b.Mul(f).Gcd(c.Mul(f)), bignum.MulRange(1, 20).Mul(f));
 }
 
 
-func NatPow() {
+export func TestNatPow(t *testing.T) {
+       tester = t;
        test_msg = "NatPowA";
        NAT_EQ(0, nat_two.Pow(0), nat_one);
 
@@ -457,13 +452,14 @@ func NatPow() {
 }
 
 
-func NatPop() {
+export func TestNatPop(t *testing.T) {
+       tester = t;
        test_msg = "NatPopA";
        TEST(0, nat_zero.Pop() == 0);
        TEST(1, nat_one.Pop() == 1);
-       TEST(2, Big.Nat(10).Pop() == 2);
-       TEST(3, Big.Nat(30).Pop() == 4);
-       TEST(4, Big.Nat(0x1248f).Shl(33).Pop() == 8);
+       TEST(2, bignum.Nat(10).Pop() == 2);
+       TEST(3, bignum.Nat(30).Pop() == 4);
+       TEST(4, bignum.Nat(0x1248f).Shl(33).Pop() == 8);
 
        test_msg = "NatPopB";
        for i := uint(0); i < 100; i++ {
@@ -471,30 +467,3 @@ func NatPop() {
        }
 }
 
-
-func main() {
-       // Naturals
-       NatConv();
-       NatAdd();
-       NatSub();
-       NatMul();
-       NatDiv();
-       NatMod();
-       NatShift();
-       NatCmp();
-       NatLog2();
-       NatGcd();
-       NatPow();
-       NatPop();
-
-       // Integers
-       // TODO add more tests
-       IntConv();
-       IntQuoRem();
-       IntDivMod();
-       IntShift();
-
-       // Rationals
-       // TODO add more tests
-       RatConv();
-}