-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
73 lines (60 loc) · 3 KB
/
test.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
import unittest
from ast_node import ValueExp, ExpPlus, ExpMinus, ExpMulti, ExpDiv, ExpAnd, ExpOr, ExpGt, ExpIf, SentencesExp, AssignExp, VariableExp, WhileExp, PrintExp, FuncCallExp, FuncDefExp
class AstNodeTest(unittest.TestCase):
def test_exp_plus(self):
self.assertEqual(ExpPlus(ValueExp(12), ValueExp(34)).evaluate({}), 46)
def test_exp_minus(self):
self.assertEqual(ExpMinus(ValueExp(12), ValueExp(34)).evaluate({}), -22)
def test_exp_multi(self):
self.assertEqual(ExpMulti(ValueExp(12), ValueExp(34)).evaluate({}), 408)
def test_exp_div(self):
self.assertEqual(ExpDiv(ValueExp(40), ValueExp(4)).evaluate({}), 10)
def test_exp_and(self):
self.assertEqual(ExpAnd(ValueExp(1), ValueExp(1)).evaluate({}), 1)
self.assertEqual(ExpAnd(ValueExp(0), ValueExp(1)).evaluate({}), 0)
self.assertEqual(ExpAnd(ValueExp(1), ValueExp(0)).evaluate({}), 0)
self.assertEqual(ExpAnd(ValueExp(0), ValueExp(0)).evaluate({}), 0)
def test_exp_and(self):
self.assertEqual(ExpOr(ValueExp(1), ValueExp(1)).evaluate({}), 1)
self.assertEqual(ExpOr(ValueExp(0), ValueExp(1)).evaluate({}), 1)
self.assertEqual(ExpOr(ValueExp(1), ValueExp(0)).evaluate({}), 1)
self.assertEqual(ExpOr(ValueExp(0), ValueExp(0)).evaluate({}), 0)
def test_exp_gt(self):
self.assertEqual(ExpGt(ValueExp(10), ValueExp(9)).evaluate({}), 1)
self.assertEqual(ExpGt(ValueExp(10), ValueExp(10)).evaluate({}), 0)
self.assertEqual(ExpGt(ValueExp(10), ValueExp(11)).evaluate({}), 0)
def test_exp_if(self):
self.assertEqual(ExpIf(ValueExp(1), ValueExp(2), ValueExp(3)).evaluate({}), 2)
self.assertEqual(ExpIf(ValueExp(0), ValueExp(2), ValueExp(3)).evaluate({}), 3)
class SentencesTest(unittest.TestCase):
def test_sentences(self):
self.assertEqual(SentencesExp([ValueExp(1), ValueExp(2), ValueExp(3)]).evaluate({}), 3)
def test_assign(self):
self.assertEqual(SentencesExp([
AssignExp('A', ValueExp(12)),
AssignExp('B', ValueExp(34)),
ExpPlus(VariableExp('A'), VariableExp('B'))
]).evaluate({}), 46)
def test_while(self):
self.assertEqual(
SentencesExp([
AssignExp('I', ValueExp(0)),
WhileExp(
ExpGt(ValueExp(10), VariableExp('I')),
AssignExp('I', ExpPlus(VariableExp('I'), ValueExp(1)))
),
VariableExp('I')
]).evaluate({})
, 10)
def test_func(self):
self.assertEqual(
SentencesExp([
FuncDefExp('plusten', ExpPlus(VariableExp('v'), ValueExp(10)), ['v']),
AssignExp('r', ValueExp(0)),
AssignExp('r', FuncCallExp('plusten', [VariableExp('r')])),
AssignExp('r', FuncCallExp('plusten', [VariableExp('r')])),
VariableExp('r')
]).evaluate({})
, 20)
if __name__ == '__main__':
unittest.main()