-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfv_test.go
73 lines (63 loc) · 1.35 KB
/
fv_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
package financial
import (
"fmt"
"testing"
)
var fvTestCases = []struct {
presentValue float64
interestRate float64
periods float64
compoundedAnnually bool
want float64
}{
{
presentValue: 100.0,
interestRate: 0.07,
periods: 10.0,
compoundedAnnually: false,
want: 170.00000000000003,
},
{
presentValue: 100.0,
interestRate: 0.07,
periods: 10.0,
compoundedAnnually: true,
want: 196.71513572895657,
},
}
func TestFV(t *testing.T) {
for _, tt := range fvTestCases {
got := FV(tt.presentValue, tt.interestRate, tt.compoundedAnnually, tt.periods)
if got != tt.want {
t.Errorf("FV(%v, %v, %v) = %v; want %v",
tt.presentValue,
tt.interestRate,
tt.periods,
got,
tt.want,
)
}
}
}
var fvBenchResult float64
func BenchmarkFV(b *testing.B) {
var r float64
presentValue := 100.0
interestRate := 0.07
periods := 10.0
compoundedAnnually := false
for n := 0; n < b.N; n++ {
r = FV(presentValue, interestRate, compoundedAnnually, periods)
}
fvBenchResult = r
}
func ExampleFV() {
presentValue := 100.0
interestRate := 0.07
periods := 10.0
compoundedAnnually := false
fv := FV(presentValue, interestRate, compoundedAnnually, periods)
fmt.Printf("FV is: %f", fv)
// Output:
// FV is: 170.000000
}