9 Comparison of two text files containing Test log
12 Comparison of floats uses a maximum relative difference:
13 (float1 - float2)/float1 <= tolerance.
15 Comparison of integers uses a maximum diffrence:
16 integer1 - integer2 <= difference
18 Comparison of strings is exact
20 Failure results in a return code of 1.
23 compare.py run_file ref_file float_tolerance integer_difference
34 sline1 = re.split(
'\s+|=', line1)
35 sline2 = re.split(
'\s+|=', line2)
39 for n
in range(len(sline1)):
42 nnm1 = rennm.findall(sline1[n])
43 flt1 = reflt.findall(sline1[n])
44 int1 = reint.findall(sline1[n])
45 dat1 = redat.findall(sline1[n])
47 nnm2 = rennm.findall(sline2[n])
48 flt2 = reflt.findall(sline2[n])
49 int2 = reint.findall(sline2[n])
50 dat2 = redat.findall(sline2[n])
59 print(
"Non numeric string mismatch at line "+str(lnum)+
": "\
60 +nnm1[0]+
" not equal to "+nnm2[0])
65 flt1a = reflte.findall(flt1[0])
66 flt2a = reflte.findall(flt2[0])
67 rdiff = abs(float(flt1a[0])-float(flt2a[0]))/(abs(float(flt1a[0]))+1.0e-6)
68 if (
not rdiff <= ftol):
70 print(
"Float mismatch at line "+str(lnum)+
": "+\
71 flt1a[0],
" not equal to ",flt2a[0],
" with max relative difference ", ftol,\
72 " Actual relative difference = ",rdiff)
77 int1a = reinte.findall(int1[0])
78 int2a = reinte.findall(int2[0])
79 adiff = abs(int(int1a[0])-int(int2a[0]))
80 if (
not adiff <= idif):
82 print(
"Integer mismatch at line "+str(lnum)+
": ",\
83 int1a[0]+
" not equal to "+int2a[0],
" with max difference ", idif,\
84 ". Actual difference = ",adiff)
91 print(
"Data string mismatch at line "+str(lnum)+
": "+\
92 dat1[0]+
" not equal to "+dat2[0])
98 flt1a = reflte.findall(flt1[0])
99 int2a = reinte.findall(int2[0])
100 rdiff = abs(float(flt1a[0])-float(int2a[0]))/(abs(float(flt1a[0]))+1.0e-6)
101 if (
not rdiff <= ftol):
102 lineerror=lineerror+1
103 print(
"Float mismatch at line "+str(lnum)+
": "+\
104 flt1a[0],
" not equal to ",int2a[0],
" with max relative difference ", ftol,\
105 " Actual relative difference = ",rdiff)
109 int1a = reinte.findall(int1[0])
110 flt2a = reflte.findall(flt2[0])
111 rdiff = abs(float(int1a[0])-float(flt2a[0]))/(abs(float(int1a[0]))+1.0e-6)
112 if (
not rdiff <= ftol):
113 lineerror=lineerror+1
114 print(
"Float mismatch at line "+str(lnum)+
": "+\
115 int1a[0],
" not equal to ",flt2a[0],
" with max relative difference ", ftol,\
116 " Actual relative difference = ",rdiff)
120 if sline1[n] != sline2[n]:
121 print(
"In looping through line elements did not match either non numeric, float, date or integer. Error at line "\
122 +str(lnum),
". Trying to compare \'"+sline1[n]+
"\' and \'"+sline2[n]+
"\'")
126 if sline1[n] != sline2[n]:
127 print(
"In looping through line elements matched multipe of non numeric, float, date and integer. Error at line "\
128 +str(lnum)+
". Trying to compare \'"+sline1[n]+
"\' and \'"+sline2[n]+
"\'")
136 file_run = open(sys.argv[1],
"r")
137 file_ref = open(sys.argv[2],
"r")
138 ftol = float(sys.argv[3])
139 idif = int(sys.argv[4])
142 file_runref = open(str(sys.argv[1])+
".ref",
"w")
145 lines_ref = file_ref.readlines()
150 rennm = re.compile(
'(^\D+[\d]*[\D]*$)')
153 reflt = re.compile(
'(^[\D]*?[-]?\d+\.\d+(?:[e][+-]?[\d]+)?[\,]?$)')
156 reint = re.compile(
'(^[-]?\d+[\,]?$)')
159 redat = re.compile(
'(^\d{4}[-]\d{2}[-]\d{2}[T]\d{2}[:]\d{2}[:]\d{2}[Z][\:]?$)')
162 reflte = re.compile(
'([+-]?\d+\.\d+(?:[e][+-]?[\d]+)?)')
163 reinte = re.compile(
'([+-]?\d+)')
170 for line_run
in file_run:
171 if re.search(
'Test : ', line_run):
174 line_strip = line_run[line_run.find(
'Test : '):]
177 lineerror =
line_diff(line_strip,lines_ref[refline],refline+1,ftol,idif)
178 error=error+lineerror
181 file_runref.write(line_strip)
184 refline = refline + 1
188 if (len(lines_ref) != refline):
189 print(
"Test failed. "+str(refline-1)+
" matches in run, "+str(len(lines_ref))+
" ref")
199 print(
"Did not find any instances of \'Test : \' in run file")
def line_diff(line1, line2, lnum, ftol, idif)