-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacci_test.go
155 lines (133 loc) · 3.9 KB
/
fibonacci_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Package fibonacci implements recursive, closure and rounding algorithms
//
package fibonacci
import (
"math/rand"
"testing"
)
func testFillSequence(t *testing.T, index int, wantNumber int) {
var sequence []int
number := FillSequence(index, &sequence)
if number != wantNumber {
t.Fatalf("FillSequence(%d) returns %v, want %v", index, number, wantNumber)
}
}
func testGetNumber(t *testing.T, index int, want int) {
number := GetNumber(index)
if number != want {
t.Fatalf("GetNumber(%d) returns %v, want %v", index, number, want)
}
}
func testComputeApproximateNumber(t *testing.T, index int, want int) {
number := ComputeApproximateNumber(index)
if number != want {
t.Fatalf("ComputeApproximateNumber(%d) returns %v, want %v", index, number, want)
}
}
func TestFillSequence(t *testing.T) {
testFillSequence(t, -1, -1)
testFillSequence(t, 0, 0)
testFillSequence(t, 1, 1)
testFillSequence(t, 2, 1)
testFillSequence(t, 10, 55)
testFillSequence(t, 25, 75025)
testFillSequence(t, 50, 12586269025)
testFillSequence(t, 78, 8944394323791464)
testFillSequence(t, 92, 7540113804746346429)
testFillSequence(t, 93, -1)
}
func TestGetNumber(t *testing.T) {
testGetNumber(t, -1, -1)
testGetNumber(t, 0, 0)
testGetNumber(t, 1, 1)
testGetNumber(t, 2, 1)
testGetNumber(t, 10, 55)
testGetNumber(t, 25, 75025)
testGetNumber(t, 50, 12586269025)
testGetNumber(t, 78, 8944394323791464)
testGetNumber(t, 92, 7540113804746346429)
testGetNumber(t, 93, -1)
}
func TestComputeApproximateNumber(t *testing.T) {
testComputeApproximateNumber(t, -1, -1)
testComputeApproximateNumber(t, 0, 0)
testComputeApproximateNumber(t, 1, 1)
testComputeApproximateNumber(t, 2, 1)
testComputeApproximateNumber(t, 10, 55)
testComputeApproximateNumber(t, 25, 75025)
testComputeApproximateNumber(t, 50, 12586269025)
testComputeApproximateNumber(t, 78, 8944394323791464)
testComputeApproximateNumber(t, 79, -1)
}
func benchmarkFillSequence(b *testing.B, size int, want int) {
var number int
for i := 1; i <= b.N; i++ {
var sequence []int
number = FillSequence(size, &sequence)
if number != want {
b.Fatalf("FillSequence returns %d, want %d", number, want)
}
}
}
func benchmarkGetNumber(b *testing.B, size int, want int) {
var number int
for i := 1; i <= b.N; i++ {
number = GetNumber(size)
if number != want {
b.Fatalf("GetNumber returns %d, want %d", number, want)
}
}
}
func benchmarkGetNumberFromSequence(b *testing.B, size int) {
var controlSequence []int
FillSequence(size, &controlSequence)
var number int
var sequence []int
for i := 1; i <= b.N; i++ {
index := rand.Intn(size)
number = FillSequence(index, &sequence)
if number != controlSequence[index] {
b.Fatalf("GetNumber returns %d, want %d", number, controlSequence[index])
}
}
}
func benchmarkComputeApproximateNumber(b *testing.B, size int, want int) {
var number int
for i := 1; i <= b.N; i++ {
number = ComputeApproximateNumber(size)
if number != want {
b.Fatalf("ComputeApproximateNumber returns %d, want %d", number, want)
}
}
}
func BenchmarkFillSequence10(b *testing.B) {
benchmarkFillSequence(b, 10, 55)
}
func BenchmarkFillSequence25(b *testing.B) {
benchmarkFillSequence(b, 25, 75025)
}
func BenchmarkFillSequence50(b *testing.B) {
benchmarkFillSequence(b, 50, 12586269025)
}
func BenchmarkFillSequence_GetRand50(b *testing.B) {
benchmarkGetNumberFromSequence(b, 50)
}
func BenchmarkGetNumber10(b *testing.B) {
benchmarkGetNumber(b, 10, 55)
}
func BenchmarkGetNumber25(b *testing.B) {
benchmarkGetNumber(b, 25, 75025)
}
func BenchmarkGetNumber50(b *testing.B) {
benchmarkGetNumber(b, 50, 12586269025)
}
func BenchmarkComputeApproximateNumber10(b *testing.B) {
benchmarkComputeApproximateNumber(b, 10, 55)
}
func BenchmarkComputeApproximateNumber25(b *testing.B) {
benchmarkComputeApproximateNumber(b, 25, 75025)
}
func BenchmarkComputeApproximateNumber50(b *testing.B) {
benchmarkComputeApproximateNumber(b, 50, 12586269025)
}