33The 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-
106import pytest
117
128import util
139from 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
10612def 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
0 commit comments