# Copyright © 2024 Intel Corporation
# SPDX-License-Identifier: Apache 2.0
# LEGAL NOTICE: Your use of this software and any required dependent software (the “Software Package”)
# is subject to the terms and conditions of the software license agreements for the Software Package,
# which may also include notices, disclaimers, or license terms for third party or open source software
# included in or with the Software Package, and your use indicates your acceptance of all such terms.
# Please refer to the “third-party-programs.txt” or other similarly-named text file included with the
# Software Package for additional details.

# tests/cpp/

# Build HTTP client library for tests if not already built
if(NOT TARGET vpunn_http_client)
    add_subdirectory(${COST_MODEL_ROOT_DIR}/src/http_client ${CMAKE_CURRENT_BINARY_DIR}/http_client)
endif()

include(FetchContent)
FetchContent_Declare(
    googletest
    GIT_REPOSITORY https://github.com/google/googletest.git
    GIT_TAG release-1.12.1
)

# Prevent GoogleTest from being installed with VPUNN
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(googletest)

enable_testing()

set (LIST_ACT_TEST_CASES "")

function(GENERATE_SHV_KERNEL_TEST KERNEL_TYPE KERNEL_NAME)
    set(K_NAME_csv "${KERNEL_NAME}")# original from csv

    set(KERNEL_NAME "SHV${KERNEL_NAME}")
    set(TEST_NAME "SHV${KERNEL_NAME}Cost")

    if(${KERNEL_TYPE} STREQUAL "ACTIVATION")
        set(TEST_NAME_SMOKE "${K_NAME_csv}_SmokeTest")
        set (TEST_CASE_LINE "LEGACY_SHV_TEST_F( ${TEST_NAME_SMOKE}, ${KERNEL_NAME}) //${K_NAME_csv}")

        set (LIST_ACT_TEST_CASES "${LIST_ACT_TEST_CASES} \n ${TEST_CASE_LINE} \n" PARENT_SCOPE)
    endif()
endfunction()

file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/../../src/shave/layers_perf.csv" config)
STRING(REPLACE " " "" config "${config}")
STRING(REPLACE "\n" ";" config "${config}")

set(SKIP_HEADER TRUE)
message(STATUS " >> Generating Legacy Shave Unit TEST ")

foreach(LINE ${config})
    if(SKIP_HEADER)
        set(SKIP_HEADER FALSE)
    else()
        STRING(REPLACE "," ";" PARAMETERS ${LINE})
        GENERATE_SHV_KERNEL_TEST(${PARAMETERS})
    endif()
endforeach()

# message(STATUS " >> Generated TEST code is : \n ${LIST_ACT_TEST_CASES} \n  >> Generated test code END")
# configure_file(
#      "${CMAKE_CURRENT_SOURCE_DIR}/shave_legacy_all.cpp.in"
#      "${CMAKE_CURRENT_BINARY_DIR}/shave/shave_legacy_all.cpp"
#    )

# Search for .cpp files in all added subdirectories
# If a new directory is added, it must be included here
# If GLOB_RECURSE will be used without specifying specific subdirectories, the it also
# will pick up any new .cpp files added or generated by cmake in the build tree
file(GLOB_RECURSE test_src 
    "${CMAKE_CURRENT_SOURCE_DIR}/apps/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/common/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/core/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/costmodel/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/http_client/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/inference/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/kernels/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/layer/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/shave/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/vpu/*.cpp"
)
add_executable(test_cost_model
    ${test_src}
)

# Mock model paths


#DPU models
target_compile_definitions(test_cost_model 
    PRIVATE
        MODEL_PATH="${CMAKE_SOURCE_DIR}/models"
        VPU_2_0_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/vpu_2_0.vpunn"
        VPU_2_7_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/vpu_2_7.vpunn"
        VPU_4_0_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/vpu_4_0.vpunn"
        VPU_4_1_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/vpu_4_1.vpunn"
        VPU_4_0_159_STRICT_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/vpu_40_159_strict.vpunn"
        NPU_5_0_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/vpu_5_1.vpunn"  
        # DMA models
        VPU_DMA_2_7_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/dma_2_7.vpunn"
        VPU_DMA_2_7_G4_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/dma_2_7_gear4.vpunn"
        VPU_DMA_4_0_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/dma_4_0.vpunn"
        NPU_DMA_5_0_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/dmann_5_0.vpunn"     
        NPU_DMA_5_0_V1_MODEL_PATH="${CMAKE_SOURCE_DIR}/models/dmann_5_0.vpunn"  
)

# Link libraries 
target_link_libraries(test_cost_model
    PRIVATE
        # Google Test
        gtest_main
        # Added for EMBARGO settings from root
        vpunn_common_settings
        npu_costmodel
        vpunn_http_client
)

# This will allow to include headers from the current directory
# as <component>/<header-file>.h, without needing to specify the full or relative path
target_include_directories(test_cost_model
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
)

# Apply warning suppression to the test target for GoogleTest headers
# This must be done to override common settings that enable warnings as errors
# if(MSVC)
#     target_compile_options(test_cost_model PRIVATE 
#         /WX-      # Disable warnings as errors for test target
#         /wd4018   # signed/unsigned mismatch 
#         /wd4389   # signed/unsigned mismatch from GoogleTest headers
#     )
# endif()

add_dependencies(test_cost_model vpunn_cpp_schema)

include(GoogleTest)
gtest_discover_tests(test_cost_model 
    DISCOVERY_TIMEOUT 30
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
# Additional modern CMake practices
if(WIN32)
    # Windows-specific settings
    set_target_properties(test_cost_model PROPERTIES
        VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    )
endif()

# Add compiler-specific optimizations for tests
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU")
    target_compile_options(test_cost_model PRIVATE
        $<$<CONFIG:Debug>:-O0 -g1>
        $<$<CONFIG:Release>:-O2>
    )
elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
    target_compile_options(test_cost_model PRIVATE
        $<$<CONFIG:Debug>:/Od /Zi>
        $<$<CONFIG:Release>:/O2>
    )
endif()
