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

Function TestInverse

math/modular/inverse_test.go:11–39  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

9import "fmt"
10
11func TestInverse(t *testing.T) {
12 testCases := []struct {
13 a int64
14 m int64
15 expectedValue int64
16 expectedError error
17 }{
18 {3, 11, 4, nil},
19 {10, 17, 12, nil},
20 {2, 6, 0, ErrorInverse},
21 {1, 0, 0, ErrorInverse},
22 }
23 for _, tc := range testCases {
24 testName := fmt.Sprintf("Testing a = %d and m = %d: ", tc.a, tc.m)
25 t.Run(testName, func(t *testing.T) {
26 inv, err := Inverse(tc.a, tc.m)
27 if err != tc.expectedError {
28 if tc.expectedError == nil {
29 t.Fatalf("Error was raised when it shouldn't: %v", err)
30 } else {
31 t.Fatalf("Error was not raised when it should")
32 }
33 }
34 if inv != tc.expectedValue {
35 t.Fatalf("expected: %d, got: %d", tc.expectedValue, inv)
36 }
37 })
38 }
39}

Callers

nothing calls this directly

Calls 1

InverseFunction · 0.85

Tested by

no test coverage detected