cmake_minimum_required(VERSION 3.23)

enable_testing()

# Aggregate target + label for host-only tests (no Docker, no retail data;
# run on Linux/macOS/Windows in CI). Each test registers itself via
# register_host_test(<target>) — adding a new host test is then two lines in
# its own CMakeLists.txt, with no workflow edits required.
#
#   cmake --build build --target host_tests   # builds every registered test
#   ctest -L host_quick --output-on-failure   # runs every registered test
add_custom_target(host_tests)

function(register_host_test test_name)
    add_dependencies(host_tests ${test_name})
    set_tests_properties(${test_name} PROPERTIES LABELS host_quick)
endfunction()

# Where a test reads repo files and writes scratch files while it runs. On a
# host that is the checkout and the test's own build folder. An Android build
# runs its tests on a device through scripts/dev/android-exec.sh, which empties
# <device dir>/run/<test> before each run and first copies the repo files named
# with lba2_test_device_data() under <device dir>/repo. Both are absolute: SDL
# on Android resolves a relative path through the Java Activity, which a test
# run as a plain executable does not have.
if(ANDROID)
    set(LBA2_TEST_DEVICE_DIR "/data/local/tmp/lba2cc")
    set(LBA2_TEST_DATA_ROOT "${LBA2_TEST_DEVICE_DIR}/repo")
else()
    set(LBA2_TEST_DATA_ROOT "${CMAKE_SOURCE_DIR}")
endif()

function(lba2_test_tmp_dir out_var test_name)
    if(ANDROID)
        set(${out_var} "${LBA2_TEST_DEVICE_DIR}/run/${test_name}" PARENT_SCOPE)
    else()
        set(${out_var} "${CMAKE_CURRENT_BINARY_DIR}" PARENT_SCOPE)
    endif()
endfunction()

# Tests that need the Java Activity, which a test run as a plain executable on
# a device does not have: creating an SDL renderer waits for the Activity
# forever, and SDL_GetPrefPath asks it for the app's storage folder and
# crashes. ctest lists them as not run.
function(lba2_test_needs_android_activity)
    if(ANDROID)
        set_tests_properties(${ARGN} PROPERTIES DISABLED TRUE)
    endif()
endfunction()

# Repo-relative paths a test opens at run time.
function(lba2_test_device_data)
    set_property(GLOBAL APPEND PROPERTY LBA2_TEST_DEVICE_DATA ${ARGN})
endfunction()

add_subdirectory(discovery)
add_subdirectory(picker)
add_subdirectory(console)
add_subdirectory(control_proto)
add_subdirectory(logging)
add_subdirectory(asset_preflight)
add_subdirectory(credits)
add_subdirectory(copymask_negx)
add_subdirectory(version)
add_subdirectory(savegame)
add_subdirectory(save_wire)
add_subdirectory(deffile)
add_subdirectory(distrib)
add_subdirectory(atomic_write)
add_subdirectory(hqfile)
add_subdirectory(random)
add_subdirectory(qsort)
add_subdirectory(demo_rng_seed)
add_subdirectory(image_load)
add_subdirectory(save_thumbnail)
add_subdirectory(ambiance_balance)
add_subdirectory(music)
add_subdirectory(focus_audio)
add_subdirectory(timer)
add_subdirectory(staging_buffer)
add_subdirectory(texfilter)
add_subdirectory(inv64)
add_subdirectory(lproj_round)
add_subdirectory(x87round)
add_subdirectory(camera_paths)
add_subdirectory(input_device)
add_subdirectory(mouse_capture)
add_subdirectory(input_funnel)
add_subdirectory(input_flow)
add_subdirectory(input_bindings)
add_subdirectory(getascii)
add_subdirectory(record_format)
if(PLATFORM_BACKEND STREQUAL "sdl3")
    add_subdirectory(present_rect)
endif()
add_subdirectory(cli_args)
add_subdirectory(video_fit)
add_subdirectory(video_source)
add_subdirectory(menu_keynav)
add_subdirectory(menu_labels)
add_subdirectory(res_menu)
add_subdirectory(res_catalog)
add_subdirectory(string_encoding)
add_subdirectory(voc_header)
add_subdirectory(iso9660)
add_subdirectory(disc_image)
add_subdirectory(cue)
add_subdirectory(cdtracks)
add_subdirectory(cdda)
add_subdirectory(render)
add_subdirectory(movement)
add_subdirectory(zone)
add_subdirectory(plasma_steps)
add_subdirectory(boot_end)
add_subdirectory(crash)
add_subdirectory(camera)
add_subdirectory(precision_paths)
add_subdirectory(numeric_contract)
add_subdirectory(muldiv64)
add_subdirectory(le_access)
add_subdirectory(ui_layout)
add_subdirectory(settings)
add_subdirectory(user_dirs)
add_subdirectory(platform)

if(LBA2_BUILD_ASM_EQUIV_TESTS)
include(cmake/asm_test_helpers.cmake)

add_asm_cpp_test(
	NAME test_grille
	TEST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/test_grille.cpp
	ASM_SOURCE ${CMAKE_SOURCE_DIR}/SOURCES/GRILLE_A.ASM
	RENAME_SYMS DecompColonne Map2Screen GetAdrBlock WorldColBrickFull WorldCodeBrick GetBlockBrick GetWorldColBrickVisible WorldColBrick CodeJeu XMap YMap ZMap XScreen YScreen BufCube BufMap TabBlock BufferBrick
	INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/SOURCES
	LIBS 3dext 3D ail ani fio obj poly svg sys lba2_platform
)
target_sources(test_grille PRIVATE ${CMAKE_SOURCE_DIR}/SOURCES/GRILLE.CPP)

add_asm_cpp_test(
	NAME test_plasma
	TEST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/test_plasma.cpp
	ASM_SOURCE ${CMAKE_SOURCE_DIR}/SOURCES/PLASMA.ASM
	RENAME_SYMS Do_Plasma Nb_Pts_Inter Nb_Pts_Control
	INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/SOURCES
)
target_sources(test_plasma PRIVATE ${CMAKE_SOURCE_DIR}/SOURCES/PLASMA.CPP)

add_asm_cpp_test(
	NAME test_func
	TEST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/test_func.cpp
	ASM_SOURCE ${CMAKE_SOURCE_DIR}/SOURCES/FUNC.ASM
	RENAME_SYMS SearchBoundColRGB
	INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/SOURCES
)
target_sources(test_func PRIVATE ${CMAKE_SOURCE_DIR}/SOURCES/FUNC.CPP)

add_asm_cpp_test(
	NAME test_flow_a
	TEST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/test_flow_a.cpp
	ASM_SOURCE ${CMAKE_SOURCE_DIR}/SOURCES/FLOW_A.ASM
	RENAME_SYMS BoxFlow ShadeBoxBlk CopyBlockShade
	INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/SOURCES
)
target_sources(test_flow_a PRIVATE ${CMAKE_SOURCE_DIR}/SOURCES/FLOW_A.CPP)

add_asm_cpp_test(
	NAME test_fire
	TEST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/test_fire.cpp
	ASM_SOURCE ${CMAKE_SOURCE_DIR}/SOURCES/FIRE.ASM
	RENAME_SYMS Do_Fire
	INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/SOURCES
)
target_sources(test_fire PRIVATE ${CMAKE_SOURCE_DIR}/SOURCES/FIRE.CPP)
target_sources(test_fire PRIVATE ${CMAKE_SOURCE_DIR}/LIB386/SYSTEM/UTILS.CPP)

add_asm_cpp_test(
	NAME test_compress
	TEST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/test_compress.cpp
	ASM_SOURCE ${CMAKE_SOURCE_DIR}/SOURCES/COMPRESS.ASM
	RENAME_SYMS AddString DeleteString
	INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/SOURCES
)
target_sources(test_compress PRIVATE ${CMAKE_SOURCE_DIR}/SOURCES/LZSS.CPP)

add_subdirectory(3D)
add_subdirectory(3DEXT)
add_subdirectory(SYSTEM)
add_subdirectory(MENU)
add_subdirectory(ANIM)
add_subdirectory(SVGA)
add_subdirectory(pol_work)
add_subdirectory(OBJECT)
add_subdirectory(SNAPSHOT)
add_subdirectory(fpu_precision)
add_subdirectory(VIDEO_AUDIO)

endif()

# Read by android-exec.sh: the device dir on the first line, then the
# repo-relative paths to copy under <device dir>/repo.
if(ANDROID)
    get_property(_device_data GLOBAL PROPERTY LBA2_TEST_DEVICE_DATA)
    list(REMOVE_DUPLICATES _device_data)
    list(JOIN _device_data "\n" _device_data)
    file(WRITE "${CMAKE_BINARY_DIR}/android-test-data.txt" "${LBA2_TEST_DEVICE_DIR}\n${_device_data}\n")
endif()
