2025-07-28 22:41:02 +08:00
#!/usr/bin/env python
#encoding=utf-8
import os
def parse_value ( value ) :
""" Parse a value with bit-width specification and return its integer representation and mask.
The mask will have 1 s where the bits are valid and 0 s where they are ' x ' .
"""
if ' h ' in value . lower ( ) :
base = 16
prefix_length = value . find ( ' h ' ) + 1
elif ' o ' in value . lower ( ) :
base = 8
prefix_length = value . find ( ' o ' ) + 1
elif ' b ' in value . lower ( ) :
base = 2
prefix_length = value . find ( ' b ' ) + 1
else :
raise ValueError ( f " Invalid format for value: { value } " )
# Extract the numeric part
num_str = value [ prefix_length : ]
# Initialize parsed value and mask
parsed_value = 0
mask = 0
# Process each character in the numeric part
#print(num_str)
for char in num_str :
if char . lower ( ) == ' x ' :
mask = mask * base
parsed_value = parsed_value * base
else :
mask = ( mask * base ) + ( base - 1 )
parsed_value = ( parsed_value * base ) + int ( char , base )
#print(value, hex(parsed_value), hex(mask))
return parsed_value , mask
def compare_files ( source_file , target_file ) :
2025-09-07 07:37:34 +08:00
warning_need_recheck = 0
2025-07-28 22:41:02 +08:00
try :
with open ( source_file , ' r ' ) as src , open ( target_file , ' r ' ) as tgt :
source_lines = src . readlines ( )
target_lines = tgt . readlines ( )
if len ( source_lines ) != len ( target_lines ) :
print ( f " Error: The number of lines in { source_file } and { target_file } do not match. " )
return
all_match = True
for i , ( src_line , tgt_line ) in enumerate ( zip ( source_lines , target_lines ) ) :
src_values = src_line . strip ( ) . split ( )
tgt_values = tgt_line . strip ( ) . split ( )
if len ( src_values ) != len ( tgt_values ) :
2025-09-07 00:52:17 +08:00
if len ( src_values ) != 1 :
print ( f " Error: Line { i + 1 } : The number of values in { source_file } and { target_file } do not match. " )
all_match = False
continue
else :
if src_values [ 0 ] . strip ( ) . lower ( ) == ' x ' :
2025-09-07 07:37:34 +08:00
if warning_need_recheck == 0 :
print ( f ' [ \033 [93mWARNING \033 [0m] Ignored lines detected. This test case requires manual comparison. \n \t [ \033 [93mTARGET \033 [0m] { target_file } \t [ \033 [93mSRC \033 [0m] { source_file } ' )
warning_need_recheck = 1
2025-09-07 00:52:17 +08:00
continue
2025-07-28 22:41:02 +08:00
line_match = True
for j , ( src_val , tgt_val ) in enumerate ( zip ( src_values , tgt_values ) ) :
try :
src_int , src_mask = parse_value ( src_val )
tgt_int , tgt_mask = parse_value ( tgt_val )
except ValueError as e :
print ( f " Error: Line { i + 1 } , Position { j + 1 } : { e } " )
line_match = False
all_match = False
continue
# Combine masks to determine which bits to compare
combined_mask = src_mask #& tgt_mask
# Compare only the bits that are not 'x' in either source or target
if combined_mask == 0 :
#print(f"Line {i+1}, Position {j+1}: Both values contain 'x' at all positions, skipping check.")
continue
masked_src = src_int & combined_mask
masked_tgt = tgt_int & combined_mask
#print(hex(masked_src), hex(masked_tgt), hex(combined_mask))
if masked_src == masked_tgt :
#print(f"Line {i+1}, Position {j+1}: Values match ({src_val} == {tgt_val}).")
pass
else :
2025-08-07 09:43:14 +08:00
print ( f " \t Line { i + 1 } , Position { j + 1 } : Values differ ( { src_val } != { tgt_val } ). " )
2025-07-28 22:41:02 +08:00
line_match = False
all_match = False
if line_match :
#print(f"Line {i+1}: All values on this line match.")
pass
if all_match :
#print("All lines match.")
#pass
return True
else :
#print("Some lines or positions do not match.")
#pass
return False
except FileNotFoundError as e :
print ( f " Error: { e } " )
except Exception as e :
print ( f " An unexpected error occurred: { e } " )
return False
def list_files_in_directory ( directory ) :
try :
# 获取目录中的所有文件和子目录
files = os . listdir ( directory )
# 过滤出文件(排除子目录)
files_only = [ f for f in files if os . path . isfile ( os . path . join ( directory , f ) ) ]
return files_only
except FileNotFoundError :
print ( f " Error: The directory ' { directory } ' does not exist. " )
return [ ]
except PermissionError :
print ( f " Error: Permission denied to access the directory ' { directory } ' . " )
return [ ]
except Exception as e :
print ( f " An unexpected error occurred: { e } " )
return [ ]
if __name__ == " __main__ " :
files = list_files_in_directory ( ' generated/log ' )
src_files = list_files_in_directory ( ' tb/shinTest/testcase_result ' )
for fi in files :
rst_f = ' generated/log/ ' + fi
src_f = ' tb/shinTest/testcase_result/ ' + fi
2025-08-13 00:30:21 +08:00
outmsg = f " TESTCASE: { fi } "
outmsg = outmsg + " " * ( 60 - len ( outmsg ) )
2025-07-28 22:41:02 +08:00
if not ( fi in src_files ) :
2025-08-13 00:30:21 +08:00
print ( f " { outmsg } [ \033 [93mIGNORE \033 [0m] " )
2025-07-28 22:41:02 +08:00
continue
if compare_files ( src_f , rst_f ) :
2025-08-13 00:30:21 +08:00
print ( f " { outmsg } [ \033 [92mPASS \033 [0m] " )
2025-07-28 22:41:02 +08:00
else :
2025-08-13 00:30:21 +08:00
print ( f " { outmsg } [ \033 [91mFAILED \033 [0m] " )