1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // http://golang.org/issue/6849.
14 func TestPrimeSmall(t
*testing
.T
) {
15 for n
:= 2; n
< 10; n
++ {
16 p
, err
:= rand
.Prime(rand
.Reader
, n
)
18 t
.Fatalf("Can't generate %d-bit prime: %v", n
, err
)
21 t
.Fatalf("%v is not %d-bit", p
, n
)
23 if !p
.ProbablyPrime(32) {
24 t
.Fatalf("%v is not prime", p
)
29 // Test that passing bits < 2 causes Prime to return nil, error
30 func TestPrimeBitsLt2(t
*testing
.T
) {
31 if p
, err
:= rand
.Prime(rand
.Reader
, 1); p
!= nil || err
== nil {
32 t
.Errorf("Prime should return nil, error when called with bits < 2")
36 func TestInt(t
*testing
.T
) {
37 // start at 128 so the case of (max.BitLen() % 8) == 0 is covered
38 for n
:= 128; n
< 140; n
++ {
39 b
:= new(big
.Int
).SetInt64(int64(n
))
40 if i
, err
:= rand
.Int(rand
.Reader
, b
); err
!= nil {
41 t
.Fatalf("Can't generate random value: %v, %v", i
, err
)
46 func testIntPanics(t
*testing
.T
, b
*big
.Int
) {
48 if err
:= recover(); err
== nil {
49 t
.Errorf("Int should panic when called with max <= 0: %v", b
)
52 rand
.Int(rand
.Reader
, b
)
55 // Test that passing a new big.Int as max causes Int to panic
56 func TestIntEmptyMaxPanics(t
*testing
.T
) {
61 // Test that passing a negative value as max causes Int to panic
62 func TestIntNegativeMaxPanics(t
*testing
.T
) {
63 b
:= new(big
.Int
).SetInt64(int64(-1))