MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / TemplateTestExtendedGCD

Function TemplateTestExtendedGCD

math/gcd/extendedgcd_test.go:7–34  ·  view source on GitHub ↗
(t *testing.T, f testExtendedFunction)

Source from the content-addressed store, hash-verified

5type testExtendedFunction func(int64, int64) (int64, int64, int64)
6
7func TemplateTestExtendedGCD(t *testing.T, f testExtendedFunction) {
8 var testCasesExtended = []struct {
9 name string
10 a int64
11 b int64
12 gcd int64
13 x int64
14 y int64
15 }{
16 {"gcd of 10 and 0", 10, 0, 10, 1, 0},
17 {"gcd of 98 and 56", 98, 56, 14, -1, 2},
18 {"gcd of 0 and 10", 0, 10, 10, 0, 1},
19 }
20 for _, tc := range testCasesExtended {
21 t.Run(tc.name, func(t *testing.T) {
22 actualGcd, actualX, actualY := f(tc.a, tc.b)
23 if actualGcd != tc.gcd {
24 t.Errorf("Expected GCD of %d and %d to be: %v, but got: %d", tc.a, tc.b, tc.gcd, actualGcd)
25 }
26 if actualX != tc.x {
27 t.Errorf("Expected x satisfying %d * x + %d * y = gcd to be: %v, but got: %d", tc.a, tc.b, tc.x, actualX)
28 }
29 if actualY != tc.y {
30 t.Errorf("Expected y satisfying %d * x + %d * y = gcd to be: %v, but got: %d", tc.a, tc.b, tc.y, actualY)
31 }
32 })
33 }
34}
35
36func TestExtendedGCDRecursive(t *testing.T) {
37 TemplateTestExtendedGCD(t, ExtendedRecursive)

Callers 2

TestExtendedGCDRecursiveFunction · 0.85
TestExtendedGCDIterativeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected