diff options
Diffstat (limited to 'src/pkg/big/int_test.go')
-rwxr-xr-x | src/pkg/big/int_test.go | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/pkg/big/int_test.go b/src/pkg/big/int_test.go index fc981e1da..9c19dd5da 100755 --- a/src/pkg/big/int_test.go +++ b/src/pkg/big/int_test.go @@ -8,6 +8,7 @@ import ( "bytes" "encoding/hex" "fmt" + "gob" "testing" "testing/quick" ) @@ -715,18 +716,25 @@ var composites = []string{ func TestProbablyPrime(t *testing.T) { + nreps := 20 + if testing.Short() { + nreps = 1 + } for i, s := range primes { p, _ := new(Int).SetString(s, 10) - if !ProbablyPrime(p, 20) { + if !ProbablyPrime(p, nreps) { t.Errorf("#%d prime found to be non-prime (%s)", i, s) } } for i, s := range composites { c, _ := new(Int).SetString(s, 10) - if ProbablyPrime(c, 20) { + if ProbablyPrime(c, nreps) { t.Errorf("#%d composite found to be prime (%s)", i, s) } + if testing.Short() { + break + } } } @@ -1053,3 +1061,41 @@ func TestModInverse(t *testing.T) { } } } + + +var gobEncodingTests = []string{ + "0", + "1", + "2", + "10", + "42", + "1234567890", + "298472983472983471903246121093472394872319615612417471234712061", +} + +func TestGobEncoding(t *testing.T) { + var medium bytes.Buffer + enc := gob.NewEncoder(&medium) + dec := gob.NewDecoder(&medium) + for i, test := range gobEncodingTests { + for j := 0; j < 2; j++ { + medium.Reset() // empty buffer for each test case (in case of failures) + stest := test + if j == 0 { + stest = "-" + test + } + var tx Int + tx.SetString(stest, 10) + if err := enc.Encode(&tx); err != nil { + t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err) + } + var rx Int + if err := dec.Decode(&rx); err != nil { + t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err) + } + if rx.Cmp(&tx) != 0 { + t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx) + } + } + } +} |