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