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