# Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
# Copyright 2015, Daniel Axtens, IBM Corporation
# zlib license, see zlib.h

CC=
CFLAGS=
EXE=
SRCDIR=
SRCTOP=
LIBNAME=
TEST_LDFLAGS=-L.. ../$(LIBNAME).a

QEMU_RUN=
QEMU_VER:=$(shell command -v $(QEMU_RUN) --version 2> /dev/null)

all: oldtests ghtests

oldtests: #set by ../configure
check_cross_dep:
ifneq (,$(QEMU_RUN))
ifeq (,$(QEMU_VER))
	$(error "You need QEMU to run tests on non-native platform")
endif
endif

ALL_SRC_FILES := $(wildcard ../*)

teststatic: check_cross_dep
	@TMPST=tmpst_$$$$; \
	HELLOST=tmphellost_$$$$; \
	if echo hello world | ${QEMU_RUN} ../minigzip$(EXE) > $$HELLOST && ${QEMU_RUN} ../minigzip$(EXE) -d < $$HELLOST && ${QEMU_RUN} ../example$(EXE) $$TMPST; then \
	  echo '		*** zlib test OK ***'; \
	else \
	  echo '		*** zlib test FAILED ***'; exit 1; \
	fi; \
	rm -f $$TMPST $$HELLOST

testshared: check_cross_dep
	@LD_LIBRARY_PATH=`pwd`/..:$(LD_LIBRARY_PATH) ; export LD_LIBRARY_PATH; \
	LD_LIBRARYN32_PATH=`pwd`/..:$(LD_LIBRARYN32_PATH) ; export LD_LIBRARYN32_PATH; \
	DYLD_LIBRARY_PATH=`pwd`/..:$(DYLD_LIBRARY_PATH) ; export DYLD_LIBRARY_PATH; \
	SHLIB_PATH=`pwd`/..:$(SHLIB_PATH) ; export SHLIB_PATH; \
	TMPSH=tmpsh_$$$$; \
	HELLOSH=tmphellosh_$$$$; \
	if echo hello world | ${QEMU_RUN} ../minigzipsh$(EXE) > $$HELLOSH && ${QEMU_RUN} ../minigzipsh$(EXE) -d < $$HELLOSH && ${QEMU_RUN} ../examplesh$(EXE) $$TMPSH; then \
	  echo '		*** zlib shared test OK ***'; \
	else \
	  echo '		*** zlib shared test FAILED ***'; exit 1; \
	fi; \
	rm -f $$TMPSH $$HELLOSH

.PHONY: ghtests
ghtests: testGH-361 testGH-364 testGH-751 testGH-1235

.PHONY: testGH-361
testGH-361:
	$(QEMU_RUN) ../minigzip$(EXE) -4 <$(SRCDIR)/GH-361/test.txt >/dev/null

switchlevels$(EXE): $(SRCDIR)/switchlevels.c
	$(CC) $(CFLAGS) -I.. -I$(SRCTOP) -o $@ $< $(TEST_LDFLAGS)

.PHONY: testGH-364
testGH-364: switchlevels$(EXE)
	$(QEMU_RUN) ./switchlevels$(EXE) 1 5 9 3 <$(SRCDIR)/GH-364/test.bin >/dev/null

.PHONY: testGH-751
testGH-751:
	$(QEMU_RUN) ../minigzip$(EXE) <$(SRCDIR)/GH-751/test.txt | $(QEMU_RUN) ../minigzip$(EXE) -d >/dev/null

gh1235$(EXE): $(SRCDIR)/gh1235.c
	$(CC) $(CFLAGS) -I.. -I$(SRCTOP) -o $@ $< $(TEST_LDFLAGS)

.PHONY: testGH-1235
testGH-1235: gh1235$(EXE)
	$(QEMU_RUN) ./gh1235$(EXE)

clean:
	rm -f *.o *.gcda *.gcno *.gcov
	rm -f switchlevels$(EXE) gh1235$(EXE)

distclean:
	rm -f Makefile
