-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_systems.py
161 lines (137 loc) · 5.29 KB
/
test_systems.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
"Test systems.py"
import unittest
from systems.errors import IllegalSourceStock
import systems.models
import systems.parse
import systems.lexer
class TestModels(unittest.TestCase):
def test_stock_maximum_rate(self):
m = systems.models.Model("Maximum")
a = m.infinite_stock("a")
b_max = 3
b = m.stock("b", systems.models.Formula(0), systems.models.Formula(b_max))
m.flow(a, b, systems.models.Rate(1))
results = m.run()
for i, result in enumerate(results):
if i > b_max:
self.assertEqual(b_max, result['b'])
def test_illegal_conversion_leak_source(self):
"You can't apply a Conversion or Leak to an infinite stock."
m = systems.models.Model("Maximum")
a = m.infinite_stock("a")
b = m.stock("b")
rates = [systems.models.Conversion(0.25), systems.models.Leak(0.25)]
for rate in rates:
with self.assertRaises(IllegalSourceStock):
m.flow(a, b, rate)
def test_infinite_destination_stock(self):
"Should allow infinite stocks as destinations stock for all rates."
rates = [systems.models.Rate(5), systems.models.Conversion(0.25), systems.models.Leak(0.25)]
for rate in rates:
m = systems.models.Model("Maximum")
a = m.stock("a", systems.models.Formula("100"))
b = m.infinite_stock("b")
m.flow(a, b, rate)
m.run(rounds=3)
def test_stock_maximum_conversion(self):
m = systems.models.Model("Maximum")
a_initial = 10
a = m.stock("a", systems.models.Formula(a_initial))
b_max = 3
b = m.stock("b", 0, systems.models.Formula(b_max))
f_rate = 0.5
m.flow(a, b, systems.models.Conversion(f_rate))
results = m.run(rounds=3)
final = results[-1]
self.assertEqual(b_max, final['b'])
# 10 - ((1 / 0.5) * 3) = 4
a_expected = a_initial - ((1 / f_rate) * b_max)
self.assertEqual(a_expected, final['a'])
def test_stock_maximum_leak(self):
m = systems.models.Model("Maximum")
a_initial = 10
a = m.stock("a", systems.models.Formula(a_initial))
b_max = 3
b = m.stock("b", systems.models.Formula(0), systems.models.Formula(b_max))
m.flow(a, b, systems.models.Leak(0.5))
results = m.run(rounds=2)
final = results[-1]
self.assertEqual(b_max, final['b'])
self.assertEqual(a_initial - b_max, final['a'])
def test_stock_minimums(self):
"Stocks should never dip below their minimum value."
m = systems.models.Model("Minimum")
a = m.stock("a", systems.models.Formula(2))
b = m.stock("b", systems.models.Formula(0))
m.flow(a,b, systems.models.Rate(1))
results = m.run(rounds=5)
final = results[-1]
self.assertEqual(0, final['a'])
self.assertEqual(2, final['b'])
def test_stock_negative_flows(self):
"Stocks should never dip below their minimum value."
m = systems.models.Model("Minimum")
c = m.stock("c", systems.models.Formula(2))
a = m.stock("a", systems.models.Formula(5))
b = m.stock("b", systems.models.Formula(0))
m.flow(a,b, systems.models.Rate("c"))
results = m.run(rounds=5)
final = results[-1]
self.assertEqual(0, final['a'])
self.assertEqual(5, final['b'])
def test_partial_fulfillment_rate(self):
m = systems.models.Model("Partial")
a = m.stock("a", systems.models.Formula(5))
b = m.stock("b", systems.models.Formula(0))
systems.parse.parse_flow(m, a, b, "10")
results = m.run(rounds=3)
final = results[-1]
self.assertEqual(0, final['a'])
self.assertEqual(5, final['b'])
def test_formula_rate(self):
m = systems.models.Model("Maximum")
a = m.infinite_stock("a")
b = m.stock("b")
c = m.stock("c")
d = m.stock("d", systems.models.Formula(3))
systems.parse.parse_flow(m, a, b, "d * 2")
systems.parse.parse_flow(m, b, c, "d")
results = m.run(rounds=3)
final = results[-1]
self.assertEqual(12, final['b'])
self.assertEqual(6, final['c'])
class TestFormula(unittest.TestCase):
def test_simple_formulas(self):
cases = [
("0", 0),
("0.5", 0.5),
("100", 100),
("inf", float("+inf")),
]
for case_in, case_out in cases:
lexed = systems.lexer.lex_formula(case_in)
formula = systems.models.Formula(lexed)
self.assertEqual(case_out, formula.compute())
def test_reference_formulas(self):
state = {'a': 10, 'b': 5, 'c': 0}
cases = [
("a", 10),
("b", 5),
("c", 0),
("a * 2", 20),
("b * 3", 15),
("c * 10", 0),
("2 * a", 20),
("3 * b", 15),
("10 * c", 0),
("b * a", 50),
("a * b", 50),
("b * b", 25),
("c * a", 0),
]
for case_in, case_out in cases:
lexed = systems.lexer.lex_formula(case_in)
formula = systems.models.Formula(lexed)
self.assertEqual(case_out, formula.compute(state))
if __name__ == "__main__":
unittest.main()