# The same particles stored three ways: an array of structs, a struct of
# arrays, and one heap allocation per particle. The tests check that the
# layouts agree and count allocations; bench.cpp times them, and its
# numbers are not checked by anything.
add_library(adv_particles STATIC layouts.cpp query.cpp)
target_include_directories(adv_particles PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

lld_add_test(adv_particles_tests SOURCES tests/test_layouts.cpp)
target_link_libraries(adv_particles_tests PRIVATE adv_particles)

# The benchmark is built in every preset so that it keeps compiling; the
# smoke test only checks that a tiny run finishes.
lld_add_example(adv_particles_bench SOURCES bench.cpp)
target_link_libraries(adv_particles_bench PRIVATE adv_particles)
add_test(NAME adv_particles_bench.smoke COMMAND adv_particles_bench 1000 3)
set_tests_properties(adv_particles_bench.smoke PROPERTIES LABELS "example" TIMEOUT 120)

# These replace the global operator new to count allocations, so they
# are separate programs. With Clang, which the tsan preset uses, the
# ThreadSanitizer runtime is linked statically and defines operator new
# too, and the linker refuses two; both programs run one thread, so TSan
# has nothing to check in them.
if(NOT CMAKE_CXX_FLAGS MATCHES "-fsanitize=thread")
  lld_add_example(adv_particles_demo SOURCES main.cpp counting_new.cpp
    EXPECTED_OUTPUT expected_output.txt)
  target_link_libraries(adv_particles_demo PRIVATE adv_particles)

  lld_add_test(adv_particles_allocation_tests SOURCES tests/test_allocations.cpp
    counting_new.cpp)
  target_link_libraries(adv_particles_allocation_tests PRIVATE adv_particles)
endif()
