-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
100 lines (92 loc) · 2.23 KB
/
main_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
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"reflect"
"testing"
)
func TestFibonacciRecursive(t *testing.T) {
t.Parallel()
var testCases = []struct {
n int
expected int
}{
{1, 1},
{2, 1},
{3, 2},
{4, 3},
{5, 5},
{6, 8},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%#v", tc.n), func(t *testing.T) {
result := fib(tc.n)
if result != tc.expected {
t.Error("\nExpected:", tc.expected, "\nReceived: ", result)
}
})
}
m := make(map[int]int)
for _, tc := range testCases {
t.Run(fmt.Sprintf("%#v", tc.n), func(t *testing.T) {
result := fibMemo(tc.n, m)
if result != tc.expected {
t.Error("\nExpected:", tc.expected, "\nReceived: ", result)
}
})
}
}
func TestFibSeries(t *testing.T) {
var testCases = []struct {
n int
expected []int
}{
{n: 1, expected: []int{1}},
{n: 2, expected: []int{1, 1}},
{n: 3, expected: []int{1, 1, 2}},
{n: 4, expected: []int{1, 1, 2, 3}},
{n: 5, expected: []int{1, 1, 2, 3, 5}},
{n: 6, expected: []int{1, 1, 2, 3, 5, 8}},
{n: 7, expected: []int{1, 1, 2, 3, 5, 8, 13}},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%#v", tc.n), func(t *testing.T) {
result := fibSeriesRecursive(tc.n)
if !reflect.DeepEqual(tc.expected, result) {
t.Error("\nExpected:", tc.expected, "\nReceived: ", result)
}
})
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%#v", tc.n), func(t *testing.T) {
result := fibSeriesMemoization(tc.n)
if !reflect.DeepEqual(tc.expected, result) {
t.Error("\nExpected:", tc.expected, "\nReceived: ", result)
}
})
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%#v", tc.n), func(t *testing.T) {
result := fibDynamic(tc.n)
if !reflect.DeepEqual(tc.expected, result) {
t.Error("\nExpected:", tc.expected, "\nReceived: ", result)
}
})
}
}
// go test -v -run=NOMATCH -bench=.
// go test -v -run=NOMATCH -bench=BenchmarkFibonacciSeriesRecursive
func BenchmarkFibonacciSeriesRecursive(b *testing.B) {
for n := 0; n < b.N; n++ {
fibSeriesRecursive(20)
}
}
func BenchmarkFibonacciSeriesMemoization(b *testing.B) {
for n := 0; n < b.N; n++ {
fibSeriesMemoization(20)
}
}
func BenchmarkFibonacciSeriesDynamicProgramming(b *testing.B) {
for n := 0; n < b.N; n++ {
fibDynamic(20)
}
}