Skip to content

Commit f44da94

Browse files
committed
compare unit tests using hashes
1 parent 539c43c commit f44da94

File tree

4 files changed

+17
-28
lines changed

4 files changed

+17
-28
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
# build and test with multiple LLVM versions
2020
- run: docker build --build-arg LLVM_VERSION=${{ matrix.llvm_versions }} -t symqemu .
2121
- run: docker run -t symqemu make -C build check
22-
- run: docker run -t symqemu sh -c "cd tests/symqemu && python3 -m unittest test.py"
22+
- run: docker run -t symqemu sh -c "cd tests/symqemu && python3.11 -m unittest test.py"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ GTAGS
2020
*.swp
2121
*.patch
2222
*.gcov
23+
24+
**/generated_outputs/

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RUN apt update && apt install -y \
66
libglib2.0-dev \
77
llvm \
88
git \
9-
python3 \
9+
python3.11 \
1010
python3-pip \
1111
cmake \
1212
wget \

tests/symqemu/test.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import filecmp
21
import pathlib
32
import shutil
43
import unittest
54
import tempfile
65
import subprocess
76
import os
7+
import hashlib
88

99
import util
1010

@@ -19,33 +19,20 @@ def run_symqemu_and_assert_correct_result(self, binary_name):
1919

2020
util.run_symqemu_on_test_binary(binary_name=binary_name, generated_test_cases_output_dir=symqemu_gen_output_dir)
2121

22-
# `filecmp.dircmp` does a "shallow" comparison, but this is not a problem here because
23-
# the timestamps should always be different, so the actual content of the files will be compared.
24-
# See https://docs.python.org/3/library/filecmp.html#filecmp.dircmp
25-
expected_vs_actual_output_comparison = filecmp.dircmp(symqemu_gen_output_dir, symqemu_ref_output_dir)
22+
expected_hashes = set()
23+
for ref_file in symqemu_ref_output_dir.iterdir():
24+
with open(ref_file, 'rb', buffering=0) as f:
25+
expected_hashes.add(hashlib.file_digest(f, "sha256").hexdigest())
2626

27-
for diff_file in expected_vs_actual_output_comparison.diff_files:
28-
ref_file = symqemu_ref_output_dir / diff_file
29-
gen_file = symqemu_gen_output_dir / diff_file
27+
testcase_not_found = False
28+
for gen_file in symqemu_gen_output_dir.iterdir():
29+
with open(gen_file, 'rb', buffering=0) as f:
30+
f_hash = hashlib.file_digest(f, "sha256").hexdigest()
31+
if not f_hash in expected_hashes:
32+
print(f"Error: content of file {gen_file} not found in expected testcases.");
33+
testcase_not_found = True
3034

31-
tmp_ref = tempfile.NamedTemporaryFile("w+")
32-
subprocess.run(["xxd", f"{ref_file}"], stdout=tmp_ref, check=True)
33-
34-
tmp_gen = tempfile.NamedTemporaryFile("w+")
35-
subprocess.run(["xxd", f"{gen_file}"], stdout=tmp_gen, check=True)
36-
37-
wdiff = subprocess.run(["wdiff", f"{tmp_ref.name}", f"{tmp_gen.name}"], capture_output=True)
38-
colordiff = subprocess.run(["colordiff"], input=wdiff.stdout, capture_output=True)
39-
40-
print(f"===== Diff found in {diff_file} ======")
41-
print(f"{colordiff.stdout.decode('utf-8').strip()}")
42-
print(f"=================================")
43-
print()
44-
45-
self.assertEqual(expected_vs_actual_output_comparison.diff_files, [])
46-
self.assertEqual(expected_vs_actual_output_comparison.left_only, [])
47-
self.assertEqual(expected_vs_actual_output_comparison.right_only, [])
48-
self.assertEqual(expected_vs_actual_output_comparison.funny_files, [])
35+
self.assertFalse(testcase_not_found)
4936

5037
def test_simple(self):
5138
self.run_symqemu_and_assert_correct_result('simple')

0 commit comments

Comments
 (0)