# Clifft Test Suite
# Uses Catch2 v3 for unit testing

include(CTest)
include(Catch)

add_executable(clifft_tests
    test_bitmask.cc
    test_batch_bits.cc
    test_batch_random_bits.cc
    test_batch_executor.cc
    test_shared_sampling.cc
    test_native_sampling.cc
    test_batch_external_inputs.cc
    test_batch_tuning.cc
    test_mask_view.cc
    test_numeric.cc
    test_page_allocation.cc
    test_parser.cc
    test_qasm2_parser.cc
    test_reference_clifford.cc
    test_tableau.cc
    test_hir.cc
    test_global_reconstruction.cc
    test_program.cc
    test_frontend.cc
    test_frontend_instruments.cc
    test_sampling_plan.cc
    test_inspection_format.cc
    test_sampling_planner.cc
    test_sampling_planner_frame.cc
    test_sampling_executor.cc
    test_fused_rotation.cc
    test_sampling_kernels.cc
    test_interleaved_batch_kernels.cc
    test_optimizer.cc
    test_known_stabilizers.cc
    test_phase_polynomial.cc
    test_phase_polynomial_pass.cc
    test_wide_phase_region.cc
    test_rotation_simplification_pass.cc
    test_active_width_analysis.cc
    test_source_map.cc
    test_logical_noise_prefix.cc
    test_schedule_dependence.cc
    test_active_width_schedule_pass.cc
    test_pass_equivalence.cc
    test_rare_probability_validation.cc
    test_importance_sampling.cc
    test_runtime_isa.cc
    test_shot_parallel.cc
    test_fuzz.cc
    test_noncomp_value_types.cc
    test_noncomp_transition_instrument.cc
    test_noncomp_classifier.cc
    test_noncomp_interactions.cc
    test_noncomp_model.cc
    test_noncomp_preparation.cc
    test_noncomp_program.cc
    test_noncomp_online.cc
    test_noncomp_resolution.cc
    test_noncomp_fixed.cc
    test_noncomp_classical.cc
    test_noncomp_status_walk.cc
    test_noncomp_rewriter.cc
    test_noncomp_continuation.cc
    test_trajectory_driver.cc
    test_noncomp_validation.cc
    test_hip_executable_plan.cc
    test_cuda_executable_plan.cc
)

target_link_libraries(clifft_tests PRIVATE clifft_hip_plan clifft_cuda_plan)

target_link_libraries(clifft_tests PRIVATE
    clifft_core
    Catch2::Catch2WithMain
)

# Only this executable compiles the replay seams. Linking its instrumented
# translation units first keeps the archive's ordinary versions out of this
# binary without changing clifft_core, Python packages, or sampling APIs.
# Include every source translation unit that instantiates choose_branch or
# OnlineState::measure here, so the linker cannot silently select an ordinary
# inline definition from the archive and bypass replay.
add_executable(clifft_noncomp_replay_tests
    test_noncomp_replay.cc
    ../src/clifft/sampling/executor.cc
    ../src/clifft/noncomp/trajectory_driver.cc
    ../src/clifft/noncomp/online.cc
)
target_compile_definitions(clifft_noncomp_replay_tests PRIVATE CLIFFT_NONCOMP_REPLAY_TESTING)
target_link_libraries(clifft_noncomp_replay_tests PRIVATE clifft_core Catch2::Catch2WithMain)
if(CLIFFT_OPENMP_ENABLED)
    target_compile_definitions(clifft_noncomp_replay_tests PRIVATE CLIFFT_USE_OPENMP)
    target_link_libraries(clifft_noncomp_replay_tests PRIVATE OpenMP::OpenMP_CXX)
endif()
if(CLIFFT_X86_RUNTIME_DISPATCH)
    target_compile_definitions(clifft_noncomp_replay_tests PRIVATE CLIFFT_ENABLE_X86_RUNTIME_DISPATCH)
endif()
if(CLIFFT_APPLE_NEON_DISPATCH)
    target_compile_definitions(clifft_noncomp_replay_tests PRIVATE CLIFFT_ENABLE_APPLE_NEON)
endif()
if(CLIFFT_COVERAGE)
    target_compile_options(clifft_noncomp_replay_tests PRIVATE --coverage)
    target_link_options(clifft_noncomp_replay_tests PRIVATE --coverage)
endif()
catch_discover_tests(clifft_noncomp_replay_tests)
# Release CI builds this named target before running all registered CTest cases.
add_dependencies(clifft_tests clifft_noncomp_replay_tests)

target_compile_definitions(clifft_tests PRIVATE
    CLIFFT_FIXTURES_DIR="${CMAKE_CURRENT_SOURCE_DIR}/fixtures"
    CLIFFT_DOCS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/../docs"
)

# Tests that call the x86 kernel entry points directly can only link on
# platforms whose core library compiles the runtime-dispatch kernels.
if(CLIFFT_X86_RUNTIME_DISPATCH)
    target_compile_definitions(clifft_tests PRIVATE CLIFFT_TESTS_HAVE_X86_KERNELS)
endif()

if(CLIFFT_APPLE_NEON_DISPATCH)
    target_compile_definitions(clifft_tests PRIVATE CLIFFT_TESTS_HAVE_APPLE_NEON)
endif()

if(CLIFFT_OPENMP_ENABLED)
    target_link_libraries(clifft_tests PRIVATE OpenMP::OpenMP_CXX)
    target_compile_definitions(clifft_tests PRIVATE
        CLIFFT_TESTS_HAVE_OPENMP
        CLIFFT_USE_OPENMP
    )
endif()

# Code coverage instrumentation for test executable
if(CLIFFT_COVERAGE)
    target_compile_options(clifft_tests PRIVATE --coverage)
    target_link_options(clifft_tests PRIVATE --coverage)
endif()

# The distance-nine structural regression is covered by optimized builds;
# repeating its unoptimized search across Debug ISA jobs dominates test time.
catch_discover_tests(clifft_tests TEST_SPEC "~[expensive]$<$<CONFIG:Debug>:~[large-schedule]>")
# A separate label lets CI Debug jobs omit costly oracle sweeps while local
# runs, optimized jobs, and sanitizers retain their full sample counts.
catch_discover_tests(clifft_tests
    TEST_SPEC "[expensive]$<$<CONFIG:Debug>:~[large-schedule]>"
    PROPERTIES LABELS expensive)

if(CLIFFT_ENABLE_HIP)
    # Keep hardware-dependent skips scoped to the HIP executable so exit code
    # 4 cannot hide a failure in the ordinary CPU suite.
    add_executable(clifft_hip_tests test_hip_sampler.cc)
    target_link_libraries(clifft_hip_tests PRIVATE
        clifft_core
        clifft_hip_backend
        Catch2::Catch2WithMain
    )
    catch_discover_tests(clifft_hip_tests PROPERTIES SKIP_RETURN_CODE 4)
endif()

if(CLIFFT_ENABLE_CUDA)
    # Same isolation as the HIP suite: device skips exit with code 4 from a
    # dedicated executable so they cannot mask a CPU failure.
    add_executable(clifft_cuda_tests test_cuda_sampler.cc)
    target_link_libraries(clifft_cuda_tests PRIVATE
        clifft_core
        clifft_cuda_backend
        Catch2::Catch2WithMain
    )
    catch_discover_tests(clifft_cuda_tests PROPERTIES SKIP_RETURN_CODE 4)
endif()

if(CLIFFT_OPENMP_ENABLED)
    # These cases need an explicit environment because processor binding and a
    # runtime-limited team are otherwise host-dependent and easy to miss in CI.
    add_test(NAME clifft_openmp_bound_layout_validation
        COMMAND clifft_tests "Sampling thread layouts validate explicit worker counts")
    set_tests_properties(clifft_openmp_bound_layout_validation PROPERTIES
        ENVIRONMENT "OMP_PROC_BIND=spread;OMP_PLACES=cores")

    add_test(NAME clifft_openmp_limited_fixed_rows
        COMMAND clifft_tests "Threaded fixed-row sampling preserves seeded shot order")
    set_tests_properties(clifft_openmp_limited_fixed_rows PROPERTIES
        ENVIRONMENT "OMP_PROC_BIND=spread;OMP_PLACES=cores;OMP_THREAD_LIMIT=1")

    add_test(NAME clifft_openmp_bound_trajectory
        COMMAND clifft_tests "trajectory: worker counts preserve seeded rows and sidecars")
    set_tests_properties(clifft_openmp_bound_trajectory PROPERTIES
        ENVIRONMENT "OMP_PROC_BIND=spread;OMP_PLACES=cores")
endif()
