diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 66d9adc66..d95b00a96 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -50,19 +50,30 @@ find_package(GTest REQUIRED CONFIG QUIET) add_executable(vulkan_tools_tests) target_sources(vulkan_tools_tests PRIVATE main.cpp + test_common.h icd/mock_icd_tests.cpp ) get_target_property(TEST_SOURCES vulkan_tools_tests SOURCES) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${TEST_SOURCES}) -target_include_directories(vulkan_tools_tests PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) +target_include_directories(vulkan_tools_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(vulkan_tools_tests GTest::gtest Vulkan::Vulkan) add_dependencies(vulkan_tools_tests generate_binary_locations) +if (WIN32) + target_compile_definitions(vulkan_tools_tests PUBLIC -DVK_USE_PLATFORM_WIN32_KHR -DWIN32_LEAN_AND_MEAN -DNOMINMAX) +endif() +set_target_properties(vulkan_tools_tests PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>DLL") if (ENABLE_ADDRESS_SANITIZER) target_compile_options(vulkan_tools_tests PUBLIC -fsanitize=address) target_link_options(vulkan_tools_tests PUBLIC -fsanitize=address) endif () +if (WIN32) + # Copy the loader shared lib (if built) to the test application directory so the test app finds it. + add_custom_command(TARGET vulkan_tools_tests POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy $ $) +endif() + include(GoogleTest) gtest_discover_tests(vulkan_tools_tests DISCOVERY_TIMEOUT 100) diff --git a/tests/icd/mock_icd_tests.cpp b/tests/icd/mock_icd_tests.cpp index ee1cbcb53..bcae57a9f 100644 --- a/tests/icd/mock_icd_tests.cpp +++ b/tests/icd/mock_icd_tests.cpp @@ -17,25 +17,7 @@ * */ -#include - -#include -#include -#include - -#include "gtest/gtest.h" -#include "vulkan/vulkan.h" - -// Location of the built binaries in this repo -#include "binary_locations.h" - -#if defined(WIN32) -#define WIN32_LEAN_AND_MEAN -#include -int set_environment_var(const char* name, const char* value) { return SetEnvironmentVariable(name, value); } -#else -int set_environment_var(const char* name, const char* value) { return setenv(name, value, 1); } -#endif +#include "test_common.h" void setup_mock_icd_env_vars() { // Necessary to point the loader at the mock driver @@ -256,7 +238,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfacePresentModesKHR) { VkSurfaceKHR surface{}; res = create_surface(instance, surface); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(surface, nullptr); + ASSERT_NE(surface, VK_NULL_HANDLE); uint32_t count = 0; std::array present_modes{}; res = vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &count, nullptr); @@ -278,7 +260,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceFormatsKHR) { VkSurfaceKHR surface{}; res = create_surface(instance, surface); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(surface, nullptr); + ASSERT_NE(surface, VK_NULL_HANDLE); uint32_t count = 0; std::array surface_formats{}; res = vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &count, nullptr); @@ -298,7 +280,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceFormats2KHR) { VkSurfaceKHR surface{}; res = create_surface(instance, surface); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(surface, nullptr); + ASSERT_NE(surface, VK_NULL_HANDLE); uint32_t count = 0; std::array surface_formats2{}; VkPhysicalDeviceSurfaceInfo2KHR surface_info{}; @@ -322,7 +304,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceSupportKHR) { VkSurfaceKHR surface{}; res = create_surface(instance, surface); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(surface, nullptr); + ASSERT_NE(surface, VK_NULL_HANDLE); VkBool32 supported = false; res = vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, 0, surface, &supported); ASSERT_EQ(res, VK_SUCCESS); @@ -335,7 +317,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceCapabilitiesKHR) { VkSurfaceKHR surface{}; res = create_surface(instance, surface); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(surface, nullptr); + ASSERT_NE(surface, VK_NULL_HANDLE); VkSurfaceCapabilitiesKHR surface_capabilities{}; res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface, &surface_capabilities); ASSERT_EQ(res, VK_SUCCESS); @@ -354,7 +336,7 @@ TEST_F(MockICD, vkGetPhysicalDeviceSurfaceCapabilities2KHR) { VkSurfaceKHR surface{}; res = create_surface(instance, surface); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(surface, nullptr); + ASSERT_NE(surface, VK_NULL_HANDLE); VkSurfaceCapabilities2KHR surface_capabilities2{}; VkPhysicalDeviceSurfaceInfo2KHR surface_info{}; surface_info.surface = surface; @@ -899,7 +881,7 @@ TEST_F(MockICD, SwapchainLifeCycle) { VkSurfaceKHR surface{}; res = create_surface(instance, surface); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(surface, nullptr); + ASSERT_NE(surface, VK_NULL_HANDLE); VkSwapchainCreateInfoKHR swapchain_create_info{}; swapchain_create_info.surface = surface; @@ -907,7 +889,7 @@ TEST_F(MockICD, SwapchainLifeCycle) { VkSwapchainKHR swapchain{}; res = vkCreateSwapchainKHR(device, &swapchain_create_info, nullptr, &swapchain); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(swapchain, nullptr); + ASSERT_NE(swapchain, VK_NULL_HANDLE); uint32_t count = 0; res = vkGetSwapchainImagesKHR(device, swapchain, &count, nullptr); @@ -916,7 +898,7 @@ TEST_F(MockICD, SwapchainLifeCycle) { std::array swapchain_images; res = vkGetSwapchainImagesKHR(device, swapchain, &count, swapchain_images.data()); ASSERT_EQ(res, VK_SUCCESS); - ASSERT_NE(swapchain_images[0], nullptr); + ASSERT_NE(swapchain_images[0], VK_NULL_HANDLE); uint32_t image_index = 10; // arbitrary non zero value res = vkAcquireNextImageKHR(device, swapchain, 0, VK_NULL_HANDLE, VK_NULL_HANDLE, &image_index); diff --git a/tests/test_common.h b/tests/test_common.h new file mode 100644 index 000000000..de06a7844 --- /dev/null +++ b/tests/test_common.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 The Khronos Group Inc. + * Copyright (c) 2023 Valve Corporation + * Copyright (c) 2023 LunarG, Inc. + * + * 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. + * + */ + +#include + +#include +#include +#include + +#include "gtest/gtest.h" +#include "vulkan/vulkan.h" + +// Location of the built binaries in this repo +#include "binary_locations.h" + +#if defined(WIN32) +#include +inline int set_environment_var(const char* name, const char* value) { return SetEnvironmentVariable(name, value); } +#else +inline int set_environment_var(const char* name, const char* value) { return setenv(name, value, 1); } +#endif