set(SRC ${CMAKE_CURRENT_SOURCE_DIR}/../src)
set(UT unittest.c unittest.h util.c util.h ${SRC}/test_counters.c)

function(def_test name)
    add_executable(${name} ${ARGV} ${UT})
    set_property(TARGET ${name} PROPERTY C_STANDARD 11)
    target_include_directories(${name} PRIVATE ${SRC})
    target_compile_definitions(${name} PRIVATE -DTESTING)
    if (UT_SAN)
        target_compile_options(${name} BEFORE
            PRIVATE -fsanitize=address
            PRIVATE -fsanitize=undefined
        )
        target_link_libraries(${name}
            -fsanitize=address
            -fsanitize=undefined
        )
    endif()
    add_test(NAME ${name} COMMAND ${name})
endfunction()

# Auxiliary binary which is executed via fork-exec by some tests.
add_executable(aux_test_launcher aux_test_launcher.c)
set_property(TARGET aux_test_launcher PROPERTY C_STANDARD 11)

def_test(test_launcher
    test_launcher.c
    ${SRC}/launcher.c
)

def_test(test_outfmt
    test_outfmt.c
    ${SRC}/launcher.c
    ${SRC}/outfmt.c
    ${SRC}/outfmt_parser.c
    ${SRC}/radix.c
    ${SRC}/text.c
)

def_test(test_outfmt_parser
    test_outfmt_parser.c
    ${SRC}/outfmt_parser.c
)

def_test(test_persist
    test_persist.c
    ${SRC}/persist_dir.c
    ${SRC}/persist_file.c
    ${SRC}/itemset.c
    ${SRC}/filemap.c
    ${SRC}/logging.c
)
target_include_directories(test_persist
    PRIVATE ${UTHASH_H_INCLUDE}
)

def_test(test_radix
    test_radix.c
    ${SRC}/radix.c
)

def_test(test_rc
    test_rc.c
    ${SRC}/rc.c
    ${SRC}/filemap.c
)

def_test(test_text
    test_text.c
    ${SRC}/text.c
)

def_test(test_itemset
    test_itemset.c
    ${SRC}/itemset.c
)
target_include_directories(test_itemset
    PRIVATE ${UTHASH_H_INCLUDE}
)

def_test(test_digest
    test_digest.c
    ${SRC}/digest.c
)
target_link_libraries(test_digest
    OpenSSL::Crypto
)
