#-----------------------------------------------------------------------------#
# vim: ts=8 sw=8 noexpandtab ft=make
#-----------------------------------------------------------------------------#

TESTS_DIR = ..
THIS_DIR = string_format
MAYBE_J1 = -j1

#-----------------------------------------------------------------------------#

# Any program added here should also be added to the `.cvsignore' file.

STRING_FORMAT_PROGS = \
	string_format_c \
	string_format_d \
	string_format_e \
	string_format_f \
	string_format_g \
	string_format_int64_d \
	string_format_int64_o \
	string_format_int64_u \
	string_format_int64_x \
	string_format_o \
	string_format_s \
	string_format_u \
	string_format_uint_o \
	string_format_uint_u \
	string_format_uint_x \
	string_format_uint64_o \
	string_format_uint64_u \
	string_format_uint64_x \
	string_format_x

# The tests don't work in the *.agc grades, because I think they
# take too long to finish and the CPU time limit is breached.
ifneq "$(findstring .agc,$(GRADE))" ""
	PROGS0 =
else
	PROGS0 = $(STRING_FORMAT_PROGS)
endif

# On solaris 5.{7,8} string_format_o fails because of a buggy
# sprintf implementation.
SOLARIS_FAILURES = string_format_o

UNAME := $(shell uname -r -s)
ifeq "$(filter-out SunOS 5.7 5.8,$(UNAME))" ""
	PROGS = $(filter-out $(SOLARIS_FAILURES),$(PROGS0))
else
	PROGS = $(PROGS0)
endif

TESTS = $(sort $(PROGS))
include ../Mmake.common

# Module-specific options should go in Mercury.options so they can be found
# by `mmc --make'.
include Mercury.options

%.runtest: %.res ;

#
# This is adapted from the rule in ../../Mmake.common.  The
# difference is that we only display the last 20 lines of the output
# file if the file fails, due to the size of the output file.
#
%.out: %
	{ [ -f $*.inp ] && cat $*.inp; } | ./$< > $@ 2>&1 || \
		{ grep . $@ /dev/null | tail -n 20; exit 1; }

#
# This is adapted from the rule in ../../Mmake.common.  The only
# differences are that we only cat the first 50 lines to stdout, as
# generally it is quite big and that we sort both the output and the
# expected output before the comparison, as the IL and C backends use
# a different ordering for lexical comparisons.  The reason for sorting
# the expected output as well is to allow for different behaviour in
# different versions of sort.
#
%.res: %.exp %.out
	@echo "Comparing $*.sorted_out with $*.sorted_exp*,"
	@echo "	results in $@"
	@-rm -f $@ $*.res[1-5]
	@sort $*.out > $*.sorted_out
	@sort $*.exp > $*.sorted_exp
	@{ diff $(DIFF_OPTS) $*.sorted_exp $*.sorted_out > $*.res1 && \
		echo "Matched $*.sorted_exp" && \
		cp $*.res1 $@; } || \
	{ test -f $*.exp2 && \
		sort $*.exp2 > $*.sorted_exp2  &&\
		diff $(DIFF_OPTS) $*.sorted_exp2 $*.sorted_out > $*.res2 && \
		echo "Matched $*.sorted_exp2" && \
		cp $*.res2 $@; } || \
	{ test -f $*.exp3 && \
		sort $*.exp3 > $*.sorted_exp3 && \
		diff $(DIFF_OPTS) $*.sorted_exp3 $*.sorted_out > $*.res3 && \
		echo "Matched $*.sorted_exp3" && \
		cp $*.res3 $@; } || \
	{ test -f $*.exp4 && \
		sort $*.exp4 > $*.sorted_exp4 && \
		diff $(DIFF_OPTS) $*.sorted_exp4 $*.sorted_out > $*.res4 && \
		echo "Matched $*.sorted_exp4" && \
		cp $*.res4 $@; } || \
	{ test -f $*.exp5 && \
		sort $*.exp5 > $*.sorted_exp5 && \
		diff $(DIFF_OPTS) $*.sorted_exp5 $*.sorted_out > $*.res5 && \
		echo "Matched $*.sorted_exp5" && \
		cp $*.res5 $@; } || \
	{ shortest=`wc -l $*.res[1-5] | grep -v total | sort -n | \
		head -1 | awk '{ print $$2; }' `; \
		echo "** $*.sorted_out did not match the expected output"; \
		echo "** (closest match was $$shortest)"; \
		echo "** The diff is located in $@"; \
		cp $$shortest $@; \
		cat $$shortest | head -n 50; \
		exit 1; }

#-----------------------------------------------------------------------------#

realclean_local: clean_sorted_files
clean_local: clean_sorted_files

clean_sorted_files:
	rm -f *.sorted_out *.sorted_exp*

#-----------------------------------------------------------------------------#
#-----------------------------------------------------------------------------#
