-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcoprime_test.go
65 lines (59 loc) · 934 Bytes
/
coprime_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package numbertheory
import (
"fmt"
"testing"
)
func ExampleCoprime() {
a := 2 * 3 * 5
b := 7 * 11 * 13
r := Coprime(a, b)
fmt.Printf("coprime(%d, %d) is %v.\n", a, b, r)
// Output:
// coprime(30, 1001) is true.
}
func TestCoprime(t *testing.T) {
compare := func(t *testing.T, a bool, b bool) {
if a != b {
t.Logf("expected %v, got %v", a, b)
t.FailNow()
}
}
cases := []struct {
a uint64
b uint64
r bool
}{
{
a: 3 * 5 * 7,
b: 7 * 11 * 13,
r: false,
},
{
a: 7 * 11 * 13,
b: 3 * 5 * 7,
r: false,
},
{
a: 3 * 5 * 7,
b: 11 * 13 * 17,
r: true,
},
{
a: 11 * 13 * 17,
b: 3 * 5 * 7,
r: true,
},
}
for k, c := range cases {
k, c := k, c
t.Run(fmt.Sprintf("test %v", k), func(t *testing.T) {
t.Parallel()
compare(t, c.r, Coprime(c.a, c.b))
})
}
}
func BenchmarkCoprime(b *testing.B) {
for i := 0; i < b.N; i++ {
Coprime(2*3*5, 5*7*11)
}
}