-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinomial_factorial_test.go
80 lines (71 loc) · 2.2 KB
/
binomial_factorial_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
package maths
import (
"fmt"
"math"
"testing"
)
func TestFactorial(t *testing.T) {
testCases := []struct {
input, expectedResult int
expectedError bool
}{
{-10, 3628800, false},
{-2, 2, false},
{-1, 1, false},
{0, 1, false},
{1, 1, false},
{2, 2, false},
{3, 6, false},
{10, 3628800, false},
{1000, 0, true},
{math.MinInt, 0, true},
}
for _, tC := range testCases {
testName := fmt.Sprintf("Input: %d", tC.input)
t.Run(testName, func(t *testing.T) {
actualResult, actualError := Factorial(tC.input)
checkResults(t, tC.expectedResult, tC.expectedError, actualResult, actualError)
})
}
}
func TestBinomial(t *testing.T) {
testCases := []struct {
n, k, expectedResult int
expectedError bool
}{
{10, -5, 252, false},
{-2, -1, 2, false},
{-1, 0, 1, false},
{0, 0, 1, false},
{1, 0, 1, false},
{1, 1, 1, false},
{2, 1, 2, false},
{2, 2, 1, false},
{10, 10, 1, false},
{10, 5, 252, false},
{math.MinInt, 1, 0, true},
{1, math.MinInt, 0, true},
{5, 6, 0, true},
{-5, -6, 0, true},
{1000, 1, 0, true},
// Can not currently test the case where fact(k) returns an error. If this were to return an error, then fact(absN) would have already thrown an error earlier.
// Can not currently test the case where fact(differenceOfAbsolute) returns an error. If this were to return an error, then fact(absN) would have already thrown an error earlier.
}
for _, tC := range testCases {
testName := fmt.Sprintf("Input: n:%d, k:%d", tC.n, tC.k)
t.Run(testName, func(t *testing.T) {
actualResult, actualError := Binomial(tC.n, tC.k)
checkResults(t, tC.expectedResult, tC.expectedError, actualResult, actualError)
})
}
}
func checkResults(t *testing.T, expectedResult int, expectedError bool, actualResult int, actualError error) {
// Check if an error was returned and matches if an error was expected.
if gotError := actualError != nil; gotError != expectedError {
t.Errorf("Expected error: %t, got error: %t, error: %v", expectedError, gotError, actualError)
}
// Check if the actual result matches the expected result
if actualResult != expectedResult {
t.Errorf("Expected result: %d, got result: %d", expectedResult, actualResult)
}
}