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