1- import filecmp
21import pathlib
32import shutil
43import unittest
54import tempfile
65import subprocess
76import os
7+ import hashlib
88
99import util
1010
@@ -19,33 +19,30 @@ 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 )
26-
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
30-
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 , [])
22+ expected_hashes = {}
23+ for ref_file in symqemu_ref_output_dir .iterdir ():
24+ with open (ref_file , 'rb' , buffering = 0 ) as f :
25+ f_hash = hashlib .file_digest (f , "sha256" ).hexdigest ()
26+ expected_hashes [f_hash ] = [False , ref_file ]
27+
28+ testcase_not_found = False
29+ for gen_file in symqemu_gen_output_dir .iterdir ():
30+ with open (gen_file , 'rb' , buffering = 0 ) as f :
31+ f_hash = hashlib .file_digest (f , "sha256" ).hexdigest ()
32+ ret = expected_hashes .get (f_hash )
33+ if ret is not None :
34+ ret [0 ] = True
35+ expected_hashes [f_hash ] = ret
36+ else :
37+ print (f"Error: content of file { gen_file } not found in expected testcases." );
38+ testcase_not_found = True
39+
40+ for (is_found , fname ) in expected_hashes .values ():
41+ # (is_found, fname) = tuple(ret)
42+ if not is_found :
43+ print (f"Warning: expected testcase { fname } has not been generated." )
44+
45+ self .assertFalse (testcase_not_found )
4946
5047 def test_simple (self ):
5148 self .run_symqemu_and_assert_correct_result ('simple' )
0 commit comments