-
Notifications
You must be signed in to change notification settings - Fork 11
/
extendedgcd_test.go
56 lines (48 loc) · 1.45 KB
/
extendedgcd_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
package gcd
import "testing"
type testExtendedFunction func(int64, int64) (int64, int64, int64)
func TemplateTestExtendedGCD(t *testing.T, f testExtendedFunction) {
var testCasesExtended = []struct {
name string
a int64
b int64
gcd int64
x int64
y int64
}{
{"gcd of 10 and 0", 10, 0, 10, 1, 0},
{"gcd of 98 and 56", 98, 56, 14, -1, 2},
{"gcd of 0 and 10", 0, 10, 10, 0, 1},
}
for _, tc := range testCasesExtended {
t.Run(tc.name, func(t *testing.T) {
actualGcd, actualX, actualY := f(tc.a, tc.b)
if actualGcd != tc.gcd {
t.Errorf("Expected GCD of %d and %d to be: %v, but got: %d", tc.a, tc.b, tc.gcd, actualGcd)
}
if actualX != tc.x {
t.Errorf("Expected x satisfying %d * x + %d * y = gcd to be: %v, but got: %d", tc.a, tc.b, tc.x, actualX)
}
if actualY != tc.y {
t.Errorf("Expected y satisfying %d * x + %d * y = gcd to be: %v, but got: %d", tc.a, tc.b, tc.y, actualY)
}
})
}
}
func TestExtendedGCDRecursive(t *testing.T) {
TemplateTestExtendedGCD(t, ExtendedRecursive)
}
func TestExtendedGCDIterative(t *testing.T) {
TemplateTestExtendedGCD(t, ExtendedIterative)
}
func TemplateBenchmarkExtendedGCD(b *testing.B, f testExtendedFunction) {
for i := 0; i < b.N; i++ {
f(98, 56)
}
}
func BenchmarkExtendedGCDRecursive(b *testing.B) {
TemplateBenchmarkExtendedGCD(b, ExtendedRecursive)
}
func BenchmarkExtendedGCDIterative(b *testing.B) {
TemplateBenchmarkExtendedGCD(b, ExtendedIterative)
}