| 1 |
"""test ../lib/cgraph/ internal generic utility code""" |
| 2 |
|
| 3 |
import os |
| 4 |
import platform |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
sys.path.append(os.path.dirname(__file__)) |
| 12 |
from gvtest import ( # pylint: disable=wrong-import-position |
| 13 |
compile_c, |
| 14 |
is_mingw, |
| 15 |
run, |
| 16 |
run_c, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
@pytest.mark.parametrize("utility", ("arena", "bitarray", "itos", "list", "tokenize")) |
| 21 |
def test_utility(utility: str): |
| 22 |
"""run the given utility’s unit tests""" |
| 23 |
|
| 24 |
# locate the unit tests |
| 25 |
src = Path(__file__).parent.resolve() / f"../lib/util/test_{utility}.c" |
| 26 |
assert src.exists() |
| 27 |
|
| 28 |
# locate lib directory that needs to be in the include path |
| 29 |
lib = Path(__file__).parent.resolve() / "../lib" |
| 30 |
|
| 31 |
# extra C flags this compilation needs |
| 32 |
cflags = ["-I", lib] |
| 33 |
if platform.system() != "Windows": |
| 34 |
cflags += ["-std=gnu17"] |
| 35 |
|
| 36 |
_, _ = run_c(src, cflags=cflags) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.parametrize("builtins", (False, True)) |
| 40 |
def test_overflow_h(builtins: bool): |
| 41 |
"""test ../lib/util/overflow.h""" |
| 42 |
|
| 43 |
# locate the unit test |
| 44 |
src = Path(__file__).parent.resolve() / "../lib/util/test_overflow.c" |
| 45 |
assert src.exists() |
| 46 |
|
| 47 |
# locate lib directory that needs to be in the include path |
| 48 |
lib = Path(__file__).parent.resolve() / "../lib" |
| 49 |
|
| 50 |
# extra C flags this compilation needs |
| 51 |
cflags = ["-I", lib] |
| 52 |
if not builtins: |
| 53 |
cflags += ["-DSUPPRESS_BUILTINS"] |
| 54 |
if platform.system() != "Windows": |
| 55 |
cflags += ["-std=gnu17"] |
| 56 |
|
| 57 |
run_c(src, cflags=cflags) |
| 58 |
|
| 59 |
|
| 60 |
@pytest.mark.parametrize( |
| 61 |
"bad_test", |
| 62 |
( |
| 63 |
1, |
| 64 |
2, |
| 65 |
3, |
| 66 |
4, |
| 67 |
pytest.param( |
| 68 |
5, |
| 69 |
marks=pytest.mark.skipif( |
| 70 |
platform.system() == "Windows" and not is_mingw(), |
| 71 |
reason="this test has not been ported to Windows", |
| 72 |
), |
| 73 |
), |
| 74 |
pytest.param( |
| 75 |
6, |
| 76 |
marks=pytest.mark.skipif( |
| 77 |
platform.system() == "Windows" and not is_mingw(), |
| 78 |
reason="this test has not been ported to Windows", |
| 79 |
), |
| 80 |
), |
| 81 |
), |
| 82 |
) |
| 83 |
def test_list_type_safetyutility(bad_test: int): |
| 84 |
""" |
| 85 |
check the type safety of ../lib/util/list.h’s interfaces |
| 86 |
|
| 87 |
Args: |
| 88 |
bad_test: Which type-violating test in ../lib/util/test_list.c to compile. |
| 89 |
""" |
| 90 |
|
| 91 |
# locate the unit tests |
| 92 |
src = Path(__file__).parent.resolve() / "../lib/util/test_list.c" |
| 93 |
assert src.exists() |
| 94 |
|
| 95 |
# locate lib directory that needs to be in the include path |
| 96 |
lib = Path(__file__).parent.resolve() / "../lib" |
| 97 |
|
| 98 |
# extra C flags this compilation needs |
| 99 |
cflags = [f"-DBAD_TEST={bad_test}", "-I", lib] |
| 100 |
if platform.system() != "Windows": |
| 101 |
cflags += ["-std=gnu17"] |
| 102 |
|
| 103 |
# this should fail to compile if strong type safety is preserved |
| 104 |
with pytest.raises(subprocess.CalledProcessError): |
| 105 |
compile_c(src, cflags=cflags) |
| 106 |
|
| 107 |
|
| 108 |
def test_gv_find_me(tmp_path: Path): |
| 109 |
"""test ../lib/util/gv_find_me.c::gv_find_me works correctly""" |
| 110 |
|
| 111 |
# locate our support test file |
| 112 |
src = Path(__file__).parent.resolve() / "test_gv_find_me.c" |
| 113 |
assert src.exists() |
| 114 |
|
| 115 |
# locate lib directory that needs to be in the include path |
| 116 |
lib = Path(__file__).resolve().parents[1] / "lib" |
| 117 |
|
| 118 |
# extra C flags this compilation needs |
| 119 |
cflags = ["-I", lib] |
| 120 |
if platform.system() != "Windows": |
| 121 |
cflags += ["-std=gnu17"] |
| 122 |
|
| 123 |
# pick a path to compile to |
| 124 |
exe = tmp_path / "test_gv_find_me.exe" |
| 125 |
|
| 126 |
_ = compile_c(src, cflags, dst=exe) |
| 127 |
|
| 128 |
# run this |
| 129 |
output = run([exe]) |
| 130 |
|
| 131 |
assert output == f"{exe}\n", "gv_find_me did not determine executable absolute path" |