# -*- Mode: C++; c-basic-offset:4; indent-tabs-mode:nil -*-
#
# Copyright (c) 2020-2026 Triad National Security, LLC
#                         All rights reserved.
#
# Copyright (c) 2020-2021 Lawrence Livermore National Security, LLC
#                         All rights reserved.
#
# This file is part of the quo-vadis project. See the LICENSE file at the
# top-level directory of this distribution.
#

################################################################################
################################################################################

add_library(
    common-test-utils
    common-test-utils.h
    common-test-utils.cc
)

target_include_directories(
    common-test-utils
    PRIVATE
      $<TARGET_PROPERTY:quo-vadis-core,INCLUDE_DIRECTORIES>
      $<$<BOOL:${MPI_FOUND}>:${MPI_C_INCLUDE_DIRS}>
)

target_link_libraries(
    common-test-utils
    PRIVATE
      quo-vadis
      $<$<BOOL:${MPI_FOUND}>:quo-vadis-mpi>
)

target_compile_options(
    common-test-utils
    PRIVATE
      "$<$<BOOL:${MPI_FOUND}>:-DCTU_HAS_MPI_SUPPORT>"
)

add_executable(
    test-process-scopes
    test-process-scopes.c
)

target_link_libraries(
    test-process-scopes
    common-test-utils
    quo-vadis
)

add_executable(
    test-process-hardware
    test-process-hardware.c
)

target_link_libraries(
    test-process-hardware
    common-test-utils
    quo-vadis
)

add_executable(
    test-process-intrinsic-scopes
    test-process-intrinsic-scopes.c
)

target_link_libraries(
    test-process-intrinsic-scopes
    common-test-utils
    quo-vadis
)

add_test(
    NAME
      process-scopes
    COMMAND
      bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
      ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
      ${CMAKE_CURRENT_BINARY_DIR}/test-process-scopes"
)

add_test(
    NAME
      process-hardware
    COMMAND
      bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
      ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
      ${CMAKE_CURRENT_BINARY_DIR}/test-process-hardware"
)

add_test(
    NAME
      process-intrinsic-scopes
    COMMAND
      bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
      ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
      ${CMAKE_CURRENT_BINARY_DIR}/test-process-intrinsic-scopes"
)

set(
    QV_RESTRICTED_TOPO_DIR
    "${CMAKE_CURRENT_SOURCE_DIR}/internal/synthetic-topologies"
)
set(
    QV_RESTRICTED_TOPO_XML
    "${QV_RESTRICTED_TOPO_DIR}/topo-01N-02P-04C-04PU-restricted.xml"
)

add_test(
    NAME
      process-intrinsic-scopes-restricted
    COMMAND
      bash -c "HWLOC_XMLFILE=${QV_RESTRICTED_TOPO_XML} \
      QVI_TEST_EXPECT_RESTRICTED=1 \
      QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
      ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
      ${CMAKE_CURRENT_BINARY_DIR}/test-process-intrinsic-scopes"
)

# Use the C linker to test for C/C++ linkage problems.
set_target_properties(
    test-process-scopes
    test-process-hardware
    test-process-intrinsic-scopes
    PROPERTIES LINKER_LANGUAGE C
)

set_tests_properties(
    process-scopes
    process-hardware
    process-intrinsic-scopes
    process-intrinsic-scopes-restricted
    PROPERTIES
      TIMEOUT 60
    LABELS "process"
)

################################################################################
################################################################################
if (OPENMP_FOUND)
    add_executable(
        test-omp
        test-omp.c
    )

    target_link_libraries(
        test-omp
        quo-vadis
        OpenMP::OpenMP_C
    )

        add_test(
            NAME
              omp
            COMMAND
              bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
              ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
              ${CMAKE_CURRENT_BINARY_DIR}/test-omp"
        )

    # Set omp test properties.
    set_tests_properties(
        omp
        PROPERTIES
          TIMEOUT 60
          LABELS "omp"
    )
endif()

################################################################################
################################################################################
if(MPI_FOUND)
    add_executable(
        test-mpi-init
        common-test-utils.h
        test-mpi-init.c
    )

    target_link_libraries(
        test-mpi-init
        common-test-utils
        quo-vadis-mpi
    )

    add_executable(
        test-mpi-api
        test-mpi-api.c
    )

    target_link_libraries(
        test-mpi-api
        common-test-utils
        quo-vadis-mpi
    )

    add_executable(
        test-mpi-scopes
        test-mpi-scopes.c
    )

    target_link_libraries(
        test-mpi-scopes
        common-test-utils
        quo-vadis-mpi
    )

    add_executable(
        test-mpi-double-split
        test-mpi-double-split.c
    )

    target_link_libraries(
        test-mpi-double-split
        common-test-utils
        quo-vadis-mpi
    )

    add_executable(
        test-mpi-split-auto
        test-mpi-split-auto.c
    )

    target_link_libraries(
        test-mpi-split-auto
        common-test-utils
        quo-vadis-mpi
    )

    add_executable(
        test-progress-thread
        test-progress-thread.c
    )

    target_link_libraries(
        test-progress-thread
        common-test-utils
        quo-vadis
        quo-vadis-mpi
    )

    add_executable(
        test-pthread-split
        common-test-utils.h
        test-pthread-split.c
    )

    target_link_libraries(
        test-pthread-split
        common-test-utils
        quo-vadis
    )

    add_executable(
        test-mpi-scope-create
        test-mpi-scope-create.c
    )

    target_link_libraries(
        test-mpi-scope-create
        common-test-utils
        quo-vadis-mpi
    )

    add_executable(
        test-mpi-dev-split
        test-mpi-dev-split.c
    )

    target_link_libraries(
        test-mpi-dev-split
        common-test-utils
        quo-vadis-mpi
    )

    # Use the C linker to test for C/C++ linkage problems.
    set_target_properties(
        test-mpi-init
        test-mpi-api
        test-mpi-scopes
        test-mpi-double-split
        test-mpi-split-auto
        test-pthread-split
        test-progress-thread
        test-mpi-scope-create
        test-mpi-dev-split
        PROPERTIES LINKER_LANGUAGE C
    )

    # TODO(skg) Automate adding MPI tests.
    add_test(
        NAME
          mpi-api
        COMMAND
          bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
          ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
          ${MPIEXEC_EXECUTABLE} \
          ${MPIEXEC_NUMPROC_FLAG} \
          ${MPIEXEC_MAX_NUMPROCS} \
          ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-api"
    )

    add_test(
        NAME
          mpi-init
        COMMAND
          bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
          ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
          ${MPIEXEC_EXECUTABLE} \
          ${MPIEXEC_NUMPROC_FLAG} \
          ${MPIEXEC_MAX_NUMPROCS} \
          ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-init"
    )

    add_test(
        NAME
          mpi-scopes
        COMMAND
          bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
          ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
          ${MPIEXEC_EXECUTABLE} \
          ${MPIEXEC_NUMPROC_FLAG} \
          ${MPIEXEC_MAX_NUMPROCS} \
          ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-scopes"
    )

    add_test(
        NAME
          mpi-dev-split
        COMMAND
          bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
          ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
          ${MPIEXEC_EXECUTABLE} \
          ${MPIEXEC_NUMPROC_FLAG} \
          ${MPIEXEC_MAX_NUMPROCS} \
          ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-dev-split"
    )

    add_test(
        NAME
          mpi-split-auto
        COMMAND
          bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
          ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
          ${MPIEXEC_EXECUTABLE} \
          ${MPIEXEC_NUMPROC_FLAG} \
          ${MPIEXEC_MAX_NUMPROCS} \
          ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-split-auto"
    )

    # Set mpi test properties.
    set_tests_properties(
        mpi-api
        mpi-init
        mpi-scopes
        mpi-dev-split
        mpi-split-auto
        PROPERTIES
          TIMEOUT 60
          LABELS "mpi"
    )

############################# HYBRID TESTS (PThreads) ##########################
################################################################################


############################# HYBRID TESTS (OpenMP) ############################
################################################################################
    if(OPENMP_FOUND)
        add_executable(
            test-mpi-threads
            test-mpi-threads.c
        )

        target_link_libraries(
            test-mpi-threads
            common-test-utils
            quo-vadis
            quo-vadis-mpi
            OpenMP::OpenMP_C
        )

        set_target_properties(
            test-mpi-threads
            PROPERTIES LINKER_LANGUAGE C
        )

        add_test(
            NAME
              mpi-threads
            COMMAND
              bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
              ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
              ${MPIEXEC_EXECUTABLE} \
              ${MPIEXEC_NUMPROC_FLAG} \
              ${MPIEXEC_MAX_NUMPROCS} \
              ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-threads"
        )
        # Set mpix test properties.
        set_tests_properties(
            mpi-threads
            PROPERTIES
              TIMEOUT 60
              LABELS "mpix"
        )
    endif()
endif()

################################################################################
################################################################################
if(QV_FORTRAN_HAPPY)
    add_executable(
        test-process-fortapi
        test-process-fortapi.f90
    )

    target_link_libraries(
        test-process-fortapi
        quo-vadisf
    )

    set_target_properties(
        test-process-fortapi
        PROPERTIES LINKER_LANGUAGE Fortran
    )
    add_test(
        NAME
          process-fortapi
        COMMAND
          bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
          ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
          ${CMAKE_CURRENT_BINARY_DIR}/test-process-fortapi"
    )

    # Set fortran test properties.
    set_tests_properties(
        process-fortapi
        PROPERTIES
          TIMEOUT 60
          LABELS "fortran"
    )

    if(MPI_Fortran_FOUND)
        add_executable(
            test-mpi-fortapi
            test-mpi-fortapi.f90
        )

        target_link_libraries(
            test-mpi-fortapi
            quo-vadis-mpif
        )

        set_target_properties(
            test-mpi-fortapi
            PROPERTIES LINKER_LANGUAGE Fortran
        )

        add_test(
            NAME
              mpi-fortapi
            COMMAND
              bash -c "QV_DAEMON=$<TARGET_FILE:quo-vadisd> \
              ${CMAKE_SOURCE_DIR}/tests/run-dtest.sh \
              ${MPIEXEC_EXECUTABLE} \
              ${MPIEXEC_NUMPROC_FLAG} \
              ${MPIEXEC_MAX_NUMPROCS} \
              ${CMAKE_CURRENT_BINARY_DIR}/test-mpi-fortapi"
        )
    endif()
endif()

# vim: ts=4 sts=4 sw=4 expandtab
