2020-05-28 16:55:48 +00:00
#!/usr/bin/env python3
2008-11-21 16:45:46 +00:00
import os , sys , re , random
### Constants:
2024-06-30 18:05:10 +00:00
os . environ [ " LIBCPUID_NO_WARN " ] = " 1 "
2024-06-29 15:01:50 +00:00
delimiter = " - " * 80
fields_x86 = [ " architecture " , " feature-level " , " purpose " , " family " , " model " , " stepping " ,
" extfamily " , " extmodel " , " cores " , " logical " ,
2022-09-20 05:40:33 +00:00
" l1d-cache " , " l1i-cache " , " l2-cache " , " l3-cache " , " l4-cache " ,
" l1d-assoc " , " l1i-assoc " , " l2-assoc " , " l3-assoc " , " l4-assoc " ,
" l1d-cacheline " , " l1i-cacheline " , " l2-cacheline " , " l3-cacheline " , " l4-cacheline " ,
" l1d-instances " , " l1i-instances " , " l2-instances " , " l3-instances " , " l4-instances " ,
" sse-size " , " codename " , " flags " ]
2024-06-29 15:01:50 +00:00
fields_arm = [ " architecture " , " feature-level " , " purpose " ,
" implementer " , " variant " , " part-num " , " revision " ,
2024-06-28 18:41:00 +00:00
" cores " , " logical " ,
" codename " , " flags " ]
2008-11-21 16:45:46 +00:00
args = sys . argv
2014-09-23 12:20:27 +00:00
fix = False
2016-03-10 00:44:17 +00:00
show_test_fast_warning = False
2008-11-21 16:45:46 +00:00
2014-07-16 07:10:51 +00:00
if len ( args ) < 3 :
2020-05-28 16:55:48 +00:00
print ( """
2014-09-23 12:20:27 +00:00
Usage : run_tests . py < cpuid_tool binary > < test file / dir > [ test file / dir . . . ] [ OPTIONS ]
2014-07-16 07:10:51 +00:00
If a test file is given , it is tested by itself .
If a directory is given , process all * . test files there , subdirectories included .
2014-09-23 12:20:27 +00:00
If the - - fix option is given , the behaviour of the cpuid_tool binary is deemed correct
and any failing tests are updated .
2020-05-28 16:55:48 +00:00
""" )
2008-11-21 16:45:46 +00:00
sys . exit ( 1 )
2014-07-16 07:10:51 +00:00
filelist = [ ]
cpuid_tool = args [ 1 ]
for arg in args [ 2 : ] :
2014-09-23 12:20:27 +00:00
if arg == " --fix " :
fix = True
continue
2016-03-10 00:44:17 +00:00
if arg == " --show-test-fast-warning " :
show_test_fast_warning = True
continue
2014-07-16 07:10:51 +00:00
if os . path . isdir ( arg ) :
# gather all *.test files from subdirs amd and intel:
for dirpath , dirnames , filenames in os . walk ( arg ) :
filelist + = [ os . path . join ( dirpath , fn ) for fn in filenames if fn [ - 5 : ] == " .test " ]
else :
filelist . append ( arg )
#f = open(args[1], "rt")
#lines = f.readlines()
#f.close()
2008-11-21 16:45:46 +00:00
# One would usually use os.tempnam, but libc gives off hell a lot of
# warnings when you attempt to use that :(
def make_tempname ( prefix ) :
chars = " "
2020-05-28 16:55:48 +00:00
for i in range ( 26 ) :
2008-11-21 16:45:46 +00:00
chars + = chr ( 97 + i )
chars + = chr ( 65 + i )
2020-05-28 16:55:48 +00:00
for i in range ( 10 ) :
2008-11-21 16:45:46 +00:00
chars + = chr ( 48 + i )
2020-05-28 16:55:48 +00:00
for i in range ( 6 ) :
2008-11-21 16:45:46 +00:00
prefix + = random . choice ( chars )
return prefix
2008-12-08 16:52:01 +00:00
def fmt_error ( err ) :
pfix = " %s : " % err [ 0 ]
return " %s expected ` %s ' \n %s got ` %s ' " % ( pfix , err [ 1 ] , " " * len ( pfix ) , err [ 2 ] )
2014-09-23 12:20:27 +00:00
def fixFile ( filename , input_lines , output_lines ) :
f = open ( filename , " wt " )
2020-05-28 16:55:48 +00:00
f . writelines ( [ s + " \n " for s in input_lines ] )
2024-07-01 07:44:07 +00:00
f . write ( delimiter + " \n " )
2020-05-28 16:55:48 +00:00
f . writelines ( [ s + " \n " for s in output_lines ] )
2014-09-23 12:20:27 +00:00
f . close ( )
2022-09-15 16:37:08 +00:00
def do_test ( inp , expected_out , binary , test_file_name , num_cpu_type ) :
2008-11-21 16:45:46 +00:00
fninp = make_tempname ( " cpuidin " )
fnoutp = make_tempname ( " cpuidout " )
f = open ( fninp , " wt " )
2020-05-28 16:55:48 +00:00
f . writelines ( [ s + " \n " for s in inp ] )
2008-11-21 16:45:46 +00:00
f . close ( )
2024-06-28 18:41:00 +00:00
architecture = os . popen ( " %s --load= %s --architecture " % ( binary , fninp ) ) . read ( ) . splitlines ( ) [ - 1 ]
if architecture == " x86 " :
fields = fields_x86
elif architecture == " ARM " :
fields = fields_arm
else :
fields = [ ]
2020-05-28 16:55:48 +00:00
cmd = " %s --load= %s --outfile= %s %s " % ( binary , fninp , fnoutp , " " . join ( [ " -- " + s for s in fields ] ) )
2008-11-21 16:45:46 +00:00
os . system ( cmd )
os . unlink ( fninp )
real_out = [ ]
2024-08-25 19:05:36 +00:00
real_out_delim = [ ] # when fixing the file, delimiters are required
2008-11-21 16:45:46 +00:00
try :
f = open ( fnoutp , " rt " )
2022-09-15 16:37:08 +00:00
for s in f . readlines ( ) :
2024-08-25 19:05:36 +00:00
real_out_delim . append ( s . strip ( ) )
2024-06-29 15:01:50 +00:00
if delimiter not in s :
2022-09-15 16:37:08 +00:00
real_out . append ( s . strip ( ) )
2008-11-21 16:45:46 +00:00
f . close ( )
os . unlink ( fnoutp )
except IOError :
return " Exception "
2022-09-15 16:37:08 +00:00
if len ( real_out ) != len ( expected_out ) or len ( real_out ) != len ( fields ) * num_cpu_type :
2024-06-29 15:01:50 +00:00
if fix :
2024-08-25 19:05:36 +00:00
fixFile ( test_file_name , inp , real_out_delim )
2024-06-29 15:01:50 +00:00
return " Number of records, fixed. "
else :
return " Unexpected number of records returned \n - expected length %d \n - real length %d \n - %d fields " % ( len ( expected_out ) , len ( real_out ) , len ( fields ) * num_cpu_type )
2008-11-21 16:45:46 +00:00
err_fields = [ ]
for i in range ( len ( real_out ) ) :
if real_out [ i ] != expected_out [ i ] :
2022-09-15 16:37:08 +00:00
err_fields . append ( ( fields [ i % len ( fields ) ] , expected_out [ i ] , real_out [ i ] ) )
2008-11-21 16:45:46 +00:00
if not err_fields :
return " OK "
else :
2014-09-23 12:20:27 +00:00
if fix :
2024-08-25 19:05:36 +00:00
fixFile ( test_file_name , inp , real_out_delim )
2014-09-23 12:20:27 +00:00
return " Mismatch, fixed. "
else :
2020-05-09 15:34:07 +00:00
return " Mismatch in fields: \n %s " % " \n " . join ( [ fmt_error ( err ) for err in err_fields ] )
2008-11-21 16:45:46 +00:00
errors = False
2020-05-28 16:55:48 +00:00
print ( " Testing... " )
2014-07-16 07:10:51 +00:00
for test_file_name in filelist :
2022-09-15 16:37:08 +00:00
num_cpu_type = 0
2014-07-16 07:10:51 +00:00
current_input = [ ]
current_output = [ ]
build_output = False
with open ( test_file_name , " rt " ) as f :
for line in f . readlines ( ) :
2022-09-15 16:37:08 +00:00
line = line . strip ( )
if line == delimiter :
2014-07-16 07:10:51 +00:00
build_output = True
2022-09-15 16:37:08 +00:00
num_cpu_type + = 1
2014-07-16 07:10:51 +00:00
else :
if build_output :
2022-09-15 16:37:08 +00:00
current_output . append ( line )
2014-07-16 07:10:51 +00:00
else :
2022-09-15 16:37:08 +00:00
current_input . append ( line )
2014-07-16 07:10:51 +00:00
#codename = current_output[len(current_output) - 2]
2022-09-15 16:37:08 +00:00
result = do_test ( current_input , current_output , cpuid_tool , test_file_name , num_cpu_type )
2020-05-28 16:55:48 +00:00
print ( " Test [ %s ]: %s " % ( test_file_name [ : - 5 ] , result ) )
2014-07-16 07:10:51 +00:00
if result != " OK " :
errors = True
build_output = False
2008-11-21 16:45:46 +00:00
2016-03-10 00:44:17 +00:00
if errors :
if show_test_fast_warning :
2020-05-28 16:55:48 +00:00
print ( """
2016-03-10 00:44:17 +00:00
You ' re running tests in fast mode; before taking any action on the errors
2020-05-28 16:55:48 +00:00
above , please confirm that the slow mode ( ' make test-old ' ) also fails . """ )
2022-09-20 04:53:16 +00:00
sys . exit ( 1 )
2016-03-10 00:44:17 +00:00
else :
2020-05-28 16:55:48 +00:00
print ( " All successfull! " )
2022-09-20 04:53:16 +00:00
sys . exit ( 0 )