| 1 |
import pytest |
| 2 |
|
| 3 |
try: |
| 4 |
import rply |
| 5 |
except ImportError: |
| 6 |
pytest.skip("rply not installed") |
| 7 |
|
| 8 |
from rpython.jit.metainterp.ruleopt.parse import * |
| 9 |
|
| 10 |
import os |
| 11 |
|
| 12 |
with open( |
| 13 |
os.path.join( |
| 14 |
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "real.rules" |
| 15 |
) |
| 16 |
) as f: |
| 17 |
ALLRULES = f.read() |
| 18 |
|
| 19 |
|
| 20 |
def test_parse_int_add_zero(): |
| 21 |
s = """\ |
| 22 |
add_zero: int_add(x, 0) |
| 23 |
=> x |
| 24 |
""" |
| 25 |
ast = parse(s) |
| 26 |
assert str(ast.rules[0]) == 'add_zero: int_add(x, 0)\n => x' |
| 27 |
|
| 28 |
def test_parse_source_positions(): |
| 29 |
s = """\ |
| 30 |
add_zero: int_add(x, 0) |
| 31 |
=> x |
| 32 |
""" |
| 33 |
ast = parse(s) |
| 34 |
rule = ast.rules[0] |
| 35 |
assert rule.sourcepos.lineno == 1 |
| 36 |
assert rule.target.sourcepos.lineno == 2 |
| 37 |
assert rule.target.sourcepos.colno == 8 |
| 38 |
|
| 39 |
def test_parse_function_many_args(): |
| 40 |
s = """\ |
| 41 |
n: op(C) |
| 42 |
C1 = f(C, C, C, C) |
| 43 |
=> C |
| 44 |
""" |
| 45 |
ast = parse(s) |
| 46 |
|
| 47 |
|
| 48 |
def test_sorry(): |
| 49 |
s = """\ |
| 50 |
eq_different_knownbits: int_eq(x, y) |
| 51 |
SORRY_Z3 |
| 52 |
=> 0 |
| 53 |
""" |
| 54 |
ast = parse(s) |
| 55 |
assert ast.rules[0].cantproof == True |
| 56 |
|
| 57 |
def test_parse_lshift_rshift(): |
| 58 |
s = """\ |
| 59 |
int_lshift_int_rshift_consts: int_lshift(int_rshift(x, C1), C1) |
| 60 |
C = (-1 >> C1) << C1 |
| 61 |
=> int_and(x, C) |
| 62 |
""" |
| 63 |
ast = parse(s) |
| 64 |
|
| 65 |
|
| 66 |
def test_parse_all(): |
| 67 |
ast = parse(ALLRULES) |
| 68 |
|
| 69 |
|
| 70 |
def test_undefined_name(): |
| 71 |
s = """\ |
| 72 |
n: op(C) |
| 73 |
=> x |
| 74 |
""" |
| 75 |
with pytest.raises(TypeCheckError) as info: |
| 76 |
ast = parse(s) |
| 77 |
assert str(info.value) == "variable 'x' is not defined" |
| 78 |
assert info.value.format(s) == '''\ |
| 79 |
Type error: variable 'x' is not defined |
| 80 |
in line 2 |
| 81 |
=> x |
| 82 |
^''' |
| 83 |
|
| 84 |
def test_doubly_defined_name(): |
| 85 |
s = """\ |
| 86 |
n: op(C) |
| 87 |
C = C + 1 |
| 88 |
=> C |
| 89 |
""" |
| 90 |
with pytest.raises(TypeCheckError) as info: |
| 91 |
ast = parse(s) |
| 92 |
assert str(info.value) == "'C' is already defined" |
| 93 |
|
| 94 |
|
| 95 |
def test_check_not_bool(): |
| 96 |
s = """\ |
| 97 |
n: op(C) |
| 98 |
check C |
| 99 |
=> C |
| 100 |
""" |
| 101 |
with pytest.raises(TypeCheckError) as info: |
| 102 |
ast = parse(s) |
| 103 |
assert str(info.value) == "expected check expression to return a bool, got int" |
| 104 |
assert info.value.format(s) == '''\ |
| 105 |
Type error: expected check expression to return a bool, got int |
| 106 |
in line 2 |
| 107 |
check C |
| 108 |
^''' |