blob: 1ef01fdc57d72f2a82dbbc7a65ab393e3c69287a [file] [log] [blame]
Courtney Goeltzenleuchter382f3c72014-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 Elliott329fb922015-10-30 13:24:12 -060010#ifdef _WIN32
11#define VK_USE_PLATFORM_WIN32_KHR
Mark Lobodzinski383bd832015-11-24 12:04:15 -070012#define NOMINMAX
13// WinSock2.h must be included *BEFORE* windows.h
14#include <winsock2.h>
Ian Elliott329fb922015-10-30 13:24:12 -060015#else
16#define VK_USE_PLATFORM_XCB_KHR
17#endif
David Pinedoa5036622015-11-06 12:54:48 -070018#include <vulkan/vulkan.h>
19#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060020
Ian Elliott329fb922015-10-30 13:24:12 -060021#ifdef _WIN32
Courtney Goeltzenleuchter94e54002015-10-07 13:28:58 -060022#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 Goeltzenleuchter382f3c72014-08-12 14:09:50 -060032#include "gtest/gtest.h"
Courtney Goeltzenleuchter722f0132014-09-01 16:33:55 -060033#include "gtest-1.7.0/include/gtest/gtest.h"
Ian Elliott329fb922015-10-30 13:24:12 -060034#ifdef _WIN32
Courtney Goeltzenleuchter94e54002015-10-07 13:28:58 -060035#pragma warning( pop )
36#endif
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060037#include "vktestbinding.h"
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060038
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060039#define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060040
Courtney Goeltzenleuchterbfae2502015-04-10 08:34:15 -060041static inline const char *vk_result_string(VkResult err)
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060042{
43 switch (err) {
44#define STR(r) case r: return #r
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060045 STR(VK_SUCCESS);
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060046 STR(VK_NOT_READY);
47 STR(VK_TIMEOUT);
48 STR(VK_EVENT_SET);
49 STR(VK_EVENT_RESET);
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060050 STR(VK_ERROR_INITIALIZATION_FAILED);
Tony Barbourd9601692015-04-16 15:59:00 -060051 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
52 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060053 STR(VK_ERROR_DEVICE_LOST);
Courtney Goeltzenleuchtera313e9b2015-09-14 18:01:17 -060054 STR(VK_ERROR_EXTENSION_NOT_PRESENT);
55 STR(VK_ERROR_LAYER_NOT_PRESENT);
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060056 STR(VK_ERROR_MEMORY_MAP_FAILED);
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060057 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060058#undef STR
59 default: return "UNKNOWN_RESULT";
60 }
61}
62
Chia-I Wu45c863f2014-12-27 22:04:00 +080063static 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 Stroyanf04d0732015-07-13 14:45:35 -060069#if defined(__linux__)
70/* Linux-specific common code: */
71
72#include <pthread.h>
73
74// Threads:
75typedef pthread_t test_platform_thread;
76
77static 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}
83static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
84{
85 return pthread_join(thread, retval);
86}
87
88// Thread IDs:
89typedef pthread_t test_platform_thread_id;
90static inline test_platform_thread_id test_platform_get_thread_id()
91{
92 return pthread_self();
93}
94
95// Thread mutex:
96typedef pthread_mutex_t test_platform_thread_mutex;
97static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
98{
99 pthread_mutex_init(pMutex, NULL);
100}
101static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
102{
103 pthread_mutex_lock(pMutex);
104}
105static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
106{
107 pthread_mutex_unlock(pMutex);
108}
109static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
110{
111 pthread_mutex_destroy(pMutex);
112}
113typedef pthread_cond_t test_platform_thread_cond;
114static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
115{
116 pthread_cond_init(pCond, NULL);
117}
118static 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}
122static 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 Stroyanf04d0732015-07-13 14:45:35 -0600128// Threads:
129typedef HANDLE test_platform_thread;
130static 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}
141static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
142{
143 return WaitForSingleObject(thread, INFINITE);
144}
145
146// Thread IDs:
147typedef DWORD test_platform_thread_id;
148static test_platform_thread_id test_platform_get_thread_id()
149{
150 return GetCurrentThreadId();
151}
152
153// Thread mutex:
154typedef CRITICAL_SECTION test_platform_thread_mutex;
155static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
156{
157 InitializeCriticalSection(pMutex);
158}
159static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
160{
161 EnterCriticalSection(pMutex);
162}
163static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
164{
165 LeaveCriticalSection(pMutex);
166}
167static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
168{
169 DeleteCriticalSection(pMutex);
170}
171typedef CONDITION_VARIABLE test_platform_thread_cond;
172static void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
173{
174 InitializeConditionVariable(pCond);
175}
176static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
177{
178 SleepConditionVariableCS(pCond, pMutex, INFINITE);
179}
180static 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 Goeltzenleuchter382f3c72014-08-12 14:09:50 -0600197#endif // TEST_COMMON_H