-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgcd_test.go
77 lines (72 loc) · 1.11 KB
/
gcd_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
66
67
68
69
70
71
72
73
74
75
76
77
package numbertheory
import (
"fmt"
"testing"
)
func ExampleGCD() {
gcd := GCD(48, 18)
fmt.Printf("The GCD of 18 and 48 is %v.", gcd)
// Output:
// The GCD of 18 and 48 is 6.
}
func TestGCD(t *testing.T) {
cases := []struct {
name string
a int
b int
expected string
}{
{
name: "48,18",
a: 48,
b: 18,
expected: "6",
},
{
name: "18,48",
a: 18,
b: 48,
expected: "6",
},
{
name: "-18,48",
a: -18,
b: 48,
expected: "-6",
},
{
name: "11,13",
a: 11,
b: 13,
expected: "1",
},
{
name: "0,11",
a: 0,
b: 11,
expected: "11",
},
{
name: "0,0",
a: 0,
b: 0,
expected: "1",
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
actual := GCD(c.a, c.b)
if fmt.Sprintf("%v", actual) != c.expected {
t.Logf("expected %v, got %v\n", c.expected, actual)
t.FailNow()
}
})
}
}
func BenchmarkGCD(b *testing.B) {
for i := 0; i < b.N; i++ {
GCD(i, i+30)
}
}