#
# Testing
#
ENABLE_TESTING()
# INCLUDE(${CMAKE_SOURCE_DIR}/platforms/cuda/cuda-cmake/FindCuda.cmake)
INCLUDE_DIRECTORIES(${CUDA_INCLUDE})
INCLUDE_DIRECTORIES(${OPENMM_DIR}/platforms/cuda/include)
INCLUDE_DIRECTORIES(${OPENMM_DIR}/platforms/cuda/src)
INCLUDE_DIRECTORIES(${OPENMM_DIR}/platforms/cuda/src/kernels)

# serialize test cases for GBVI and GBSAOBC softcore runs
# if INCLUDE_SERIALIZATION is TRUE

SET( INCLUDE_SERIALIZATION FALSE )
#SET( INCLUDE_SERIALIZATION TRUE )

SET( SHARED_OPENMM_TARGET OpenMMFreeEnergy)
SET( SHARED_CUDA_TARGET OpenMMCuda)

IF( INCLUDE_SERIALIZATION )
    INCLUDE_DIRECTORIES(${OPENMM_DIR}/serialization/include)
    SET( SHARED_OPENMM_SERIALIZATION "OpenMMSerialization" )
    SET( SHARED_FREE_ENERGY_SERIALIZATION "FreeEnergySerialization" )
ENDIF( INCLUDE_SERIALIZATION )

IF (UNIX AND CMAKE_BUILD_TYPE MATCHES Debug)
    SET(SHARED_CUDA_TARGET   ${SHARED_CUDA_TARGET}_d)
    SET(SHARED_OPENMM_TARGET ${SHARED_OPENMM_TARGET}_d)
    IF( INCLUDE_SERIALIZATION )
        SET(SHARED_OPENMM_SERIALIZATION ${SHARED_OPENMM_SERIALIZATION}_d)
        SET(SHARED_FREE_ENERGY_SERIALIZATION ${SHARED_FREE_ENERGY_SERIALIZATION}_d)
    ENDIF( INCLUDE_SERIALIZATION )
ENDIF (UNIX AND CMAKE_BUILD_TYPE MATCHES Debug)

# Automatically create tests using files named "Test*.cpp"
FILE(GLOB TEST_PROGS "*Test*.cpp")
FOREACH(TEST_PROG ${TEST_PROGS})
    GET_FILENAME_COMPONENT(TEST_ROOT ${TEST_PROG} NAME_WE)

    # Link with shared library

    CUDA_ADD_EXECUTABLE(${TEST_ROOT} ${TEST_PROG})
    IF( INCLUDE_SERIALIZATION )
        TARGET_LINK_LIBRARIES(${TEST_ROOT} ${SHARED_TARGET} ${SHARED_OPENMM_TARGET} ${SHARED_CUDA_TARGET} ${SHARED_OPENMM_SERIALIZATION} ${SHARED_FREE_ENERGY_SERIALIZATION})
    ELSE( INCLUDE_SERIALIZATION )
        TARGET_LINK_LIBRARIES(${TEST_ROOT} ${SHARED_TARGET} ${SHARED_OPENMM_TARGET} ${SHARED_CUDA_TARGET})
    ENDIF( INCLUDE_SERIALIZATION )
    SET(DEFINE_STRING "-DUSE_SOFTCORE")
    IF( INCLUDE_SERIALIZATION )
        SET(DEFINE_STRING "${DEFINE_STRING} -DOPENMM_SERIALIZE")
    ENDIF( INCLUDE_SERIALIZATION )

    IF( ${TEST_ROOT} STREQUAL "TestCudaGBVISoftcoreForce2" )
    
        # serialize

        SET(DEFINE_STRING "-DTEST_PLATFORM=0 ")
        IF( INCLUDE_SERIALIZATION )
            SET(DEFINE_STRING "${DEFINE_STRING} -DOPENMM_SERIALIZE ")
            SET(TARGET_LINK_LIBRARIES_STRING "${SHARED_TARGET} ${SHARED_OPENMM_SERIALIZATION}")
        ELSE( INCLUDE_SERIALIZATION )
            SET(TARGET_LINK_LIBRARIES_STRING "${SHARED_TARGET}")
        ENDIF( INCLUDE_SERIALIZATION )
    
        # obc

        SET(OBC_DEFINE_STRING "${DEFINE_STRING} -DIMPLICIT_SOLVENT=1")
        SET(OBC_TEST "TestCudaGBSAOBCSoftcoreForce2")
        CUDA_ADD_EXECUTABLE(${OBC_TEST} ${TEST_PROG})

        IF( INCLUDE_SERIALIZATION )
            TARGET_LINK_LIBRARIES(${OBC_TEST} ${SHARED_TARGET} ${SHARED_OPENMM_SERIALIZATION} )
        ELSE( INCLUDE_SERIALIZATION )
            TARGET_LINK_LIBRARIES(${OBC_TEST} ${SHARED_TARGET})
        ENDIF( INCLUDE_SERIALIZATION )
        SET_TARGET_PROPERTIES(${OBC_TEST} PROPERTIES COMPILE_FLAGS ${OBC_DEFINE_STRING} )
        ADD_TEST(${OBC_TEST} ${EXECUTABLE_OUTPUT_PATH}/${OBC_TEST})

        # nonbond

        SET(NONBOND_DEFINE_STRING "${DEFINE_STRING} -DIMPLICIT_SOLVENT=0")
        SET(NONBOND_TEST "TestCudaNonbondSoftcoreForce2")
        CUDA_ADD_EXECUTABLE(${NONBOND_TEST} ${TEST_PROG})

        IF( INCLUDE_SERIALIZATION )
            TARGET_LINK_LIBRARIES(${NONBOND_TEST} ${SHARED_TARGET} ${SHARED_OPENMM_SERIALIZATION} )
        ELSE( INCLUDE_SERIALIZATION )
            TARGET_LINK_LIBRARIES(${NONBOND_TEST} ${SHARED_TARGET})
        ENDIF( INCLUDE_SERIALIZATION )
        SET_TARGET_PROPERTIES(${NONBOND_TEST} PROPERTIES COMPILE_FLAGS ${NONBOND_DEFINE_STRING} )
        ADD_TEST(${NONBOND_TEST} ${EXECUTABLE_OUTPUT_PATH}/${NONBOND_TEST})

        # gbvi

        SET(DEFINE_STRING "${DEFINE_STRING} -DIMPLICIT_SOLVENT=2")
        SET_TARGET_PROPERTIES(${TEST_ROOT} PROPERTIES COMPILE_FLAGS ${DEFINE_STRING} )

    ENDIF( ${TEST_ROOT} STREQUAL "TestCudaGBVISoftcoreForce2" )

    #MESSAGE( "${TEST_ROOT} ${DEFINE_STRING}" )
    SET_TARGET_PROPERTIES(${TEST_ROOT} PROPERTIES COMPILE_FLAGS ${DEFINE_STRING} )
    ADD_TEST(${TEST_ROOT} ${EXECUTABLE_OUTPUT_PATH}/${TEST_ROOT})

ENDFOREACH(TEST_PROG ${TEST_PROGS})
