33 |
|
|
34 |
|
int lin1cnt=0, lin2cnt=0; /* file line position */ |
35 |
|
|
36 |
+ |
const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */ |
37 |
+ |
"th","st","nd","rd","th","th","th","th","th","th" |
38 |
+ |
}; |
39 |
+ |
#define num_sfx(n) nsuffix[(n)%10] |
40 |
+ |
|
41 |
|
/* file types */ |
42 |
|
const char *file_type[] = { |
43 |
|
"Unrecognized", |
53 |
|
"BINARY_unknown", |
54 |
|
NULL /* terminator */ |
55 |
|
}; |
56 |
< |
|
56 |
> |
/* keep consistent with above */ |
57 |
|
enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, TYP_FLOAT, |
58 |
|
TYP_DOUBLE, TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, TYP_BINARY}; |
59 |
|
|
74 |
|
#define adv_linecnt(htp) (lin1cnt += (htp == &hdr1), \ |
75 |
|
lin2cnt += (htp == &hdr2)) |
76 |
|
|
77 |
+ |
typedef struct { /* dynamic line buffer */ |
78 |
+ |
char *str; |
79 |
+ |
int len; |
80 |
+ |
int siz; |
81 |
+ |
} LINEBUF; |
82 |
+ |
|
83 |
+ |
#define init_line(bp) ((bp)->str = 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 |
+ |
static int doneWarn = 0; |
113 |
+ |
|
114 |
+ |
bp->len = 0; |
115 |
+ |
if (!bp->str) { |
116 |
+ |
bp->str = (char *)malloc(bp->siz = 512); |
117 |
+ |
if (!bp->str) |
118 |
+ |
goto memerr; |
119 |
+ |
} |
120 |
+ |
while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) { |
121 |
+ |
bp->len += strlen(bp->str + bp->len); |
122 |
+ |
if (bp->str[bp->len-1] == '\n') |
123 |
+ |
break; /* found EOL */ |
124 |
+ |
if (bp->len < bp->siz - 4) |
125 |
+ |
continue; /* at EOF? */ |
126 |
+ |
if (bp->siz >= MAXBUF) { |
127 |
+ |
if ((report >= REP_WARN) & !doneWarn) { |
128 |
+ |
fprintf(stderr, |
129 |
+ |
"%s: warning - input line(s) past %ld MByte limit\n", |
130 |
+ |
progname, MAXBUF>>20); |
131 |
+ |
doneWarn++; |
132 |
+ |
} |
133 |
+ |
break; /* return MAXBUF partial line */ |
134 |
+ |
} |
135 |
+ |
if ((bp->siz += bp->siz/2) > MAXBUF) |
136 |
+ |
bp->siz = MAXBUF; |
137 |
+ |
bp->str = (char *)realloc(bp->str, bp->siz); |
138 |
+ |
if (!bp->str) |
139 |
+ |
goto memerr; |
140 |
+ |
} |
141 |
+ |
return(bp->len); |
142 |
+ |
memerr: |
143 |
+ |
fprintf(stderr, |
144 |
+ |
"%s: out of memory in read_line() allocating %d byte buffer\n", |
145 |
+ |
progname, bp->siz); |
146 |
+ |
exit(2); |
147 |
+ |
} |
148 |
+ |
|
149 |
+ |
/* Free line buffer */ |
150 |
+ |
static void |
151 |
+ |
free_line(LINEBUF *bp) |
152 |
+ |
{ |
153 |
+ |
if (bp->str) free(bp->str); |
154 |
+ |
init_line(bp); |
155 |
+ |
} |
156 |
+ |
|
157 |
|
/* Get type ID from name (or 0 if not found) */ |
158 |
|
static int |
159 |
|
xlate_type(const char *nm) |
193 |
|
return(1); |
194 |
|
} |
195 |
|
|
196 |
+ |
/* Compare two color values for equivalence */ |
197 |
+ |
static int |
198 |
+ |
color_check(COLOR c1, COLOR c2) |
199 |
+ |
{ |
200 |
+ |
int p; |
201 |
+ |
|
202 |
+ |
if (!real_check(colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU)*(1./3.), |
203 |
+ |
colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.)) |
204 |
+ |
return(0); |
205 |
+ |
|
206 |
+ |
p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED; |
207 |
+ |
if (colval(c1,BLU) > colval(c1,p)) p = BLU; |
208 |
+ |
|
209 |
+ |
return(real_check(colval(c1,p), colval(c2,p))); |
210 |
+ |
} |
211 |
+ |
|
212 |
|
/* Compare two strings for equivalence */ |
213 |
|
static int |
214 |
|
equiv_string(char *s1, char *s2) |
303 |
|
val++; |
304 |
|
if (!*val) /* nothing set? */ |
305 |
|
return(0); |
226 |
– |
vln = strlen(val); /* eliminate space and newline at end */ |
227 |
– |
while (--vln > 0 && isspace(val[vln])) |
228 |
– |
; |
229 |
– |
val[++vln] = '\0'; |
306 |
|
/* check if key to ignore */ |
307 |
|
for (n = 0; hdr_ignkey[n]; n++) |
308 |
|
if (!strcmp(key, hdr_ignkey[n])) |
309 |
|
return(0); |
310 |
+ |
vln = strlen(val); /* eliminate space and newline at end */ |
311 |
+ |
while (isspace(val[--vln])) |
312 |
+ |
; |
313 |
+ |
val[++vln] = '\0'; |
314 |
|
if (!(tep = lu_find(htp, key))) |
315 |
|
return(-1); /* memory allocation error */ |
316 |
|
if (!tep->key) |
350 |
|
|
351 |
|
/* Compare two sets of header variables */ |
352 |
|
static int |
353 |
< |
headers_match(LUTAB *hp1, LUTAB *hp2) |
353 |
> |
headers_match() |
354 |
|
{ |
355 |
< |
int ne = lu_doall(hp1, match_val, hp2); |
355 |
> |
int ne = lu_doall(&hdr1, match_val, &hdr2); |
356 |
|
if (ne < 0) |
357 |
|
return(0); /* something didn't match! */ |
358 |
|
/* non-fatal if second header has extra */ |
359 |
< |
if (report >= REP_WARN && (ne = lu_doall(hp2, NULL, NULL) - ne)) |
359 |
> |
if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne)) |
360 |
|
printf("%s: warning - '%s' has %d extra header setting(s)\n", |
361 |
|
progname, f2name, ne); |
362 |
|
return(1); /* good match */ |
411 |
|
} |
412 |
|
if (getheader(fin, setheadvar, htp) < 0) { |
413 |
|
fputs(name, stderr); |
414 |
< |
fputs(": Unknown error reading header\n", stderr); |
414 |
> |
fputs(": unknown error reading header\n", stderr); |
415 |
|
return(-1); |
416 |
|
} |
417 |
|
adv_linecnt(htp); /* for trailing emtpy line */ |
430 |
|
badeof: |
431 |
|
if (report != REP_QUIET) { |
432 |
|
fputs(name, stdout); |
433 |
< |
fputs(": Unexpected end-of-file\n", stdout); |
433 |
> |
fputs(": unexpected end-of-file\n", stdout); |
434 |
|
} |
435 |
|
return(-1); |
436 |
|
} |
476 |
|
return(1); /* success! */ |
477 |
|
if (report != REP_QUIET) { |
478 |
|
fputs(f1name, stdout); |
479 |
< |
fputs(": Unexpected end-of-file\n", stdout); |
479 |
> |
fputs(": unexpected end-of-file\n", stdout); |
480 |
|
} |
481 |
|
return(0); |
482 |
|
} |
483 |
|
if (c2 == EOF) { |
484 |
|
if (report != REP_QUIET) { |
485 |
|
fputs(f2name, stdout); |
486 |
< |
fputs(": Unexpected end-of-file\n", stdout); |
486 |
> |
fputs(": unexpected end-of-file\n", stdout); |
487 |
|
} |
488 |
|
return(0); |
489 |
|
} |
504 |
|
static int |
505 |
|
compare_text() |
506 |
|
{ |
507 |
< |
char l1buf[4096], l2buf[4096]; |
507 |
> |
LINEBUF l1buf, l2buf; |
508 |
|
|
509 |
|
if (report >= REP_VERBOSE) { |
510 |
|
fputs(progname, stdout); |
511 |
|
fputs(": comparing inputs as ASCII text\n", stdout); |
512 |
|
} |
513 |
< |
/* compare a line at a time */ |
514 |
< |
while (fgets(l1buf, sizeof(l1buf), f1in)) { |
513 |
> |
init_line(&l1buf); init_line(&l2buf); /* compare a line at a time */ |
514 |
> |
while (read_line(&l1buf, f1in)) { |
515 |
|
lin1cnt++; |
516 |
< |
if (!*sskip2(l1buf,0)) |
516 |
> |
if (!*sskip2(l1buf.str,0)) |
517 |
|
continue; /* ignore empty lines */ |
518 |
< |
while (fgets(l2buf, sizeof(l2buf), f2in)) { |
518 |
> |
|
519 |
> |
while (read_line(&l2buf, f2in)) { |
520 |
|
lin2cnt++; |
521 |
< |
if (*sskip2(l2buf,0)) |
521 |
> |
if (*sskip2(l2buf.str,0)) |
522 |
|
break; /* found other non-empty line */ |
523 |
|
} |
524 |
< |
if (feof(f2in)) { |
524 |
> |
if (!l2buf.len) { /* input 2 EOF? */ |
525 |
|
if (report != REP_QUIET) { |
526 |
|
fputs(f2name, stdout); |
527 |
< |
fputs(": Unexpected end-of-file\n", stdout); |
527 |
> |
fputs(": unexpected end-of-file\n", stdout); |
528 |
|
} |
529 |
+ |
free_line(&l1buf); free_line(&l2buf); |
530 |
|
return(0); |
531 |
|
} |
532 |
|
/* compare non-empty lines */ |
533 |
< |
if (!equiv_string(l1buf, l2buf)) { |
533 |
> |
if (!equiv_string(l1buf.str, l2buf.str)) { |
534 |
|
if (report != REP_QUIET) { |
535 |
|
printf("%s: inputs '%s' and '%s' differ at line %d|%d\n", |
536 |
|
progname, f1name, f2name, |
537 |
|
lin1cnt, lin2cnt); |
538 |
< |
if (report >= REP_VERBOSE) { |
538 |
> |
if ( report >= REP_VERBOSE && |
539 |
> |
(l1buf.len < 256) & |
540 |
> |
(l2buf.len < 256) ) { |
541 |
|
fputs("------------- Mismatch -------------\n", stdout); |
542 |
|
printf("%s@%d:\t%s", f1name, |
543 |
< |
lin1cnt, l1buf); |
543 |
> |
lin1cnt, l1buf.str); |
544 |
|
printf("%s@%d:\t%s", f2name, |
545 |
< |
lin2cnt, l2buf); |
545 |
> |
lin2cnt, l2buf.str); |
546 |
|
} |
547 |
|
} |
548 |
+ |
free_line(&l1buf); free_line(&l2buf); |
549 |
|
return(0); |
550 |
|
} |
551 |
|
} |
552 |
< |
/* check for EOF on input 2 */ |
553 |
< |
while (fgets(l2buf, sizeof(l2buf), f2in)) { |
554 |
< |
if (!*sskip2(l2buf,0)) |
552 |
> |
free_line(&l1buf); /* check for EOF on input 2 */ |
553 |
> |
while (read_line(&l2buf, f2in)) { |
554 |
> |
if (!*sskip2(l2buf.str,0)) |
555 |
|
continue; |
556 |
|
if (report != REP_QUIET) { |
557 |
|
fputs(f1name, stdout); |
558 |
< |
fputs(": Unexpected end-of-file\n", stdout); |
558 |
> |
fputs(": unexpected end-of-file\n", stdout); |
559 |
|
} |
560 |
+ |
free_line(&l2buf); |
561 |
|
return(0); |
562 |
|
} |
563 |
+ |
free_line(&l2buf); |
564 |
|
return(good_RMS()); /* final check for reals */ |
565 |
|
} |
566 |
|
|
572 |
|
COLOR *scan1, *scan2; |
573 |
|
int x, y; |
574 |
|
|
575 |
+ |
if (report >= REP_VERBOSE) { |
576 |
+ |
fputs(progname, stdout); |
577 |
+ |
fputs(": comparing inputs as HDR images\n", stdout); |
578 |
+ |
} |
579 |
|
fgetsresolu(&rs1, f1in); |
580 |
|
fgetsresolu(&rs2, f2in); |
581 |
|
if (rs1.rt != rs2.rt) { |
602 |
|
if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) | |
603 |
|
(freadscan(scan2, scanlen(&rs2), f2in) < 0)) { |
604 |
|
if (report != REP_QUIET) |
605 |
< |
printf("%s: Unexpected end-of-file\n", |
605 |
> |
printf("%s: unexpected end-of-file\n", |
606 |
|
progname); |
607 |
|
free(scan1); |
608 |
|
free(scan2); |
609 |
|
return(0); |
610 |
|
} |
611 |
|
for (x = 0; x < scanlen(&rs1); x++) { |
612 |
< |
if (real_check(colval(scan1[x],RED), |
522 |
< |
colval(scan2[x],RED)) & |
523 |
< |
real_check(colval(scan1[x],GRN), |
524 |
< |
colval(scan2[x],GRN)) & |
525 |
< |
real_check(colval(scan1[x],BLU), |
526 |
< |
colval(scan2[x],BLU))) |
612 |
> |
if (color_check(scan1[x], scan2[x])) |
613 |
|
continue; |
614 |
|
if (report != REP_QUIET) { |
615 |
|
printf( |
636 |
|
return(good_RMS()); /* final check of RMS */ |
637 |
|
} |
638 |
|
|
553 |
– |
const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */ |
554 |
– |
"th","st","nd","rd","th","th","th","th","th","th" |
555 |
– |
}; |
556 |
– |
|
639 |
|
/* Compare two inputs that are known to be 32-bit floating-point data */ |
640 |
|
static int |
641 |
|
compare_float() |
643 |
|
long nread = 0; |
644 |
|
float f1, f2; |
645 |
|
|
646 |
+ |
if (report >= REP_VERBOSE) { |
647 |
+ |
fputs(progname, stdout); |
648 |
+ |
fputs(": comparing inputs as 32-bit IEEE floats\n", stdout); |
649 |
+ |
} |
650 |
|
while (getbinary(&f1, sizeof(f1), 1, f1in)) { |
651 |
|
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
652 |
|
goto badeof; |
655 |
|
continue; |
656 |
|
if (report != REP_QUIET) |
657 |
|
printf("%s: %ld%s float values differ\n", |
658 |
< |
progname, nread, nsuffix[nread%10]); |
658 |
> |
progname, nread, num_sfx(nread)); |
659 |
|
return(0); |
660 |
|
} |
661 |
|
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
662 |
|
return(good_RMS()); /* final check of RMS */ |
663 |
|
badeof: |
664 |
|
if (report != REP_QUIET) |
665 |
< |
printf("%s: Unexpected end-of-file\n", progname); |
665 |
> |
printf("%s: unexpected end-of-file\n", progname); |
666 |
|
return(0); |
667 |
|
} |
668 |
|
|
673 |
|
long nread = 0; |
674 |
|
double f1, f2; |
675 |
|
|
676 |
+ |
if (report >= REP_VERBOSE) { |
677 |
+ |
fputs(progname, stdout); |
678 |
+ |
fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout); |
679 |
+ |
} |
680 |
|
while (getbinary(&f1, sizeof(f1), 1, f1in)) { |
681 |
|
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
682 |
|
goto badeof; |
684 |
|
if (real_check(f1, f2)) |
685 |
|
continue; |
686 |
|
if (report != REP_QUIET) |
687 |
< |
printf("%s: %ld%s float values differ\n", |
688 |
< |
progname, nread, nsuffix[nread%10]); |
687 |
> |
printf("%s: %ld%s double values differ\n", |
688 |
> |
progname, nread, num_sfx(nread)); |
689 |
|
return(0); |
690 |
|
} |
691 |
|
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
692 |
|
return(good_RMS()); /* final check of RMS */ |
693 |
|
badeof: |
694 |
|
if (report != REP_QUIET) |
695 |
< |
printf("%s: Unexpected end-of-file\n", progname); |
695 |
> |
printf("%s: unexpected end-of-file\n", progname); |
696 |
|
return(0); |
697 |
|
} |
698 |
|
|
739 |
|
} |
740 |
|
break; |
741 |
|
} |
742 |
< |
if (a != argc-2) |
742 |
> |
if (a != argc-2) /* make sure of two inputs */ |
743 |
|
usage(); |
744 |
< |
if (!f1name) f1name = argv[a]; |
655 |
< |
if (!f2name) f2name = argv[a+1]; |
656 |
< |
|
657 |
< |
if (!strcmp(f1name, f2name)) { /* inputs are same? */ |
744 |
> |
if (!strcmp(argv[a], argv[a+1])) { /* inputs are same? */ |
745 |
|
if (report >= REP_WARN) |
746 |
|
printf("%s: warning - identical inputs given\n", |
747 |
|
progname); |
748 |
|
return(0); |
749 |
|
} |
750 |
+ |
if (!f1name) f1name = argv[a]; |
751 |
+ |
if (!f2name) f2name = argv[a+1]; |
752 |
|
/* open inputs */ |
753 |
|
SET_FILE_BINARY(stdin); /* in case we're using it */ |
754 |
|
if (!f1in && !(f1in = fopen(f1name, "rb"))) { |
755 |
|
fprintf(stderr, "%s: cannot open for reading\n", f1name); |
756 |
< |
return(1); |
756 |
> |
return(2); |
757 |
|
} |
758 |
|
if (!strcmp(f2name, "-")) { |
759 |
|
f2in = stdin; |
760 |
|
f2name = stdin_name; |
761 |
|
} else if (!(f2in = fopen(f2name, "rb"))) { |
762 |
|
fprintf(stderr, "%s: cannot open for reading\n", f2name); |
763 |
< |
return(1); |
763 |
> |
return(2); |
764 |
|
} |
765 |
|
/* load headers */ |
766 |
|
if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0) |
767 |
< |
return(1); |
767 |
> |
return(2); |
768 |
|
if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0) |
769 |
< |
return(1); |
769 |
> |
return(2); |
770 |
|
if (typ1 != typ2) { |
771 |
|
if (report != REP_QUIET) |
772 |
|
printf("%s: '%s' is %s and '%s' is %s\n", |
775 |
|
return(1); |
776 |
|
} |
777 |
|
ign_header |= !has_header(typ1); /* check headers if indicated */ |
778 |
< |
if (!ign_header && !headers_match(&hdr1, &hdr2)) |
778 |
> |
if (!ign_header && !headers_match()) |
779 |
|
return(1); |
780 |
|
lu_done(&hdr1); lu_done(&hdr2); |
781 |
|
if (!ign_header & (report >= REP_WARN)) { |