#
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
# OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
#
# Permission is hereby granted to use or copy this program
# for any purpose,  provided the above notices are retained on all copies.
# Permission to modify the code and to distribute modified code is granted,
# provided the above notices are retained, and a notice that the code was
# modified is included with the above copyright notice.

# Common libs to _LDADD for all tests.
test_ldadd = $(nodist_libgc_la_OBJECTS) $(top_builddir)/libgc.la \
            $(EXTRA_TEST_LIBS)

TESTS += gctest$(EXEEXT)
check_PROGRAMS += gctest
gctest_SOURCES = tests/test.c
gctest_LDADD = $(test_ldadd)
if THREADS
gctest_LDADD += $(THREADDLLIBS)
endif
gctest_DEPENDENCIES = $(top_builddir)/libgc.la

if EMSCRIPTEN
# Note: because of libtool, you'll need to point your browser to
# .libs/gctest.html, not gctest.html at topdir.
check_PROGRAMS += gctest.html
gctest_html_SOURCES = $(gctest_SOURCES)
gctest_html_LDADD = $(gctest_LDADD)
# Bug in the linker not being able to determine that _memalign and _memset
# are needed? it's part of mmap.
gctest_html_LDFLAGS = -s "EXPORTED_FUNCTIONS=['_memalign', '_main', '_memset']"
endif

TESTS += leaktest$(EXEEXT)
check_PROGRAMS += leaktest
leaktest_SOURCES = tests/leak_test.c
leaktest_LDADD = $(test_ldadd)

TESTS += middletest$(EXEEXT)
check_PROGRAMS += middletest
middletest_SOURCES = tests/middle.c
middletest_LDADD = $(test_ldadd)

TESTS += smashtest$(EXEEXT)
check_PROGRAMS += smashtest
smashtest_SOURCES = tests/smash_test.c
smashtest_LDADD = $(test_ldadd)

TESTS += hugetest$(EXEEXT)
check_PROGRAMS += hugetest
hugetest_SOURCES = tests/huge_test.c
hugetest_LDADD = $(test_ldadd)

TESTS += realloc_test$(EXEEXT)
check_PROGRAMS += realloc_test
realloc_test_SOURCES = tests/realloc_test.c
realloc_test_LDADD = $(test_ldadd)

TESTS += staticrootstest$(EXEEXT)
check_PROGRAMS += staticrootstest
staticrootstest_SOURCES = tests/staticrootstest.c
staticrootstest_CFLAGS = -DSTATICROOTSLIB2
staticrootstest_LDADD = $(nodist_libgc_la_OBJECTS) $(EXTRA_TEST_LIBS) \
                        libstaticrootslib_test.la libstaticrootslib2_test.la
check_LTLIBRARIES += libstaticrootslib_test.la libstaticrootslib2_test.la
libstaticrootslib_test_la_SOURCES = tests/staticrootslib.c
libstaticrootslib_test_la_LIBADD = $(test_ldadd)
libstaticrootslib_test_la_LDFLAGS = -no-undefined -rpath /nowhere
libstaticrootslib_test_la_DEPENDENCIES = $(top_builddir)/libgc.la
libstaticrootslib2_test_la_SOURCES = tests/staticrootslib.c
libstaticrootslib2_test_la_LIBADD = $(test_ldadd)
libstaticrootslib2_test_la_CFLAGS = -DSTATICROOTSLIB2
libstaticrootslib2_test_la_LDFLAGS = -no-undefined -rpath /nowhere
if ENABLE_SHARED
staticrootstest_LDADD += $(top_builddir)/libgc.la
endif

if KEEP_BACK_PTRS
TESTS += tracetest$(EXEEXT)
check_PROGRAMS += tracetest
tracetest_SOURCES = tests/trace_test.c
tracetest_LDADD = $(test_ldadd)
endif

if THREADS

TESTS += test_atomic_ops$(EXEEXT)
check_PROGRAMS += test_atomic_ops
test_atomic_ops_SOURCES = tests/test_atomic_ops.c
# Really should need only $(ATOMIC_OPS_LIBS)
test_atomic_ops_LDADD = $(test_ldadd) $(THREADDLLIBS)

TESTS += threadleaktest$(EXEEXT)
check_PROGRAMS += threadleaktest
threadleaktest_SOURCES = tests/thread_leak_test.c
threadleaktest_LDADD = $(test_ldadd) $(THREADDLLIBS)

TESTS += threadkey_test$(EXEEXT)
check_PROGRAMS += threadkey_test
threadkey_test_SOURCES = tests/threadkey_test.c
threadkey_test_LDADD = $(test_ldadd) $(THREADDLLIBS)

TESTS += subthreadcreate_test$(EXEEXT)
check_PROGRAMS += subthreadcreate_test
subthreadcreate_test_SOURCES = tests/subthread_create.c
subthreadcreate_test_LDADD = $(test_ldadd) $(THREADDLLIBS)

TESTS += initsecondarythread_test$(EXEEXT)
check_PROGRAMS += initsecondarythread_test
initsecondarythread_test_SOURCES = tests/initsecondarythread.c
initsecondarythread_test_LDADD = $(test_ldadd) $(THREADDLLIBS)

endif

if CPLUSPLUS
TESTS += test_cpp$(EXEEXT)
check_PROGRAMS += test_cpp
test_cpp_SOURCES = tests/test_cpp.cc
if AVOID_CPP_LIB
test_cpp_LDADD = gc_badalc.o gc_cpp.o $(test_ldadd) $(CXXLIBS)
else
test_cpp_LDADD = libgccpp.la $(nodist_libgc_la_OBJECTS) \
                $(EXTRA_TEST_LIBS) $(CXXLIBS)
## In case of static libraries build, libgc.a is already referenced in
## dependency_libs attribute of libgccpp.la file.
if ENABLE_SHARED
test_cpp_LDADD += $(top_builddir)/libgc.la
endif
endif
endif

if ENABLE_DISCLAIM

TESTS += disclaim_test$(EXEEXT)
check_PROGRAMS += disclaim_test
disclaim_test_SOURCES = tests/disclaim_test.c
disclaim_test_LDADD = $(test_ldadd)
if THREADS
disclaim_test_LDADD += $(THREADDLLIBS)
endif

TESTS += disclaim_bench$(EXEEXT)
check_PROGRAMS += disclaim_bench
disclaim_bench_SOURCES = tests/disclaim_bench.c
disclaim_bench_LDADD = $(test_ldadd)

TESTS += disclaim_weakmap_test$(EXEEXT)
check_PROGRAMS += disclaim_weakmap_test
disclaim_weakmap_test_SOURCES = tests/disclaim_weakmap_test.c
disclaim_weakmap_test_LDADD = $(test_ldadd)
if THREADS
disclaim_weakmap_test_LDADD += $(THREADDLLIBS)
endif

endif

# Run the tests directly (without test-driver):
.PHONY: check-without-test-driver
check-without-test-driver: $(TESTS)
	./gctest$(EXEEXT)
	./hugetest$(EXEEXT)
	./leaktest$(EXEEXT)
	./middletest$(EXEEXT)
	./realloc_test$(EXEEXT)
	./smashtest$(EXEEXT)
	./staticrootstest$(EXEEXT)
	test ! -f disclaim_bench$(EXEEXT) || ./disclaim_bench$(EXEEXT)
	test ! -f disclaim_test$(EXEEXT) || ./disclaim_test$(EXEEXT)
	test ! -f disclaim_weakmap_test$(EXEEXT) || ./disclaim_weakmap_test$(EXEEXT)
	test ! -f initsecondarythread_test$(EXEEXT) \
	  || ./initsecondarythread_test$(EXEEXT)
	test ! -f test_atomic_ops$(EXEEXT) || ./test_atomic_ops$(EXEEXT)
	test ! -f threadkey_test$(EXEEXT) || ./threadkey_test$(EXEEXT)
	test ! -f threadleaktest$(EXEEXT) || ./threadleaktest$(EXEEXT)
	test ! -f subthreadcreate_test$(EXEEXT) || ./subthreadcreate_test$(EXEEXT)
	test ! -f test_cpp$(EXEEXT) || ./test_cpp$(EXEEXT)
	test ! -f tracetest$(EXEEXT) || ./tracetest$(EXEEXT)
	./cordtest$(EXEEXT)
