N_TESTS ?= 100 # override this in make/local. If <= 0, N_TESTS + 1 is interpreted as the number of batches to group the tests into

##
# GTEST_MAIN is the file that contains the google test
##
GTEST_MAIN = $(GTEST)/src/gtest_main.cc
GTEST_CXXFLAGS += -isystem $(GTEST)/include -isystem $(GTEST) -O$O

##
# Build the google test library.
##
$(GTEST)/src/gtest-all.o: CXXFLAGS += $(GTEST_CXXFLAGS)

test/%$(EXE) : CXXFLAGS += $(GTEST_CXXFLAGS)
test/%$(EXE) : test/%.o $(GTEST_MAIN) $(GTEST)/src/gtest-all.o
	$(LINK.cpp) -o $@ $^


##
# Rule for generating dependencies.
##
.PRECIOUS: test/%.d
test/%.d : test/%.cpp
	@mkdir -p $(dir $@)
	@set -e; \
	rm -f $@; \
	$(COMPILE.cpp) -MM $< > $@.$$$$; \
	sed -e 's,\($(*F)\)\.o[ :]*,$(@D)/\1.o $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

##
# Adding a test for multiple translation units. If this fails,
# a new function is probably missing an inline
##

test/unit/multiple_translation_units%.o : test/unit/multiple_translation_units%.cpp
	$(COMPILE.cpp) -fPIC -O$O $< $(OUTPUT_OPTION)

test/unit/libmultiple.so : test/unit/multiple_translation_units1.o test/unit/multiple_translation_units2.o
	$(LINK.cpp) -shared -fPIC $(OUTPUT_OPTION) $^

test/unit/multiple_translation_units_test.cpp : test/unit/libmultiple.so

############################################################
#
# CVODES tests
##

CVODES_TESTS := $(subst .cpp,$(EXE),$(shell find test -name *cvodes*_test.cpp) $(shell find test -name *_bdf_*_test.cpp) $(shell find test -name *_adams_*_test.cpp))
$(CVODES_TESTS) : $(LIBCVODES)

############################################################
#
# IDAS tests
##

IDAS_TESTS := $(subst .cpp,$(EXE),$(shell find test -name *idas*_test.cpp) $(shell find test -name *_dae_*_test.cpp))
$(IDAS_TESTS) : $(LIBIDAS)

############################################################
#
# OpenCL tests
##

OPENCL_TESTS := $(subst .cpp,$(EXE),$(shell find test -name *opencl*_test.cpp))
$(OPENCL_TESTS) : LDFLAGS += $(LDLIBS_OPENCL)

############################################################
#
# Target to verify header files within Stan has
# enough include calls
##
HEADER_TESTS := $(addsuffix -test,$(shell find stan -name '*.hpp' -type f))

ifeq ($(OS_TYPE),win)
  DEV_NULL = nul
else
  DEV_NULL = /dev/null
endif

.PHONY: HEADER_TESTS
%.hpp-test : %.hpp test/dummy.cpp
	$(COMPILE.cpp) -O0 -include $^ -o $(DEV_NULL)

test/dummy.cpp:
	@mkdir -p test
	@touch $@
	@echo "int main() {return 0;}" >> $@

.PHONY: test-headers
test-headers: $(HEADER_TESTS)

############################################################
##
# Test generator for distribution tests
##
test/prob/generate_tests$(EXE) : test/prob/generate_tests.cpp
	@mkdir -p $(dir $@)
	$(LINK.cpp) $< $(OUTPUT_OPTION)



## FIXME: think about how to do this generally using test_types
# test_types := v fd fv ffd ffv

test_name = $(shell echo $(1) | sed 's,_[0-9]\{5\},_test.hpp,g')

.SECONDEXPANSION:
test/prob/%_generated_v_test.cpp test/prob/%_generated_fd_test.cpp test/prob/%_generated_fv_test.cpp test/prob/%_generated_ffd_test.cpp test/prob/%_generated_ffv_test.cpp: test/prob/$$(call test_name,$$*) test/prob/generate_tests$(EXE)
	$(WINE) test/prob/generate_tests$(EXE) $< $(N_TESTS)

LIST_OF_GENERATED_TESTS := $(shell find test/prob -type f -name '*_test.hpp' | sed 's,_test.hpp,_00000_generated_v_test.cpp,g') $(shell find test/prob -type f -name '*_test.hpp' | sed 's,_test.hpp,_00000_generated_fd_test.cpp,g') $(shell find test/prob -type f -name '*_test.hpp' | sed 's,_test.hpp,_00000_generated_fv_test.cpp,g') $(shell find test/prob -type f -name '*_test.hpp' | sed 's,_test.hpp,_00000_generated_ffd_test.cpp,g') $(shell find test/prob -type f -name '*_test.hpp' | sed 's,_test.hpp,_00000_generated_ffv_test.cpp,g')

.PHONY: generate-tests
generate-tests: $(LIST_OF_GENERATED_TESTS)

##
# Include the test dependencies
##
include make/test-math-dependencies