cmake_minimum_required(VERSION 3.16)
project(tests LANGUAGES C)

enable_testing()

# The headers are documented as C23-first but expected to work on older standards.
# Pinned here so that the claim is actually exercised; configure with
# -DDLIBC_C_STANDARD=23 to build them the way the README describes.
set(DLIBC_C_STANDARD 11 CACHE STRING "C standard the tests are compiled against")

# The library's stated goal is memory safety, so the take, aliasing and destructor
# tests are worth a lot more when the allocator is watching. Off by default, as it
# slows the tests down and needs a compiler that ships the sanitizer runtimes.
option(DLIBC_SANITIZE "Build the tests with AddressSanitizer and UndefinedBehaviorSanitizer" OFF)

add_executable(test_vector
    tests/test_vector.c
    tests/vector_altcap.c
)

add_executable(test_set
    tests/test_set.c
    tests/set_altcap.c
)

foreach(dlibc_target test_vector test_set)
    set_target_properties(${dlibc_target} PROPERTIES
        C_STANDARD ${DLIBC_C_STANDARD}
        C_STANDARD_REQUIRED ON
        C_EXTENSIONS OFF
    )

    if(NOT MSVC)
        target_compile_options(${dlibc_target} PRIVATE -Wall -Wextra -Wpedantic -Werror)

        if(DLIBC_SANITIZE)
            target_compile_options(${dlibc_target} PRIVATE -g -fno-omit-frame-pointer -fsanitize=address,undefined)
            target_link_options(${dlibc_target} PRIVATE -fsanitize=address,undefined)
        endif()
    endif()
endforeach()

add_test(NAME test_vector COMMAND test_vector)
add_test(NAME test_set COMMAND test_set)
