# Copyright (c) 2026 CNES.
#
# All rights reserved. Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
enable_testing()

# Force Release runtime library for MSVC to match GTest build
if(MSVC)
  string(REPLACE "/MDd" "/MD" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
  string(REPLACE "/D_DEBUG" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
endif()

# List of test targets generated by the ADD_TESTCASE macro below.
set(TEST_TARGETS)

# Create a new test suite.
macro(ADD_TESTCASE testname sources)
  set(libraries ${ARGN})
  add_executable(test_${testname} ${sources})
  target_link_libraries(test_${testname} GTest::gtest_main ${libraries}
                        cpp_coverage)
  add_test(NAME test_${testname}
           COMMAND ${EXECUTABLE_OUTPUT_PATH}/test_${testname}
                   --gtest_output=xml:test_${testname}.xml)
  if(ENABLE_COVERAGE)
    add_coverage(test_${testname})
  endif()
  # Force Release runtime for MSVC to match GTest build
  if(MSVC)
    target_compile_options(test_${testname} PRIVATE /MD /bigobj)
    if(CMAKE_BUILD_TYPE STREQUAL "Debug")
      target_compile_definitions(test_${testname}
                                 PRIVATE _ITERATOR_DEBUG_LEVEL=0)
    endif()
  endif()
  list(APPEND TEST_TARGETS test_${testname})
endmacro()

# dateutils
file(GLOB_RECURSE dateutils_src "${CMAKE_CURRENT_SOURCE_DIR}/dateutils/*.cpp")
add_testcase(dateutils "${dateutils_src}")

# serialization_buffer
add_testcase(serialization_buffer
             "${CMAKE_CURRENT_SOURCE_DIR}/serialization_buffer.cpp")

# parallel_for
add_testcase(parallel_for "${CMAKE_CURRENT_SOURCE_DIR}/parallel_for.cpp")

# math
add_testcase(math "${CMAKE_CURRENT_SOURCE_DIR}/math.cpp")

# axis
file(GLOB_RECURSE axis_src "${CMAKE_CURRENT_SOURCE_DIR}/math/axis/*.cpp")
add_testcase(axis "${axis_src}")

# config
file(GLOB_RECURSE config_src "${CMAKE_CURRENT_SOURCE_DIR}/config/*.cpp")
add_testcase(config "${config_src}")

# interpolate
file(GLOB_RECURSE interpolate_src
     "${CMAKE_CURRENT_SOURCE_DIR}/math/interpolate/*.cpp")
add_testcase(interpolate "${interpolate_src}" BLAS::BLAS)

# statistics
file(GLOB_RECURSE statistics_src
     "${CMAKE_CURRENT_SOURCE_DIR}/math/statistics/*.cpp")
add_testcase(statistics "${statistics_src}")

# geometry
file(GLOB_RECURSE geometry_src "${CMAKE_CURRENT_SOURCE_DIR}/geometry/*.cpp")
add_testcase(geometry "${geometry_src}" pyinterp BLAS::BLAS)

# geohash
file(GLOB_RECURSE geohash_src "${CMAKE_CURRENT_SOURCE_DIR}/geohash/*.cpp")
add_testcase(geohash "${geohash_src}" pyinterp)

# fft
file(GLOB_RECURSE fft_src "${CMAKE_CURRENT_SOURCE_DIR}/math/fft/*.cpp")
add_testcase(fft "${fft_src}" BLAS::BLAS ${FFT_LIBRARIES})

# fill
file(GLOB_RECURSE fill_src "${CMAKE_CURRENT_SOURCE_DIR}/math/fill/*.cpp")
add_testcase(fill "${fill_src}" BLAS::BLAS ${FFT_LIBRARIES})

# Add a custom target "test_all" that runs all the test cases with CTest.
add_custom_target(
  test_all
  COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
  COMMENT "Running all test cases"
  DEPENDS ${TEST_TARGETS})
