-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_if_to_phi.py
53 lines (39 loc) · 1.42 KB
/
test_if_to_phi.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
import ast
import inspect
import pytest
from ast_tools.passes import apply_passes, if_to_phi
def mux(select, t, f):
return t if select else f
@pytest.mark.parametrize('phi_args, expected_name', [
([mux], '__phi'), # test passing function
(['mux'], 'mux'), # test passing name
([mux, 'foo'], 'foo'), # test passing function and free name
([mux, 'mux'], 'mux0'), # test passing function and used name
])
def test_basic(phi_args, expected_name):
def basic(s):
return 0 if s else 1
phi_basic = apply_passes([if_to_phi(*phi_args)])(basic)
for s in (True, False):
assert basic(s) == phi_basic(s)
assert inspect.getsource(phi_basic) == f'''\
def basic(s):
return {expected_name}(s, 0, 1)
'''
@pytest.mark.parametrize('phi_args, expected_name', [
([mux], '__phi'), # test passing function
(['mux'], 'mux'), # test passing name
([mux, 'foo'], 'foo'), # test passing function and free name
([mux, 'mux'], 'mux0'), # test passing function and used name
])
def test_nested(phi_args, expected_name):
def nested(s, t):
return 0 if s else 1 if t else 2
phi_nested = apply_passes([if_to_phi(*phi_args)])(nested)
for s in (True, False):
for t in (True, False):
assert nested(s, t) == phi_nested(s, t)
assert inspect.getsource(phi_nested) == f'''\
def nested(s, t):
return {expected_name}(s, 0, {expected_name}(t, 1, 2))
'''