# Test binaries are registered here as they are added in later phases.
# Each test is a standalone executable using tests/prc_test.h (see that
# header for the assertion macros), registered as a CTest test.
#
# Pattern for adding a new test:
#
#   add_executable(test_NAME tests/unit/test_NAME.c)
#   target_link_libraries(test_NAME prc_static)
#   target_include_directories(test_NAME PRIVATE tests)
#   add_test(NAME test_NAME COMMAND test_NAME)

add_executable(test_bitwrite tests/unit/test_bitwrite.c)
target_link_libraries(test_bitwrite prc_static)
target_include_directories(test_bitwrite PRIVATE tests)
add_test(NAME test_bitwrite COMMAND test_bitwrite)

add_executable(test_current_name tests/unit/test_current_name.c)
target_link_libraries(test_current_name prc_static)
target_include_directories(test_current_name PRIVATE tests)
add_test(NAME test_current_name COMMAND test_current_name)

add_executable(test_section_residue tests/unit/test_section_residue.c)
target_link_libraries(test_section_residue prc_static)
target_include_directories(test_section_residue PRIVATE tests)
add_test(NAME test_section_residue COMMAND test_section_residue)

add_executable(test_compress_tess tests/unit/test_compress_tess.c)
target_link_libraries(test_compress_tess prc_static)
target_include_directories(test_compress_tess PRIVATE tests)
add_test(NAME test_compress_tess COMMAND test_compress_tess)

add_executable(test_traversal tests/unit/test_traversal.c)
target_link_libraries(test_traversal prc_static)
target_include_directories(test_traversal PRIVATE tests)
add_test(NAME test_traversal COMMAND test_traversal)

add_executable(test_normals tests/unit/test_normals.c)
target_link_libraries(test_normals prc_static)
target_include_directories(test_normals PRIVATE tests)
add_test(NAME test_normals COMMAND test_normals)

add_executable(test_tess_3d tests/unit/test_tess_3d.c)
target_link_libraries(test_tess_3d prc_static)
target_include_directories(test_tess_3d PRIVATE tests)
add_test(NAME test_tess_3d COMMAND test_tess_3d)

add_executable(test_write_materials tests/unit/test_write_materials.c)
target_link_libraries(test_write_materials prc_static)
target_include_directories(test_write_materials PRIVATE tests)
add_test(NAME test_write_materials COMMAND test_write_materials)

add_executable(test_compressed_texture_write tests/unit/test_compressed_texture_write.c)
target_link_libraries(test_compressed_texture_write prc_static)
target_include_directories(test_compressed_texture_write PRIVATE tests)
add_test(NAME test_compressed_texture_write COMMAND test_compressed_texture_write)

add_executable(test_compressed_point_colours tests/unit/test_compressed_point_colours.c)
target_link_libraries(test_compressed_point_colours prc_static)
target_include_directories(test_compressed_point_colours PRIVATE tests)
add_test(NAME test_compressed_point_colours COMMAND test_compressed_point_colours)
add_executable(test_triangles_texture_read tests/unit/test_triangles_texture_read.c)
target_link_libraries(test_triangles_texture_read prc_static)
target_include_directories(test_triangles_texture_read PRIVATE tests)
add_test(NAME test_triangles_texture_read COMMAND test_triangles_texture_read)
add_executable(test_compressed_color_write tests/unit/test_compressed_color_write.c)
target_link_libraries(test_compressed_color_write prc_static)
target_include_directories(test_compressed_color_write PRIVATE tests)
add_test(NAME test_compressed_color_write COMMAND test_compressed_color_write)

add_executable(test_tess_primitives tests/unit/test_tess_primitives.c)
target_link_libraries(test_tess_primitives prc_static)
target_include_directories(test_tess_primitives PRIVATE tests)
add_test(NAME test_tess_primitives COMMAND test_tess_primitives)

add_executable(test_vertex_colors tests/unit/test_vertex_colors.c)
target_link_libraries(test_vertex_colors prc_static)
target_include_directories(test_vertex_colors PRIVATE tests)
add_test(NAME test_vertex_colors COMMAND test_vertex_colors)

add_executable(test_wire_tess tests/unit/test_wire_tess.c)
target_link_libraries(test_wire_tess prc_static)
target_include_directories(test_wire_tess PRIVATE tests)
add_test(NAME test_wire_tess COMMAND test_wire_tess)

add_executable(test_globals tests/unit/test_globals.c)
target_link_libraries(test_globals prc_static)
target_include_directories(test_globals PRIVATE tests)
add_test(NAME test_globals COMMAND test_globals)

add_executable(test_file_structure tests/unit/test_file_structure.c)
target_link_libraries(test_file_structure prc_static)
target_include_directories(test_file_structure PRIVATE tests)
add_test(NAME test_file_structure COMMAND test_file_structure)

add_executable(test_write_tree tests/unit/test_write_tree.c)
target_link_libraries(test_write_tree prc_static)
target_include_directories(test_write_tree PRIVATE tests)
add_test(NAME test_write_tree COMMAND test_write_tree)
add_executable(test_compressed_winding_write tests/unit/test_compressed_winding_write.c)
target_link_libraries(test_compressed_winding_write prc_static)
target_include_directories(test_compressed_winding_write PRIVATE tests)
add_test(NAME test_compressed_winding_write COMMAND test_compressed_winding_write)

add_executable(test_compressed_curve_release tests/unit/test_compressed_curve_release.c)
target_link_libraries(test_compressed_curve_release prc_static)
target_include_directories(test_compressed_curve_release PRIVATE tests)
add_test(NAME test_compressed_curve_release COMMAND test_compressed_curve_release)

add_executable(test_pdf_file_id tests/unit/test_pdf_file_id.c)
target_link_libraries(test_pdf_file_id prc_static)
target_include_directories(test_pdf_file_id PRIVATE tests)
target_compile_definitions(test_pdf_file_id PRIVATE PRC_TEST_EXAMPLES_DIR="${PROJECT_SOURCE_DIR}/examples")
add_test(NAME test_pdf_file_id COMMAND test_pdf_file_id)

add_executable(test_tess_index_mapping tests/unit/test_tess_index_mapping.c)
target_link_libraries(test_tess_index_mapping prc_static)
target_include_directories(test_tess_index_mapping PRIVATE tests)
add_test(NAME test_tess_index_mapping COMMAND test_tess_index_mapping)
add_executable(test_ref_numbering tests/unit/test_ref_numbering.c)
target_link_libraries(test_ref_numbering prc_static)
target_include_directories(test_ref_numbering PRIVATE tests)
add_test(NAME test_ref_numbering COMMAND test_ref_numbering)
add_executable(test_write_validation tests/unit/test_write_validation.c)
target_link_libraries(test_write_validation prc_static)
target_include_directories(test_write_validation PRIVATE tests)
add_test(NAME test_write_validation COMMAND test_write_validation)

# The write pipeline's ablation switches (src/prc_write_ablate.h). The plain
# run checks that nothing is ablated without a switch; the others exist only
# where the switches do, in a PRC_ENABLE_DIAG_ENV build.
add_executable(test_write_ablation tests/unit/test_write_ablation.c)
target_link_libraries(test_write_ablation prc_static)
target_include_directories(test_write_ablation PRIVATE tests)
add_test(NAME test_write_ablation COMMAND test_write_ablation)
if(PRC_ENABLE_DIAG_ENV)
  set(PRC_ABLATION_DUMP_DIR "${CMAKE_CURRENT_BINARY_DIR}/ablation_dump")
  file(MAKE_DIRECTORY "${PRC_ABLATION_DUMP_DIR}")
  add_test(NAME test_write_ablation_normals COMMAND test_write_ablation)
  set_tests_properties(test_write_ablation_normals PROPERTIES
    ENVIRONMENT "PRC_DIAG_ABLATE_NORMALS=1")
  add_test(NAME test_write_ablation_colors_deflate COMMAND test_write_ablation)
  set_tests_properties(test_write_ablation_colors_deflate PROPERTIES
    ENVIRONMENT "PRC_DIAG_ABLATE_COLORS=1;PRC_DIAG_ABLATE_DEFLATE=1;PRC_DIAG_ABLATE_DUMP_DIR=${PRC_ABLATION_DUMP_DIR}")
  add_test(NAME test_write_ablation_tiny_huffman COMMAND test_write_ablation)
  set_tests_properties(test_write_ablation_tiny_huffman PROPERTIES
    ENVIRONMENT "PRC_DIAG_ABLATE_TINY_TEXTURES=1;PRC_DIAG_ABLATE_HUFFMAN=1")
  add_test(NAME test_write_ablation_huffman_optional COMMAND test_write_ablation)
  set_tests_properties(test_write_ablation_huffman_optional PROPERTIES
    ENVIRONMENT "PRC_DIAG_HUFFMAN_WHERE_OPTIONAL=1")
  add_test(NAME test_write_ablation_all COMMAND test_write_ablation)
  set_tests_properties(test_write_ablation_all PROPERTIES
    ENVIRONMENT "PRC_DIAG_ABLATE_NORMALS=1;PRC_DIAG_ABLATE_COLORS=1;PRC_DIAG_ABLATE_TEXTURES=1;PRC_DIAG_ABLATE_HUFFMAN=1;PRC_DIAG_ABLATE_DEFLATE=1;PRC_DIAG_ABLATE_DUMP_DIR=${PRC_ABLATION_DUMP_DIR}")
  # Each run writes and removes the same files.
  set_tests_properties(test_write_ablation test_write_ablation_normals
    test_write_ablation_colors_deflate test_write_ablation_tiny_huffman test_write_ablation_huffman_optional
    test_write_ablation_all
    PROPERTIES RESOURCE_LOCK prc_write_ablation)
endif()
add_executable(test_schema_bounds tests/unit/test_schema_bounds.c)
target_link_libraries(test_schema_bounds prc_static)
target_include_directories(test_schema_bounds PRIVATE tests)
add_test(NAME test_schema_bounds COMMAND test_schema_bounds)
add_executable(test_text_primitive_index tests/unit/test_text_primitive_index.c)
target_link_libraries(test_text_primitive_index prc_static)
target_include_directories(test_text_primitive_index PRIVATE tests)
add_test(NAME test_text_primitive_index COMMAND test_text_primitive_index)
add_executable(test_markup_decode tests/unit/test_markup_decode.c)
target_link_libraries(test_markup_decode prc_static)
target_include_directories(test_markup_decode PRIVATE tests)
add_test(NAME test_markup_decode COMMAND test_markup_decode)
add_executable(test_app_id tests/unit/test_app_id.c)
target_link_libraries(test_app_id prc_static)
target_include_directories(test_app_id PRIVATE tests)
target_compile_definitions(test_app_id PRIVATE PRC_TEST_EXAMPLES_DIR="${PROJECT_SOURCE_DIR}/examples")
add_test(NAME test_app_id COMMAND test_app_id)
add_executable(test_version tests/unit/test_version.c)
target_link_libraries(test_version prc_static)
target_include_directories(test_version PRIVATE tests)
add_test(NAME test_version COMMAND test_version)
add_executable(test_pdf_default_view tests/unit/test_pdf_default_view.c)
target_link_libraries(test_pdf_default_view prc_static)
target_include_directories(test_pdf_default_view PRIVATE tests)
add_test(NAME test_pdf_default_view COMMAND test_pdf_default_view)
