Karl Schultz | 929a100 | 2016-02-04 11:33:21 -0700 | [diff] [blame] | 1 | /* |
Mark Lobodzinski | 103c58a | 2020-01-29 13:20:00 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2015-2020 The Khronos Group Inc. |
| 3 | * Copyright (c) 2015-2020 Valve Corporation |
| 4 | * Copyright (c) 2015-2020 LunarG, Inc. |
Karl Schultz | 929a100 | 2016-02-04 11:33:21 -0700 | [diff] [blame] | 5 | * |
Jon Ashburn | 3ebf125 | 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 | 929a100 | 2016-02-04 11:33:21 -0700 | [diff] [blame] | 9 | * |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Karl Schultz | 929a100 | 2016-02-04 11:33:21 -0700 | [diff] [blame] | 11 | * |
Jon Ashburn | 3ebf125 | 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 | 929a100 | 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 | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 27 | #ifndef TEST_COMMON_H |
| 28 | #define TEST_COMMON_H |
| 29 | |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 30 | #include <assert.h> |
Mark Lobodzinski | 6d4a6d9 | 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 | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 35 | |
Ian Elliott | c11750d | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 36 | #ifdef _WIN32 |
Mark Lobodzinski | 450e463 | 2015-11-24 12:04:15 -0700 | [diff] [blame] | 37 | // WinSock2.h must be included *BEFORE* windows.h |
| 38 | #include <winsock2.h> |
Ian Elliott | c11750d | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 39 | #endif |
Mark Lobodzinski | faa9081 | 2015-11-25 13:26:15 -0700 | [diff] [blame] | 40 | |
Mark Lobodzinski | 103c58a | 2020-01-29 13:20:00 -0700 | [diff] [blame^] | 41 | // sdk_platform header redefines NOMINMAX |
| 42 | #undef NOMINMAX |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 43 | #include <vulkan/vk_sdk_platform.h> |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 44 | #include <vulkan/vulkan.h> |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 45 | |
Ian Elliott | c11750d | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 46 | #ifdef _WIN32 |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 47 | #pragma warning(push) |
Courtney Goeltzenleuchter | 58f3eff | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 48 | /* |
| 49 | warnings 4251 and 4275 have to do with potential dll-interface mismatch |
| 50 | between library (gtest) and users. Since we build the gtest library |
| 51 | as part of the test build we know that the dll-interface will match and |
| 52 | can disable these warnings. |
| 53 | */ |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 54 | #pragma warning(disable : 4251) |
| 55 | #pragma warning(disable : 4275) |
Courtney Goeltzenleuchter | 58f3eff | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 56 | #endif |
Cody Northrop | 40354b8 | 2018-03-27 12:22:53 -0600 | [diff] [blame] | 57 | |
Mark Lobodzinski | 8bdb1f3 | 2018-03-27 17:14:34 -0600 | [diff] [blame] | 58 | // GTest and Xlib collide due to redefinitions of "None" and "Bool" |
| 59 | #ifdef VK_USE_PLATFORM_XLIB_KHR |
| 60 | #pragma push_macro("None") |
| 61 | #pragma push_macro("Bool") |
| 62 | #undef None |
| 63 | #undef Bool |
| 64 | #endif |
| 65 | |
Cody Northrop | 40354b8 | 2018-03-27 12:22:53 -0600 | [diff] [blame] | 66 | // Use the NDK's header on Android |
| 67 | #ifndef __ANDROID__ |
Mark Lobodzinski | 42f743f | 2018-05-31 09:58:13 -0600 | [diff] [blame] | 68 | #include "gtest/gtest.h" |
Mark Lobodzinski | 8bdb1f3 | 2018-03-27 17:14:34 -0600 | [diff] [blame] | 69 | #else |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 70 | #include "gtest/gtest.h" |
Mark Lobodzinski | 8bdb1f3 | 2018-03-27 17:14:34 -0600 | [diff] [blame] | 71 | #endif |
| 72 | |
| 73 | // Redefine Xlib definitions |
| 74 | #ifdef VK_USE_PLATFORM_XLIB_KHR |
| 75 | #pragma pop_macro("Bool") |
| 76 | #pragma pop_macro("None") |
| 77 | #endif |
Cody Northrop | 40354b8 | 2018-03-27 12:22:53 -0600 | [diff] [blame] | 78 | |
Ian Elliott | c11750d | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 79 | #ifdef _WIN32 |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 80 | #pragma warning(pop) |
Courtney Goeltzenleuchter | 58f3eff | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 81 | #endif |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 82 | #include "vktestbinding.h" |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 83 | |
Petr Kraus | 6bb16b4 | 2019-09-09 22:05:50 +0200 | [diff] [blame] | 84 | #define ASSERT_VK_SUCCESS(err) \ |
| 85 | { \ |
| 86 | const VkResult resolved_err = err; \ |
| 87 | ASSERT_EQ(VK_SUCCESS, resolved_err) << vk_result_string(resolved_err); \ |
| 88 | } |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 89 | |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 90 | static inline const char *vk_result_string(VkResult err) { |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 91 | switch (err) { |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 92 | #define STR(r) \ |
| 93 | case r: \ |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 94 | return #r |
| 95 | STR(VK_SUCCESS); |
| 96 | STR(VK_NOT_READY); |
| 97 | STR(VK_TIMEOUT); |
| 98 | STR(VK_EVENT_SET); |
| 99 | STR(VK_EVENT_RESET); |
| 100 | STR(VK_ERROR_INITIALIZATION_FAILED); |
| 101 | STR(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 102 | STR(VK_ERROR_OUT_OF_DEVICE_MEMORY); |
| 103 | STR(VK_ERROR_DEVICE_LOST); |
| 104 | STR(VK_ERROR_EXTENSION_NOT_PRESENT); |
| 105 | STR(VK_ERROR_LAYER_NOT_PRESENT); |
| 106 | STR(VK_ERROR_MEMORY_MAP_FAILED); |
| 107 | STR(VK_ERROR_INCOMPATIBLE_DRIVER); |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 108 | #undef STR |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 109 | default: |
| 110 | return "UNKNOWN_RESULT"; |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 114 | static inline void test_error_callback(const char *expr, const char *file, unsigned int line, const char *function) { |
Chia-I Wu | a9a506a | 2014-12-27 22:04:00 +0800 | [diff] [blame] | 115 | ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'"; |
| 116 | } |
| 117 | |
Karl Schultz | 2e5ed33 | 2017-12-12 10:33:01 -0500 | [diff] [blame] | 118 | #if defined(__linux__) || defined(__APPLE__) |
Petr Kraus | 6bb16b4 | 2019-09-09 22:05:50 +0200 | [diff] [blame] | 119 | /* Linux-specific common code: */ |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 120 | |
| 121 | #include <pthread.h> |
| 122 | |
| 123 | // Threads: |
| 124 | typedef pthread_t test_platform_thread; |
| 125 | |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 126 | static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) { |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 127 | pthread_attr_t thread_attr; |
| 128 | pthread_attr_init(&thread_attr); |
| 129 | return pthread_create(thread, &thread_attr, func, data); |
| 130 | } |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 131 | static inline int test_platform_thread_join(test_platform_thread thread, void **retval) { return pthread_join(thread, retval); } |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 132 | |
| 133 | // Thread IDs: |
| 134 | typedef pthread_t test_platform_thread_id; |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 135 | static inline test_platform_thread_id test_platform_get_thread_id() { return pthread_self(); } |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 136 | |
| 137 | // Thread mutex: |
| 138 | typedef pthread_mutex_t test_platform_thread_mutex; |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 139 | static inline void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_init(pMutex, NULL); } |
| 140 | static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_lock(pMutex); } |
| 141 | static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_unlock(pMutex); } |
| 142 | static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_destroy(pMutex); } |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 143 | typedef pthread_cond_t test_platform_thread_cond; |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 144 | static inline void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { pthread_cond_init(pCond, NULL); } |
| 145 | static inline void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) { |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 146 | pthread_cond_wait(pCond, pMutex); |
| 147 | } |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 148 | static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { pthread_cond_broadcast(pCond); } |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 149 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 150 | #elif defined(_WIN32) // defined(__linux__) |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 151 | // Threads: |
| 152 | typedef HANDLE test_platform_thread; |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 153 | static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) { |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 154 | DWORD threadID; |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 155 | *thread = CreateThread(NULL, // default security attributes |
| 156 | 0, // use default stack size |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 157 | (LPTHREAD_START_ROUTINE)func, |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 158 | data, // thread function argument |
| 159 | 0, // use default creation flags |
| 160 | &threadID); // returns thread identifier |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 161 | return (*thread != NULL); |
| 162 | } |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 163 | static inline int test_platform_thread_join(test_platform_thread thread, void **retval) { |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 164 | return WaitForSingleObject(thread, INFINITE); |
| 165 | } |
| 166 | |
| 167 | // Thread IDs: |
| 168 | typedef DWORD test_platform_thread_id; |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 169 | static test_platform_thread_id test_platform_get_thread_id() { return GetCurrentThreadId(); } |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 170 | |
| 171 | // Thread mutex: |
| 172 | typedef CRITICAL_SECTION test_platform_thread_mutex; |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 173 | static void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); } |
| 174 | static void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); } |
| 175 | static void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); } |
| 176 | static void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); } |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 177 | typedef CONDITION_VARIABLE test_platform_thread_cond; |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 178 | static void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { InitializeConditionVariable(pCond); } |
| 179 | static void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) { |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 180 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 181 | } |
Mark Lobodzinski | 6d4a6d9 | 2016-09-07 16:27:38 -0600 | [diff] [blame] | 182 | static void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { WakeAllConditionVariable(pCond); } |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 183 | #else // defined(_WIN32) |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 184 | |
| 185 | #error The "test_common.h" file must be modified for this OS. |
| 186 | |
Petr Kraus | 6bb16b4 | 2019-09-09 22:05:50 +0200 | [diff] [blame] | 187 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 188 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 189 | // contents of this file must be created. |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 190 | |
Petr Kraus | 6bb16b4 | 2019-09-09 22:05:50 +0200 | [diff] [blame] | 191 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 192 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 193 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 194 | #endif // defined(_WIN32) |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 195 | |
Mark Lobodzinski | 64318ba | 2017-01-26 13:34:13 -0700 | [diff] [blame] | 196 | #endif // TEST_COMMON_H |