forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmillerrabinprimalitytest_test.go
41 lines (37 loc) · 1.04 KB
/
millerrabinprimalitytest_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// millerrabinprimality_test.go
// description: Test for Miller-Rabin Primality Test
// author(s) [Taj](https://github.com/tjgurwara99)
// see millerrabinprimalitytest.go
package prime
import "testing"
func TestMillerRabinTest(t *testing.T) {
var tests = []struct {
name string
input int64
expected bool
rounds int64
err error
}{
{"smallest prime", 2, true, 5, nil},
{"random prime", 3, true, 5, nil},
{"neither prime nor composite", 1, false, 5, nil},
{"random non-prime", 10, false, 5, nil},
{"another random prime", 23, true, 5, nil},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output, err := MillerRabinTest(test.input, test.rounds)
if err != test.err {
t.Errorf("For input: %d, unexpected error: %v, expected error: %v", test.input, err, test.err)
}
if output != test.expected {
t.Errorf("For input: %d, expected: %v", test.input, output)
}
})
}
}
func BenchmarkMillerRabinPrimalityTest(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = MillerRabinTest(23, 5)
}
}