cmake_minimum_required(VERSION 3.10)

project(signal_lib_tests)

# Build signal-lib tests
find_package(elcore50-signal-lib QUIET)

macro(build_fft_test FORMAT MEM TYPE DIM)
    if(${DIM} STREQUAL "SL_1D")
        set(TEST_NAME "test_${FORMAT}_${TYPE}_1D_${MEM}.elf")
        add_executable(${TEST_NAME} ${TEST_SOURCES_1D})
    else()
        set(TEST_NAME "test_${FORMAT}_${TYPE}_2D_${MEM}.elf")
        add_executable(${TEST_NAME} ${TEST_SOURCES_2D})
    endif()
    set_target_properties(${TEST_NAME} PROPERTIES LINK_FLAGS "-T${LD_SCRIPT} -nostartfiles")
    target_compile_definitions(${TEST_NAME} PRIVATE
        MAX_SIZE=262144
        FORMAT=SL_${SET_FORMAT}
        MEM_TYPE=SL_${MEM}
        TRANSFORM_TYPES=SL_${TYPE}
        FFT_DIM=${DIM}
    )
    target_compile_options(${TEST_NAME} PRIVATE -Wno-unused-command-line-argument)
    if(${ELCORE50_CL_BUILD})
        target_link_libraries(${TEST_NAME} PRIVATE
            elcore50-signal-lib elcore50-signal-lib-test elcore-runtime)
    else()
        target_link_libraries(${TEST_NAME} PRIVATE elcore50-signal-lib elcore50-signal-lib-test)
    endif()
    install(TARGETS ${TEST_NAME} DESTINATION libraries/tests/signal-lib/elf)
    add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
endmacro()

include_directories(include/)
file(GLOB TEST_SOURCES_1D test_fft_1d/*.c)
file(GLOB TEST_SOURCES_2D test_fft_2d/*.c)
file(GLOB LIBTEST_SOURCES common/*.c)

add_library(elcore50-signal-lib-test ${LIBTEST_SOURCES})
target_compile_options(elcore50-signal-lib-test PRIVATE -Wno-unused-command-line-argument)

# linker script related shenanigans
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wno-unused-command-line-argument")
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -g -Wa,-g")

set(FORMATS "FLOAT" "FRACTIONAL" "HFLOAT")
set(CALC_MEMS "DDR" "XYRAM")
set(FFT_TYPES "FFT" "IFFT")
set(LD_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/common/elcore50.xl")
foreach(SET_FORMAT ${FORMATS})
    foreach(SET_CALC_MEMS ${CALC_MEMS})
        foreach(SET_TYPE ${FFT_TYPES})
            build_fft_test(${SET_FORMAT} ${SET_CALC_MEMS} ${SET_TYPE} "SL_1D")
            build_fft_test(${SET_FORMAT} ${SET_CALC_MEMS} ${SET_TYPE} "SL_2D")
        endforeach()
    endforeach()
endforeach()

# build conv tests
set(DIM_LIST "1d" "2d")
foreach(DIM ${DIM_LIST})
    file(GLOB CONV_SOURCES_${DIM} test_conv/*_${DIM}.c)
    set(TEST_CONV_NAME "test_conv_${DIM}.elf")
    add_executable(${TEST_CONV_NAME} ${CONV_SOURCES_${DIM}})
    set_target_properties(${TEST_CONV_NAME} PROPERTIES LINK_FLAGS
        "-T${LD_SCRIPT} -nostartfiles")
    target_compile_options(${TEST_CONV_NAME} PRIVATE -Wno-unused-command-line-argument)
    if(${ELCORE50_CL_BUILD})
        target_link_libraries(${TEST_CONV_NAME} PRIVATE tests-argparser
            elcore50-signal-lib elcore50-signal-lib-test elcore-runtime)
    else()
        target_link_libraries(${TEST_CONV_NAME} PRIVATE tests-argparser
            elcore50-signal-lib elcore50-signal-lib-test)
    endif()
    install(TARGETS ${TEST_CONV_NAME} DESTINATION libraries/tests/signal-lib/elf)
    add_test(NAME ${TEST_CONV_NAME} COMMAND ${TEST_CONV_NAME})
endforeach()

# build fir filters tests
file(GLOB FILTERS_SOURCES test_filters/*.c)
set(TEST_FILTERS_NAME "test_fir_filter.elf")
add_executable(${TEST_FILTERS_NAME} ${FILTERS_SOURCES})
set_target_properties(${TEST_FILTERS_NAME} PROPERTIES LINK_FLAGS
    "-T${LD_SCRIPT} -nostartfiles")
target_compile_options(${TEST_FILTERS_NAME} PRIVATE -Wno-unused-command-line-argument)
if(${ELCORE50_CL_BUILD})
    target_link_libraries(${TEST_FILTERS_NAME} PRIVATE tests-argparser
        elcore50-signal-lib elcore50-signal-lib-test elcore-runtime)
else()
    target_link_libraries(${TEST_FILTERS_NAME} PRIVATE tests-argparser
        elcore50-signal-lib elcore50-signal-lib-test)
endif()
install(TARGETS ${TEST_FILTERS_NAME} DESTINATION libraries/tests/signal-lib/elf)
add_test(NAME ${TEST_FILTERS_NAME} COMMAND ${TEST_FILTERS_NAME})

enable_testing()
include(CTest)
