Karl Schultz | 929a100 | 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 | 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 | |
| 30 | #include <stdlib.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdbool.h> |
| 33 | #include <string.h> |
| 34 | #include <assert.h> |
| 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 | #define NOMINMAX |
| 38 | // WinSock2.h must be included *BEFORE* windows.h |
| 39 | #include <winsock2.h> |
Ian Elliott | c11750d | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 40 | #endif |
Mark Lobodzinski | faa9081 | 2015-11-25 13:26:15 -0700 | [diff] [blame] | 41 | |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 42 | #include <vulkan/vulkan.h> |
| 43 | #include <vulkan/vk_sdk_platform.h> |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 44 | |
Ian Elliott | c11750d | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 45 | #ifdef _WIN32 |
Courtney Goeltzenleuchter | 58f3eff | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 46 | #pragma warning( push ) |
| 47 | /* |
| 48 | warnings 4251 and 4275 have to do with potential dll-interface mismatch |
| 49 | between library (gtest) and users. Since we build the gtest library |
| 50 | as part of the test build we know that the dll-interface will match and |
| 51 | can disable these warnings. |
| 52 | */ |
| 53 | #pragma warning(disable: 4251) |
| 54 | #pragma warning(disable: 4275) |
| 55 | #endif |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 56 | #include "gtest/gtest.h" |
Courtney Goeltzenleuchter | 5b80493 | 2014-09-01 16:33:55 -0600 | [diff] [blame] | 57 | #include "gtest-1.7.0/include/gtest/gtest.h" |
Ian Elliott | c11750d | 2015-10-30 13:24:12 -0600 | [diff] [blame] | 58 | #ifdef _WIN32 |
Courtney Goeltzenleuchter | 58f3eff | 2015-10-07 13:28:58 -0600 | [diff] [blame] | 59 | #pragma warning( pop ) |
| 60 | #endif |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 61 | #include "vktestbinding.h" |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 62 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 63 | #define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err) |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 64 | |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 65 | static inline const char *vk_result_string(VkResult err) |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 66 | { |
| 67 | switch (err) { |
| 68 | #define STR(r) case r: return #r |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 69 | STR(VK_SUCCESS); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 70 | STR(VK_NOT_READY); |
| 71 | STR(VK_TIMEOUT); |
| 72 | STR(VK_EVENT_SET); |
| 73 | STR(VK_EVENT_RESET); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 74 | STR(VK_ERROR_INITIALIZATION_FAILED); |
Tony Barbour | d1c3572 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 75 | STR(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 76 | STR(VK_ERROR_OUT_OF_DEVICE_MEMORY); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 77 | STR(VK_ERROR_DEVICE_LOST); |
Courtney Goeltzenleuchter | 55659b7 | 2015-09-14 18:01:17 -0600 | [diff] [blame] | 78 | STR(VK_ERROR_EXTENSION_NOT_PRESENT); |
| 79 | STR(VK_ERROR_LAYER_NOT_PRESENT); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 80 | STR(VK_ERROR_MEMORY_MAP_FAILED); |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 81 | STR(VK_ERROR_INCOMPATIBLE_DRIVER); |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 82 | #undef STR |
| 83 | default: return "UNKNOWN_RESULT"; |
| 84 | } |
| 85 | } |
| 86 | |
Chia-I Wu | a9a506a | 2014-12-27 22:04:00 +0800 | [diff] [blame] | 87 | static inline void test_error_callback(const char *expr, const char *file, |
| 88 | unsigned int line, const char *function) |
| 89 | { |
| 90 | ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'"; |
| 91 | } |
| 92 | |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 93 | #if defined(__linux__) |
| 94 | /* Linux-specific common code: */ |
| 95 | |
| 96 | #include <pthread.h> |
| 97 | |
| 98 | // Threads: |
| 99 | typedef pthread_t test_platform_thread; |
| 100 | |
| 101 | static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void*), void *data) |
| 102 | { |
| 103 | pthread_attr_t thread_attr; |
| 104 | pthread_attr_init(&thread_attr); |
| 105 | return pthread_create(thread, &thread_attr, func, data); |
| 106 | } |
| 107 | static inline int test_platform_thread_join(test_platform_thread thread, void **retval) |
| 108 | { |
| 109 | return pthread_join(thread, retval); |
| 110 | } |
| 111 | |
| 112 | // Thread IDs: |
| 113 | typedef pthread_t test_platform_thread_id; |
| 114 | static inline test_platform_thread_id test_platform_get_thread_id() |
| 115 | { |
| 116 | return pthread_self(); |
| 117 | } |
| 118 | |
| 119 | // Thread mutex: |
| 120 | typedef pthread_mutex_t test_platform_thread_mutex; |
| 121 | static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex) |
| 122 | { |
| 123 | pthread_mutex_init(pMutex, NULL); |
| 124 | } |
| 125 | static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex) |
| 126 | { |
| 127 | pthread_mutex_lock(pMutex); |
| 128 | } |
| 129 | static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex) |
| 130 | { |
| 131 | pthread_mutex_unlock(pMutex); |
| 132 | } |
| 133 | static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex) |
| 134 | { |
| 135 | pthread_mutex_destroy(pMutex); |
| 136 | } |
| 137 | typedef pthread_cond_t test_platform_thread_cond; |
| 138 | static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond) |
| 139 | { |
| 140 | pthread_cond_init(pCond, NULL); |
| 141 | } |
| 142 | static inline void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex) |
| 143 | { |
| 144 | pthread_cond_wait(pCond, pMutex); |
| 145 | } |
| 146 | static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond) |
| 147 | { |
| 148 | pthread_cond_broadcast(pCond); |
| 149 | } |
| 150 | |
| 151 | #elif defined(_WIN32) // defined(__linux__) |
Mike Stroyan | 4268d1f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 152 | // Threads: |
| 153 | typedef HANDLE test_platform_thread; |
| 154 | static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void *), void *data) |
| 155 | { |
| 156 | DWORD threadID; |
| 157 | *thread = CreateThread(NULL, // default security attributes |
| 158 | 0, // use default stack size |
| 159 | (LPTHREAD_START_ROUTINE)func, |
| 160 | data, // thread function argument |
| 161 | 0, // use default creation flags |
| 162 | &threadID); // returns thread identifier |
| 163 | return (*thread != NULL); |
| 164 | } |
| 165 | static inline int test_platform_thread_join(test_platform_thread thread, void **retval) |
| 166 | { |
| 167 | return WaitForSingleObject(thread, INFINITE); |
| 168 | } |
| 169 | |
| 170 | // Thread IDs: |
| 171 | typedef DWORD test_platform_thread_id; |
| 172 | static test_platform_thread_id test_platform_get_thread_id() |
| 173 | { |
| 174 | return GetCurrentThreadId(); |
| 175 | } |
| 176 | |
| 177 | // Thread mutex: |
| 178 | typedef CRITICAL_SECTION test_platform_thread_mutex; |
| 179 | static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex) |
| 180 | { |
| 181 | InitializeCriticalSection(pMutex); |
| 182 | } |
| 183 | static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex) |
| 184 | { |
| 185 | EnterCriticalSection(pMutex); |
| 186 | } |
| 187 | static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex) |
| 188 | { |
| 189 | LeaveCriticalSection(pMutex); |
| 190 | } |
| 191 | static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex) |
| 192 | { |
| 193 | DeleteCriticalSection(pMutex); |
| 194 | } |
| 195 | typedef CONDITION_VARIABLE test_platform_thread_cond; |
| 196 | static void test_platform_thread_init_cond(test_platform_thread_cond* pCond) |
| 197 | { |
| 198 | InitializeConditionVariable(pCond); |
| 199 | } |
| 200 | static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex) |
| 201 | { |
| 202 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 203 | } |
| 204 | static void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond) |
| 205 | { |
| 206 | WakeAllConditionVariable(pCond); |
| 207 | } |
| 208 | #else // defined(_WIN32) |
| 209 | |
| 210 | #error The "test_common.h" file must be modified for this OS. |
| 211 | |
| 212 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 213 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 214 | // contents of this file must be created. |
| 215 | |
| 216 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 217 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 218 | |
| 219 | #endif // defined(_WIN32) |
| 220 | |
Courtney Goeltzenleuchter | b7f5050 | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 221 | #endif // TEST_COMMON_H |