-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_arithmetic_expression.py
165 lines (141 loc) · 4.49 KB
/
test_arithmetic_expression.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
import operator
import unittest
from arithmetic_expression import (
ALL_OPS,
DIGITS,
Logger,
PostfixConverter,
Reader,
analyze1,
analyze2,
eval_postfix,
)
class ReaderTests(unittest.TestCase):
def setUp(self):
self.reader = Reader("example_input")
def test_is_end(self):
for _ in range(13):
self.reader.read()
self.assertTrue(self.reader.is_end())
def test_read_and_peek(self):
for letter in ["e", "x", "a", "m", "p", "l", "e", "_", "i", "n", "p", "u", "t"]:
self.assertEqual(self.reader.peek(), letter)
self.assertEqual(self.reader.read(), letter)
def tearDown(self):
self.reader = None
class LoggerTests(unittest.TestCase):
def setUp(self):
reader = Reader("example_input")
self.logger = Logger(reader=reader)
def test_call(self):
with self.logger("test"):
self.assertIn((0, "test"), self.logger.contexts)
def tearDown(self):
self.logger = None
class Analyze1Tests(unittest.TestCase):
def test_analyze1(self):
self.assertEqual(analyze1("1&!0|!!!1"), 1)
self.assertEqual(analyze1("1&0|!1&1"), 0)
class PostfixConverterTests(unittest.TestCase):
def check_conversions(
self, converter: PostfixConverter, examples: dict, ops: dict = None
):
for example, expected in examples.items():
result = "".join(ch or "_" for ch in converter.convert(example))
self.assertEqual(
result, expected, f"Conversion failed for example: {example}"
)
if ops:
value = eval_postfix(converter.convert(example), ops)
try:
true_value = eval(example)
self.assertEqual(value, true_value)
except Exception:
pass
def test_empty(self):
empty = PostfixConverter()
self.check_conversions(
empty,
{
# "": "", -- not a valid expression
"2": "_2",
" 2 ": "_2",
" 235 ": "_235",
},
ops=DIGITS,
)
def test_plus(self):
plus = PostfixConverter(["+"])
self.check_conversions(
plus,
{
"+5": "__5+",
"3+5": "_3_5+",
"2 + 34 + 5": "_2_34+_5+",
},
ops={
**DIGITS,
"+": operator.add,
},
)
def test_boolean(self):
boolean = PostfixConverter(["!", "&", "^|"])
self.check_conversions(
boolean,
{
"1|0": "_1_0|",
"!0": "__0!",
"1|!0": "_1__0!|",
"1|0|1": "_1_0|_1|",
"1|!0|!1": "_1__0!|__1!|",
"1|!!0": "_1___0!!|",
" 1 & 0 | 1 & 1 ": "_1_0&_1_1&|",
" !1 & 0 | !1 & !!0 ": "__1!_0&__1!___0!!&|",
" 1 & (0 | 1) & 1 ": "_1_0_1|&_1&",
},
ALL_OPS,
)
def test_arithmetics(self):
arith = PostfixConverter(["!", "*/", "+-"])
self.check_conversions(
arith,
{
"+5": "__5+",
"3+5": "_3_5+",
"3!": "_3_!",
"!0": "__0!",
"3! + !0": "_3_!__0!+",
"2 + 34 + -5": "_2_34+__5-+",
"2 - 3 + 4 - 5 + 6 * 7": "_2_3-_4+_5-_6_7*+",
},
ALL_OPS,
)
def test_unary(self):
arith = PostfixConverter(["!", "*/", "+-"], unary="!+-")
self.check_conversions(
arith,
{
"4/-2": "_4__2-/",
"--2": "___2--",
"2 - -1": "_2__1--",
},
ALL_OPS,
)
def test_brackets(self):
arith = PostfixConverter(["!", "*/", "+-"])
self.check_conversions(
arith,
{
"1 * 2 - 3 * 4 / 2 + 5": "_1_2*_3_4*_2/-_5+",
"1 * (2 - 3) * 4 / 2 + 5": "_1_2_3-*_4*_2/_5+",
"({1 * 2} - [(3 * 4) / 2] + 5)": "_1_2*_3_4*_2/-_5+",
"((1 * 2) - 3 * (4 / 2 + 5))": "_1_2*_3_4_2/_5+*-",
},
ALL_OPS,
)
class Analyze2Tests(unittest.TestCase):
def test_analyze2(self):
self.assertEqual(analyze2("2+34+-5"), 31)
self.assertEqual(analyze2("2-3+4-5+6*7"), 40)
if __name__ == "__main__":
unittest.main()