cmake_minimum_required(VERSION 3.16)
project(dsp1_contract_tests C)
enable_testing()
include(${CMAKE_CURRENT_LIST_DIR}/../../runner/dsp1_backend.cmake)
get_filename_component(_runner ${CMAKE_CURRENT_LIST_DIR}/../../runner/src ABSOLUTE)

function(dsp1_test_target name)
    add_executable(${name} ${name}.c ${ARGN})
    target_include_directories(${name} PRIVATE ${_runner} ${_runner}/snes)
    target_compile_features(${name} PRIVATE c_std_11)
    if(MSVC)
        target_compile_options(${name} PRIVATE /W4 /WX)
    else()
        target_compile_options(${name} PRIVATE -Wall -Wextra -Werror)
        target_link_libraries(${name} PRIVATE m)
    endif()
endfunction()

set(_device ${_runner}/snes/dsp1.c ${_runner}/snes/dsp1_hle.c)
dsp1_test_target(snapshot_identity_test)
add_test(NAME snapshot_identity COMMAND snapshot_identity_test)
dsp1_test_target(dsp1_selection_test ${_device})
foreach(_fixture present missing)
    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${_fixture})
    add_test(NAME selection_${_fixture} COMMAND dsp1_selection_test ${_fixture})
    set_tests_properties(selection_${_fixture} PROPERTIES
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${_fixture}
        ENVIRONMENT "SNESRECOMP_DSP1_ROM=")
    if(_fixture STREQUAL "present")
        set_tests_properties(selection_${_fixture} PROPERTIES ENVIRONMENT
            "SNESRECOMP_DSP1_ROM=${CMAKE_CURRENT_BINARY_DIR}/present/dsp1b.rom")
    endif()
endforeach()
dsp1_test_target(dsp1_hle_test ${_runner}/snes/dsp1_hle.c)
add_test(NAME commands COMMAND dsp1_hle_test)
if(SNESRECOMP_DSP1_IMPL STREQUAL "HLE")
    dsp1_test_target(dsp1_hle_host_test ${_device})
    add_test(NAME host_protocol COMMAND dsp1_hle_host_test)
else()
    dsp1_test_target(dsp1_test ${_device})
    add_test(NAME instruction_core COMMAND dsp1_test)
    dsp1_test_target(dsp1_firmware_test ${_device})
    add_test(NAME firmware_differential COMMAND dsp1_firmware_test)
    set_tests_properties(firmware_differential PROPERTIES
        SKIP_REGULAR_EXPRESSION "SKIP")
endif()
