find_package(Threads REQUIRED)

include_directories(cutest/)

set(UNIT_TESTS_FILES
  pack.c
  pipe.c
  sds.c
  parser.c
  network.c
  unit_sizes.c
  hashtable.c
  http_client.c
  )

if(FLB_METRICS)
  set(UNIT_TESTS_FILES
    ${UNIT_TESTS_FILES}
    metrics.c
    )
endif()

set(UNIT_TESTS_DATA
  data/pack/json_single_map_001.json
  data/pack/json_single_map_002.json
  data/parser/json.conf
  data/parser/regex.conf
  )

set(FLB_TESTS_DATA_PATH ${CMAKE_CURRENT_SOURCE_DIR}/)
configure_file(
  "${CMAKE_CURRENT_SOURCE_DIR}/flb_tests_internal.h.in"
  "${CMAKE_CURRENT_SOURCE_DIR}/flb_tests_internal.h"
  )

# Move data files to the path the test binaries can find them
macro(FLB_TEST_COPY_DATA path)
  configure_file(${path} ${CMAKE_CURRENT_BINARY_DIR}/${path} COPYONLY)
endmacro()

foreach(test_data ${UNIT_TESTS_DATA})
  FLB_TEST_COPY_DATA(${test_data})
endforeach()

# Prepare list of unit tests
foreach(source_file ${UNIT_TESTS_FILES})
  get_filename_component(source_file_we ${source_file} NAME_WE)
  set(source_file_we flb-it-${source_file_we})
  add_executable(
    ${source_file_we}
    ${source_file}
    )

  if(FLB_JEMALLOC)
    target_link_libraries(${source_file_we} libjemalloc ${CMAKE_THREAD_LIBS_INIT})
  else()
    target_link_libraries(${source_file_we} ${CMAKE_THREAD_LIBS_INIT})
  endif()

  target_link_libraries(${source_file_we} fluent-bit-static)

  add_test(${source_file_we} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${source_file_we})
  if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR
      "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
    set_property(TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -g -O3")
  endif()
endforeach()
