Debian Codegraph @

contrib/iapbs/test/ndiff.awk

raw
1 ### -*-awk-*-
2 ### ====================================================================
3 ### @Awk-file{
4 ### author = "Nelson H. F. Beebe",
5 ### version = "1.00",
6 ### date = "28 January 2000",
7 ### time = "08:38:46 MST",
8 ### filename = "ndiff.awk",
9 ### copyright = "Copyright (c) 2000 Nelson H. F. Beebe. This
10 ### code is licensed under the GNU General Public
11 ### License, version 2 or later.",
12 ### address = "Center for Scientific Computing
13 ### University of Utah
14 ### Department of Mathematics, 322 INSCC
15 ### 155 S 1400 E RM 233
16 ### Salt Lake City, UT 84112-0090
17 ### USA",
18 ### telephone = "+1 801 581 5254",
19 ### FAX = "+1 801 585 1640, +1 801 581 4148",
20 ### URL = "http://www.math.utah.edu/~beebe",
21 ### checksum = "10489 476 1904 14057",
22 ### email = "[email protected], [email protected],
23 ### [email protected] (Internet)",
24 ### codetable = "ISO/ASCII",
25 ### keywords = "numerical file differencing",
26 ### supported = "yes",
27 ### docstring = "This program compares two putatively similar
28 ### files, ignoring small numeric differences.
29 ### Complete documentation can be found in the
30 ### accompanying UNIX manual page file,
31 ### ndiff.man.
32 ###
33 ### Usage:
34 ### awk -f ndiff.awk \
35 ### [-v ABSERR=x] \
36 ### [-v FIELDS=n1a-n1b,n2,n3a-n3b,...] \
37 ### [-v FS=regexp] \
38 ### [-v MINWIDTH=n] \
39 ### [-v QUIET=n] \
40 ### [-v RELERR=x] \
41 ### [-v SILENT=n] \
42 ### infile1 infile2
43 ###
44 ### The checksum field above contains a CRC-16
45 ### checksum as the first value, followed by the
46 ### equivalent of the standard UNIX wc (word
47 ### count) utility output of lines, words, and
48 ### characters. This is produced by Robert
49 ### Solovay's checksum utility.",
50 ### }
51 ### ====================================================================
52
53 BEGIN \
54 {
55 initialize()
56
57 compare_files(ARGV[1], ARGV[2])
58
59 exit (Ndiff != 0)
60 }
61
62
63 function abs(a)
64 {
65 ## Return the absolute value of the argument.
66
67 return ((a < 0) ? -a : a)
68 }
69
70
71 function awkfloat(s)
72 {
73 ## Convert a numeric string to an awk floating-point number, and
74 ## return the result as a floating-point number.
75 ##
76 ## Fortran use has any of E, e, D, d, Q, or q, or even nothing at
77 ## all, for the exponent letter, but awk and C only allow E and e.
78 ##
79 ## Ada usefully permits nonsignificant underscores for
80 ## readability: 3.14159265358979323846 and
81 ## 3.14159_26535_89793_23846 are equivalent.
82 ##
83 ## We can safely assume that there are no leading or trailing
84 ## whitespace characters, because all strings passed to this
85 ## function are the result of splitting lines into
86 ## whitespace-delimited fields.
87
88 gsub("_","",s) # remove Ada-style separators
89 gsub("[DdQq]","e",s) # convert Fortran exponent letters to awk-style
90 if (match(s,"[0-9.][-+][0-9]+$")) # then letter-less exponent
91 s = substr(s,1,RSTART) "e" substr(s,RSTART+1) # insert exponent letter e
92 return (0 + s) # coerce to a number
93 }
94
95
96 function compare_all(f1line,f2line,f1parts,f2parts,n, k)
97 {
98 ## Compare all fields in f1line and f2line, assuming that they have
99 ## already been split into n parts in f1parts[] and f2parts[].
100 ##
101 ## If any fields differ, print a diff-style report, and increment
102 ## global variable Ndiff,
103
104 for (k = 1; k <= n; ++k)
105 {
106 if (diff_field(f1parts[k], f2parts[k], k) != 0)
107 {
108 report_difference(f1line,f2line,k)
109 return
110 }
111 }
112 }
113
114
115 function compare_files(file1,file2, f1line,f2line,f1parts,f2parts,n1,n2)
116 {
117 ## Compare all lines in two files, printing a diff-style report of
118 ## differences. If any numeric differences have been found, print a
119 ## one-line report of which matching line had the largest numeric
120 ## difference. Finally, print a diagnostic if the files differ in
121 ## length.
122
123 NRLINE = 0
124 while (((getline f1line < file1) > 0) && \
125 ((getline f2line < file2) > 0))
126 {
127 NRLINE++
128 n1 = split(f1line,f1parts)
129 n2 = split(f2line,f2parts)
130 if (n1 == n2)
131 {
132 if (N_Fields == 0)
133 compare_all(f1line,f2line,f1parts,f2parts,n1)
134 else
135 compare_some(f1line,f2line,f1parts,f2parts,n1)
136 }
137 else
138 report_difference(f1line,f2line,max(n1,n2))
139 }
140 if (QUIET == 0)
141 {
142 if (Max_Abserr > 0)
143 printf("### Maximum absolute error in matching lines = %.2e at line %d field %d\n", \
144 Max_Abserr, Max_Abserr_NR, Max_Abserr_NF)
145 if (Max_Relerr > 0)
146 printf("### Maximum relative error in matching lines = %.2e at line %d field %d\n", \
147 Max_Relerr, Max_Relerr_NR, Max_Relerr_NF)
148 }
149 if ((getline f1line < file1) > 0)
150 warning("file " file2 " is short")
151 if ((getline f2line < file2) > 0)
152 warning("file " file1 " is short")
153 }
154
155
156 function compare_some(f1line,f2line,f1parts,f2parts,n, k,m)
157 {
158 ## Compare selected fields in f1line and f2line, assuming that they
159 ## have already been split into n parts in f1parts[] and f2parts[].
160 ## The globals (N_Fields, Fields[]) define which fields are to be
161 ## compared.
162 ##
163 ## If any fields differ, print a diff-style report, and increment
164 ## global variable Ndiff.
165
166 for (k = 1; (k <= N_Fields) && (k <= n); ++k)
167 {
168 m = Fields[k]
169 if ((m <= n) && (diff_field(f1parts[m], f2parts[m], m) != 0))
170 {
171 report_difference(f1line,f2line,m)
172 return
173 }
174 }
175 }
176
177
178 function diff_field(field1,field2,nfield)
179 {
180 ## If both fields are identical as strings, return 0.
181 ##
182 ## Otherwise, if both fields are numeric, return 0 if they are close
183 ## enough (as determined by the globals ABSERR and RELERR), or are
184 ## both ignorable (as determined by MINWIDTH), and otherwise return
185 ## 1.
186 ##
187 ## Otherwise, return 1.
188 ##
189 ## The computed absolute and relative errors are saved in global
190 ## variables (This_Abserr and This_Relerr) for later use in
191 ## diagnostic reports. These values are always zero for
192 ## nonnumeric fields.
193
194 This_Abserr = 0
195 This_Relerr = 0
196
197 if (field1 == field2) # handle the commonest, and easiest, case first
198 return (0)
199 else if ((field1 ~ NUMBER_PATTERN) && (field2 ~ NUMBER_PATTERN))
200 {
201 ## Handle MINWIDTH test while the fields are still strings
202 if (ignore(field1) && ignore(field2))
203 return (0)
204
205 ## Now coerce both fields to floating-point numbers,
206 ## converting Fortran-style exponents, if necessary.
207 field1 = awkfloat(field1)
208 field2 = awkfloat(field2)
209
210 This_Abserr = abs(field1 - field2)
211 This_Relerr = maxrelerr(field1,field2)
212 if ( ((ABSERR != "") && (This_Abserr > ABSERR)) || \
213 ((RELERR != "") && (This_Relerr > RELERR)) )
214 {
215 if (This_Abserr > Max_Abserr)
216 {
217 Max_Abserr_NF = nfield
218 Max_Abserr_NR = NRLINE
219 Max_Abserr = This_Abserr
220 }
221 if (This_Relerr > Max_Relerr)
222 {
223 Max_Relerr_NF = nfield
224 Max_Relerr_NR = NRLINE
225 Max_Relerr = This_Relerr
226 }
227 return (1)
228 }
229 else
230 return (0)
231 }
232 else
233 return (1)
234 }
235
236
237 function error(message)
238 {
239 ## Issue an error message and terminate with a failing status code.
240
241 warning("ERROR: " message)
242 exit(1)
243 }
244
245
246 function ignore(field)
247 {
248 ## Return 1 if field is ignorable, because it is shorter than
249 ## MINWIDTH and appears to be a real number. Otherwise, return 0.
250
251 return ((MINWIDTH > 0) && \
252 (length(field) < MINWIDTH) && \
253 (field ~ "[.DdEeQq]"))
254 }
255
256
257 function initialize( eps)
258 {
259 ## Process command-line options, and initialize global variables.
260
261 Stderr = "/dev/stderr"
262
263 Macheps = machine_epsilon()
264
265 if (ABSERR != "")
266 ABSERR = abs(awkfloat(ABSERR)) # coerce to positive number
267
268 if (RELERR != "")
269 {
270 RELERR = abs(awkfloat(RELERR)) # coerce to positive number
271 if (RELERR < Macheps)
272 warning("RELERR = " RELERR " is below machine epsilon " Macheps)
273 else if (RELERR >= 1) # RELERR=nnn means nnn*(machine epsilon)
274 RELERR *= Macheps
275 }
276
277 if ((ABSERR == "") && (RELERR == "")) # supply default (see man pages)
278 RELERR = max(1.0e-15, 8.0 * Macheps)
279
280 ## printf( "RELERR is %15.10f\n", RELERR )
281 ## Coerce remaining options to numbers
282 MINWIDTH += 0
283 QUIET += 0
284 SILENT += 0
285
286 Max_Relerr = 0
287 Max_Relerr_NR = 0
288 Max_Relerr_NF = 0
289
290 Max_Abserr = 0
291 Max_Abserr_NR = 0
292 Max_Abserr_NF = 0
293
294 This_Abserr = 0
295 This_Relerr = 0
296
297 if (FIELDS != "")
298 initialize_fields()
299 else
300 N_Fields = 0
301
302 ## The precise value of this regular expression to match both an
303 ## integer and a floating-point number is critical, and documented
304 ## in the accompanying manual page: it must match not only the
305 ## awk- and C-style -nnn, -n.nnn, and -n.nnne+nn, but also the
306 ## Fortran styles -nnn, -n.nnn, -n.D+nn, -.nnD+nn, -nD+nn,
307 ## -n.nnnQ+nn, -n.nnnd+nn, and -n.nnn+nnn. The Fortran forms will
308 ## be converted by awkfloat() to awk-form. Ada permits an
309 ## nonsignificant underscore between digits, so we support that
310 ## too.
311
312 NUMBER_PATTERN = "^[-+]?([0-9](_?[0-9])*([.]?([0-9](_?[0-9])*)*)?|[.][0-9](_?[0-9])*)([DdEeQq]?[-+]?[0-9](_?[0-9])*)?$"
313
314 Ndiff = 0
315 if (ARGC != 3)
316 error("Incorrect argument count\n\tUsage: awk -f ndiff.awk [-v ABSERR=x] [-v FIELDS=n1a-n1b,n2,n3a-n3b,...] [-v FS='regexp'] [-v MINWIDTH=n] [-v RELERR=x] infile1 infile2")
317 }
318
319
320 function initialize_fields( j,k,m,n,numbers,parts)
321 {
322 ## Convert a FIELDS=n1a-n1b,n2,n3a-n3b,... specification to a list
323 ## of N_Fields numbers in Fields[].
324
325 N_Fields = 0
326 n = split(FIELDS,parts,",")
327 for (k = 1; k <= n; ++k)
328 {
329 m = split(parts[k],numbers,"-+")
330 if (m == 1)
331 {
332 if (parts[k] !~ "^[0-9]+$")
333 error("non-numeric FIELDS value [" parts[k] "]")
334 else if (parts[k] == 0)
335 error("zero FIELDS value [" parts[k] "]: fields are numbered from 1")
336 else
337 Fields[++N_Fields] = parts[k]
338 }
339 else if (m == 2)
340 {
341 if ((numbers[1] !~ "^[0-9]+$") || \
342 (numbers[2] !~ "^[0-9]+$"))
343 error("non-numeric FIELDS range [" parts[k] "]")
344 else if ((numbers[1] == 0) || (numbers[2] == 0))
345 error("zero value in FIELDS range [" parts[k] "]: fields are numbered from 1")
346 else if (numbers[1] > numbers[2])
347 error("bad FIELDS range [" parts[k] "]")
348 else if ((numbers[2] - numbers[1] + 1) > 100)
349 error("FIELDS range [" parts[k] "] exceeds 100")
350 else
351 {
352 for (j = numbers[1]; j <= numbers[2]; ++j)
353 Fields[++N_Fields] = j
354 }
355 }
356 else
357 error("bad FIELDS range [" parts[k] "]")
358 }
359 ## printf("DEBUG: Fields = [")
360 ## for (k = 1; k <= N_Fields; ++k)
361 ## printf("%d,", Fields[k])
362 ## print "]"
363 ## exit(0)
364 }
365
366
367 function machine_epsilon( x)
368 {
369 ## Tests on these architectures with awk, gawk, mawk, and nawk all
370 ## produced identical results:
371 ##
372 ## Apple Macintosh PPC G3 Rhapsody 5.5
373 ## DEC Alpha OSF/1 4.0F
374 ## HP 9000/735 HP-UX 10.01
375 ## IBM PowerPC AIX 4.2
376 ## Intel Pentium III GNU/Linux 2.2.12-20smp (Redhat 6.1)
377 ## NeXT Turbostation Mach 3.3
378 ## SGI Indigo/2 IRIX 5.3
379 ## SGI Origin 200 IRIX 6.5
380 ## Sun SPARC GNU/Linux 2.2.12-42smp (Redhat 6.1)
381 ## Sun SPARC Solaris 2.6
382 ## Sun SPARC Solaris 2.7
383 ##
384 ## /usr/local/bin/awk: 2.22045e-16
385 ## /usr/local/bin/gawk: 2.22045e-16
386 ## /usr/local/bin/mawk: 2.22045e-16
387 ## /usr/local/bin/nawk: 2.22045e-16
388 ##
389 ## Thus, there does not appear to be concern for surprises from
390 ## long registers, such as on the Intel x86 architecture.
391
392 x = 1.0
393 while ((1.0 + x/2.0) != 1.0)
394 x /= 2.0
395 return (x)
396 }
397
398
399 function max(a,b)
400 {
401 ## Return the (numerically or lexicographically) larger of the two
402 ## arguments.
403
404 return ((a > b) ? a : b)
405 }
406
407
408 function maxrelerr(x,y)
409 {
410 ## Return the maximum relative error of two values.
411
412 #x = abs(x + 0) # coerce to nonnegative numbers
413 #y = abs(y + 0) # coerce to nonnegative numbers
414
415 ## See the documentation of the -relerr option in ndiff.man for the
416 ## explanation of this complex definition:
417
418 if (x == y)
419 return (0)
420 else if ((x != 0) && (y != 0))
421 return (abs(x-y)/min(abs(x),abs(y)))
422 else if ((x == 0) && (y != 0))
423 return (1)
424 else if ((y == 0) && (x != 0))
425 return (1)
426 else
427 return (0)
428 }
429
430
431 function min(a,b)
432 {
433 ## Return the (numerically or lexicographically) smaller of the two
434 ## arguments.
435
436 return ((a < b) ? a : b)
437 }
438
439
440 function report_difference(f1line,f2line,nfield, emult)
441 {
442 ## Print a diff-style difference of two lines, but also show in
443 ## the separator line the field number at which they differ, and
444 ## the global absolute and relative errors, if they are nonzero.
445
446 if (SILENT == 0)
447 {
448 printf("%dc%d\n", NRLINE, NRLINE)
449 printf("< %s\n", f1line)
450 ## if ((This_Abserr != 0) || (This_Relerr != 0))
451 ## {
452 ## emult = This_Relerr / Macheps
453 ## if (emult >= 10000)
454 ## printf("--- field %d\tabsolute error %.2e\trelative error %.2e\n",
455 ## nfield, This_Abserr, This_Relerr)
456 ## else
457 ## printf("--- field %d\tabsolute error %.2e\trelative error %.2e [%d*(machine epsilon)]\n",
458 ## nfield, This_Abserr, This_Relerr, int(emult + 0.5))
459 ##}
460 ##else
461 ## printf("--- field %d\n", nfield)
462 printf("> %s\n", f2line)
463 }
464 Ndiff++
465 }
466
467
468 function warning(message)
469 {
470 ## Print a warning message on stderr, using emacs
471 ## compile-command-style message format.
472
473 if (FNR > 0)
474 print FILENAME ":" FNR ":%%" message >Stderr
475 else # special case for diagnostics during initialization
476 print message >Stderr
477 }