Karl Schultz | b8328c0 | 2016-02-04 11:33:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016 The Khronos Group Inc. |
| 3 | * Copyright (c) 2015-2016 Valve Corporation |
| 4 | * Copyright (c) 2015-2016 LunarG, Inc. |
| 5 | * |
Jon Ashburn | 4f80d67 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
Karl Schultz | b8328c0 | 2016-02-04 11:33:21 -0700 | [diff] [blame] | 9 | * |
Jon Ashburn | 4f80d67 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Karl Schultz | b8328c0 | 2016-02-04 11:33:21 -0700 | [diff] [blame] | 11 | * |
Jon Ashburn | 4f80d67 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
Karl Schultz | b8328c0 | 2016-02-04 11:33:21 -0700 | [diff] [blame] | 17 | * |
| 18 | * Author: Chia-I Wu <olvaffe@gmail.com> |
| 19 | * Author: Chris Forbes <chrisf@ijw.co.nz> |
| 20 | * Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
| 21 | * Author: Mark Lobodzinski <mark@lunarg.com> |
| 22 | * Author: Mike Stroyan <mike@LunarG.com> |
| 23 | * Author: Tobin Ehlis <tobine@google.com> |
| 24 | * Author: Tony Barbour <tony@LunarG.com> |
| 25 | */ |
| 26 | |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 27 | #ifndef TEST_COMMON_H |
| 28 | #define TEST_COMMON_H |
| 29 | |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 30 | #include <assert.h> |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 31 | #include <stdbool.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 35 | |
Ian Elliott | 329fb92 | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 36 | #ifdef _WIN32 |
Biswapriyo Nath | 2979391 | 2020-07-29 10:28:18 +0530 | [diff] [blame] | 37 | #ifndef NOMINMAX |
Mark Lobodzinski | 383bd83 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 38 | #define NOMINMAX |
Biswapriyo Nath | 2979391 | 2020-07-29 10:28:18 +0530 | [diff] [blame] | 39 | #endif |
Mark Lobodzinski | 383bd83 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 40 | // WinSock2.h must be included *BEFORE* windows.h |
| 41 | #include <winsock2.h> |
Ian Elliott | 329fb92 | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 42 | #endif |
Mark Lobodzinski | 06e3626 | 2015-11-25 13:26:15 -0700 | [diff] [blame] | 43 | |
David Pinedo | a503662 | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 44 | #include <vulkan/vk_sdk_platform.h> |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 45 | #include <vulkan/vulkan.h> |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 46 | |
Biswapriyo Nath | 2979391 | 2020-07-29 10:28:18 +0530 | [diff] [blame] | 47 | #ifdef _MSC_VER |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 48 | #pragma warning(push) |
Courtney Goeltzenleuchter | 94e5400 | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 49 | /* |
Biswapriyo Nath | 2979391 | 2020-07-29 10:28:18 +0530 | [diff] [blame] | 50 | MSVC warnings 4251 and 4275 have to do with potential dll-interface mismatch |
Courtney Goeltzenleuchter | 94e5400 | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 51 | between library (gtest) and users. Since we build the gtest library |
| 52 | as part of the test build we know that the dll-interface will match and |
| 53 | can disable these warnings. |
| 54 | */ |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 55 | #pragma warning(disable : 4251) |
| 56 | #pragma warning(disable : 4275) |
Courtney Goeltzenleuchter | 94e5400 | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 57 | #endif |
Cody Northrop | 1c28bfe | 2018-03-27 12:22:53 -0600 | [diff] [blame] | 58 | |
Mark Lobodzinski | a4c3dd7 | 2018-03-27 17:14:34 -0600 | [diff] [blame] | 59 | // GTest and Xlib collide due to redefinitions of "None" and "Bool" |
| 60 | #ifdef VK_USE_PLATFORM_XLIB_KHR |
| 61 | #pragma push_macro("None") |
| 62 | #pragma push_macro("Bool") |
| 63 | #undef None |
| 64 | #undef Bool |
| 65 | #endif |
| 66 | |
Cody Northrop | 1c28bfe | 2018-03-27 12:22:53 -0600 | [diff] [blame] | 67 | // Use the NDK's header on Android |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 68 | #include "gtest/gtest.h" |
Mark Lobodzinski | a4c3dd7 | 2018-03-27 17:14:34 -0600 | [diff] [blame] | 69 | |
| 70 | // Redefine Xlib definitions |
| 71 | #ifdef VK_USE_PLATFORM_XLIB_KHR |
| 72 | #pragma pop_macro("Bool") |
| 73 | #pragma pop_macro("None") |
| 74 | #endif |
Cody Northrop | 1c28bfe | 2018-03-27 12:22:53 -0600 | [diff] [blame] | 75 | |
Biswapriyo Nath | 2979391 | 2020-07-29 10:28:18 +0530 | [diff] [blame] | 76 | #ifdef _MSC_VER |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 77 | #pragma warning(pop) |
Courtney Goeltzenleuchter | 94e5400 | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 78 | #endif |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 79 | |
Courtney Goeltzenleuchter | 0b01860 | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 80 | #define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err) |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 81 | |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 82 | static inline const char *vk_result_string(VkResult err) { |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 83 | switch (err) { |
Mark Lobodzinski | e2859eb | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 84 | #define STR(r) \ |
| 85 | case r: \ |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 86 | return #r |
| 87 | STR(VK_SUCCESS); |
| 88 | STR(VK_NOT_READY); |
| 89 | STR(VK_TIMEOUT); |
| 90 | STR(VK_EVENT_SET); |
| 91 | STR(VK_EVENT_RESET); |
| 92 | STR(VK_ERROR_INITIALIZATION_FAILED); |
| 93 | STR(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 94 | STR(VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 95 | STR(VK_ERROR_DEVICE_LOST); |
| 96 | STR(VK_ERROR_EXTENSION_NOT_PRESENT); |
| 97 | STR(VK_ERROR_LAYER_NOT_PRESENT); |
| 98 | STR(VK_ERROR_MEMORY_MAP_FAILED); |
| 99 | STR(VK_ERROR_INCOMPATIBLE_DRIVER); |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 100 | #undef STR |
Mark Lobodzinski | e2859eb | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 101 | default: |
| 102 | return "UNKNOWN_RESULT"; |
Courtney Goeltzenleuchter | 382f3c7 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 106 | static inline void test_error_callback(const char *expr, const char *file, unsigned int line, const char *function) { |
Chia-I Wu | 45c863f | 2014-12-27 22:04:00 +0800 | [diff] [blame] | 107 | ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'"; |
| 108 | } |
| 109 | |
Karl Schultz | d81ebfb | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 110 | #if defined(__linux__) || defined(__APPLE__) |
Dave Houlton | f031f07 | 2018-02-06 17:49:16 -0700 | [diff] [blame] | 111 | /* Linux-specific common code: */ |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 112 | |
| 113 | #include <pthread.h> |
| 114 | |
| 115 | // Threads: |
| 116 | typedef pthread_t test_platform_thread; |
| 117 | |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 118 | static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) { |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 119 | pthread_attr_t thread_attr; |
| 120 | pthread_attr_init(&thread_attr); |
| 121 | return pthread_create(thread, &thread_attr, func, data); |
| 122 | } |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 123 | static inline int test_platform_thread_join(test_platform_thread thread, void **retval) { return pthread_join(thread, retval); } |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 124 | |
| 125 | // Thread IDs: |
| 126 | typedef pthread_t test_platform_thread_id; |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 127 | static inline test_platform_thread_id test_platform_get_thread_id() { return pthread_self(); } |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 128 | |
| 129 | // Thread mutex: |
| 130 | typedef pthread_mutex_t test_platform_thread_mutex; |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 131 | static inline void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_init(pMutex, NULL); } |
| 132 | static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_lock(pMutex); } |
| 133 | static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_unlock(pMutex); } |
| 134 | static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_destroy(pMutex); } |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 135 | typedef pthread_cond_t test_platform_thread_cond; |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 136 | static inline void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { pthread_cond_init(pCond, NULL); } |
| 137 | static inline void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) { |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 138 | pthread_cond_wait(pCond, pMutex); |
| 139 | } |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 140 | static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { pthread_cond_broadcast(pCond); } |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 141 | |
Mark Lobodzinski | e2859eb | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 142 | #elif defined(_WIN32) // defined(__linux__) |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 143 | // Threads: |
| 144 | typedef HANDLE test_platform_thread; |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 145 | static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) { |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 146 | DWORD threadID; |
Mark Lobodzinski | e2859eb | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 147 | *thread = CreateThread(NULL, // default security attributes |
| 148 | 0, // use default stack size |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 149 | (LPTHREAD_START_ROUTINE)func, |
Mark Lobodzinski | e2859eb | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 150 | data, // thread function argument |
| 151 | 0, // use default creation flags |
| 152 | &threadID); // returns thread identifier |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 153 | return (*thread != NULL); |
| 154 | } |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 155 | static inline int test_platform_thread_join(test_platform_thread thread, void **retval) { |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 156 | return WaitForSingleObject(thread, INFINITE); |
| 157 | } |
| 158 | |
| 159 | // Thread IDs: |
| 160 | typedef DWORD test_platform_thread_id; |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 161 | static test_platform_thread_id test_platform_get_thread_id() { return GetCurrentThreadId(); } |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 162 | |
| 163 | // Thread mutex: |
| 164 | typedef CRITICAL_SECTION test_platform_thread_mutex; |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 165 | static void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); } |
| 166 | static void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); } |
| 167 | static void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); } |
| 168 | static void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); } |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 169 | typedef CONDITION_VARIABLE test_platform_thread_cond; |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 170 | static void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { InitializeConditionVariable(pCond); } |
| 171 | static void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) { |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 172 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 173 | } |
Mark Lobodzinski | 30b6bc4 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 174 | static void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { WakeAllConditionVariable(pCond); } |
Mark Lobodzinski | e2859eb | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 175 | #else // defined(_WIN32) |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 176 | |
| 177 | #error The "test_common.h" file must be modified for this OS. |
| 178 | |
Dave Houlton | f031f07 | 2018-02-06 17:49:16 -0700 | [diff] [blame] | 179 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 180 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 181 | // contents of this file must be created. |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 182 | |
Dave Houlton | f031f07 | 2018-02-06 17:49:16 -0700 | [diff] [blame] | 183 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 184 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 185 | |
Mark Lobodzinski | e2859eb | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 186 | #endif // defined(_WIN32) |
Mike Stroyan | f04d073 | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 187 | |
Mark Lobodzinski | e2859eb | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 188 | #endif // TEST_COMMON_H |