#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# This  is  a  generic  compare  test  invoked  by  the  Test  script   after #
# test/prog/foo  has  been run and its output stream(s) put into test/out/foo #
# and/or test/err/foo.  We extract the  name  from  $0,  and  check  for  the #
# existence of the output stream files. If they exist, we read them in, doing #
# some simple canonicalizing.  We then do a diff() on  the  two  arrays,  and #
# report whether they are the same.                                           #
#                                                                             #
# The  main  point  of  this  program  is to compare files that may differ in #
# trivial ways, and not report differences if the trivia are different.       #
#                                                                             #
# Some of the trivia that we ignore:                                          #
#                                                                             #
# White stuff is reduced to a single space.                                   #
#                                                                             #
# Lines that contain dates or times in several formats  have  the  dates  and #
# times  deleted (actually, replaced with '*').  This only works with numeric #
# dates such as 95/4/13 or 7-5-1996 and times like 16:42:27; we don't  bother #
# with trying to handle dates with words in them.                             #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$| = 1;
$x = 0;		# Exit status.
($me = $0) =~ s"^.*/"";
$timep = '\d+[-/:]\d+[-/:]\d+';

file:
for $d ('out','err') {
	$f1 = "test/std$d/$me";		# Sample (expected) output.
	$f2 = "test/$d/$me";		# Actual output.
	if (open(F1,$f1)) {
		if (open(F2,$f2)) {
			for (<F1>) {			# Read in the expected output.
				s/\b$timep\b/*/g;	# Wipe out date.
				s/\b$timep\b/*/g;	# Wipe out time.
				s/\s+/ /g;			# Reduce white space.
				$f1[$#f1+1] = $_	# Append to file's list.
					if !m/^\s$/;	# Ignore blank lines.
			}
			for (<F2>) {			# Read in the actual output.
				s/\b$timep\b/*/g;
				s/\b$timep\b/*/g;
				s/\s+/ /g;
				$f2[$#f2+1] = $_
					if !m/^\s$/;
	}	}	}
	if ($#f1 != $#f2) {
		print "$me: $f2 differs in size from $f1.\n";
		++$x;
		next;
	}
	for ($i = 0; $i <= $#f1; ++$i) {
		if ($f1[$i] ne $f2[$i]) {
			print "$me: $f2 differs from $f1.\n";
			print "\t$f2    has \"$f2[$i]\"\n";
			print "\t$f1 has \"$f1[$i]\"\n";
			++$x;
			next file;
}	}	}
