#
# Testing
#

ENABLE_TESTING()

# INCLUDE(${CMAKE_SOURCE_DIR}/platforms/cuda/cuda-cmake/FindCuda.cmake)
INCLUDE_DIRECTORIES(${CUDA_INCLUDE})

SET( INCLUDE_SERIALIZATION FALSE )
#SET( INCLUDE_SERIALIZATION TRUE )

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

# Automatically create tests using files named "Test*.cpp"
FILE(GLOB TEST_PROGS "*Test*.cpp")
# TestCudaRandom has never worked on windows, so let's stop polluting the dashboard with it.
IF (${CMAKE_GENERATOR} MATCHES "Visual Studio")
    FILE(GLOB TEST_CUDA_RANDOM_PROG "*TestCudaRandom.cpp")
    LIST(REMOVE_ITEM TEST_PROGS "${TEST_CUDA_RANDOM_PROG}")
ENDIF (${CMAKE_GENERATOR} MATCHES "Visual Studio")

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})
    TARGET_LINK_LIBRARIES(${TEST_ROOT} ${SHARED_TARGET})

    # Three tests are created from the file TestCudaGBVIForce2.cpp by
    # setting preprocessor definitions

    #    TestCudaNonbondForce2
    #    TestCudaGBSAOBCForce2
    #    TestCudaGBVIForce2

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

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

        SET(OBC_DEFINE_STRING "${DEFINE_STRING} -DIMPLICIT_SOLVENT=1")
        SET(OBC_TEST "TestCudaGBSAOBCForce2")
        CUDA_ADD_EXECUTABLE(${OBC_TEST} ${TEST_PROG})
        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 "TestCudaNonbondForce2")
        CUDA_ADD_EXECUTABLE(${NONBOND_TEST} ${TEST_PROG})
        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} )

        # libs

        IF( INCLUDE_SERIALIZATION )
            TARGET_LINK_LIBRARIES(${OBC_TEST} ${SHARED_TARGET} ${SHARED_OPENMM_SERIALIZATION} )
            TARGET_LINK_LIBRARIES(${NONBOND_TEST} ${SHARED_TARGET} ${SHARED_OPENMM_SERIALIZATION} )
            TARGET_LINK_LIBRARIES(${TEST_ROOT} ${SHARED_TARGET} ${SHARED_OPENMM_SERIALIZATION} )
        ELSE( INCLUDE_SERIALIZATION )
            TARGET_LINK_LIBRARIES(${OBC_TEST} ${SHARED_TARGET})
            TARGET_LINK_LIBRARIES(${NONBOND_TEST} ${SHARED_TARGET})
            TARGET_LINK_LIBRARIES(${TEST_ROOT} ${SHARED_TARGET})
        ENDIF( INCLUDE_SERIALIZATION )

    ENDIF( ${TEST_ROOT} STREQUAL "TestCudaGBVIForce2" )

    #MESSAGE( "vcm ${TEST_ROOT} ${DEFINE_STRING}" )

    ADD_TEST(${TEST_ROOT} ${EXECUTABLE_OUTPUT_PATH}/${TEST_ROOT})

    # Link with static library
#     SET(TEST_STATIC ${TEST_ROOT}Static)
#     CUDA_ADD_EXECUTABLE(${TEST_STATIC} ${TEST_PROG})
#     SET_TARGET_PROPERTIES(${TEST_STATIC}
#                 PROPERTIES
#                 COMPILE_FLAGS "-DOPENMM_USE_STATIC_LIBRARIES"
#                 )
#     TARGET_LINK_LIBRARIES(${TEST_STATIC} ${STATIC_TARGET})
#     ADD_TEST(${TEST_STATIC} ${EXECUTABLE_OUTPUT_PATH}/${TEST_STATIC})

ENDFOREACH(TEST_PROG ${TEST_PROGS})

install(TARGETS TestCudaHarmonicBondForce
    RUNTIME DESTINATION bin)

