Debian Codegraph @

pypy/interpreter/test/test_location.py

raw symbols SCIP
1 import pytest
2
3 from hypothesis import given, strategies, example
4
5 from pypy.interpreter.location import (encode_positions,
6 decode_positions, _offset2lineno, linetable2lnotab,
7 marklines, _decode_entry, DecodeError)
8
9 def check_positions(positions, firstlineno=1, expected=None):
10 if expected is None:
11 expected = positions
12 res = decode_positions(
13 encode_positions(positions, firstlineno),
14 firstlineno
15 )
16 assert res == expected
17
18 def test_encode_positions():
19 # valid
20 positions = [(1, 1, 1, 5), (1, 1, 5, 10), (2, 2, 5, 10)]
21 check_positions(positions)
22 check_positions(positions, 0)
23
24 def test_encode_positions_invalid():
25 # everything invalid
26 positions = [(1, 1, 1, 5), (-1, -1, -1, -1), (2, 2, 5, 10)]
27 check_positions(positions)
28 check_positions(positions, 0)
29
30 def test_encode_positions_invalid_but_lineno_is_fine():
31 # just lineno valid
32 positions = [(1, 1, 1, 5), (1, -1, -1, -1), (2, 2, 5, 10)]
33 check_positions(positions)
34 check_positions(positions, 0)
35
36 def test_out_of_range_positions():
37 positions = [
38 (5, 1000, 1, 1), # too large end_lineno - lineno
39 (6, 1, 300, 400), # col_offset too big
40 (6, 1, 0, 300), # end_col_offset too big
41 (7, 1, 3, 2), # end_lineno smaller than lineno
42 ]
43 check_positions(positions, expected=[(5, -1, -1, -1), (6, -1, -1, -1), (6, -1, -1, -1), (7, -1, -1, -1)])
44
45 def test_lineno_smaller_than_firstlineno():
46 positions = [
47 (1, 1, 1, 1),
48 (2, 2, 2, 2),
49 (3, 3, 3, 3),
50 (4, 4, 4, 4),
51 (5, 5, 5, 5)
52 ]
53 check_positions(positions, 5, expected=[(-1, -1, -1, -1)] * 4 + [(5, 5, 5, 5)])
54
55
56 def test_offset2lineno():
57 positions = [(lineno, lineno, 1, 1) for lineno in [1, 1, 5, 3, 23, 1999]]
58 table = encode_positions(positions, 1)
59 for stopat in range(len(positions)):
60 assert _offset2lineno(table, 1, stopat) == positions[stopat][0]
61
62
63 def lnotab_offset2lineno(tab, line, stopat):
64 addr = 0
65 for i in range(0, len(tab), 2):
66 addr = addr + ord(tab[i])
67 if addr > stopat:
68 break
69 line_offset = ord(tab[i+1])
70 if line_offset > 0x80:
71 line_offset -= 0x100
72 line = line + line_offset
73 return line
74
75 def test_linetable2lnotab():
76 positions = [(lineno, lineno, 1, 1) for lineno in [1, 1, 1, 3, 3, 2, 2, 17]]
77 table = encode_positions(positions, 1)
78 lnotab = linetable2lnotab(table, 1)
79 # check that the bdeltas are even
80 for bdelta in lnotab[::2]:
81 assert ord(bdelta) & 1 == 0
82 for stopat in range(len(positions)):
83 assert lnotab_offset2lineno(lnotab, 1, stopat * 2) == positions[stopat][0]
84
85 def test_marklines():
86 positions = [(lineno, lineno, 1, 1) for lineno in [1, 1, 1, 3, -1, 3, 2, -1, -1, -1, 2, 17, -1, 1]]
87 table = encode_positions(positions, 1)
88 lines = marklines(table, 1)
89 assert lines == [1, -1, -1, 3, -1, -1, 2, -1, -1, -1, -1, 17, -1, 1]
90
91
92
93 # check crash-safety
94
95 def go_through_positions(table, firstlineno):
96 position = 0
97 while position < len(table):
98 lineno, end_lineno, col_offset, end_col_offset, position = _decode_entry(table, firstlineno, position)
99
100
101 @given(strategies.binary(), strategies.integers(min_value=0, max_value=2**30))
102 def test_decode_doesnt_crash(b, firstlineno):
103 try:
104 go_through_positions(b, firstlineno)
105 except Exception as e:
106 if not isinstance(e, DecodeError):
107 raise
108
109 def test_decode_entry_empty_string():
110 with pytest.raises(DecodeError):
111 _decode_entry(b'', 1, 0)