Skip to content

Commit 4c2c92e

Browse files
authored
Merge pull request #150 from intel/lukevalenty/ct_pbt
🧪 add some tuple pbt tests
2 parents 002d596 + 074db41 commit 4c2c92e

File tree

9 files changed

+360
-15
lines changed

9 files changed

+360
-15
lines changed

.github/workflows/unit_tests.yml

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ jobs:
9898
- name: Install build tools
9999
run: |
100100
${{ matrix.install }}
101-
sudo apt install -y ninja-build
101+
sudo apt install -y ninja-build python3-venv python3-pip
102+
python3 -m venv ${{github.workspace}}/test_venv
103+
source ${{github.workspace}}/test_venv/bin/activate
104+
pip install -r ${{github.workspace}}/requirements.txt
105+
echo "${{github.workspace}}/test_venv/bin" >> $GITHUB_PATH
106+
102107
103108
- name: Restore CPM cache
104109
env:
@@ -177,7 +182,11 @@ jobs:
177182
- name: Install build tools
178183
run: |
179184
${{ matrix.install }}
180-
sudo apt install -y ninja-build
185+
sudo apt install -y ninja-build python3-venv python3-pip
186+
python3 -m venv ${{github.workspace}}/test_venv
187+
source ${{github.workspace}}/test_venv/bin/activate
188+
pip install -r ${{github.workspace}}/requirements.txt
189+
echo "${{github.workspace}}/test_venv/bin" >> $GITHUB_PATH
181190
182191
- name: Restore CPM cache
183192
env:
@@ -292,7 +301,11 @@ jobs:
292301
- name: Install build tools
293302
run: |
294303
${{ matrix.install }}
295-
sudo apt install -y ninja-build
304+
sudo apt install -y ninja-build python3-venv python3-pip
305+
python3 -m venv ${{github.workspace}}/test_venv
306+
source ${{github.workspace}}/test_venv/bin/activate
307+
pip install -r ${{github.workspace}}/requirements.txt
308+
echo "${{github.workspace}}/test_venv/bin" >> $GITHUB_PATH
296309
297310
- name: Restore CPM cache
298311
env:
@@ -338,7 +351,11 @@ jobs:
338351

339352
- name: Install build tools
340353
run: |
341-
sudo apt update && sudo apt install -y gcc-${{env.DEFAULT_GCC_VERSION}} g++-${{env.DEFAULT_GCC_VERSION}} ninja-build valgrind
354+
sudo apt update && sudo apt install -y gcc-${{env.DEFAULT_GCC_VERSION}} g++-${{env.DEFAULT_GCC_VERSION}} ninja-build valgrind python3-venv python3-pip
355+
python3 -m venv ${{github.workspace}}/test_venv
356+
source ${{github.workspace}}/test_venv/bin/activate
357+
pip install -r ${{github.workspace}}/requirements.txt
358+
echo "${{github.workspace}}/test_venv/bin" >> $GITHUB_PATH
342359
343360
- name: Restore CPM cache
344361
env:
@@ -409,7 +426,11 @@ jobs:
409426

410427
- name: Install build tools
411428
run: |
412-
sudo apt update && sudo apt install -y clang-${{env.MULL_LLVM_VERSION}} ninja-build
429+
sudo apt update && sudo apt install -y clang-${{env.MULL_LLVM_VERSION}} ninja-build python3-venv python3-pip
430+
python3 -m venv ${{github.workspace}}/test_venv
431+
source ${{github.workspace}}/test_venv/bin/activate
432+
pip install -r ${{github.workspace}}/requirements.txt
433+
echo "${{github.workspace}}/test_venv/bin" >> $GITHUB_PATH
413434
414435
- name: Install mull
415436
env:

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
.cmake-format.yaml
1111
CMakePresets.json
1212
/toolchains
13+
__pycache__
14+
.mypy_cache
15+
.pytest_cache
16+
.hypothesis

include/stdx/tuple_algorithms.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,16 +319,16 @@ template <tuplelike... Ts> constexpr auto cartesian_product_copy(Ts &&...ts) {
319319
return []<typename First, typename... Rest>(First &&first,
320320
Rest &&...rest) {
321321
auto const c = cartesian_product_copy(std::forward<Rest>(rest)...);
322-
return std::forward<First>(first).apply(
323-
[&]<typename... Elems>(Elems &&...elems) {
324-
auto const prepend = [&]<typename E>(E &&e) {
325-
return c.apply([&](auto... subs) {
326-
return make_tuple(tuple_cat(
327-
make_tuple(std::forward<E>(e)), subs)...);
328-
});
329-
};
330-
return tuple_cat(prepend(std::forward<Elems>(elems))...);
331-
});
322+
return std::forward<First>(first).apply([&]<typename... Elems>(
323+
Elems &&...elems) {
324+
[[maybe_unused]] auto const prepend = [&]<typename E>(E &&e) {
325+
return c.apply([&](auto... subs) {
326+
return make_tuple(
327+
tuple_cat(make_tuple(std::forward<E>(e)), subs)...);
328+
});
329+
};
330+
return tuple_cat(prepend(std::forward<Elems>(elems))...);
331+
});
332332
}(std::forward<Ts>(ts)...);
333333
}
334334
}

requirements.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pytest==8.3.3
2+
pytest-forked==1.6.0
3+
pytest-xdist==3.6.1
4+
hypothesis==6.112.5
5+
attrs==24.2.0
6+
execnet==2.1.1
7+
pluggy==1.5.0
8+
sortedcontainers==2.4.0
9+
iniconfig==2.0.0
10+
packaging==24.1
11+
py==1.11.0

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
6464
endif()
6565

6666
add_subdirectory(fail)
67+
add_subdirectory(pbt)

test/pbt/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
if(${CMAKE_CXX_STANDARD} GREATER_EQUAL 20)
2+
add_executable(pbt_prototype_driver EXCLUDE_FROM_ALL
3+
pbt_prototype_driver.cpp)
4+
target_link_libraries(pbt_prototype_driver PUBLIC sanitizers warnings stdx)
5+
6+
add_unit_test(
7+
tuple
8+
PYTEST
9+
FILES
10+
tuple.py
11+
EXTRA_ARGS
12+
-vv
13+
-n2
14+
--compile-commands=${CMAKE_BINARY_DIR}/compile_commands.json
15+
--prototype-driver=${CMAKE_CURRENT_SOURCE_DIR}/pbt_prototype_driver.cpp)
16+
endif()

test/pbt/conftest.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import pytest
2+
import hypothesis
3+
import json
4+
import subprocess
5+
import tempfile
6+
import os
7+
import re
8+
9+
hypothesis.settings.register_profile("ci", max_examples=500)
10+
hypothesis.settings.register_profile("fast", max_examples=10)
11+
12+
13+
14+
def pytest_addoption(parser):
15+
parser.addoption("--compiler", action="store", help="C++ compiler", default=None, required=False)
16+
parser.addoption("--compiler-args", action="store", help="C++ compiler arguments", default="", required=False)
17+
parser.addoption("--includes", action="store", help="C++ include directories", default="", required=False)
18+
19+
parser.addoption("--compile-commands", action="store", help="cmake compiler commands", default=None, required=False)
20+
parser.addoption("--prototype-driver", action="store", help="Prototype .cpp filename to gather compilation command from", default=None, required=False)
21+
22+
@pytest.fixture(scope="module")
23+
def cmake_compilation_command(pytestconfig):
24+
compile_commands_filename = pytestconfig.getoption("compile_commands")
25+
prototype_driver_filename = pytestconfig.getoption("prototype_driver")
26+
27+
if compile_commands_filename is None or prototype_driver_filename is None:
28+
return None
29+
30+
def f(filename):
31+
with open(compile_commands_filename, "r") as f:
32+
db = json.load(f)
33+
for obj in db:
34+
if obj["file"] == prototype_driver_filename:
35+
cmd = obj["command"]
36+
cmd = cmd.replace(prototype_driver_filename, filename)
37+
cmd = re.sub(r"-o .*?\.cpp\.o", f"-o {filename}.o", cmd)
38+
return cmd.split(" ")
39+
40+
return f
41+
42+
@pytest.fixture(scope="module")
43+
def args_compilation_command(pytestconfig):
44+
compiler = pytestconfig.getoption("compiler")
45+
if compiler is None:
46+
return None
47+
48+
include_dirs = [f"-I{i}" for i in pytestconfig.getoption("includes").split(",") if i]
49+
compiler_args = [i for i in pytestconfig.getoption("compiler_args").split(",") if i]
50+
51+
def f(filename):
52+
compile_command = [
53+
compiler, temp_cpp_file_path,
54+
"-o", temp_cpp_file_path + ".o"
55+
] + compiler_args + include_args
56+
return compile_command
57+
58+
return f
59+
60+
61+
62+
@pytest.fixture(scope="module")
63+
def compile(cmake_compilation_command, args_compilation_command):
64+
cmd = cmake_compilation_command
65+
if cmd is None:
66+
cmd = args_compilation_command
67+
68+
def f(code_str):
69+
code_str += "\n"
70+
with tempfile.NamedTemporaryFile(delete=False, suffix=".cpp") as temp_cpp_file:
71+
temp_cpp_file.write(code_str.encode('utf-8'))
72+
temp_cpp_file_path = temp_cpp_file.name
73+
74+
try:
75+
compile_command = cmd(temp_cpp_file_path)
76+
result = subprocess.run(compile_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
77+
78+
if result.returncode == 0:
79+
return True
80+
else:
81+
error_message = (
82+
f"Compiler returned non-zero exit code: {result.returncode}\n"
83+
f"Compilation command: {' '.join(compile_command)}\n"
84+
f"Source code:\n{code_str}\n"
85+
f"Compiler stderr:\n{result.stderr.decode('utf-8')}\n"
86+
f"Compiler stdout:\n{result.stdout.decode('utf-8')}\n"
87+
)
88+
pytest.fail(error_message)
89+
90+
except Exception as e:
91+
pytest.fail(str(e))
92+
finally:
93+
os.remove(temp_cpp_file_path)
94+
if os.path.exists(temp_cpp_file_path + ".out"):
95+
os.remove(temp_cpp_file_path + ".out")
96+
97+
return f
98+

test/pbt/pbt_prototype_driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int main() {}

0 commit comments

Comments
 (0)