### Build rules for BALL's class tests
PROJECT("BALL_tests")

ENABLE_TESTING()

OPTION(BALL_TEST_VERBOSE "Use verbose output for the tests" ON)

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/TEST)
INCLUDE_DIRECTORIES(${PROJECT_BINARY_DIR})

## For now, disable optimization for the tests
IF (CMAKE_COMPILER_IS_INTELCXX OR CMAKE_COMPILER_IS_GNUCXX)
	SET(CMAKE_CXX_FLAGS_RELEASE_SAVED ${CMAKE_CXX_FLAGS_RELEASE})
	SET(CMAKE_CXX_FLAGS_RELEASE "-O0")
ENDIF()

## On 32 Bit Windows, disable optimization for the tests
IF(CMAKE_GENERATOR STREQUAL "Visual Studio 10")
	SET(CMAKE_CXX_FLAGS_RELEASE_SAVED ${CMAKE_CXX_FLAGS_RELEASE})
	SET(CMAKE_CXX_FLAGS_RELEASE "-O0")
ENDIF()

INCLUDE(cmake/BALLTestExecutables.cmake)
INCLUDE(cmake/VIEWTestExecutables.cmake)

## BASIC
ADD_CUSTOM_TARGET(BASIC_TEST)
ADD_DEPENDENCIES(BASIC_TEST ${BALL_BASIC_TESTS})

## MATHS
ADD_CUSTOM_TARGET(MATHS_TEST)
ADD_DEPENDENCIES(MATHS_TEST ${BALL_MATHS_TESTS})

## SYSTEM
ADD_CUSTOM_TARGET(SYSTEM_TEST)
ADD_DEPENDENCIES(SYSTEM_TEST ${BALL_SYSTEM_TESTS})

## TODO: all of them...

## Restore build flags
IF (CMAKE_COMPILER_IS_INTELCXX OR CMAKE_COMPILER_IS_GNUCXX)
	SET(${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_RELEASE_SAVED})
ENDIF()

SET(BALLTEST_SUPPORT_LIBRARIES "")
IF (UNIX)
	LIST(APPEND BALLTEST_SUPPORT_LIBRARIES "pthread")
ENDIF ()

# Now, add all tests as targets
SET(ALL_TESTS "")
FOREACH(group ${BALL_TESTS})
	FOREACH(test ${${group}})
		IF (WIN32)
			# Workaround for the stupid MSVC
			ADD_EXECUTABLE(${test} ${test}.C stringStub.C)
		ELSE()
			ADD_EXECUTABLE(${test} ${test}.C)
		ENDIF()
		TARGET_LINK_LIBRARIES(${test} BALL ${BALLTEST_SUPPORT_LIBRARIES})
		SET_PROPERTY(TARGET ${test} PROPERTY FOLDER ${group})
		IF (NOT BALL_TEST_VERBOSE)
			ADD_TEST(${test} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test})
		ELSE()
			ADD_TEST(${test} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test} "-v")
		ENDIF()
		LIST(APPEND ALL_TESTS ${test})
	ENDFOREACH()
ENDFOREACH()

# Set the timeout of some tests to 2h
# This is needed for the memcheck running on the CI server
# which takes _very_ long
SET_TESTS_PROPERTIES (
	ScoringFunction_test
	AssignBondOrderProcessor_test
	PROPERTIES TIMEOUT 7200
)

IF (BALL_HAS_VIEW)
	FOREACH(test ${VIEW_TESTS})
		IF (WIN32)
			# Workaround for the stupid MSVC
			ADD_EXECUTABLE(${test} ${test}.C stringStub.C)
		ELSE()
			ADD_EXECUTABLE(${test} ${test}.C)
		ENDIF()
		TARGET_LINK_LIBRARIES(${test} BALL VIEW ${BALLTEST_SUPPORT_LIBRARIES})
		SET_PROPERTY(TARGET ${test} PROPERTY FOLDER BALL_VIEW_TESTS)
		IF (NOT BALL_TEST_VERBOSE)
			ADD_TEST(${test} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test})
		ELSE()
			ADD_TEST(${test} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test} "-v")
		ENDIF()
	ENDFOREACH()

	LIST(APPEND ALL_TESTS ${VIEW_TESTS})
ENDIF()

# Create a suitable target
ADD_CUSTOM_TARGET(build_tests)
ADD_DEPENDENCIES(build_tests ${ALL_TESTS})
