-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
324 lines (291 loc) · 15.1 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#! /usr/bin/python
# -*- coding: utf-8 -*-
import unittest
import math
import calc
class ComputationTest(unittest.TestCase):
_session = None #: An instance of calc.Session.
_session_full = None #: An instance of calc.Session.
def setUp(self):
"""
This creates two sessions: one that takes a string of input in its
constructor, and one that loads the same information (in an acceptable
order). They should both behave identically.
"""
self._session_full = calc.Session(
''.join([
"b = 2; a = 1; c = a + b; d = g(c); f = g(c, 5); g = h();",
"g(a) = a b; g(x, y) = x + y; h() = ceil(4.009);",
"g(a + b + c + d + f + 10.007 - e, h()); 77(b) - 22b; g(-c); g(g(g(2)));",
"`number 5` = 5; `5.6` = 5.6; `number 5` + `5.6`",
])
)
self._session = calc.Session()
self._session.setVariable(self._session.createVariable("b = 2"))
self._session.setVariable(self._session.createVariable("a = 1"))
self._session.setVariable(self._session.createVariable("c = a + b"))
self._session.setFunction(self._session.createFunction("g(a) = a b"))
self._session.setVariable(self._session.createVariable("d = g(c)"))
self._session.setFunction(self._session.createFunction("g(x, y) = x + y"))
self._session.setVariable(self._session.createVariable("f = g(c, 5)"))
self._session.setFunction(self._session.createFunction("h() = ceil(4.009)"))
self._session.setVariable(self._session.createVariable("g = h()"))
self._session.addEquation(self._session.createEquation("g(a + b + c + d + f + 10.007 - e, h())"))
self._session.addEquation(self._session.createEquation("77(b) - 22b"))
self._session.addEquation(self._session.createEquation("g(-c)"))
self._session.addEquation(self._session.createEquation("g(g(g(2)))"))
self._session.setVariable(self._session.createVariable("`number 5` = 5"))
self._session.setVariable(self._session.createVariable("`5.6` = 5.6"))
self._session.addEquation(self._session.createEquation("`number 5` + `5.6`"))
def testLexer(self):
"""
This test ensures that the more primitive logic of the lexer is working
as intended -- that is to say that it ensures that negation is properly
enforced and that parentheses work properly.
"""
self.assertEqual(self._session.createEquation("1").evaluate(), 1)
self.assertEqual(self._session.createEquation("100").evaluate(), 100)
self.assertEqual(self._session.createEquation("-1").evaluate(), -1)
self.assertEqual(self._session.createEquation("-100").evaluate(), -100)
self.assertEqual(self._session.createEquation("0.5").evaluate(), 0.5)
self.assertEqual(self._session.createEquation("-0.5").evaluate(), -0.5)
self.assertEqual(self._session.createEquation(".5").evaluate(), 0.5)
self.assertEqual(self._session.createEquation("-.5").evaluate(), -0.5)
self.assertEqual(self._session.createEquation("1 - (1)").evaluate(), 0)
self.assertEqual(self._session.createEquation("-1 - (-1)").evaluate(), 0)
self.assertEqual(self._session.createEquation("1-(-(-1))").evaluate(), 0)
self.assertEqual(self._session.createEquation("-1-(-(-1))").evaluate(), -2)
self.assertEqual(self._session.createEquation("1 - ---1").evaluate(), 2)
self.assertEqual(self._session.createEquation("1 - --1").evaluate(), 0)
self.assertEqual(self._session.createEquation("-1-(-(-1 + 1 + 1 * 5))").evaluate(), 4)
def testOperators(self):
"""
This test provides a more thorough test of the lexer, ensuring that it
properly handles every operator the calculator knows about.
"""
self.assertEqual(self._session.createEquation("10 - 33").evaluate(), -23)
self.assertEqual(self._session.createEquation("10 - -33").evaluate(), 43)
self.assertEqual(self._session.createEquation("10 + --33").evaluate(), 43)
self.assertEqual(self._session.createEquation("10 + 6").evaluate(), 16)
self.assertEqual(self._session.createEquation("10 % 3").evaluate(), 10 % 3)
self.assertEqual(self._session.createEquation("10/5").evaluate(), 2)
self.assertEqual(self._session.createEquation("10/3").evaluate(), float(10) / 3)
self.assertEqual(self._session.createEquation("10\\3").evaluate(), 10 // 3)
self.assertEqual(self._session.createEquation("5 * 5").evaluate(), 25)
self.assertEqual(self._session.createEquation("1 > 2").evaluate(), 2)
self.assertEqual(self._session.createEquation("10^2").evaluate(), 100)
self.assertEqual(self._session.createEquation("1 < 2").evaluate(), 1)
self.assertEqual(self._session.createEquation("1 > 2").evaluate(), 2)
def testBuiltInVariables(self):
"""
This test runs through every built-in variable the calculator knows about
and ensures that a compiled expression consisting of each one will
produce a correct value.
"""
self.assertEqual(self._session.createEquation("e").evaluate(), math.e)
self.assertEqual(self._session.createEquation("pi").evaluate(), math.pi)
def testBuiltInFunctions(self):
"""
This test runs through every built-in function the calculator knows
about and ensures that a compiled expression consisting of each one will
produce a correct value.
"""
self.assertEqual(self._session.createEquation("abs(-1)").evaluate(), abs(-1))
self.assertEqual(self._session.createEquation("acos(-1)").evaluate(), math.acos(-1))
self.assertEqual(self._session.createEquation("asin(-1)").evaluate(), math.asin(-1))
self.assertEqual(self._session.createEquation("atan(-1)").evaluate(), math.atan(-1))
self.assertEqual(self._session.createEquation("atan(-1, -1)").evaluate(), math.atan2(-1, -1))
self.assertEqual(self._session.createEquation("ceil(0.1)").evaluate(), math.ceil(0.1))
self.assertEqual(self._session.createEquation("cos(-1)").evaluate(), math.cos(-1))
self.assertEqual(self._session.createEquation("degrees(-1)").evaluate(), math.degrees(-1))
self.assertEqual(self._session.createEquation("e(1.46, 2)").evaluate(), 146)
self.assertEqual(self._session.createEquation("fact(5)").evaluate(), 120)
self.assertEqual(self._session.createEquation("floor(1.9)").evaluate(), math.floor(1.9))
self.assertEqual(self._session.createEquation("ln(5)").evaluate(), math.log(5, math.e))
self.assertEqual(self._session.createEquation("log(5)").evaluate(), math.log(5))
self.assertEqual(self._session.createEquation("log(5, 2)").evaluate(), math.log(5, 2))
self.assertEqual(self._session.createEquation("ncr(5, 2)").evaluate(), 10)
self.assertEqual(self._session.createEquation("npr(5, 2)").evaluate(), 20)
self.assertEqual(self._session.createEquation("radians(-1)").evaluate(), math.radians(-1))
self.assertEqual(type(self._session.createEquation("random()").evaluate()), float)
self.assertEqual(type(self._session.createEquation("random(1, 10)").evaluate()), float)
self.assertEqual(type(self._session.createEquation("randomint(1, 6)").evaluate()), int)
self.assertEqual(self._session.createEquation("sin(-1)").evaluate(), math.sin(-1))
self.assertEqual(self._session.createEquation("sqrt(16)").evaluate(), 4)
self.assertEqual(self._session.createEquation("sum(1, 5)").evaluate(), 15)
self.assertEqual(self._session.createEquation("sum(1, 5, 2)").evaluate(), 9)
self.assertEqual(self._session.createEquation("tan(-1)").evaluate(), math.tan(-1))
def testVariables(self):
"""
This test ensures that every variable within the sessions reports the
proper value.
"""
variables = self._session.getVariables()
variables_full = self._session_full.getVariables()
self.assertEqual(variables['a'].evaluate(), 1)
self.assertEqual(variables_full['a'].evaluate(), 1)
self.assertEqual(variables['b'].evaluate(), 2)
self.assertEqual(variables_full['b'].evaluate(), 2)
self.assertEqual(variables['c'].evaluate(), 3)
self.assertEqual(variables_full['c'].evaluate(), 3)
self.assertEqual(variables['d'].evaluate(), 6)
self.assertEqual(variables_full['d'].evaluate(), 6)
self.assertEqual(variables['f'].evaluate(), 8)
self.assertEqual(variables_full['f'].evaluate(), 8)
self.assertEqual(variables['g'].evaluate(), 5)
self.assertEqual(variables_full['g'].evaluate(), 5)
self.assertEqual(variables['number 5'].evaluate(), 5)
self.assertEqual(variables_full['number 5'].evaluate(), 5)
self.assertEqual(variables['5.6'].evaluate(), 5.6)
self.assertEqual(variables_full['5.6'].evaluate(), 5.6)
def testFunctions(self):
"""
This test ensures that every function within the sessions reports proper
values.
"""
self.assertEqual(self._session.createEquation("g(1)").evaluate(), 2)
self.assertEqual(self._session_full.createEquation("g(1)").evaluate(), 2)
self.assertEqual(self._session.createEquation("g(-3)").evaluate(), -6)
self.assertEqual(self._session_full.createEquation("g(-3)").evaluate(), -6)
self.assertEqual(self._session.createEquation("g(1, 3)").evaluate(), 4)
self.assertEqual(self._session_full.createEquation("g(1, 3)").evaluate(), 4)
self.assertEqual(self._session.createEquation("g(-3, -1)").evaluate(), -4)
self.assertEqual(self._session_full.createEquation("g(-3, -1)").evaluate(), -4)
self.assertEqual(self._session.createEquation("h()").evaluate(), 5)
self.assertEqual(self._session_full.createEquation("h()").evaluate(), 5)
def testEquations(self):
"""
This test ensures that the result of evaluating the session is consistent
with expectations.
"""
(_, equations) = self._session.evaluate()
(_, equations_full) = self._session_full.evaluate()
self.assertEqual(equations[0][1], 35.007 - math.e)
self.assertEqual(equations_full[0][1], 35.007 - math.e)
self.assertEqual(equations[1][1], 110)
self.assertEqual(equations_full[1][1], 110)
self.assertEqual(equations[2][1], -6)
self.assertEqual(equations_full[2][1], -6)
self.assertEqual(equations[3][1], 16)
self.assertEqual(equations_full[3][1], 16)
self.assertEqual(equations[4][1], 10.6)
self.assertEqual(equations_full[4][1], 10.6)
def testErrors(self):
"""
This test runs through a list of scenarios that should trigger errors,
failing if one gets through.
"""
try:
self._session.createEquation("5 + 66 2 + 12")
self.fail("No error generated. Expected %s." % (calc.ConsecutiveFactorError.__class__.__name__))
except calc.ConsecutiveFactorError: pass
try:
self._session.createEquation("5 -+ 6 + 8")
self.fail("No error generated. Expected %s." % (calc.ConsecutiveOperatorError.__class__.__name__))
except calc.ConsecutiveOperatorError: pass
try:
self._session.createEquation("66 / 0").evaluate()
self.fail("No error generated. Expected %s." % (calc.DivisionByZeroError.__class__.__name__))
except calc.DivisionByZeroError: pass
try:
self._session.createEquation("fake_function(1, 2, 3)")
self.fail("No error generated. Expected %s." % (calc.FunctionError.__class__.__name__))
except calc.FunctionError: pass
try:
self._session.createEquation("5 + '6 * 10")
self.fail("No error generated. Expected %s." % (calc.IllegalCharacterError.__class__.__name__))
except calc.IllegalCharacterError: pass
try:
self._session.createEquation("6 + (55) + 17 -")
self.fail("No error generated. Expected %s." % (calc.IncompleteExpressionError.__class__.__name__))
except calc.IncompleteExpressionError: pass
try:
self._session.createEquation(None)
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createVariable(None)
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createFunction(None)
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createEquation("")
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createVariable("")
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createFunction("")
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createEquation("x = 5")
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createVariable("g(x, y) = x + y")
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createFunction("77 + 12")
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.addEquation(None)
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.setVariable(None)
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.setFunction(None)
self.fail("No error generated. Expected %s." % (calc.InstantiationError.__class__.__name__))
except calc.InstantiationError: pass
try:
self._session.createEquation("()").evaluate()
self.fail("No error generated. Expected %s." % (calc.NullSubexpressionError.__class__.__name__))
except calc.NullSubexpressionError: pass
session = calc.Session("g = 67q; q = 5g; x(y, z)= 2 x(y, z)")
try:
session.createEquation("g").evaluate()
self.fail("No error generated. Expected %s." % (calc.RecursionError.__class__.__name__))
except calc.RecursionError: pass
try:
session.createEquation("x(1, 2)").evaluate()
self.fail("No error generated. Expected %s." % (calc.RecursionError.__class__.__name__))
except calc.RecursionError: pass
try:
self._session.createEquation("75^2048").evaluate()
self.fail("No error generated. Expected %s." % (calc.ThresholdError.__class__.__name__))
except calc.ThresholdError: pass
try:
self._session.createEquation("6 + ((55) + 17")
self.fail("No error generated. Expected %s." % (calc.UnbalancedParenthesesError.__class__.__name__))
except calc.UnbalancedParenthesesError: pass
try:
self._session.createEquation("6 + ((55) + 17")
self.fail("No error generated. Expected %s." % (calc.UnbalancedParenthesesError.__class__.__name__))
except calc.UnbalancedParenthesesError: pass
try:
self._session.createEquation("5 + )")
self.fail("No error generated. Expected %s." % (calc.UnexpectedCharacterError.__class__.__name__))
except calc.UnexpectedCharacterError: pass
try:
self._session.createEquation("5, + 66")
self.fail("No error generated. Expected %s." % (calc.UnexpectedCharacterError.__class__.__name__))
except calc.UnexpectedCharacterError: pass
try:
self._session.createEquation("fake_function(1, 2, 3")
self.fail("No error generated. Expected %s." % (calc.UnterminatedFunctionError.__class__.__name__))
except calc.UnterminatedFunctionError: pass
try:
self._session.createEquation("fake_variable")
self.fail("No error generated. Expected %s." % (calc.VariableError.__class__.__name__))
except calc.VariableError: pass
test_computation = unittest.main()