-
Notifications
You must be signed in to change notification settings - Fork 0
/
amount_test.go
135 lines (123 loc) · 2.94 KB
/
amount_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
package coin
import (
"fmt"
"math/big"
"testing"
"github.com/mkobetic/coin/assert"
)
var cad, usd *Commodity
func init() {
Commodities["CAD"] = &Commodity{Id: "CAD", Decimals: 2}
Commodities["USD"] = &Commodity{Id: "USD", Decimals: 2}
cad = DefaultCommodity()
usd = Commodities["USD"]
}
func Test_ParseAmount(t *testing.T) {
for i, fix := range []struct {
in, out string
}{
{"10", "10.00"},
{"-50.01", "-50.01"},
{"0.011", "0.01"},
{"-100.00", "-100.00"},
} {
amt, err := parseAmount(fix.in, cad)
assert.NoError(t, err)
res := fmt.Sprintf("%a", amt)
assert.Equal(t, res, fix.out, "%d. not equal", i)
}
}
func Test_AmountWidth(t *testing.T) {
for i, fix := range []struct {
amt string
decimals, width int
}{
{"0.09", 2, 4},
{"-0.09", 2, 5},
} {
amt, err := parseAmount(fix.amt, cad)
assert.NoError(t, err)
width := amt.Width(fix.decimals)
assert.Equal(t, width, fix.width, "%d. not equal", i)
}
}
func Test_AmountIsEqual(t *testing.T) {
amt1, err := parseAmount("12.33", cad)
assert.NoError(t, err)
amt2, err := parseAmount("12.33", usd)
assert.NoError(t, err)
assert.True(t, amt1.IsEqual(amt2))
assert.True(t, amt1.Commodity != amt2.Commodity)
}
func Test_FormatWidthAmount(t *testing.T) {
for i, fix := range []struct {
amt string
width, decimals int
out string
}{
{"10", 5, 0, " 10"},
{"-50.01", 10, 2, " -50.01"},
{"0.001", 2, 3, "0.000"},
} {
amt, err := parseAmount(fix.amt, cad)
assert.NoError(t, err)
res := fmt.Sprintf("%*.*f", fix.width, fix.decimals, amt)
assert.Equal(t, res, fix.out, "%d. not equal", i)
}
}
func Test_AmountAddIn(t *testing.T) {
for i, fix := range []struct {
a, b, c string
}{
{"100.0", "500.00", "600.00"},
{"100.00", "-50.0", "50.00"},
{"-100.00", "50", "-50.00"},
} {
a := MustParseAmount(fix.a, cad)
b := MustParseAmount(fix.b, cad)
err := a.AddIn(b)
assert.NoError(t, err)
c := fmt.Sprintf("%a", a)
assert.Equal(t, c, fix.c, "%d. not equal", i)
}
}
func Test_AmountTimes(t *testing.T) {
for i, fix := range []struct {
a, b, c string
}{
{"100.0", "50.00", "5000.00"},
{"100.00", "-50.0", "-5000.00"},
{"-100.00", "50", "-5000.00"},
} {
a := MustParseAmount(fix.a, cad)
b := MustParseAmount(fix.b, cad)
c := a.Times(b)
cc := fmt.Sprintf("%a", c)
assert.Equal(t, cc, fix.c, "%d. not equal", i)
}
}
func Test_NewAmountFracDec(t *testing.T) {
for i, fix := range []struct {
a, b, c string
}{
{"-704", "1", "-704.00"},
{"-2853", "25", "-114.12"},
{"-2709", "50", "-54.18"},
{"-9633", "100", "-96.33"},
{"-332", "5", "-66.40"},
{"-1063", "10", "-106.30"},
{"105276", "25", "4211.04"},
{"200", "1", "200.00"},
} {
a := mustParseBigInt(fix.a)
b := mustParseBigInt(fix.b)
c := NewAmountFrac(a, b, cad)
cc := fmt.Sprintf("%a", c)
assert.Equal(t, cc, fix.c, "%d. not equal", i)
}
}
func mustParseBigInt(s string) *big.Int {
i := new(big.Int)
i.SetString(s, 10)
return i
}