forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexponentiation_test.go
89 lines (83 loc) · 2.01 KB
/
exponentiation_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
78
79
80
81
82
83
84
85
86
87
88
89
// exponentiation_test.go
// description: Test for ModularExponentiation
// author(s) [Taj](https://github.com/tjgurwara99)
// see exponentiation.go
package modular
import "testing"
type cases struct {
name string
description string
base int64
exponent int64
mod int64
expected int64
expectedError error
}
var testCases = []cases{
{
name: "Test 1",
description: "Test 1: 3^6 % 3 == 0",
base: 3,
exponent: 6,
mod: 3,
expected: 0,
expectedError: nil,
},
{
name: "Test 2",
description: "Test 2: 33^60 % 25 == 1",
base: 33,
exponent: 60,
mod: 25,
expected: 1,
expectedError: nil,
},
{
name: "Test 3",
description: "Test 3: 17^60 % 23 == 2",
base: 17,
exponent: 60,
mod: 23,
expected: 2,
expectedError: nil,
},
{
name: "Test 4",
description: "Test 4: 17^60 % 1 == 0", // handling result when we get mod = 1
base: 17,
exponent: 60,
mod: 1,
expected: 0,
expectedError: nil,
},
{
name: "Error test 1",
description: "Testing whether we receive the expected errors gracefully",
base: 50,
exponent: -1,
mod: 2,
expected: -1,
expectedError: ErrorNegativeExponent,
},
}
func TestExponentiation(t *testing.T) {
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
result, err := Exponentiation(test.base, test.exponent, test.mod)
if err != test.expectedError {
t.Logf("Test Failed for %s", test.name)
t.Logf("Unexpected error occurred")
t.Errorf("Expected error: %v, Received error: %v", test.expectedError, err)
}
if result != test.expected {
t.Logf("Test Failed for %s", test.description)
t.Fatalf("Expected: %d, Received: %d", test.expected, result)
}
})
}
}
func BenchmarkExponentiation(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = Exponentiation(17, 60, 23)
}
}