if BUILD_CHECK_TESTS
  TESTS_BIN = bt_core bt_ic bt_gst bt_cmd bt_edit
else
  TESTS_BIN =
endif

if USE_ALSA
ALSA_C_TEST_FILES=tests/lib/ic/e-aseq-discoverer.c
else
ALSA_C_TEST_FILES=
endif

TESTS = \
  $(TESTS_BIN) \
  tests/bt-cmd-info.sh \
  tests/bt-cmd-cli.sh \
  tests/bt-dec-launch.sh \
  tests/bt-edit-cli.sh \
  tests/povalid.sh \
  tests/xmlvalid.sh

CHECK_REGISTRY = $(top_builddir)/tests/test-registry.reg

# prevent endless loops, this requires bash, see idea in bt-check.c
#   ulimit -St20;
#
# http://www.cynapses.org/tmp/gcc/malloc_perturb
#    MALLOC_PERTURB_=$(($RANDOM % 255 + 1))
#
# set theme (check which theme is used)
#    GTK2_RC_FILES=$HOME/.themes/Darkilouche/gtk-2.0/gtkrc
#
# hint alsa/pulseaudio to be quiet
#    PULSE_SINK=???
#    ALSA_CARD, ALSA_PCM_CARD, DISPLAY_ALSA_DEVICE, AUDIODEV
#
# for gtk/x11 issues
#    GDK_SYNCHRONIZE=1
#
TESTS_ENVIRONMENT = \
	CK_DEFAULT_TIMEOUT=20 \
	LANG=C \
	XDG_CACHE_HOME=$(abs_builddir) \
	GSETTINGS_BACKEND=memory \
	GSETTINGS_SCHEMA_DIR=$(abs_top_builddir) \
	NO_AT_BRIDGE=1 \
	GST_DEBUG_FILE=/dev/null \
	GST_REGISTRY_1_0=$(CHECK_REGISTRY) \
	GST_PLUGIN_SYSTEM_PATH_1_0= \
	GST_PLUGIN_PATH_1_0=$(top_builddir)/.libs:$(GST_PLUGINS_DIR) \
	$(LIBTOOL) --mode=execute

# This should make the icon theme look here for icons too, but instead it breaks tons of things
#	XDG_DATA_DIRS=$XDG_DATA_DIRS:$(abs_top_srcdir)

# the core dumps of some machines have PIDs appended, test registry and
# profiling data
# most of the files are there due to XDG_CACHE_HOME=$(abs_builddir)
CLEANFILES += core core.* *.gcno *.gcda bt_*.tar.bz2 \
  tests/test-registry.* fontconfig/*

if BUILD_CHECK_TESTS
noinst_PROGRAMS=$(TESTS_BIN)

/tmp/test.wav:
	$(AM_V_GEN) gst-launch-@GST_MAJORMINOR@ -q audiotestsrc num-buffers=10 ! wavenc ! filesink location=$@

gschemas.compiled: org.buzztrax.gschema.xml
	$(AM_V_GEN) $(GLIB_COMPILE_SCHEMAS) .

tests/bt-cmd-info.sh:
	gschemas.compiled
tests/bt-cmd-cli.sh:
	gschemas.compiled
tests/bt-edit-cli.sh:
	gschemas.compiled


check_LTLIBRARIES = \
  libbt-check.la \
  libbt-check-ui.la \
  libbtcore-check.la \
  libbtic-check.la \
  libbtgst-check.la \
  libbt-cmd-check.la \
  libbt-edit-check.la

libbt_check_la_LIBADD = \
	libbuzztrax-ic.la \
	libbuzztrax-core.la \
	$(BASE_DEPS_LIBS) $(BT_LIBS) $(CHECK_LIBS)
libbt_check_la_LDFLAGS = \
	-Wl,--rpath -Wl,$(abs_top_builddir)/.libs
libbt_check_la_SOURCES = \
	tests/bt-check.c tests/bt-check.h \
	tests/bt-test-application.c tests/bt-test-application.h \
	tests/bt-test-plugin.c tests/bt-test-plugin.h \
	tests/btic-test-device.c tests/btic-test-device.h
EXTRA_libbt_check_la_DEPENDENCIES = /tmp/test.wav gschemas.compiled

libbt_check_ui_la_LIBADD = \
  $(BASE_DEPS_LIBS) $(GUI_DEPS_LIBS) $(BT_LIBS)
libbt_check_ui_la_LDFLAGS = \
	-Wl,--rpath -Wl,$(abs_top_builddir)/.libs
libbt_check_ui_la_CFLAGS = $(GUI_DEPS_CFLAGS) $(CHECK_CFLAGS)
libbt_check_ui_la_SOURCES = \
	tests/bt-check-ui.c tests/bt-check-ui.h
  
bt_core_LDADD = \
	libbtcore-check.la \
	libbuzztrax-core.la \
	libbt-check.la $(BASE_DEPS_LIBS) $(BT_LIBS) $(LIBM) $(CHECK_LIBS)
bt_core_LDFLAGS =  \
	-Wl,--rpath -Wl,$(abs_top_builddir)/.libs
bt_core_SOURCES = \
	tests/m-bt-core.c

bt_ic_LDADD = \
	libbtic-check.la \
	libbuzztrax-ic.la \
	libbuzztrax-core.la \
	libbt-check.la $(BASE_DEPS_LIBS) $(BT_LIBS) $(LIBM) $(CHECK_LIBS)
bt_ic_LDFLAGS =  \
	-Wl,--rpath -Wl,$(abs_top_builddir)/.libs
bt_ic_SOURCES = \
	tests/m-bt-ic.c

bt_gst_LDADD = \
  libbtgst-check.la \
	libbuzztrax-gst.la \
	libbt-check.la $(BASE_DEPS_LIBS) $(CHECK_LIBS) $(LIBM)
bt_gst_LDFLAGS =  \
	-Wl,--rpath -Wl,$(abs_top_builddir)/.libs
bt_gst_SOURCES = \
  tests/m-bt-gst.c

bt_cmd_LDADD = \
	libbt-cmd-check.la \
	libbuzztrax-core.la \
	libbt-check.la $(BASE_DEPS_LIBS) $(BT_LIBS) $(LIBM) $(CHECK_LIBS)
bt_cmd_LDFLAGS =  \
	-Wl,--rpath -Wl,$(abs_top_builddir)/.libs
bt_cmd_SOURCES = \
	tests/m-bt-cmd.c

bt_edit_LDADD = \
	libbt-edit-check.la \
	libbuzztrax-core.la \
	libbuzztrax-ic.la \
	libbt-check.la libbt-check-ui.la \
	$(BASE_DEPS_LIBS) $(GUI_DEPS_LIBS) $(BT_LIBS) $(LIBM) $(CHECK_LIBS)
bt_edit_LDFLAGS =  \
	-Wl,--rpath -Wl,$(abs_top_builddir)/.libs
bt_edit_CFLAGS = $(GUI_DEPS_CFLAGS) $(CHECK_CFLAGS)
bt_edit_SOURCES = \
	tests/m-bt-edit.c

libbtcore_check_la_LIBADD = $(BASE_DEPS_LIBS)
libbtcore_check_la_CFLAGS = $(BASE_DEPS_CFLAGS) $(CHECK_CFLAGS)
libbtcore_check_la_SOURCES = tests/lib/core/m-bt-core.h \
	tests/lib/core/e-application.c \
	tests/lib/core/e-audio-session.c \
	tests/lib/core/e-cmd-pattern.c tests/lib/core/t-cmd-pattern.c \
	tests/lib/core/e-core.c tests/lib/core/t-core.c \
	tests/lib/core/e-machine.c tests/lib/core/t-machine.c \
	tests/lib/core/e-parameter-group.c tests/lib/core/t-parameter-group.c \
	tests/lib/core/e-pattern.c tests/lib/core/t-pattern.c \
	tests/lib/core/e-pattern-control-source.c tests/lib/core/t-pattern-control-source.c \
	tests/lib/core/e-processor-machine.c tests/lib/core/t-processor-machine.c \
	tests/lib/core/e-sequence.c tests/lib/core/t-sequence.c \
	tests/lib/core/e-settings.c \
	tests/lib/core/e-setup.c tests/lib/core/t-setup.c \
	tests/lib/core/e-sink-bin.c tests/lib/core/t-sink-bin.c \
	tests/lib/core/e-sink-machine.c tests/lib/core/t-sink-machine.c \
	tests/lib/core/e-song.c tests/lib/core/t-song.c \
	tests/lib/core/e-song-io.c tests/lib/core/t-song-io.c \
	tests/lib/core/e-song-io-native.c tests/lib/core/t-song-io-native.c \
	tests/lib/core/e-song-info.c tests/lib/core/t-song-info.c \
	tests/lib/core/e-source-machine.c tests/lib/core/t-source-machine.c \
	tests/lib/core/e-tools.c tests/lib/core/t-tools.c \
	tests/lib/core/e-value-group.c tests/lib/core/t-value-group.c \
	tests/lib/core/e-wave.c tests/lib/core/t-wave.c \
	tests/lib/core/e-wave-table.c tests/lib/core/t-wave-table.c \
	tests/lib/core/e-wire.c tests/lib/core/t-wire.c

libbtic_check_la_LIBADD = $(BASE_DEPS_LIBS)
libbtic_check_la_CFLAGS = $(BASE_DEPS_CFLAGS) $(CHECK_CFLAGS)
libbtic_check_la_SOURCES = tests/lib/ic/m-bt-ic.h \
	tests/lib/ic/e-device.c \
	tests/lib/ic/e-ic.c tests/lib/ic/t-ic.c \
	tests/lib/ic/e-learn.c \
	tests/lib/ic/e-registry.c \
	$(ALSA_C_TEST_FILES)

libbtgst_check_la_LIBADD = $(BASE_DEPS_LIBS) $(LIBM)
libbtgst_check_la_CFLAGS = $(BASE_DEPS_CFLAGS) $(CHECK_CFLAGS)
libbtgst_check_la_SOURCES = tests/lib/gst/m-bt-gst.h \
	tests/lib/gst/e-combine.c \
	tests/lib/gst/e-envelope-ad.c \
	tests/lib/gst/e-envelope-d.c \
	tests/lib/gst/e-filter-svf.c \
	tests/lib/gst/e-osc-synth.c \
	tests/lib/gst/e-toneconversion.c tests/lib/gst/t-toneconversion.c \
	tests/lib/gst/t-elements.c

libbt_cmd_check_la_LIBADD = \
	libbtcmd.la \
	$(BASE_DEPS_LIBS)
libbt_cmd_check_la_CFLAGS = $(GUI_DEPS_CFLAGS) $(CHECK_CFLAGS)
libbt_cmd_check_la_SOURCES = tests/ui/cmd/m-bt-cmd.h \
	tests/ui/cmd/e-cmd-application.c tests/ui/cmd/t-cmd-application.c

libbt_edit_check_la_LIBADD = \
	libbtedit.la \
	$(BASE_DEPS_LIBS) $(GUI_DEPS_LIBS)
libbt_edit_check_la_CFLAGS = $(GUI_DEPS_CFLAGS) $(CHECK_CFLAGS)
libbt_edit_check_la_SOURCES = tests/ui/edit/m-bt-edit.h \
	tests/ui/edit/e-about-dialog.c \
	tests/ui/edit/e-change-log.c tests/ui/edit/t-change-log.c \
	tests/ui/edit/e-crash-recover-dialog.c \
	tests/ui/edit/e-edit-application.c \
	tests/ui/edit/e-interaction-controller-menu.c \
	tests/ui/edit/e-machine-actions.c \
	tests/ui/edit/e-machine-canvas-item.c \
	tests/ui/edit/e-machine-list-model.c \
	tests/ui/edit/e-machine-preset-properties-dialog.c \
	tests/ui/edit/e-machine-preferences-dialog.c \
	tests/ui/edit/e-machine-properties-dialog.c \
	tests/ui/edit/e-machine-rename-dialog.c \
	tests/ui/edit/e-main-page-info.c \
	tests/ui/edit/e-main-page-machines.c \
	tests/ui/edit/e-main-page-patterns.c tests/ui/edit/t-main-page-patterns.c \
	tests/ui/edit/e-main-page-sequence.c \
	tests/ui/edit/t-main-page-waves.c \
	tests/ui/edit/e-main-pages.c \
	tests/ui/edit/e-main-window.c \
	tests/ui/edit/e-missing-framework-elements-dialog.c \
	tests/ui/edit/e-missing-song-elements-dialog.c \
	tests/ui/edit/e-object-list-model.c tests/ui/edit/t-object-list-model.c \
	tests/ui/edit/e-pattern-list-model.c \
	tests/ui/edit/e-pattern-properties-dialog.c \
	tests/ui/edit/e-render-dialog.c \
	tests/ui/edit/e-sequence-grid-model.c \
	tests/ui/edit/e-settings-dialog.c \
	tests/ui/edit/e-signal-analysis-dialog.c \
	tests/ui/edit/e-tip-dialog.c \
	tests/ui/edit/e-ui-resources.c \
	tests/ui/edit/e-wave-list-model.c \
	tests/ui/edit/e-wavelevel-list-model.c \
	tests/ui/edit/e-wire-canvas-item.c

else
noinst_PROGRAMS =
endif


noinst_PROGRAMS += bmltest_info bmltest_process

bmltest_info_SOURCES = tests/lib/bml/bmltest_info.c tests/lib/bml/bmltest_info.h
bmltest_info_CFLAGS = $(PTHREAD_CFLAGS) $(BML_CFLAGS)
bmltest_info_LDADD = $(LIBM) $(PTHREAD_LIBS) $(BML_LIBS) libbml.la

bmltest_process_SOURCES = tests/lib/bml/bmltest_process.c  tests/lib/bml/bmltest_process.h
bmltest_process_CFLAGS = $(PTHREAD_CFLAGS) $(BML_CFLAGS)
bmltest_process_LDADD = $(LIBM) $(PTHREAD_LIBS) $(BML_LIBS) libbml.la


songdatadir = $(datadir)/$(PACKAGE)/songs
songdata_DATA = \
	tests/songs/buzz1.xml \
	tests/songs/buzz2.xml \
	tests/songs/buzz3.xml \
	tests/songs/buzz4.xml \
	tests/songs/buzz5.xml \
	tests/songs/buzz6.xml \
	tests/songs/buzz7.xml \
	tests/songs/buzz8.xml \
	tests/songs/buzz9.xml \
	tests/songs/combi1.xml \
	tests/songs/combi2.xml \
	tests/songs/combi3.xml \
	tests/songs/melo1.xml \
	tests/songs/melo2.xml \
	tests/songs/melo3.xml \
	tests/songs/melo4.xml \
	tests/songs/melo5.xml \
	tests/songs/melo6.xml

noinst_songdata = \
	tests/songs/broken1.xml \
	tests/songs/broken1.bzt \
	tests/songs/broken2.xml \
	tests/songs/broken3.xml \
	tests/songs/broken4.xml \
	tests/songs/empty.xml \
	tests/songs/example.xml \
	tests/songs/legacy1.xml \
	tests/songs/samples1.bzt \
	tests/songs/samples2.bzt \
	tests/songs/simple1.xml \
	tests/songs/simple2.xml \
	tests/songs/simple3.xml \
	tests/songs/simple4.xml \
	tests/songs/simple5.xml \
	tests/songs/simple6.xml \
	tests/songs/simple1.bzt \
	tests/songs/simple2.bzt \
	tests/songs/simple3.bzt \
	tests/songs/simple4.bzt \
	tests/songs/simple5.bzt \
	tests/songs/test-simple0.xml \
	tests/songs/test-simple1.xml \
	tests/songs/test-simple2.xml \
	tests/songs/test-simple3.xml \
	tests/songs/test-simple4.xml \
	tests/songs/test-simple5.xml

EXTRA_DIST += \
	$(songdata_DATA) $(noinst_songdata) \
	tests/povalid.sh tests/xmlvalid.sh tests/shunit2 \
	tests/bt-cfg.sh.in tests/bt-cmd-info.sh tests/bt-cmd-encode.sh \
	tests/bt-cmd-cli.sh tests/bt-dec-launch.sh tests/bt-edit-cli.sh \
	tests/lib/bml/sorttable.js tests/lib/bml/testmachine.sh

test-clean-local:
	@rm -f \
	  $(top_builddir)/tests/songs/*.txt \
	  $(top_builddir)/tests/songs/*.ogg \
	  $(abs_builddir)/buzztrax/*.log \
	  $(top_builddir)/event-sound-cache.tdb.* \
	  $(top_builddir)/bt_*.tar.bz2 \
	  /tmp/test.wav;
	@rm -rf gstreamer-1.0;
	@if test -d $(abs_builddir)/buzztrax; then \
	  rmdir $(abs_builddir)/buzztrax; \
	fi;

# make check           -- run all checks
# make (test).check    -- run the given check once
#	make (test).check BT_CHECKS="test5*" -- run the given testcase(s) only
%.check: %
	@$(TESTS_ENVIRONMENT)	\
	./$*

LOOPS ?= 10
# make (test).loop     -- run the given check 10 times
# make (test).loop LOOPS=20 -- run the given check 20 times
%.loop: %
	@for i in `seq 1 $(LOOPS)`; do \
	$(TESTS_ENVIRONMENT) \
	./$*; done

# make (test).forever  -- run the given check indefinitely
%.forever: %
	@while true; do	\
	$(TESTS_ENVIRONMENT) \
	./$* || break; done

# make (test).gdb      -- start up gdb for the given test
%.gdb: %
	@CK_FORK=no		\
	$(TESTS_ENVIRONMENT)	\
	gdb ./$*

# make (test).refdbg   -- run test under refdbg
%.refdbg: %
	@CK_FORK=no		\
	$(TESTS_ENVIRONMENT)	\
	refdbg -c "btnum=20 ; logobjs=0 ; $(REFDBG_RULE)" \
	./$*

# make (test).strace  -- run test under strace
%.strace: %
	@CK_FORK=no		\
	$(TESTS_ENVIRONMENT)	\
	strace -e file -o /tmp/strace.log ./$*

## todo: check if code has been compiled using --enable-debug
##
##check-local:
##	echo "========================================"
##	echo "No check based tests will run"
##	echo "build with --enable-debug=yes"
##	echo "========================================"

.PHONY: \
  coverage class-coverage \
  valgrind test-status

# make coverage        -- generate coverage report from make check run
if USE_LCOV
coverage::
	$(MKDIR_P) ./coverage
	lcov --compat-libtool --directory . --zerocounters
	-$(MAKE) check
	lcov --compat-libtool --directory ./src --capture --output-file ./coverage/$(PACKAGE)_tmp.info
	lcov --compat-libtool --extract ./coverage/$(PACKAGE)_tmp.info "$(PWD)/src*" -o ./coverage/$(PACKAGE).info
	$(RM) ./coverage/$(PACKAGE)_tmp.info
	genhtml -o ./coverage --num-spaces 2 ./coverage/$(PACKAGE).info
endif
if USE_BCOV
## CK_FORK=no bcov -l../src/tests/lib/core/.libs/libbuzztrax-core.so ./.libs/bt_cmd
## CK_FORK=no bcov -l../src/tests/lib/core/.libs/libbuzztrax-core.so -l../src/tests/lib/ic/.libs/libbuzztrax-ic.so ./.libs/bt_core
## CK_FORK=no libtool --mode=execute bcov -l../src/tests/lib/core/.libs/libbuzztrax-core.so -l../src/tests/lib/ic/.libs/libbuzztrax-ic.so ./bt_core
## - we're missing the libs coverage, but it is not due to BSymbolic :/
coverage:: $(TESTS_BIN)
	echo "command make check" >.bcovdump.all; \
	echo "date "`date` >>.bcovdump.all; \
	for i in $^; do \
	  CK_FORK="no" $(TESTS_ENVIRONMENT) bcov \
	    -l$(top_builddir)/src/.libs/libbuzztrax-core.so \
	    -l$(top_builddir)/src/.libs/libbuzztrax-ic.so \
	    ./$$i; \
	  if test -f .bcovdump; then \
	    tail -n +5 .bcovdump >>.bcovdump.all; \
	    rm -f .bcovdump; \
	  fi \
	done; \
	$(MKDIR_P) coverage; \
	bcov-report -i $(top_abs_srcdir) .bcovdump.all coverage
endif

# make class-coverage  -- generate coverage report on class level
class-coverage::
	@for dir in lib/core/ lib/ic ui/cmd ui/edit; do \
	  (cd $(top_srcdir)/src/$$dir; ls -1 *.c) | egrep -v "(marshal|bt-edit|bt-cmd)" >/tmp/c1.txt; \
	  (cd $(top_srcdir)/tests/$$dir; ls -1 s-*.c | cut -c3-) >/tmp/c2.txt; \
	  total=`cat /tmp/c1.txt | wc -l`; \
	  done=`comm -1 -2 /tmp/c1.txt /tmp/c2.txt | wc -l`; \
	  percent=`echo "$${done}00/$$total" | bc`; \
	  echo "# $$percent % ($$done/$$total) $$dir"; \
	  comm -2 -3 /tmp/c1.txt /tmp/c2.txt | sed "s/^/  /g"; \
	done; \
	rm /tmp/c1.txt /tmp/c2.txt


if USE_VALGRIND

VALPREFIX = \
	CK_FORK=no CK_DEFAULT_TIMEOUT=500 \
	LANG=C XDG_CACHE_HOME=$(abs_builddir) \
	G_SLICE=always-malloc G_DEBUG=gc-friendly \
	GLIBCPP_FORCE_NEW=1 GLIBCXX_FORCE_NEW=1
VALDEFAULT = @VALGRIND@/bin/valgrind
VALSUPP = @VALGRIND@/lib/valgrind
VALSUPPRESSIONDEF = --suppressions=$(VALSUPP)/default.supp
VALSUPPRESSIONOWN = --suppressions=$(top_builddir)/buzztrax.supp \
	--suppressions=$(top_builddir)/gst.supp \
	--suppressions=$(top_builddir)/gtk.supp
VALSUPPRESSION = $(VALSUPPRESSIONDEF) $(VALSUPPRESSIONOWN)
VALOPTIONS = --trace-children=yes --num-callers=30 --read-var-info=yes \
  --tool=memcheck --leak-check=full --leak-resolution=high --track-origins=yes
VALCMD = $(VALPREFIX) $(VALDEFAULT) $(VALOPTIONS) $(VALSUPPRESSION)

## for file in /tmp/bt_core.valgrind.pid*; do grep -q "ERROR SUMMARY: 0 errors from 0 contexts" $file ; if test $? == 0; then rm $file; fi; done
## grep -H "ERROR SUMMARY: " /tmp/bt_core.valgrind.pid* | uniq

# make valgrind        -- valgrind all tests
valgrind:: $(TESTS_BIN)
	for i in $^; do \
	  rm -rf /tmp/$$i.valgrind.pid*; \
	  $(VALCMD) --log-file=/tmp/$$i.valgrind.pid%p ./$$i; \
	  grep -l "ERROR SUMMARY: 0 errors from 0 contexts" /tmp/$$i.valgrind.pid* | xargs rm; \
	done

# make (test).valgrind -- valgrind the given test"
%.valgrind: %
	rm -rf /tmp/$*.valgrind.pid*; \
	$(VALCMD) --log-file=/tmp/$*.valgrind.pid%p ./$*; \
	grep -l "ERROR SUMMARY: 0 errors from 0 contexts" /tmp/$*.valgrind.pid* | xargs rm;

endif

# make test-status     -- show info about disabled tests
test-status::
	@grep -r -A2 -B6 -Hn --include="*.c" "#ifdef __CHECK_DISABLED__" . | grep -e '\.c-[0-9][0-9]*-\(\/\/\|\/\*\|\ \*\|ST\)' 
	@echo "number of disabled tests"
	@grep -r -Hn --include="*.c" --color=auto "#ifdef __CHECK_DISABLED__" . | wc -l

