-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
gcd_test.go
49 lines (40 loc) · 967 Bytes
/
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
package gcd
import "testing"
type testFunction func(int64, int64) int64
var testCases = []struct {
name string
a int64
b int64
output int64
}{
{"gcd of 10 and 0", 10, 0, 10},
{"gcd of 98 and 56", 98, 56, 14},
{"gcd of 0 and 10", 0, 10, 10},
}
func TemplateTestGCD(t *testing.T, f testFunction) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := f(tc.a, tc.b)
if actual != tc.output {
t.Errorf("Expected GCD of %d and %d to be: %v, but got: %d", tc.a, tc.b, tc.output, actual)
}
})
}
}
func TestGCDRecursive(t *testing.T) {
TemplateTestGCD(t, Recursive)
}
func TestGCDIterative(t *testing.T) {
TemplateTestGCD(t, Iterative)
}
func TemplateBenchmarkGCD(b *testing.B, f testFunction) {
for i := 0; i < b.N; i++ {
f(98, 56)
}
}
func BenchmarkGCDRecursive(b *testing.B) {
TemplateBenchmarkGCD(b, Recursive)
}
func BenchmarkGCDIterative(b *testing.B) {
TemplateBenchmarkGCD(b, Iterative)
}