Skip to content

Commit 3893ab0

Browse files
committed
Move some functions into util
1 parent 6af6b05 commit 3893ab0

File tree

3 files changed

+95
-97
lines changed

3 files changed

+95
-97
lines changed

conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import json
2020
import re
2121
import shlex
22+
import shutil
2223
from pathlib import Path
2324
from random import shuffle
24-
import shutil
2525

2626
import pytest
2727

test_partdiff.py

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,11 @@
33
The test is parametrized by `pytest_generate_tests` in `conftest.py` via its `test_id` argument.
44
"""
55

6-
import os
7-
import subprocess
8-
from pathlib import Path
9-
106
import pytest
117

128
import util
139
from util import ReferenceSource, partdiff_params_tuple
1410

15-
REFERENCE_IMPLEMENTATION_DIR = Path.cwd() / "reference_implementation"
16-
REFERENCE_IMPLEMENTATION_EXEC = REFERENCE_IMPLEMENTATION_DIR / "partdiff"
17-
18-
19-
def ensure_reference_implementation_exists() -> None:
20-
"""Ensure that the reference implementation exists.
21-
22-
If it doesn't exist, `make` is used to automatically compile it.
23-
"""
24-
25-
def is_executable():
26-
executable = REFERENCE_IMPLEMENTATION_EXEC
27-
return executable.exists() and os.access(executable, os.X_OK)
28-
29-
if is_executable():
30-
return
31-
subprocess.check_output(["make", "-C", REFERENCE_IMPLEMENTATION_DIR])
32-
assert is_executable()
33-
34-
35-
def get_reference_output(
36-
partdiff_params: partdiff_params_tuple,
37-
reference_output_data: dict[partdiff_params_tuple, str],
38-
reference_source: ReferenceSource,
39-
) -> str:
40-
"""Acquire the reference output.
41-
42-
Args:
43-
partdiff_params (partdiff_params_tuple): The parameter combination to get the output for.
44-
reference_output_data (dict[partdiff_params_tuple, str]): The cached reference output.
45-
reference_source (ReferenceSource): The source of the reference output (cache, impl, or auto).
46-
47-
Raises:
48-
RuntimeError: When reference_source=cache and the output for a parameter combination isn't cached.
49-
50-
Returns:
51-
str: The reference output for the params.
52-
"""
53-
# Force the number of threads to 1:
54-
partdiff_params = ("1",) + partdiff_params[1:6]
55-
56-
def get_from_cache():
57-
return reference_output_data[partdiff_params]
58-
59-
def get_from_impl():
60-
ensure_reference_implementation_exists()
61-
command_line = [REFERENCE_IMPLEMENTATION_EXEC] + list(partdiff_params)
62-
return subprocess.check_output(command_line).decode("utf-8")
63-
64-
assert reference_source in ReferenceSource
65-
66-
match reference_source:
67-
case ReferenceSource.auto:
68-
if partdiff_params in reference_output_data:
69-
return get_from_cache()
70-
return get_from_impl()
71-
case ReferenceSource.cache:
72-
if partdiff_params not in reference_output_data:
73-
raise RuntimeError(
74-
'Parameter combination "{}" was not found in cache'.format(
75-
" ".join(partdiff_params)
76-
)
77-
)
78-
return get_from_cache()
79-
case ReferenceSource.impl:
80-
return get_from_impl()
81-
82-
83-
def get_actual_output(
84-
partdiff_params: partdiff_params_tuple,
85-
partdiff_executable: list[str],
86-
use_valgrind: bool,
87-
cwd: Path | None,
88-
) -> str:
89-
"""Get the actual output for a parameter combination.
90-
91-
Args:
92-
partdiff_params (partdiff_params_tuple): The parameter combination.
93-
partdiff_executable (list[str]): The executable to run.
94-
use_valgrind (bool): Wether valgrind shall be used.
95-
cwd (Path | None): The working directory of the executable.
96-
97-
Returns:
98-
str: The output of the executable.
99-
"""
100-
command_line = partdiff_executable + list(partdiff_params)
101-
if use_valgrind:
102-
command_line = ["valgrind", "--leak-check=full"] + command_line
103-
return subprocess.check_output(command_line, cwd=cwd).decode("utf-8")
104-
10511

10612
def test_partdiff_parametrized(
10713
pytestconfig: pytest.Config,
@@ -124,10 +30,10 @@ def test_partdiff_parametrized(
12430
reference_source = pytestconfig.getoption("reference_source")
12531
cwd = pytestconfig.getoption("cwd")
12632

127-
actual_output = get_actual_output(
33+
actual_output = util.get_actual_output(
12834
partdiff_params, partdiff_executable, use_valgrind, cwd
12935
)
130-
reference_output = get_reference_output(
36+
reference_output = util.get_reference_output(
13137
partdiff_params, reference_output_data, reference_source
13238
)
13339

util.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
"""Utility functions that are used by both `conftest.py` and `test_partdiff.py`."""
22

3+
import os
34
import re
5+
import subprocess
46
from collections.abc import Iterator
57
from enum import StrEnum
68
from functools import cache
79
from pathlib import Path
810

11+
REFERENCE_IMPLEMENTATION_DIR = Path.cwd() / "reference_implementation"
12+
REFERENCE_IMPLEMENTATION_EXEC = REFERENCE_IMPLEMENTATION_DIR / "partdiff"
13+
914
ReferenceSource = StrEnum("ReferenceSource", ["auto", "cache", "impl"])
1015

1116
RE_MATRIX_FLOAT = re.compile(r"[01]\.[0-9]{4}")
@@ -204,3 +209,90 @@ def get_reference_output_data_map() -> dict[partdiff_params_tuple, str]:
204209
dict[partdiff_params_tuple, str]: A dict mapping parameter combinations to the corresponding output.
205210
"""
206211
return dict(iter_reference_output_data())
212+
213+
214+
def ensure_reference_implementation_exists() -> None:
215+
"""Ensure that the reference implementation exists.
216+
217+
If it doesn't exist, `make` is used to automatically compile it.
218+
"""
219+
220+
def is_executable():
221+
executable = REFERENCE_IMPLEMENTATION_EXEC
222+
return executable.exists() and os.access(executable, os.X_OK)
223+
224+
if is_executable():
225+
return
226+
subprocess.check_output(["make", "-C", REFERENCE_IMPLEMENTATION_DIR])
227+
assert is_executable()
228+
229+
230+
def get_reference_output(
231+
partdiff_params: partdiff_params_tuple,
232+
reference_output_data: dict[partdiff_params_tuple, str],
233+
reference_source: ReferenceSource,
234+
) -> str:
235+
"""Acquire the reference output.
236+
237+
Args:
238+
partdiff_params (partdiff_params_tuple): The parameter combination to get the output for.
239+
reference_output_data (dict[partdiff_params_tuple, str]): The cached reference output.
240+
reference_source (ReferenceSource): The source of the reference output (cache, impl, or auto).
241+
242+
Raises:
243+
RuntimeError: When reference_source=cache and the output for a parameter combination isn't cached.
244+
245+
Returns:
246+
str: The reference output for the params.
247+
"""
248+
# Force the number of threads to 1:
249+
partdiff_params = ("1",) + partdiff_params[1:6]
250+
251+
def get_from_cache():
252+
return reference_output_data[partdiff_params]
253+
254+
def get_from_impl():
255+
ensure_reference_implementation_exists()
256+
command_line = [REFERENCE_IMPLEMENTATION_EXEC] + list(partdiff_params)
257+
return subprocess.check_output(command_line).decode("utf-8")
258+
259+
assert reference_source in ReferenceSource
260+
261+
match reference_source:
262+
case ReferenceSource.auto:
263+
if partdiff_params in reference_output_data:
264+
return get_from_cache()
265+
return get_from_impl()
266+
case ReferenceSource.cache:
267+
if partdiff_params not in reference_output_data:
268+
raise RuntimeError(
269+
'Parameter combination "{}" was not found in cache'.format(
270+
" ".join(partdiff_params)
271+
)
272+
)
273+
return get_from_cache()
274+
case ReferenceSource.impl:
275+
return get_from_impl()
276+
277+
278+
def get_actual_output(
279+
partdiff_params: partdiff_params_tuple,
280+
partdiff_executable: list[str],
281+
use_valgrind: bool,
282+
cwd: Path | None,
283+
) -> str:
284+
"""Get the actual output for a parameter combination.
285+
286+
Args:
287+
partdiff_params (partdiff_params_tuple): The parameter combination.
288+
partdiff_executable (list[str]): The executable to run.
289+
use_valgrind (bool): Wether valgrind shall be used.
290+
cwd (Path | None): The working directory of the executable.
291+
292+
Returns:
293+
str: The output of the executable.
294+
"""
295+
command_line = partdiff_executable + list(partdiff_params)
296+
if use_valgrind:
297+
command_line = ["valgrind", "--leak-check=full"] + command_line
298+
return subprocess.check_output(command_line, cwd=cwd).decode("utf-8")

0 commit comments

Comments
 (0)