-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfactorial_test.go
52 lines (47 loc) · 946 Bytes
/
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
package combinatorics
import (
"fmt"
"testing"
)
func ExampleFactorial() {
n, _ := Factorial(5)
fmt.Printf("The factorial of 5 is %v.\n", n)
// Output:
// The factorial of 5 is 120.
}
func TestFactorial(t *testing.T) {
compare := func(t *testing.T, a uint64, b uint64) {
if a != b {
t.Logf("expected %v, got %v", a, b)
t.FailNow()
}
}
cases := map[int64]struct {
expected uint64
err bool
}{
-1: {0, true},
0: {1, false},
5: {120, false},
12: {479001600, false},
13: {6227020800, false},
50: {0, true},
}
for n, c := range cases {
n, c := n, c
t.Run(fmt.Sprintf("test %v", n), func(t *testing.T) {
t.Parallel()
actual, err := Factorial(n)
compare(t, c.expected, actual)
if c.err != (err != nil) {
t.Logf("expected error %v, got error %v", c.err, err != nil)
t.FailNow()
}
})
}
}
func BenchmarkFactorial(b *testing.B) {
for i := 0; i < b.N; i++ {
Factorial(i)
}
}