# port/test/CMakeLists.txt: cross-cutting tests of the game's sources.
#
# layout_asserts: the struct layouts of the
# game's headers as _Static_asserts, generated by tools/gen_layout_asserts.py
# from the headers' offset comments and config/struct_classes.txt. It is
# compiled as the game compiles (include path, definitions, record layout
# options), so a layout that moves fails the build. Only the frozen structs'
# asserts apply on the host (disc and ELF overlays, save records); the
# runtime structs' EE-layout asserts compile with -DICO_LAYOUT_EE=1 only.

ico_include_dirs(common _ico_layout_incs)
add_executable(layout_asserts layout_asserts.c)
target_include_directories(layout_asserts PRIVATE ${_ico_layout_incs})
target_compile_definitions(layout_asserts PRIVATE ${ICO_GAME_DEFINITIONS})
target_compile_options(layout_asserts PRIVATE ${ICO_SEMANTIC_OPTIONS} ${ICO_GAME_LAYOUT_OPTIONS}
                       ${ICO_GAME_WARNINGS})

if(ICO_RUN_TESTS)
    # the asserts hold at compile time; running the program only says so
    add_test(NAME layout_asserts COMMAND layout_asserts)
endif()

# The committed layout_asserts.c must match the headers and the
# classification: rerun tools/gen_layout_asserts.py after changing either.
if(Python3_FOUND)
    add_test(NAME layout_asserts_fresh
             COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/gen_layout_asserts.py" --check)
endif()

# font_audit: every code point of the port's
# string literals and the font corpus lies in the subset's unicode ranges
if(Python3_FOUND)
    add_test(NAME font_audit COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/font_audit.py")
    # strings_sync: every string id has a line in every community translation table
    # (tools/strings_sync.py --check): a new id appended to the enum without its
    # `[UI_STR_X] = NULL` line in the end block of each table fails here, and so does a
    # block's quoted English that differs from strings_en.c (--refresh-comments renews it);
    # strings_sync_selftest checks the tool's parse rules on fixed inputs
    add_test(NAME strings_sync COMMAND "${Python3_EXECUTABLE}" -I "${CMAKE_SOURCE_DIR}/tools/strings_sync.py" --check)
    add_test(NAME strings_sync_selftest COMMAND "${Python3_EXECUTABLE}" -I "${CMAKE_SOURCE_DIR}/tools/strings_sync.py" --self-test)
endif()

# font_subset_arimo: the port's main font (tools/subset_font_arimo.py --check):
# the recorded subset line is the tool's ranges and the community
# translations' characters; with fontTools the committed font maps exactly
# those; with fontTools 4.63.0 and the source font (tools/fetch_deps.sh) it is
# byte for byte the tool's output (a check it cannot run is named as skipped)
if(Python3_FOUND)
    add_test(NAME font_subset_arimo
             COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/subset_font_arimo.py" --check)
endif()

# font_subset_ja: the Japanese text's font (tools/subset_font_ja.py --check):
# the recorded subset line is the code points the tables and the corpus use
# outside Arimo's; with fontTools the committed font maps exactly those; with
# fontTools 4.63.0 and the source font (tools/fetch_deps.sh) it is byte for
# byte the tool's output (a check it cannot run is named as skipped)
if(Python3_FOUND)
    add_test(NAME font_subset_ja
             COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/subset_font_ja.py" --check)
endif()

# font_subset_ko: the Korean text's font (tools/subset_font_ko.py --check), the
# same three checks for Noto Sans KR's subset: the Hangul of the tables and
# the corpus, and the Korean table's other characters outside Arimo's
if(Python3_FOUND)
    add_test(NAME font_subset_ko
             COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/subset_font_ko.py" --check)
endif()

# font_subset_zh: the Chinese text's font (tools/subset_font_zh.py --check), the
# same three checks for Noto Sans TC's subset: the Chinese table's and corpus'
# characters outside Arimo's subset (but Hangul)
if(Python3_FOUND)
    add_test(NAME font_subset_zh
             COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/subset_font_zh.py" --check)
endif()

# content_table_chains: the Natural chains table
# (port/render/rd_content_chain.c) against the disc's stage animations, and
# every stage animation scanned for another chain let down by scaling
# (tools/content_table_check.py chains); skipped (77) without the disc image
if(Python3_FOUND)
    add_test(NAME content_table_chains
             COMMAND "${Python3_EXECUTABLE}" -I "${CMAKE_SOURCE_DIR}/tools/content_table_check.py"
                     --disc "${ICO_DISC_IMAGE}" chains)
    set_tests_properties(content_table_chains PROPERTIES SKIP_RETURN_CODE 77)
    # the same against the Japanese disc (its boot ELF, its tables'
    # port/data/gen/jpn/table_desc.c, region_jpn.c's groups)
    add_test(NAME content_table_chains_jpn
             COMMAND "${Python3_EXECUTABLE}" -I "${CMAKE_SOURCE_DIR}/tools/content_table_check.py"
                     --version jpn --disc "${ICO_DISC_IMAGE_JPN}" chains)
    set_tests_properties(content_table_chains_jpn PROPERTIES SKIP_RETURN_CODE 77)
    # and against the Korean disc (its tables' port/data/gen/kor/table_desc.c)
    add_test(NAME content_table_chains_kor
             COMMAND "${Python3_EXECUTABLE}" -I "${CMAKE_SOURCE_DIR}/tools/content_table_check.py"
                     --version kor --disc "${ICO_DISC_IMAGE_KOR}" chains)
    set_tests_properties(content_table_chains_kor PROPERTIES SKIP_RETURN_CODE 77)
endif()

# unused_content_audit_<disc>: docs/port/UNUSED_CONTENT_AUDIT.md's section for the disc against the
# disc's image (tools/unused_content_audit.py --check): the DATA.DF entries no table names, the
# stage rows no exit leads to, the models no row names, the loose items; skipped (77) without the
# image
if(Python3_FOUND)
    foreach(uca_slug IN ITEMS pal jpn kor ntsc aug6 jun26 cn)
        if(uca_slug STREQUAL "pal")
            set(uca_image "${ICO_DISC_IMAGE}")
        else()
            string(TOUPPER "${uca_slug}" uca_upper)
            set(uca_image "${ICO_DISC_IMAGE_${uca_upper}}")
        endif()
        add_test(NAME unused_content_audit_${uca_slug}
                 COMMAND "${Python3_EXECUTABLE}" -I "${CMAKE_SOURCE_DIR}/tools/unused_content_audit.py"
                         --slug ${uca_slug} --disc "${uca_image}" --check)
        set_tests_properties(unused_content_audit_${uca_slug} PROPERTIES SKIP_RETURN_CODE 77)
    endforeach()
endif()

# offset_audit: every raw EE-offset access the host
# compiles (ICO_RAW/ICO_RAWP, (char *)x + 0xNN, ((int *)x)[N], variable
# strides, scalar and record views of a record, pad<HEX> members, static
# initialisers) checked against the named field at that EE offset, through
# host offsetof probes; constant-size clears and copies against the host
# span; pointers held in 32 bits; calls through unprototyped declarations.
# It preprocesses and probes the game units of this build's
# compile_commands.json with the build's own compiler, so it runs on native
# builds only.
if(Python3_FOUND AND CMAKE_EXPORT_COMPILE_COMMANDS AND NOT CMAKE_CROSSCOMPILING)
    add_test(NAME offset_audit
             COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/offset_audit.py"
                     --build "${CMAKE_BINARY_DIR}")
    set_tests_properties(offset_audit PROPERTIES TIMEOUT 900)
    if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
        # the unprototyped-call pass reads gcc's gnu23 diagnostics: on gcc it
        # must run
        set_tests_properties(offset_audit PROPERTIES
            FAIL_REGULAR_EXPRESSION "unprototyped-call pass did not run")
    endif()
    # template_audit: every
    # record copied whole over storage of another record type (a template
    # moved through a view, memcpy between two record types) must have its
    # layout registered with static assertions in its unit; unregistered
    # copies and host layout differences fail. Records passed through a
    # `void *` (the mail data, `void *` members) are paired writer to reader
    # and checked the same way.
    add_test(NAME template_audit
             COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/template_audit.py"
                     --build "${CMAKE_BINARY_DIR}")
    set_tests_properties(template_audit PROPERTIES TIMEOUT 900)
endif()

# const_write_audit: no game unit writes an object
# it declares const. The PS2 build keeps tables the game writes in .rodata
# (no page protection on the EE); gcc keeps those stores, clang deletes them
# (on Android motionLimitDef's rows would stay unsorted and the held hands'
# arms go straight up). It compiles the game units of this build's
# compile_commands.json to unoptimised LLVM IR with the build's own
# compiler, so it runs on native clang builds (CI's linux-x64-clang;
# tools/check_android_flags.sh runs it for the Android build).
if(Python3_FOUND AND CMAKE_EXPORT_COMPILE_COMMANDS AND NOT CMAKE_CROSSCOMPILING
   AND CMAKE_C_COMPILER_ID MATCHES "Clang")
    add_test(NAME const_write_audit
             COMMAND "${Python3_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/check_const_writes.py"
                     --build "${CMAKE_BINARY_DIR}")
    set_tests_properties(const_write_audit PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
endif()
