74 |
|
#define adv_linecnt(htp) (lin1cnt += (htp == &hdr1), \ |
75 |
|
lin2cnt += (htp == &hdr2)) |
76 |
|
|
77 |
+ |
typedef struct { /* dynamic line buffer */ |
78 |
+ |
char *ptr; |
79 |
+ |
int len; |
80 |
+ |
int siz; |
81 |
+ |
} LINEBUF; |
82 |
+ |
|
83 |
+ |
#define init_line(bp) ((bp)->ptr = NULL, (bp)->siz = 0) |
84 |
+ |
/* 100 MByte limit on line buffer */ |
85 |
+ |
#define MAXBUF (100L<<20) |
86 |
+ |
|
87 |
|
/* input files */ |
88 |
|
char *progname = NULL; |
89 |
|
const char stdin_name[] = "<stdin>"; |
92 |
|
|
93 |
|
/* running real differences */ |
94 |
|
double diff2sum = 0; |
95 |
< |
int nsum = 0; |
95 |
> |
long nsum = 0; |
96 |
|
|
97 |
|
/* Report usage and exit */ |
98 |
|
static void |
102 |
|
fputs(progname, stderr); |
103 |
|
fputs(" [-h][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n", |
104 |
|
stderr); |
105 |
< |
exit(1); |
105 |
> |
exit(2); |
106 |
|
} |
107 |
|
|
108 |
+ |
/* Read a text line, increasing buffer size as necessary */ |
109 |
+ |
static int |
110 |
+ |
read_line(LINEBUF *bp, FILE *fp) |
111 |
+ |
{ |
112 |
+ |
bp->len = 0; |
113 |
+ |
if (!bp->ptr) { |
114 |
+ |
bp->ptr = (char *)malloc(bp->siz = 512); |
115 |
+ |
if (!bp->ptr) |
116 |
+ |
goto memerr; |
117 |
+ |
} |
118 |
+ |
while (fgets(bp->ptr + bp->len, bp->siz - bp->len, fp)) { |
119 |
+ |
bp->len += strlen(bp->ptr + bp->len); |
120 |
+ |
if (bp->ptr[bp->len-1] == '\n') |
121 |
+ |
break; /* found EOL */ |
122 |
+ |
if (bp->len < bp->siz - 4) |
123 |
+ |
continue; /* at EOF? */ |
124 |
+ |
if (bp->siz >= MAXBUF) |
125 |
+ |
break; /* don't go to extremes */ |
126 |
+ |
if ((bp->siz += bp->siz/2) > MAXBUF) |
127 |
+ |
bp->siz = MAXBUF; |
128 |
+ |
bp->ptr = (char *)realloc(bp->ptr, bp->siz); |
129 |
+ |
if (!bp->ptr) |
130 |
+ |
goto memerr; |
131 |
+ |
} |
132 |
+ |
return(bp->len); |
133 |
+ |
memerr: |
134 |
+ |
fprintf(stderr, |
135 |
+ |
"%s: out of memory in read_line() allocating %d byte buffer\n", |
136 |
+ |
progname, bp->siz); |
137 |
+ |
exit(2); |
138 |
+ |
} |
139 |
+ |
|
140 |
+ |
/* Free line buffer */ |
141 |
+ |
static void |
142 |
+ |
free_line(LINEBUF *bp) |
143 |
+ |
{ |
144 |
+ |
bp->len = 0; |
145 |
+ |
if (!bp->ptr) return; |
146 |
+ |
free(bp->ptr); |
147 |
+ |
bp->ptr = NULL; |
148 |
+ |
} |
149 |
+ |
|
150 |
|
/* Get type ID from name (or 0 if not found) */ |
151 |
|
static int |
152 |
|
xlate_type(const char *nm) |
186 |
|
return(1); |
187 |
|
} |
188 |
|
|
189 |
+ |
/* Compare two color values for equivalence */ |
190 |
+ |
static int |
191 |
+ |
color_check(COLOR c1, COLOR c2) |
192 |
+ |
{ |
193 |
+ |
int p; |
194 |
+ |
|
195 |
+ |
if (!real_check(colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU)*(1./3.), |
196 |
+ |
colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.)) |
197 |
+ |
return(0); |
198 |
+ |
|
199 |
+ |
p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED; |
200 |
+ |
if (colval(c1,BLU) > colval(c1,p)) p = BLU; |
201 |
+ |
|
202 |
+ |
return(real_check(colval(c1,p), colval(c2,p))); |
203 |
+ |
} |
204 |
+ |
|
205 |
|
/* Compare two strings for equivalence */ |
206 |
|
static int |
207 |
|
equiv_string(char *s1, char *s2) |
343 |
|
|
344 |
|
/* Compare two sets of header variables */ |
345 |
|
static int |
346 |
< |
headers_match(LUTAB *hp1, LUTAB *hp2) |
346 |
> |
headers_match() |
347 |
|
{ |
348 |
< |
int ne = lu_doall(hp1, match_val, hp2); |
348 |
> |
int ne = lu_doall(&hdr1, match_val, &hdr2); |
349 |
|
if (ne < 0) |
350 |
|
return(0); /* something didn't match! */ |
351 |
|
/* non-fatal if second header has extra */ |
352 |
< |
if (report >= REP_WARN && (ne = lu_doall(hp2, NULL, NULL) - ne)) |
352 |
> |
if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne)) |
353 |
|
printf("%s: warning - '%s' has %d extra header setting(s)\n", |
354 |
|
progname, f2name, ne); |
355 |
|
return(1); /* good match */ |
497 |
|
static int |
498 |
|
compare_text() |
499 |
|
{ |
500 |
< |
char l1buf[4096], l2buf[4096]; |
500 |
> |
LINEBUF l1buf, l2buf; |
501 |
|
|
502 |
|
if (report >= REP_VERBOSE) { |
503 |
|
fputs(progname, stdout); |
504 |
|
fputs(": comparing inputs as ASCII text\n", stdout); |
505 |
|
} |
506 |
< |
/* compare a line at a time */ |
507 |
< |
while (fgets(l1buf, sizeof(l1buf), f1in)) { |
506 |
> |
init_line(&l1buf); init_line(&l2buf); /* compare a line at a time */ |
507 |
> |
while (read_line(&l1buf, f1in)) { |
508 |
|
lin1cnt++; |
509 |
< |
if (!*sskip2(l1buf,0)) |
509 |
> |
if (!*sskip2(l1buf.ptr,0)) |
510 |
|
continue; /* ignore empty lines */ |
511 |
< |
while (fgets(l2buf, sizeof(l2buf), f2in)) { |
511 |
> |
|
512 |
> |
while (read_line(&l2buf, f2in)) { |
513 |
|
lin2cnt++; |
514 |
< |
if (*sskip2(l2buf,0)) |
514 |
> |
if (*sskip2(l2buf.ptr,0)) |
515 |
|
break; /* found other non-empty line */ |
516 |
|
} |
517 |
|
if (feof(f2in)) { |
519 |
|
fputs(f2name, stdout); |
520 |
|
fputs(": unexpected end-of-file\n", stdout); |
521 |
|
} |
522 |
+ |
free_line(&l1buf); free_line(&l2buf); |
523 |
|
return(0); |
524 |
|
} |
525 |
|
/* compare non-empty lines */ |
526 |
< |
if (!equiv_string(l1buf, l2buf)) { |
526 |
> |
if (!equiv_string(l1buf.ptr, l2buf.ptr)) { |
527 |
|
if (report != REP_QUIET) { |
528 |
|
printf("%s: inputs '%s' and '%s' differ at line %d|%d\n", |
529 |
|
progname, f1name, f2name, |
530 |
|
lin1cnt, lin2cnt); |
531 |
< |
if (report >= REP_VERBOSE) { |
531 |
> |
if ( report >= REP_VERBOSE && |
532 |
> |
(l1buf.len < 256) & |
533 |
> |
(l2buf.len < 256) ) { |
534 |
|
fputs("------------- Mismatch -------------\n", stdout); |
535 |
|
printf("%s@%d:\t%s", f1name, |
536 |
< |
lin1cnt, l1buf); |
536 |
> |
lin1cnt, l1buf.ptr); |
537 |
|
printf("%s@%d:\t%s", f2name, |
538 |
< |
lin2cnt, l2buf); |
538 |
> |
lin2cnt, l2buf.ptr); |
539 |
|
} |
540 |
|
} |
541 |
+ |
free_line(&l1buf); free_line(&l2buf); |
542 |
|
return(0); |
543 |
|
} |
544 |
|
} |
545 |
|
/* check for EOF on input 2 */ |
546 |
< |
while (fgets(l2buf, sizeof(l2buf), f2in)) { |
547 |
< |
if (!*sskip2(l2buf,0)) |
546 |
> |
while (read_line(&l2buf, f2in)) { |
547 |
> |
if (!*sskip2(l2buf.ptr,0)) |
548 |
|
continue; |
549 |
|
if (report != REP_QUIET) { |
550 |
|
fputs(f1name, stdout); |
551 |
|
fputs(": unexpected end-of-file\n", stdout); |
552 |
|
} |
553 |
+ |
free_line(&l1buf); free_line(&l2buf); |
554 |
|
return(0); |
555 |
|
} |
556 |
+ |
free_line(&l1buf); free_line(&l2buf); |
557 |
|
return(good_RMS()); /* final check for reals */ |
558 |
|
} |
559 |
|
|
602 |
|
return(0); |
603 |
|
} |
604 |
|
for (x = 0; x < scanlen(&rs1); x++) { |
605 |
< |
if (real_check(colval(scan1[x],RED), |
531 |
< |
colval(scan2[x],RED)) & |
532 |
< |
real_check(colval(scan1[x],GRN), |
533 |
< |
colval(scan2[x],GRN)) & |
534 |
< |
real_check(colval(scan1[x],BLU), |
535 |
< |
colval(scan2[x],BLU))) |
605 |
> |
if (color_check(scan1[x], scan2[x])) |
606 |
|
continue; |
607 |
|
if (report != REP_QUIET) { |
608 |
|
printf( |
638 |
|
|
639 |
|
if (report >= REP_VERBOSE) { |
640 |
|
fputs(progname, stdout); |
641 |
< |
fputs(": comparing inputs as 32-bit IEEE float\n", stdout); |
641 |
> |
fputs(": comparing inputs as 32-bit IEEE floats\n", stdout); |
642 |
|
} |
643 |
|
while (getbinary(&f1, sizeof(f1), 1, f1in)) { |
644 |
|
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
668 |
|
|
669 |
|
if (report >= REP_VERBOSE) { |
670 |
|
fputs(progname, stdout); |
671 |
< |
fputs(": comparing inputs as 64-bit IEEE double\n", stdout); |
671 |
> |
fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout); |
672 |
|
} |
673 |
|
while (getbinary(&f1, sizeof(f1), 1, f1in)) { |
674 |
|
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
732 |
|
} |
733 |
|
break; |
734 |
|
} |
735 |
< |
if (a != argc-2) |
735 |
> |
if (a != argc-2) /* make sure of two inputs */ |
736 |
|
usage(); |
737 |
< |
if (!f1name) f1name = argv[a]; |
668 |
< |
if (!f2name) f2name = argv[a+1]; |
669 |
< |
|
670 |
< |
if (!strcmp(f1name, f2name)) { /* inputs are same? */ |
737 |
> |
if (!strcmp(argv[a], argv[a+1])) { /* inputs are same? */ |
738 |
|
if (report >= REP_WARN) |
739 |
|
printf("%s: warning - identical inputs given\n", |
740 |
|
progname); |
741 |
|
return(0); |
742 |
|
} |
743 |
+ |
if (!f1name) f1name = argv[a]; |
744 |
+ |
if (!f2name) f2name = argv[a+1]; |
745 |
|
/* open inputs */ |
746 |
|
SET_FILE_BINARY(stdin); /* in case we're using it */ |
747 |
|
if (!f1in && !(f1in = fopen(f1name, "rb"))) { |
748 |
|
fprintf(stderr, "%s: cannot open for reading\n", f1name); |
749 |
< |
return(1); |
749 |
> |
return(2); |
750 |
|
} |
751 |
|
if (!strcmp(f2name, "-")) { |
752 |
|
f2in = stdin; |
753 |
|
f2name = stdin_name; |
754 |
|
} else if (!(f2in = fopen(f2name, "rb"))) { |
755 |
|
fprintf(stderr, "%s: cannot open for reading\n", f2name); |
756 |
< |
return(1); |
756 |
> |
return(2); |
757 |
|
} |
758 |
|
/* load headers */ |
759 |
|
if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0) |
760 |
< |
return(1); |
760 |
> |
return(2); |
761 |
|
if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0) |
762 |
< |
return(1); |
762 |
> |
return(2); |
763 |
|
if (typ1 != typ2) { |
764 |
|
if (report != REP_QUIET) |
765 |
|
printf("%s: '%s' is %s and '%s' is %s\n", |
768 |
|
return(1); |
769 |
|
} |
770 |
|
ign_header |= !has_header(typ1); /* check headers if indicated */ |
771 |
< |
if (!ign_header && !headers_match(&hdr1, &hdr2)) |
771 |
> |
if (!ign_header && !headers_match()) |
772 |
|
return(1); |
773 |
|
lu_done(&hdr1); lu_done(&hdr2); |
774 |
|
if (!ign_header & (report >= REP_WARN)) { |