-
Notifications
You must be signed in to change notification settings - Fork 0
/
cashflows_test.go
147 lines (136 loc) · 4.34 KB
/
cashflows_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
// Copyright (c) 2019-2023 The goinvest/fin developers. All rights reserved.
// Project site: https://github.com/goinvest/fin
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE file for the project.
package cf
import (
"math"
"testing"
)
const tolerance = 0.000001
func TestMIRR(t *testing.T) {
testCases := []struct {
cashflows []float64
costOfCapital float64
expected float64
}{
{[]float64{-1000, 500, 400, 300, 100}, 0.10, 0.121063},
{[]float64{-1000, 100, 300, 400, 600}, 0.10, 0.113281},
{[]float64{1000, 100, 300, 400, 600}, 0.10, math.Inf(1)},
}
for _, tc := range testCases {
mirr := MIRR(tc.cashflows, tc.costOfCapital)
if !math.IsInf(mirr, 1) && !almostEqual(tc.expected, mirr) {
t.Errorf("MIRR calculated = %f, expected = %f", mirr, tc.expected)
} else if math.IsInf(mirr, 1) && !math.IsInf(tc.expected, 1) {
t.Errorf("MIRR = +Inf, expected = %f", tc.expected)
}
}
}
func TestIRR(t *testing.T) {
testCases := []struct {
cashflows []float64
expected float64
options IRROptions
}{
{[]float64{-1000, 500, 400, 300, 100}, 0.144888, IRROptions{1e-5, 10}},
{[]float64{-1000, 100, 300, 400, 600}, 0.117906, IRROptions{1e-5, 10}},
}
for _, tc := range testCases {
irr := IRR(tc.cashflows, tc.options)
if !math.IsNaN(irr) && !almostEqual(tc.expected, irr) {
t.Errorf("IRR calculated = %f, expected = %f", irr, tc.expected)
} else if math.IsNaN(irr) && !math.IsNaN(tc.expected) {
t.Errorf("IRR = NaN, expected = %f", tc.expected)
}
}
}
func TestIRRWithOptions(t *testing.T) {
testCases := []struct {
cashflows []float64
expected float64
}{
{[]float64{-1000, 500, 400, 300, 100}, 0.144888},
{[]float64{-1000, 100, 300, 400, 600}, 0.117906},
{[]float64{1000, 100, 300, 400, 600}, math.NaN()},
}
for _, tc := range testCases {
irr := IRR(tc.cashflows)
if !math.IsNaN(irr) && !almostEqual(tc.expected, irr) {
t.Errorf("IRR calculated = %f, expected = %f", irr, tc.expected)
} else if math.IsNaN(irr) && !math.IsNaN(tc.expected) {
t.Errorf("IRR = NaN, expected = %f", tc.expected)
}
}
}
func TestDiscountedPaybackPeriod(t *testing.T) {
testCases := []struct {
cashflows []float64
discountRate float64
expected float64
}{
{[]float64{-1000, 500, 400, 300, 100}, 0.10, 2.9533333},
{[]float64{-1000, 100, 300, 400, 600}, 0.10, 3.8800000},
{[]float64{-1000, -100, -300, -400, -600}, 0.10, math.NaN()},
}
for _, tc := range testCases {
paybackPeriod := DiscountedPaybackPeriod(tc.cashflows, tc.discountRate)
if !math.IsNaN(paybackPeriod) && !almostEqual(tc.expected, paybackPeriod) {
t.Errorf("Payback Period calculated = %f, expected = %f", paybackPeriod, tc.expected)
} else if math.IsNaN(paybackPeriod) && !math.IsNaN(tc.expected) {
t.Errorf("Payback Period = NaN, expected = %f", tc.expected)
}
}
}
func TestPaybackPeriod(t *testing.T) {
testCases := []struct {
cashflows []float64
expected float64
}{
{[]float64{-1000, 500, 400, 300, 100}, 2.3333333},
{[]float64{-1000, 100, 300, 400, 600}, 3.3333333},
{[]float64{-1000, -100, -300, -400, -600}, math.NaN()},
}
for _, tc := range testCases {
paybackPeriod := PaybackPeriod(tc.cashflows)
if !math.IsNaN(paybackPeriod) && !almostEqual(tc.expected, paybackPeriod) {
t.Errorf("Payback Period calculated = %f, expected = %f", paybackPeriod, tc.expected)
} else if math.IsNaN(paybackPeriod) && !math.IsNaN(tc.expected) {
t.Errorf("Payback Period = NaN, expected = %f", tc.expected)
}
}
}
func TestNPV(t *testing.T) {
testCases := []struct {
cashflows []float64
discountRate float64
expected float64
}{
{[]float64{-1000, 500, 400, 300, 100}, 0.10, 78.819753},
{[]float64{-3000, 1300, 1300, 1300}, 0.08, 350.226083},
}
for _, tc := range testCases {
npv := NPV(tc.cashflows, tc.discountRate)
if !almostEqual(tc.expected, npv) {
t.Errorf("NPV calculated = %f, expected = %f", npv, tc.expected)
}
}
}
func TestNCF(t *testing.T) {
testCases := []struct {
cashflows []float64
expected float64
}{
{[]float64{-1000, 500, 400, 300, 100}, 300},
{[]float64{-1000, 100, 300, 400, 600}, 400},
}
for _, tc := range testCases {
ncf := NCF(tc.cashflows)
if !almostEqual(tc.expected, ncf) {
t.Errorf("NCF calculated = %f, expected = %f", ncf, tc.expected)
}
}
}
func almostEqual(f1, f2 float64) bool {
return math.Abs(f1-f2) < tolerance
}