Files
eb001/tb/shinTest/check_testcase.py

148 lines
5.0 KiB
Python
Executable File

#!/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 1s where the bits are valid and 0s 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):
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):
print(f"Error: Line {i+1}: The number of values in {source_file} and {target_file} do not match.")
all_match = False
continue
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:
print(f"\tLine {i+1}, Position {j+1}: Values differ ({src_val} != {tgt_val}).")
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
if not (fi in src_files):
print(f"Testcase:{fi}\t\t\t[\033[93mIGNORE\033[0m]")
continue
if compare_files(src_f, rst_f):
print(f"Testcase:{fi}\t\t\t[\033[92mPASS\033[0m]")
else:
print(f"Testcase:{fi}\t\t\t[\033[91mFAILED\033[0m]")