# Run regression tests of all kind
#  make:               run all tests until the first error
#  make -j8:           same, parallel on 8 threads
#  make decl.dab       run binary save/load test on decl.awk (works on any .awk file anywhere)
#  make decl.diff      run decl awk test - works on any ./*.awk
#  make decl.out       run decl awk test, save output don't compare - works on any ./*.awk
#  make stage1.diff    run zfifo test stage1.zf
#  make Makefile.dabs  update da bin test list
#  make fpe.out        run the FPE (floating-point-exception) tests

LMAWK=../lmawk
ZFTEST=zfifo/zfifo_test

REF_AWK=mawk
IN=test.in

# awk tests with reference output
TESTS = io_print1.diff io_printf.diff \
        nan_if.diff nan_ops.diff nan_io.diff nan_isnan.diff \
        math_func.diff math_fmod.diff arr_orig.diff arr_order.diff \
        getln_nul.diff valueof.diff wc.diff reg012.diff wfrq0.diff \
        decl.diff fpe.diff re_test.diff call.diff acall.diff \
        stack_grow.diff

# zfifo tests
ZFTESTS = stage1.diff stage2.diff large_writes.diff small_reads.diff

# da_bin tests
include Makefile.dabs

.SUFFIXES: .diff .out .ref .awk .zf .dab

# the .zf.out rule dependency on ZFTEST doesn't really work
all: $(ZFTEST)
	make all_

all_: $(TESTS) $(ZFTESTS) $(DABTESTS)
	@echo "*** QC PASS ***"

.awk.out: $(LMAWK)
	-$(LMAWK) -f $*.awk < $(IN) > $@ 2>&1; true

.out.diff: $*.ref
	@diff -u $*.ref $*.out
	@rm $*.out

#.awk.ref:
#	$(REF_AWK) -f $*.awk < $(IN) > $@ 2>&1 ; true

### non-awk generic tests ###
.zf.out: $(ZFTEST)
	$(ZFTEST) < $*.zf > $@ 2>&1; true

$(ZFTEST): ../zfifo.h ../zfifo.c zfifo/zfifo_test.c
	cd zfifo && make

.awk.dab: $(LMAWK) ./da_bin_test.sh
	LMAWK=$(LMAWK) ./da_bin_test.sh $*.awk

Makefile.dabs:
	@echo "Generating Makefile.dabs..."
	@echo -n "DABTESTS=" > $@
	@ls *.awk ../examples/*.awk | sed "s/.awk$$/.dab\\\\/" >>$@
	@echo ""  >>$@


### custom tests ###
fpe.out: fpe/fpetest1.awk fpe/fpetest2.awk fpe/fpetest3.awk
	@echo "-> fpe test"
	@-$(LMAWK) -f fpe/fpetest1.awk >/dev/null 2>&1; r1=$$?; \
	 $(LMAWK) -f fpe/fpetest2.awk >/dev/null 2>&1; r2=$$?; \
	 $(LMAWK) -f fpe/fpetest3.awk  2>/dev/null > fpe/fpetest3.tmp; r3=$$?; \
	 $(LMAWK) -f fpe/eval.awk -v "r1=$$r1" -v "r2=$$r2" -v "r3=$$r3" < fpe/fpetest3.tmp  > $@

re_test.out: re_test/re_test
	@echo "-> re_test"
	@re_test/re_test >$@

clean:
	-rm *.out re_test/*.o re_test/re_test zfifo/*.o zfifo/zfifo_test 2>/dev/null ; true

FORCE:
