-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
201 lines (182 loc) · 5.8 KB
/
tests.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from unittest import TestCase
from fractions import Fraction
from lazy import lazy_alloc, Asset
class TestLazy(TestCase):
def assert_deltas(self, assets, deltas):
self.assertEqual(
[Fraction(d) for d in deltas],
[a.delta for a in assets],
)
def test_one(self):
"""Two assets equal desired weight, add exactly enough
to bring the underweighted up to the other
"""
assets = [
Asset(Fraction(100), Fraction("0.5")),
Asset(Fraction(200), Fraction("0.5")),
]
lazy_alloc(assets, Fraction(100))
self.assert_deltas(
assets,
[100, 0]
)
def test_two(self):
"""Two assets equal desired weight, add enough to bring them up to
equal, then a bit more, which should be divided evenly"""
assets = [
Asset(Fraction(100), Fraction("0.5")),
Asset(Fraction(200), Fraction("0.5")),
]
lazy_alloc(assets, Fraction(110))
self.assert_deltas(
assets,
[105, 5]
)
def test_three(self):
"""Two assets equal desired weight, but not enough contributions
to bring it equal. All contributions should go into the one"""
assets = [
Asset(Fraction(100), Fraction("0.5")),
Asset(Fraction(200), Fraction("0.5")),
]
lazy_alloc(assets, Fraction(50))
self.assert_deltas(
assets,
[50, 0]
)
def test_four(self):
"""Two assets, and more money is given than required"""
assets = [
Asset(Fraction(100), Fraction("0.5")),
Asset(Fraction(200), Fraction("0.5")),
]
lazy_alloc(assets, Fraction(500))
self.assert_deltas(
assets,
[300, 200]
)
def test_five(self):
assets = [
Asset(Fraction(9000), Fraction("0.55")),
Asset(Fraction(4000), Fraction("0.30")),
Asset(Fraction(3500), Fraction("0.15")),
]
lazy_alloc(assets, Fraction(2000))
self.assert_deltas(
assets,
[Fraction("705.88"), Fraction("1294.12"), 0]
)
def test_six(self):
assets = [
Asset(Fraction(9000), Fraction("0.55")),
Asset(Fraction(4000), Fraction("0.30")),
Asset(Fraction(3500), Fraction("0.15")),
]
lazy_alloc(assets, Fraction(10000))
self.assert_deltas(
assets,
[Fraction("5575.00"), Fraction("3950.00"), Fraction("475.00")]
)
def test_w_one(self):
"""Tests withdraws, equal withdraw from each"""
assets = [
Asset(Fraction(100), Fraction(0.5)),
Asset(Fraction(100), Fraction(0.5)),
]
lazy_alloc(
assets,
Fraction(-100),
)
self.assert_deltas(
assets,
[Fraction(-50), Fraction(-50)],
)
def test_w_two(self):
"""Tests withdraws, some from one, then equal from both"""
assets = [
Asset(Fraction(150), Fraction(0.5)),
Asset(Fraction(100), Fraction(0.5)),
]
lazy_alloc(
assets,
Fraction(-100),
)
self.assert_deltas(
assets,
[Fraction(-75), Fraction(-25)],
)
def test_w_three(self):
assets = [
Asset(Fraction(9000), Fraction("0.55")),
Asset(Fraction(4000), Fraction("0.30")),
Asset(Fraction(3500), Fraction("0.15")),
]
lazy_alloc(assets, Fraction(-10000))
self.assert_deltas(
assets,
[Fraction("-5425"), Fraction("-2050"), Fraction("-2525")]
)
def test_w_four(self):
assets = [
Asset(Fraction(1000), Fraction(0.33)),
Asset(Fraction(2000), Fraction(0.33)),
Asset(Fraction(3000), Fraction(0.33)),
]
lazy_alloc(assets, Fraction(-3000))
self.assert_deltas(
assets,
[Fraction(0), Fraction("-1000"), Fraction("-2000")]
)
def test_not_normalized(self):
assets = [
Asset(Fraction(100), Fraction(0.2)),
Asset(Fraction(200), Fraction(0.2)),
Asset(Fraction(100), Fraction(0.2)),
]
lazy_alloc(assets, Fraction(300))
self.assert_deltas(
assets,
[Fraction("133.33"), Fraction("33.33"), Fraction("133.33")]
)
def test_withdraw_not_normalized(self):
assets = [
Asset(Fraction(100), Fraction(0.2)),
Asset(Fraction(200), Fraction(0.2)),
Asset(Fraction(100), Fraction(0.2)),
]
lazy_alloc(assets, Fraction(-300))
self.assert_deltas(
assets,
[Fraction("-66.67"), Fraction("-166.67"), Fraction("-66.67")]
)
def test_zero_alloc(self):
assets = [
Asset(Fraction(100), Fraction(1)),
Asset(Fraction(100), Fraction(0)),
]
lazy_alloc(assets, Fraction(10))
self.assert_deltas(
assets,
[Fraction(10), Fraction(0)]
)
def test_zero_withdraw(self):
assets = [
Asset(Fraction(100), Fraction(1)),
Asset(Fraction(100), Fraction(0)),
]
lazy_alloc(assets, Fraction(-10))
self.assert_deltas(
assets,
[Fraction(0), Fraction(-10)]
)
def test_zero_withdraw_two(self):
assets = [
Asset(Fraction(100), Fraction(0)),
Asset(Fraction(100), Fraction(0.5)),
Asset(Fraction(100), Fraction(0.5)),
]
lazy_alloc(assets, Fraction(-150))
self.assert_deltas(
assets,
[Fraction(-100), Fraction(-25), Fraction(-25)]
)