File: test_location.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (111 lines) | stat: -rw-r--r-- 3,623 bytes parent folder | download | duplicates (2)
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
import pytest

from hypothesis import given, strategies, example

from pypy.interpreter.location import (encode_positions,
    decode_positions, _offset2lineno, linetable2lnotab,
    marklines, _decode_entry, DecodeError)

def check_positions(positions, firstlineno=1, expected=None):
    if expected is None:
        expected = positions
    res = decode_positions(
        encode_positions(positions, firstlineno),
        firstlineno
    )
    assert res == expected

def test_encode_positions():
    # valid
    positions = [(1, 1, 1, 5), (1, 1, 5, 10), (2, 2, 5, 10)]
    check_positions(positions)
    check_positions(positions, 0)

def test_encode_positions_invalid():
    #                          everything invalid
    positions = [(1, 1, 1, 5), (-1, -1, -1, -1), (2, 2, 5, 10)]
    check_positions(positions)
    check_positions(positions, 0)

def test_encode_positions_invalid_but_lineno_is_fine():
    #                          just lineno valid
    positions = [(1, 1, 1, 5), (1, -1, -1, -1), (2, 2, 5, 10)]
    check_positions(positions)
    check_positions(positions, 0)

def test_out_of_range_positions():
    positions = [
        (5, 1000, 1, 1), # too large end_lineno - lineno
        (6, 1, 300, 400), # col_offset too big
        (6, 1, 0, 300), # end_col_offset too big
        (7, 1, 3, 2), # end_lineno smaller than lineno
    ]
    check_positions(positions, expected=[(5, -1, -1, -1), (6, -1, -1, -1), (6, -1, -1, -1), (7, -1, -1, -1)])

def test_lineno_smaller_than_firstlineno():
    positions = [
        (1, 1, 1, 1),
        (2, 2, 2, 2),
        (3, 3, 3, 3),
        (4, 4, 4, 4),
        (5, 5, 5, 5)
    ]
    check_positions(positions, 5, expected=[(-1, -1, -1, -1)] * 4 + [(5, 5, 5, 5)])


def test_offset2lineno():
    positions = [(lineno, lineno, 1, 1) for lineno in [1, 1, 5, 3, 23, 1999]]
    table = encode_positions(positions, 1)
    for stopat in range(len(positions)):
        assert _offset2lineno(table, 1, stopat) == positions[stopat][0]


def lnotab_offset2lineno(tab, line, stopat):
    addr = 0
    for i in range(0, len(tab), 2):
        addr = addr + ord(tab[i])
        if addr > stopat:
            break
        line_offset = ord(tab[i+1])
        if line_offset > 0x80:
            line_offset -= 0x100
        line = line + line_offset
    return line

def test_linetable2lnotab():
    positions = [(lineno, lineno, 1, 1) for lineno in [1, 1, 1, 3, 3, 2, 2, 17]]
    table = encode_positions(positions, 1)
    lnotab = linetable2lnotab(table, 1)
    # check that the bdeltas are even
    for bdelta in lnotab[::2]:
        assert ord(bdelta) & 1 == 0
    for stopat in range(len(positions)):
        assert lnotab_offset2lineno(lnotab, 1, stopat * 2) == positions[stopat][0]

def test_marklines():
    positions = [(lineno, lineno, 1, 1) for lineno in [1, 1, 1, 3, -1, 3, 2, -1, -1, -1, 2, 17, -1, 1]]
    table = encode_positions(positions, 1)
    lines = marklines(table, 1)
    assert lines == [1, -1, -1, 3, -1, -1, 2, -1, -1, -1, -1, 17, -1, 1]



# check crash-safety

def go_through_positions(table, firstlineno):
    position = 0
    while position < len(table):
        lineno, end_lineno, col_offset, end_col_offset, position = _decode_entry(table, firstlineno, position)


@given(strategies.binary(), strategies.integers(min_value=0, max_value=2**30))
def test_decode_doesnt_crash(b, firstlineno):
    try:
        go_through_positions(b, firstlineno)
    except Exception as e:
        if not isinstance(e, DecodeError):
            raise

def test_decode_entry_empty_string():
    with pytest.raises(DecodeError):
        _decode_entry(b'', 1, 0)