
if(MSVC)
    # warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    add_definitions(/wd4996)
endif()

macro(ncnn_add_test name)
    add_executable(test_${name} test_${name}.cpp)
    target_link_libraries(test_${name} PRIVATE ncnn)

    add_test(NAME test_${name} COMMAND ${CMAKE_COMMAND} -DTEST_EXECUTABLE=$<TARGET_FILE:test_${name}> -P ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/run_test.cmake)

    # add test to a virtual project group
    set_property(TARGET test_${name} PROPERTY FOLDER "tests")
endmacro()

macro(ncnn_add_layer_test class)
    string(TOLOWER ${class} name)

    # enable if WITH_LAYER_xxx option ON
    if(${WITH_LAYER_${name}})
        file(GLOB test_${name}_SRCS "test_${name}.cpp" "test_${name}_*.cpp" LIST_DIRECTORIES FALSE)

        foreach(test_file ${test_${name}_SRCS})
            get_filename_component(test_filename ${test_file} NAME_WE)
            add_executable(${test_filename} ${test_file})
            target_link_libraries(${test_filename} PRIVATE ncnn)

            add_test(NAME ${test_filename} COMMAND ${CMAKE_COMMAND} -DTEST_EXECUTABLE=$<TARGET_FILE:${test_filename}> -P ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/run_test.cmake)

            # add test to a virtual project group
            set_property(TARGET ${test_filename} PROPERTY FOLDER "tests")
        endforeach()
    endif()
endmacro()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/layer)

if(NCNN_PIXEL_AFFINE)
    ncnn_add_test(mat_pixel_affine)
endif()

if(NCNN_PIXEL_DRAWING)
    ncnn_add_test(mat_pixel_drawing)
endif()

if(NCNN_PIXEL_ROTATE)
    ncnn_add_test(mat_pixel_rotate)
endif()

if(NCNN_PIXEL)
    ncnn_add_test(mat_pixel_resize)
    ncnn_add_test(mat_pixel)
    ncnn_add_test(squeezenet)
endif()

ncnn_add_test(c_api)
ncnn_add_test(cpu)

if(NCNN_VULKAN)
    ncnn_add_test(command)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
    target_link_libraries(test_squeezenet PRIVATE nodefs.js)
endif()

ncnn_add_layer_test(AbsVal)
ncnn_add_layer_test(BatchNorm)
ncnn_add_layer_test(Bias)
ncnn_add_layer_test(BinaryOp)
ncnn_add_layer_test(BNLL)
ncnn_add_layer_test(Cast)
ncnn_add_layer_test(Clip)
ncnn_add_layer_test(Concat)
ncnn_add_layer_test(Convolution)
ncnn_add_layer_test(Convolution1D)
ncnn_add_layer_test(Convolution3D)
ncnn_add_layer_test(ConvolutionDepthWise)
ncnn_add_layer_test(ConvolutionDepthWise1D)
ncnn_add_layer_test(ConvolutionDepthWise3D)
ncnn_add_layer_test(Crop)
ncnn_add_layer_test(Deconvolution)
ncnn_add_layer_test(Deconvolution1D)
ncnn_add_layer_test(Deconvolution3D)
ncnn_add_layer_test(DeconvolutionDepthWise)
ncnn_add_layer_test(DeconvolutionDepthWise1D)
ncnn_add_layer_test(DeconvolutionDepthWise3D)
ncnn_add_layer_test(DeepCopy)
ncnn_add_layer_test(DeformableConv2D)
ncnn_add_layer_test(Dequantize)
ncnn_add_layer_test(Dropout)
ncnn_add_layer_test(Einsum)
ncnn_add_layer_test(Eltwise)
ncnn_add_layer_test(ELU)
ncnn_add_layer_test(ExpandDims)
ncnn_add_layer_test(Flatten)
ncnn_add_layer_test(Fold)
ncnn_add_layer_test(GELU)
ncnn_add_layer_test(GLU)
ncnn_add_layer_test(Gemm)
ncnn_add_layer_test(GridSample)
ncnn_add_layer_test(GroupNorm)
ncnn_add_layer_test(GRU)
ncnn_add_layer_test(HardSigmoid)
ncnn_add_layer_test(HardSwish)
ncnn_add_layer_test(InnerProduct)
ncnn_add_layer_test(InstanceNorm)
ncnn_add_layer_test(Interp)
ncnn_add_layer_test(LayerNorm)
ncnn_add_layer_test(LRN)
ncnn_add_layer_test(LSTM)
ncnn_add_layer_test(MatMul)
ncnn_add_layer_test(MemoryData)
ncnn_add_layer_test(Mish)
ncnn_add_layer_test(MultiHeadAttention)
ncnn_add_layer_test(Noop)
ncnn_add_layer_test(Normalize)
ncnn_add_layer_test(Packing)
ncnn_add_layer_test(Padding)
ncnn_add_layer_test(Permute)
ncnn_add_layer_test(PixelShuffle)
ncnn_add_layer_test(Pooling)
ncnn_add_layer_test(Pooling1D)
ncnn_add_layer_test(Pooling3D)
ncnn_add_layer_test(Power)
ncnn_add_layer_test(PReLU)
ncnn_add_layer_test(PriorBox)
ncnn_add_layer_test(Quantize)
ncnn_add_layer_test(Reduction)
ncnn_add_layer_test(ReLU)
ncnn_add_layer_test(Reorg)
ncnn_add_layer_test(Requantize)
ncnn_add_layer_test(Reshape)
ncnn_add_layer_test(RNN)
ncnn_add_layer_test(ROIPooling)
ncnn_add_layer_test(ROIAlign)
ncnn_add_layer_test(Scale)
ncnn_add_layer_test(SELU)
ncnn_add_layer_test(ShuffleChannel)
ncnn_add_layer_test(Sigmoid)
ncnn_add_layer_test(Slice)
ncnn_add_layer_test(Softmax)
ncnn_add_layer_test(Softplus)
ncnn_add_layer_test(Squeeze)
ncnn_add_layer_test(Swish)
ncnn_add_layer_test(TanH)
ncnn_add_layer_test(Tile)
ncnn_add_layer_test(UnaryOp)
ncnn_add_layer_test(Unfold)
ncnn_add_layer_test(Yolov3DetectionOutput)
