# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the \"License\");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an \"AS IS\" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.20)
project(multi_message CXX)

# Finds the package holoscan
find_package(holoscan REQUIRED CONFIG
             PATHS "/opt/nvidia/holoscan" "/workspace/holoscan-sdk/install")

add_executable(multi_message_per_receiver
  multi_message_per_receiver.cpp
  common_ops.hpp
)
target_link_libraries(multi_message_per_receiver
  PRIVATE
  holoscan::core
)

add_executable(multi_message_sum_of_all
  multi_message_sum_of_all.cpp
  common_ops.hpp
)
target_link_libraries(multi_message_sum_of_all
  PRIVATE
  holoscan::core
)

add_executable(single_message_timeout
  single_message_timeout.cpp
  common_ops.hpp
)
target_link_libraries(single_message_timeout
  PRIVATE
  holoscan::core
)

# Testing
if(BUILD_TESTING)
  find_package(Python3 REQUIRED COMPONENTS Interpreter)

  add_test(NAME EXAMPLE_CPP_MULTI_MESSAGE_PER_RECEIVER_TEST
    COMMAND bash -c
      "${CMAKE_CURRENT_BINARY_DIR}/multi_message_per_receiver 2>&1 | \
      ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/../bin/test_pattern_validation.py --strip-quotes \
        --pass-pattern 'message received on in1: Hello from tx1' \
        --pass-pattern 'message received on in2: Hello from tx2' \
        --pass-pattern 'message received on in3: Hello from tx3' \
        --fail-pattern 'Unable to convert argument type'"
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  )

  add_test(NAME EXAMPLE_CPP_MULTI_MESSAGE_SUM_OF_ALL_TEST
    COMMAND bash -c
      "${CMAKE_CURRENT_BINARY_DIR}/multi_message_sum_of_all 2>&1 | \
       ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/../bin/test_pattern_validation.py --strip-quotes \
         --pass-pattern 'messages received on in1: \\[tx1, tx1, tx1, tx1' \
         --pass-pattern 'messages received on in2: \\[tx2, tx2, tx2' \
         --pass-pattern 'messages received on in3: \\[tx3' \
         --fail-pattern 'messages received on in3: \\[tx3, tx3, tx3, tx3' \
         --fail-pattern 'Unable to convert argument type'"
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  )
  
  add_test(NAME EXAMPLE_CPP_SINGLE_MESSAGE_TIMEOUT_TEST
           COMMAND single_message_timeout
           WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
          )
  set_tests_properties(
    EXAMPLE_CPP_SINGLE_MESSAGE_TIMEOUT_TEST PROPERTIES
    # check that despite min_sum=5, only 3 messages arrived due to the execution_frequency
    PASS_REGULAR_EXPRESSION "3 messages received on in"
    FAIL_REGULAR_EXPRESSION "Unable to convert argument type"
  )
endif()
