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

##
# LIBGTEST is the google test library
# GTEST_MAIN is the file that contains the google test
##
LIBGTEST = test/libgtest.a
GTEST_MAIN = $(GTEST)/src/gtest_main.cc
CFLAGS_GTEST += -isystem $(GTEST)/include -isystem $(GTEST)

##
# Build the google test library.
## 
$(LIBGTEST): $(LIBGTEST)(test/gtest.o)

test/gtest.o: $(GTEST)/src/gtest-all.cc
	@mkdir -p test
	$(COMPILE.c) -O$O $(CFLAGS_GTEST) $< $(OUTPUT_OPTION)

##
# Rule for building a test
##
test/%_test.o : test/%_test.cpp
	@mkdir -p $(dir $@)
	$(COMPILE.c) -O$O $(CFLAGS_GTEST) $< $(OUTPUT_OPTION)

##
# Rule for building a test executable
##
test/%$(EXE) : test/%.o $(LIBGTEST) $(LIBCVODES)
	@mkdir -p $(dir $@)
	$(LINK.c) -O$O $(GTEST_MAIN) $< $(CFLAGS_GTEST) $(OUTPUT_OPTION) $(LIBGTEST) $(LDLIBS) $(LIBCVODES)


##
# Rule for generating dependencies.
##
.PRECIOUS: test/%_test.d
test/%_test.d : test/%_test.cpp
	@mkdir -p $(dir $@)
	@set -e; \
	rm -f $@; \
	$(CC) $(CFLAGS) -O$O $(CFLAGS_GTEST) $(TARGET_ARCH) -MM $< > $@.$$$$; \
	sed -e 's,\($(notdir $*)\)\(_test\)\.o[ :]*,$(dir $@)\1_test\$(EXE) $@ : ,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$



############################################################
##
# 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.c) -O0 -include $< -o $(DEV_NULL) test/dummy.cpp

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.c) -O$(O_STANC) $(CFLAGS) $< $(OUTPUT_OPTION)


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

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

.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
