22import os ,sys
33
44usage = '''
5- python CompareFile.py file1 file2 [accuracy]
5+ python CompareFile.py file1 file2 [accuracy] [-abs 0]
66
77accuracy - the accuracy of two float value.
88 default value is 8, which means
99 pass when the difference of two
1010 value is smaller than 1e-8.
11+ -abs - if compare the absolute value, default is False
1112
1213This script is used to compare whether two files are
1314same.
2021'''
2122
2223def ReadParam ():
23- if len (sys .argv ) not in [3 ,4 ]:
24+ if len (sys .argv ) not in [3 ,4 , 5 , 6 ]:
2425 print (usage )
2526 sys .exit (1 )
2627 file1 = sys .argv [1 ]
2728 file2 = sys .argv [2 ]
2829 accur = 8
29- if len (sys .argv ) == 4 : accur = int (sys .argv [3 ])
30+ absolute = False
31+ if len (sys .argv ) == 4 :
32+ accur = int (sys .argv [3 ])
33+ elif len (sys .argv ) == 5 and sys .argv [3 ] == '-abs' :
34+ absolute = bool (sys .argv [4 ])
35+ elif len (sys .argv ) == 6 :
36+ accur = int (sys .argv [3 ])
37+ absolute = bool (sys .argv [5 ])
38+
3039 epsilon = 0.1 ** accur
31- return file1 ,file2 ,epsilon
40+ return file1 ,file2 ,epsilon , absolute
3241
3342def ReadFile (file1 ,lines ):
3443 if os .path .isfile (file1 ):
@@ -69,7 +78,7 @@ def ExitError(iline,line1,line2,jnumber=-1):
6978
7079
7180if __name__ == "__main__" :
72- file1 ,file2 ,epsilon = ReadParam ()
81+ file1 ,file2 ,epsilon , absolute = ReadParam ()
7382 lines1 = []
7483 lines2 = []
7584 ReadFile (file1 ,lines1 )
@@ -86,8 +95,19 @@ def ExitError(iline,line1,line2,jnumber=-1):
8695 x1 = IsComplex (sline1 [j ])
8796 x2 = IsComplex (sline2 [j ])
8897 if x1 and x2 :
89- if abs (x1 [0 ] - x2 [0 ]) > epsilon : ExitError (i ,sline1 [j ],sline2 [j ],j )
90- if abs (x1 [1 ] - x2 [1 ]) > epsilon : ExitError (i ,sline1 [j ],sline2 [j ],j )
98+ if absolute :
99+ if abs ((x1 [0 ]** 2 + x1 [1 ]** 2 )** 0.5 - (x2 [0 ]** 2 + x2 [1 ]** 2 )** 0.5 ) > epsilon :
100+ ExitError (i ,sline1 [j ],sline2 [j ],j )
101+ else :
102+ if abs (x1 [0 ] - x2 [0 ]) > epsilon :
103+ ExitError (i ,sline1 [j ],sline2 [j ],j )
104+ if abs (x1 [1 ] - x2 [1 ]) > epsilon :
105+ ExitError (i ,sline1 [j ],sline2 [j ],j )
91106 elif IsFloat (sline1 [j ]) and IsFloat (sline2 [j ]):
92- if abs (float (sline1 [j ]) - float (sline2 [j ])) > epsilon : ExitError (i ,sline1 [j ],sline2 [j ],j )
107+ if absolute :
108+ if abs (abs (float (sline1 [j ])) - abs (float (sline2 [j ]))) > epsilon :
109+ ExitError (i ,sline1 [j ],sline2 [j ],j )
110+ else :
111+ if abs (float (sline1 [j ]) - float (sline2 [j ])) > epsilon :
112+ ExitError (i ,sline1 [j ],sline2 [j ],j )
93113 elif sline1 [j ] != sline2 [j ]: ExitError (i ,sline1 [j ],sline2 [j ],j )
0 commit comments