forked from xdslproject/xdsl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_frontend_python_code_check.py
More file actions
156 lines (130 loc) · 3.91 KB
/
Copy pathtest_frontend_python_code_check.py
File metadata and controls
156 lines (130 loc) · 3.91 KB
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
import ast
import pytest
from xdsl.frontend.pyast.exception import CodeGenerationException
from xdsl.frontend.pyast.python_code_check import CheckAndInlineConstants
def test_const_correctly_evaluated_I():
src = """
a: Const[i32] = 2 ** 5
x = a
"""
stmts = ast.parse(src).body
CheckAndInlineConstants.run(stmts, __file__)
assert ast.unparse(stmts).endswith("x = 32")
def test_const_correctly_evaluated_II():
src = """
a: Const[i32] = 4
x: i64 = a + 2
"""
stmts = ast.parse(src).body
CheckAndInlineConstants.run(stmts, __file__)
assert ast.unparse(stmts).endswith("x: i64 = 4 + 2")
def test_const_correctly_evaluated_III():
src = """
a: Const[i32] = 4
b: Const[i32] = len([1, 2, 3, 4])
x: Const[i32] = a + b
y = x
"""
stmts = ast.parse(src).body
CheckAndInlineConstants.run(stmts, __file__)
assert ast.unparse(stmts).endswith("y = 8")
def test_const_correctly_evaluated_IV():
src = """
a: Const[i32] = 4
def foo(y: i32):
x: i32 = a + y
"""
stmts = ast.parse(src).body
CheckAndInlineConstants.run(stmts, __file__)
assert ast.unparse(stmts).endswith("x: i32 = 4 + y")
def test_const_correctly_evaluated_V():
src = """
a: Const[i32] = 4
b: Const[i32] = 4
def foo(y: i32):
c: Const[i32] = a + b + 2
x: i32 = c
"""
stmts = ast.parse(src).body
CheckAndInlineConstants.run(stmts, __file__)
assert ast.unparse(stmts).endswith("x: i32 = 10")
def test_raises_exception_on_assignemnt_to_const_I():
src = """
a: Const[i32] = 2 ** 5
a = 34
"""
stmts = ast.parse(src).body
with pytest.raises(CodeGenerationException) as err:
CheckAndInlineConstants.run(stmts, __file__)
assert err.value.msg == "Constant 'a' is already defined and cannot be assigned to."
def test_raises_exception_on_assignemnt_to_const_II():
src = """
x: Const[i32] = 100
def foo():
x: i32 = 2
return
"""
stmts = ast.parse(src).body
with pytest.raises(CodeGenerationException) as err:
CheckAndInlineConstants.run(stmts, __file__)
assert err.value.msg == "Constant 'x' is already defined."
def test_raises_exception_on_assignemnt_to_const_III():
src = """
y: Const[i32] = 100
@block
def bb0():
y = 32
return
bb0()
"""
stmts = ast.parse(src).body
with pytest.raises(CodeGenerationException) as err:
CheckAndInlineConstants.run(stmts, __file__)
assert err.value.msg == "Constant 'y' is already defined and cannot be assigned to."
def test_raises_exception_on_assignemnt_to_const_IV():
src = """
z: Const[i32] = 100
def foo(x: i32):
@block
def bb0(z: i32):
return
bb0(x)
"""
stmts = ast.parse(src).body
with pytest.raises(CodeGenerationException) as err:
CheckAndInlineConstants.run(stmts, __file__)
assert (
err.value.msg
== "Constant 'z' is already defined and cannot be used as a function/block argument name."
)
def test_raises_exception_on_duplicate_const():
src = """
z: Const[i32] = 100
z: Const[i32] = 2
"""
stmts = ast.parse(src).body
with pytest.raises(CodeGenerationException) as err:
CheckAndInlineConstants.run(stmts, __file__)
assert err.value.msg == "Constant 'z' is already defined."
def test_raises_exception_on_evaluation_error_I():
src = """
z: Const[i32] = 23 / 0
"""
stmts = ast.parse(src).body
with pytest.raises(CodeGenerationException) as err:
CheckAndInlineConstants.run(stmts, __file__)
assert (
err.value.msg
== "Non-constant expression cannot be assigned to constant variable 'z' or cannot be evaluated."
)
def test_raises_exception_on_evaluation_error_II():
src = """
a: Const[i32] = x + 12
"""
stmts = ast.parse(src).body
with pytest.raises(CodeGenerationException) as err:
CheckAndInlineConstants.run(stmts, __file__)
assert (
err.value.msg
== "Non-constant expression cannot be assigned to constant variable 'a' or cannot be evaluated."
)