This repository has been archived by the owner on Nov 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
test_expr.py
97 lines (77 loc) · 4.32 KB
/
test_expr.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
from expr import BinaryOp, UnaryOp, VarExpr, eval_expr, format_expr, simplify_expr
def test_eval_binary():
assert eval_expr(BinaryOp('+', 1, 2)) == 3
assert eval_expr(BinaryOp('-', 1, 2)) == -1
assert eval_expr(BinaryOp('*', 1, 2)) == 2
assert eval_expr(BinaryOp('/', 1, 2)) == 0.5
def test_eval_unary():
assert eval_expr(UnaryOp('+', 1)) == 1
assert eval_expr(UnaryOp('-', 1)) == -1
def test_format_simple():
assert format_expr(BinaryOp('+', 1, 2)) == '1 + 2'
assert format_expr(BinaryOp('-', 1, 2)) == '1 - 2'
assert format_expr(BinaryOp('*', 1, 2)) == '1 * 2'
assert format_expr(BinaryOp('/', 1, 2)) == '1 / 2'
assert format_expr(UnaryOp('+', 1)) == '+1'
assert format_expr(UnaryOp('-', 1)) == '-1'
def test_format_complex():
# Same operator
assert format_expr(BinaryOp('+', BinaryOp('+', 1, 2), 3)) == '1 + 2 + 3'
assert format_expr(BinaryOp('+', 1, BinaryOp('+', 2, 3))) == '1 + (2 + 3)'
assert format_expr(BinaryOp('-', BinaryOp('-', 1, 2), 3)) == '1 - 2 - 3'
assert format_expr(BinaryOp('-', 1, BinaryOp('-', 2, 3))) == '1 - (2 - 3)'
assert format_expr(BinaryOp('*', BinaryOp('*', 1, 2), 3)) == '1 * 2 * 3'
assert format_expr(BinaryOp('*', 1, BinaryOp('*', 2, 3))) == '1 * (2 * 3)'
assert format_expr(BinaryOp('/', BinaryOp('/', 1, 2), 3)) == '1 / 2 / 3'
assert format_expr(BinaryOp('/', 1, BinaryOp('/', 2, 3))) == '1 / (2 / 3)'
# Equal precedence
assert format_expr(BinaryOp('+', BinaryOp('-', 1, 2), 3)) == '1 - 2 + 3'
assert format_expr(BinaryOp('-', BinaryOp('+', 1, 2), 3)) == '1 + 2 - 3'
assert format_expr(BinaryOp('+', 1, BinaryOp('-', 2, 3))) == '1 + (2 - 3)'
assert format_expr(BinaryOp('-', 1, BinaryOp('+', 2, 3))) == '1 - (2 + 3)'
assert format_expr(BinaryOp('*', BinaryOp('/', 1, 2), 3)) == '1 / 2 * 3'
assert format_expr(BinaryOp('/', BinaryOp('*', 1, 2), 3)) == '1 * 2 / 3'
assert format_expr(BinaryOp('*', 1, BinaryOp('/', 2, 3))) == '1 * (2 / 3)'
assert format_expr(BinaryOp('/', 1, BinaryOp('*', 2, 3))) == '1 / (2 * 3)'
# Different precedence
assert format_expr(BinaryOp('+', BinaryOp('*', 1, 2), 3)) == '1 * 2 + 3'
assert format_expr(BinaryOp('*', BinaryOp('+', 1, 2), 3)) == '(1 + 2) * 3'
assert format_expr(BinaryOp('+', 1, BinaryOp('*', 2, 3))) == '1 + 2 * 3'
assert format_expr(BinaryOp('*', 1, BinaryOp('+', 2, 3))) == '1 * (2 + 3)'
def test_simplify():
# Constants
assert format_expr(simplify_expr(BinaryOp('+', 1, 3))) == '4'
assert format_expr(simplify_expr(UnaryOp('-', 1))) == '-1'
# Variables
assert format_expr(simplify_expr(BinaryOp('+', 2, VarExpr('x')))) == '2 + x'
assert format_expr(simplify_expr(BinaryOp('-', 2, VarExpr('x')))) == '2 - x'
assert format_expr(simplify_expr(BinaryOp('*', 2, VarExpr('x')))) == '2 * x'
assert format_expr(simplify_expr(BinaryOp('/', 2, VarExpr('x')))) == '2 / x'
assert format_expr(simplify_expr(BinaryOp('+', VarExpr('x'), 2))) == 'x + 2'
assert format_expr(simplify_expr(BinaryOp('-', VarExpr('x'), 2))) == 'x - 2'
assert format_expr(simplify_expr(BinaryOp('*', VarExpr('x'), 2))) == 'x * 2'
assert format_expr(simplify_expr(BinaryOp('/', VarExpr('x'), 2))) == 'x / 2'
# Add zero
assert format_expr(simplify_expr(BinaryOp('+', 0, VarExpr('x')))) == 'x'
assert format_expr(simplify_expr(BinaryOp('+', VarExpr('x'), 0))) == 'x'
# Subtract zero
assert format_expr(simplify_expr(BinaryOp('-', 0, VarExpr('x')))) == '-x'
assert format_expr(simplify_expr(BinaryOp('-', VarExpr('x'), 0))) == 'x'
# Multiply zero
assert format_expr(simplify_expr(BinaryOp('*', 0, VarExpr('x')))) == '0'
assert format_expr(simplify_expr(BinaryOp('*', VarExpr('x'), 0))) == '0'
# Multiply one
assert format_expr(simplify_expr(BinaryOp('*', 1, VarExpr('x')))) == 'x'
assert format_expr(simplify_expr(BinaryOp('*', VarExpr('x'), 1))) == 'x'
# Divide one
assert format_expr(simplify_expr(BinaryOp('/', VarExpr('x'), 1))) == 'x'
# Nested
assert format_expr(simplify_expr(
BinaryOp('+',
BinaryOp('*', 1, VarExpr('x')),
BinaryOp('+', 0, VarExpr('y'))
))) == 'x + y'
# Unary plus
assert format_expr(simplify_expr(UnaryOp('+', VarExpr('x')))) == 'x'
# Double minus
assert format_expr(simplify_expr(UnaryOp('-', UnaryOp('-', VarExpr('x'))))) == 'x'