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