forked from ChizhovVadim/xirr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xirr_test.go
57 lines (54 loc) · 1.23 KB
/
xirr_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
package xirr
import (
"math"
"testing"
"time"
)
func TestXirr(t *testing.T) {
var tests = []struct {
Payments []Payment
Xirr float64
}{
{
Payments: []Payment{
Payment{mustParseDate("2018-01-01"), -500000},
Payment{mustParseDate("2018-08-22"), 500000},
Payment{mustParseDate("2020-03-13"), 1000000},
Payment{mustParseDate("2020-03-18"), 400000},
Payment{mustParseDate("2020-04-16"), 631928.98},
Payment{mustParseDate("2021-01-05"), -1000000},
Payment{mustParseDate("2021-02-10"), 794000},
},
Xirr: 2.591,
},
{
Payments: []Payment{
Payment{mustParseDate("2022-08-26"), -20},
Payment{mustParseDate("2022-09-26"), 0.12},
Payment{mustParseDate("2022-10-26"), 0.12},
Payment{mustParseDate("2022-11-28"), 0.12},
Payment{mustParseDate("2022-12-26"), 0.12},
Payment{mustParseDate("2023-01-26"), 20.12},
},
Xirr: 1.074,
},
}
for i, test := range tests {
var xirr, err = XIRR(test.Payments)
if err != nil {
t.Error(err)
continue
}
if math.Abs(xirr-test.Xirr) > 0.005 {
t.Errorf("%v %v %v", i, test, xirr)
continue
}
}
}
func mustParseDate(s string) time.Time {
var t, err = time.Parse("2006-01-02", s)
if err != nil {
panic(err)
}
return t
}