#
# LDASTools Frame Utilities - A library to extend manipulation of LIGO/Virgo Frame
# Files
#
# Copyright (C) 2018 California Institute of Technology
#
# LDASTools Frame Utilities is free software; you may redistribute it and/or
# modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 (GPLv2) of the
# License or at your discretion, any later version.
#
# LDASTools Frame Utilities is distributed in the hope that it will be useful, but
# without any warranty or even the implied warranty of merchantability
# or fitness for a particular purpose. See the GNU General Public
# License (GPLv2) for more details.
#
# Neither the names of the California Institute of Technology (Caltech),
# The Massachusetts Institute of Technology (M.I.T), The Laser
# Interferometer Gravitational-Wave Observatory (LIGO), nor the names
# of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# You should have received a copy of the licensing terms for this
# software included in the file LICENSE located in the top-level
# directory of this package. If you did not, you can view a copy at
# http://dcc.ligo.org/M1500244/LICENSE
#

find_program( MEMORYCHECK_COMMAND valgrind )
set( MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full" )

#------------------------------------------------------------------------
# Variable definitions
#------------------------------------------------------------------------

set( LDADD
  ldasframe
  ${Boost_LIBRARIES}
)

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Testing of C++ routines
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

cx_target_executable(
  test_createRDS
  NOINST
  SOURCES test_createRDS.cc
  LDADD ${LDADD}
  INCLUDE_DIRECTORIES BEFORE PRIVATE
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_BINARY_DIR}/include
  ${CMAKE_BINARY_DIR}
  ${LDASTOOLS_INCLUDE_DIRS}
  )

cx_target_test(
  test_createRDS
  COMMAND test_createRDS
  )

cx_target_executable(
  test_createRDS_frame_generation
  NOINST
  SOURCES
  test_createRDS_frame_generation.cc
  frame_generator.cc
  LDADD ${LDADD}
  INCLUDE_DIRECTORIES BEFORE PRIVATE
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_BINARY_DIR}/include
  ${CMAKE_BINARY_DIR}
  ${LDASTOOLS_INCLUDE_DIRS}
  )


#cx_target_test(
#  test_createRDS_frame_generation
#  COMMAND test_createRDS_frame_generation
#  )
# set_tests_properties( test_createRDS_frame_generation PROPERTIES TIMEOUT 300)

cx_target_executable(
  test_createRDS_cpp
  NOINST
  SOURCES test_createRDS_cpp.cc
  LDADD ${LDADD}
  INCLUDE_DIRECTORIES BEFORE PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_BINARY_DIR}/include
    ${CMAKE_BINARY_DIR}
    ${LDASTOOLS_INCLUDE_DIRS}
  )

if ( EXISTS /ifocache/llcache )
  cx_target_test(
    test_createRDS_cpp
    COMMAND test_createRDS_cpp --log_level=message
    SETUP_COMMAND
      ${CMAKE_COMMAND} -E remove -f --
      ${CMAKE_CURRENT_BINARY_DIR}/L-testCreateRDSResample-1268056448-64.gwf
      ${CMAKE_CURRENT_BINARY_DIR}/L-testCreateRDSResample-1268056512-64.gwf
      ${CMAKE_CURRENT_BINARY_DIR}/L-testCreateRDSResample-1268056576-64.gwf
      ${CMAKE_CURRENT_BINARY_DIR}/L-testCreateRDSResampleRename-1268056448-64.gwf
      ${CMAKE_CURRENT_BINARY_DIR}/L-testCreateRDSResampleRename-1268056512-64.gwf
      ${CMAKE_CURRENT_BINARY_DIR}/L-testCreateRDSResampleRename-1268056576-64.gwf
    ENVIRONMENT
    SRCDIR=${CMAKE_CURRENT_SOURCE_DIR}
    LDAS_TOP_BUILD_DIR=${CMAKE_BINARY_DIR}
    )
endif( )

cx_target_executable(
  test_intervals
  NOINST
  SOURCES
  test_intervals.cc
  LDADD ${LDADD}
  INCLUDE_DIRECTORIES BEFORE PRIVATE
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_BINARY_DIR}/include
  ${CMAKE_BINARY_DIR}
  ${LDASTOOLS_INCLUDE_DIRS}
  )

cx_target_test(
  test_intervals
  COMMAND test_intervals
  )
# set_tests_properties( test_createRDS_frame_generation PROPERTIES TIMEOUT 300)

if ( false )
  #----------------------------------------------------------------------------
  # this test currently doesn't run for frameCPP version 3.x as the library
  #  doe not properly read version 8 frames.
  #----------------------------------------------------------------------------
if( EXISTS /ifocache/llcache/kafka/H1/H-H1_llhoft-137162/H-H1_llhoft-1371620572-1.gwf )
  add_test( NAME test_ldas_analysis_ready
    COMMAND $<TARGET_FILE:ldas_analysis_ready>
      --frame-file-pattern H-H1_llhoft-1371620572-1.gwf
    WORKING_DIRECTORY /ifocache/llcache/kafka/H1/H-H1_llhoft-137162
    )
endif( )
endif( )

#------------------------------------------------------------------------

# #392/#389: verify ldas_analysis_ready --version and -V both print the
# expected first line ("<program-name> <version>") and exit 0 per GNU
# §4.7.1. Uses the CMake package version variable so the check tracks
# release bumps automatically.
add_test(
  NAME test_ldas_analysis_ready_version_long
  COMMAND ${CMAKE_COMMAND}
    -E env
    LDAS_ANALYSIS_READY_BIN=$<TARGET_FILE:ldas_analysis_ready>
    EXPECTED_VERSION=${LDAS_TOOLS_FRAMEAPI_VERSION}
    bash -c "output=$(\"$LDAS_ANALYSIS_READY_BIN\" --version) && \
             expected=\"ldas_analysis_ready $EXPECTED_VERSION\" && \
             [ \"$output\" = \"$expected\" ] || \
             { echo \"expected: $expected\" >&2; \
               echo \"actual:   $output\" >&2; exit 1; }"
  )
add_test(
  NAME test_ldas_analysis_ready_version_short
  COMMAND ${CMAKE_COMMAND}
    -E env
    LDAS_ANALYSIS_READY_BIN=$<TARGET_FILE:ldas_analysis_ready>
    EXPECTED_VERSION=${LDAS_TOOLS_FRAMEAPI_VERSION}
    bash -c "output=$(\"$LDAS_ANALYSIS_READY_BIN\" -V) && \
             expected=\"ldas_analysis_ready $EXPECTED_VERSION\" && \
             [ \"$output\" = \"$expected\" ] || \
             { echo \"expected: $expected\" >&2; \
               echo \"actual:   $output\" >&2; exit 1; }"
  )

#------------------------------------------------------------------------

cx_scheme_sanitizer_set_environment( )
