summaryrefslogtreecommitdiff
path: root/tools/tests/test_preprocess_pragmas.py
blob: 16caf3fd62675553fa78da0d2f27eeba3ae7daf2 (plain)
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
import difflib
from pathlib import Path
import subprocess


# change to True to print output source on failed tests
PRINT_FAILED_OUTPUT = False

PREPROCESS_PRAGMAS_P = Path("tools/preprocess_pragmas")


def fake_struct(pragma_line, i):
    return f"struct increment_block_number_{pragma_line:05}_{i:03};"


def fake_structs(pragma_line, amount):
    return "\n".join(fake_struct(pragma_line, i) for i in range(amount))


data = {
    "test_noarg": (
        "gc-us",
        "source.c",
        """\
abc
#pragma increment_block_number
def
""",
        f"""\
abc
{fake_structs(2, 256)}
#line 3 "source.c"
def
""",
    ),
    "test_one_arg_match": (
        "gc-us",
        "source.c",
        """\
abc
#pragma increment_block_number "gc-us:17"
def
""",
        f"""\
abc
{fake_structs(2, 17)}
#line 3 "source.c"
def
""",
    ),
    "test_one_arg_no_match": (
        "gc-us",
        "source.c",
        """\
abc
#pragma increment_block_number "gc-us-mq:17"
def
""",
        f"""\
abc
{fake_structs(2, 256)}
#line 3 "source.c"
def
""",
    ),
    "test_several_args": (
        "gc-us",
        "source.c",
        """\
abc
#pragma increment_block_number "gc-us-mq:200 gc-us:250"
def
""",
        f"""\
abc
{fake_structs(2, 250)}
#line 3 "source.c"
def
""",
    ),
}

for test_name, (version, filename, source_in, expected_source_out) in data.items():
    p = subprocess.Popen(
        [str(PREPROCESS_PRAGMAS_P), version, filename],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        encoding="UTF-8",
    )
    source_out, _ = p.communicate(input=source_in)
    if p.returncode != 0:
        print(f"{PREPROCESS_PRAGMAS_P} ended with {p.returncode} on {test_name}")
        exit(1)
    if source_out != expected_source_out:
        print(f"failed test {test_name}")
        if PRINT_FAILED_OUTPUT:
            print(source_out)
        for l in difflib.unified_diff(
            expected_source_out.splitlines(),
            source_out.splitlines(),
            "expected output",
            "actual output",
        ):
            print(l)
        exit(1)

print("all tests ok")