1*b7893ccfSSadaf Ebrahimi /*
2*b7893ccfSSadaf Ebrahimi * Copyright (c) 2015-2016 The Khronos Group Inc.
3*b7893ccfSSadaf Ebrahimi * Copyright (c) 2015-2016 Valve Corporation
4*b7893ccfSSadaf Ebrahimi * Copyright (c) 2015-2016 LunarG, Inc.
5*b7893ccfSSadaf Ebrahimi *
6*b7893ccfSSadaf Ebrahimi * Licensed under the Apache License, Version 2.0 (the "License");
7*b7893ccfSSadaf Ebrahimi * you may not use this file except in compliance with the License.
8*b7893ccfSSadaf Ebrahimi * You may obtain a copy of the License at
9*b7893ccfSSadaf Ebrahimi *
10*b7893ccfSSadaf Ebrahimi * http://www.apache.org/licenses/LICENSE-2.0
11*b7893ccfSSadaf Ebrahimi *
12*b7893ccfSSadaf Ebrahimi * Unless required by applicable law or agreed to in writing, software
13*b7893ccfSSadaf Ebrahimi * distributed under the License is distributed on an "AS IS" BASIS,
14*b7893ccfSSadaf Ebrahimi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*b7893ccfSSadaf Ebrahimi * See the License for the specific language governing permissions and
16*b7893ccfSSadaf Ebrahimi * limitations under the License.
17*b7893ccfSSadaf Ebrahimi *
18*b7893ccfSSadaf Ebrahimi * Author: Chia-I Wu <[email protected]>
19*b7893ccfSSadaf Ebrahimi * Author: Chris Forbes <[email protected]>
20*b7893ccfSSadaf Ebrahimi * Author: Courtney Goeltzenleuchter <[email protected]>
21*b7893ccfSSadaf Ebrahimi * Author: Mark Lobodzinski <[email protected]>
22*b7893ccfSSadaf Ebrahimi * Author: Mike Stroyan <[email protected]>
23*b7893ccfSSadaf Ebrahimi * Author: Tobin Ehlis <[email protected]>
24*b7893ccfSSadaf Ebrahimi * Author: Tony Barbour <[email protected]>
25*b7893ccfSSadaf Ebrahimi */
26*b7893ccfSSadaf Ebrahimi
27*b7893ccfSSadaf Ebrahimi #ifndef TEST_COMMON_H
28*b7893ccfSSadaf Ebrahimi #define TEST_COMMON_H
29*b7893ccfSSadaf Ebrahimi
30*b7893ccfSSadaf Ebrahimi #include <assert.h>
31*b7893ccfSSadaf Ebrahimi #include <stdbool.h>
32*b7893ccfSSadaf Ebrahimi #include <stdio.h>
33*b7893ccfSSadaf Ebrahimi #include <stdlib.h>
34*b7893ccfSSadaf Ebrahimi #include <string.h>
35*b7893ccfSSadaf Ebrahimi
36*b7893ccfSSadaf Ebrahimi #ifdef _WIN32
37*b7893ccfSSadaf Ebrahimi #define NOMINMAX
38*b7893ccfSSadaf Ebrahimi // WinSock2.h must be included *BEFORE* windows.h
39*b7893ccfSSadaf Ebrahimi #include <winsock2.h>
40*b7893ccfSSadaf Ebrahimi #endif
41*b7893ccfSSadaf Ebrahimi
42*b7893ccfSSadaf Ebrahimi #include <vulkan/vk_sdk_platform.h>
43*b7893ccfSSadaf Ebrahimi #include <vulkan/vulkan.h>
44*b7893ccfSSadaf Ebrahimi
45*b7893ccfSSadaf Ebrahimi #ifdef _WIN32
46*b7893ccfSSadaf Ebrahimi #pragma warning(push)
47*b7893ccfSSadaf Ebrahimi /*
48*b7893ccfSSadaf Ebrahimi warnings 4251 and 4275 have to do with potential dll-interface mismatch
49*b7893ccfSSadaf Ebrahimi between library (gtest) and users. Since we build the gtest library
50*b7893ccfSSadaf Ebrahimi as part of the test build we know that the dll-interface will match and
51*b7893ccfSSadaf Ebrahimi can disable these warnings.
52*b7893ccfSSadaf Ebrahimi */
53*b7893ccfSSadaf Ebrahimi #pragma warning(disable : 4251)
54*b7893ccfSSadaf Ebrahimi #pragma warning(disable : 4275)
55*b7893ccfSSadaf Ebrahimi #endif
56*b7893ccfSSadaf Ebrahimi
57*b7893ccfSSadaf Ebrahimi // GTest and Xlib collide due to redefinitions of "None" and "Bool"
58*b7893ccfSSadaf Ebrahimi #ifdef VK_USE_PLATFORM_XLIB_KHR
59*b7893ccfSSadaf Ebrahimi #pragma push_macro("None")
60*b7893ccfSSadaf Ebrahimi #pragma push_macro("Bool")
61*b7893ccfSSadaf Ebrahimi #undef None
62*b7893ccfSSadaf Ebrahimi #undef Bool
63*b7893ccfSSadaf Ebrahimi #endif
64*b7893ccfSSadaf Ebrahimi
65*b7893ccfSSadaf Ebrahimi // Use the NDK's header on Android
66*b7893ccfSSadaf Ebrahimi #ifndef __ANDROID__
67*b7893ccfSSadaf Ebrahimi #include "gtest/gtest.h"
68*b7893ccfSSadaf Ebrahimi #else
69*b7893ccfSSadaf Ebrahimi #include "gtest/gtest.h"
70*b7893ccfSSadaf Ebrahimi #endif
71*b7893ccfSSadaf Ebrahimi
72*b7893ccfSSadaf Ebrahimi // Redefine Xlib definitions
73*b7893ccfSSadaf Ebrahimi #ifdef VK_USE_PLATFORM_XLIB_KHR
74*b7893ccfSSadaf Ebrahimi #pragma pop_macro("Bool")
75*b7893ccfSSadaf Ebrahimi #pragma pop_macro("None")
76*b7893ccfSSadaf Ebrahimi #endif
77*b7893ccfSSadaf Ebrahimi
78*b7893ccfSSadaf Ebrahimi #ifdef _WIN32
79*b7893ccfSSadaf Ebrahimi #pragma warning(pop)
80*b7893ccfSSadaf Ebrahimi #endif
81*b7893ccfSSadaf Ebrahimi #include "vktestbinding.h"
82*b7893ccfSSadaf Ebrahimi
83*b7893ccfSSadaf Ebrahimi #define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
84*b7893ccfSSadaf Ebrahimi
vk_result_string(VkResult err)85*b7893ccfSSadaf Ebrahimi static inline const char *vk_result_string(VkResult err) {
86*b7893ccfSSadaf Ebrahimi switch (err) {
87*b7893ccfSSadaf Ebrahimi #define STR(r) \
88*b7893ccfSSadaf Ebrahimi case r: \
89*b7893ccfSSadaf Ebrahimi return #r
90*b7893ccfSSadaf Ebrahimi STR(VK_SUCCESS);
91*b7893ccfSSadaf Ebrahimi STR(VK_NOT_READY);
92*b7893ccfSSadaf Ebrahimi STR(VK_TIMEOUT);
93*b7893ccfSSadaf Ebrahimi STR(VK_EVENT_SET);
94*b7893ccfSSadaf Ebrahimi STR(VK_EVENT_RESET);
95*b7893ccfSSadaf Ebrahimi STR(VK_ERROR_INITIALIZATION_FAILED);
96*b7893ccfSSadaf Ebrahimi STR(VK_ERROR_OUT_OF_HOST_MEMORY);
97*b7893ccfSSadaf Ebrahimi STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
98*b7893ccfSSadaf Ebrahimi STR(VK_ERROR_DEVICE_LOST);
99*b7893ccfSSadaf Ebrahimi STR(VK_ERROR_EXTENSION_NOT_PRESENT);
100*b7893ccfSSadaf Ebrahimi STR(VK_ERROR_LAYER_NOT_PRESENT);
101*b7893ccfSSadaf Ebrahimi STR(VK_ERROR_MEMORY_MAP_FAILED);
102*b7893ccfSSadaf Ebrahimi STR(VK_ERROR_INCOMPATIBLE_DRIVER);
103*b7893ccfSSadaf Ebrahimi #undef STR
104*b7893ccfSSadaf Ebrahimi default:
105*b7893ccfSSadaf Ebrahimi return "UNKNOWN_RESULT";
106*b7893ccfSSadaf Ebrahimi }
107*b7893ccfSSadaf Ebrahimi }
108*b7893ccfSSadaf Ebrahimi
test_error_callback(const char * expr,const char * file,unsigned int line,const char * function)109*b7893ccfSSadaf Ebrahimi static inline void test_error_callback(const char *expr, const char *file, unsigned int line, const char *function) {
110*b7893ccfSSadaf Ebrahimi ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'";
111*b7893ccfSSadaf Ebrahimi }
112*b7893ccfSSadaf Ebrahimi
113*b7893ccfSSadaf Ebrahimi #if defined(__linux__) || defined(__APPLE__)
114*b7893ccfSSadaf Ebrahimi /* Linux-specific common code: */
115*b7893ccfSSadaf Ebrahimi
116*b7893ccfSSadaf Ebrahimi #include <pthread.h>
117*b7893ccfSSadaf Ebrahimi
118*b7893ccfSSadaf Ebrahimi // Threads:
119*b7893ccfSSadaf Ebrahimi typedef pthread_t test_platform_thread;
120*b7893ccfSSadaf Ebrahimi
test_platform_thread_create(test_platform_thread * thread,void * (* func)(void *),void * data)121*b7893ccfSSadaf Ebrahimi static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) {
122*b7893ccfSSadaf Ebrahimi pthread_attr_t thread_attr;
123*b7893ccfSSadaf Ebrahimi pthread_attr_init(&thread_attr);
124*b7893ccfSSadaf Ebrahimi return pthread_create(thread, &thread_attr, func, data);
125*b7893ccfSSadaf Ebrahimi }
test_platform_thread_join(test_platform_thread thread,void ** retval)126*b7893ccfSSadaf Ebrahimi static inline int test_platform_thread_join(test_platform_thread thread, void **retval) { return pthread_join(thread, retval); }
127*b7893ccfSSadaf Ebrahimi
128*b7893ccfSSadaf Ebrahimi // Thread IDs:
129*b7893ccfSSadaf Ebrahimi typedef pthread_t test_platform_thread_id;
test_platform_get_thread_id()130*b7893ccfSSadaf Ebrahimi static inline test_platform_thread_id test_platform_get_thread_id() { return pthread_self(); }
131*b7893ccfSSadaf Ebrahimi
132*b7893ccfSSadaf Ebrahimi // Thread mutex:
133*b7893ccfSSadaf Ebrahimi typedef pthread_mutex_t test_platform_thread_mutex;
test_platform_thread_create_mutex(test_platform_thread_mutex * pMutex)134*b7893ccfSSadaf Ebrahimi static inline void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_init(pMutex, NULL); }
test_platform_thread_lock_mutex(test_platform_thread_mutex * pMutex)135*b7893ccfSSadaf Ebrahimi static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_lock(pMutex); }
test_platform_thread_unlock_mutex(test_platform_thread_mutex * pMutex)136*b7893ccfSSadaf Ebrahimi static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_unlock(pMutex); }
test_platform_thread_delete_mutex(test_platform_thread_mutex * pMutex)137*b7893ccfSSadaf Ebrahimi static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_destroy(pMutex); }
138*b7893ccfSSadaf Ebrahimi typedef pthread_cond_t test_platform_thread_cond;
test_platform_thread_init_cond(test_platform_thread_cond * pCond)139*b7893ccfSSadaf Ebrahimi static inline void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { pthread_cond_init(pCond, NULL); }
test_platform_thread_cond_wait(test_platform_thread_cond * pCond,test_platform_thread_mutex * pMutex)140*b7893ccfSSadaf Ebrahimi static inline void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) {
141*b7893ccfSSadaf Ebrahimi pthread_cond_wait(pCond, pMutex);
142*b7893ccfSSadaf Ebrahimi }
test_platform_thread_cond_broadcast(test_platform_thread_cond * pCond)143*b7893ccfSSadaf Ebrahimi static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { pthread_cond_broadcast(pCond); }
144*b7893ccfSSadaf Ebrahimi
145*b7893ccfSSadaf Ebrahimi #elif defined(_WIN32) // defined(__linux__)
146*b7893ccfSSadaf Ebrahimi // Threads:
147*b7893ccfSSadaf Ebrahimi typedef HANDLE test_platform_thread;
test_platform_thread_create(test_platform_thread * thread,void * (* func)(void *),void * data)148*b7893ccfSSadaf Ebrahimi static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) {
149*b7893ccfSSadaf Ebrahimi DWORD threadID;
150*b7893ccfSSadaf Ebrahimi *thread = CreateThread(NULL, // default security attributes
151*b7893ccfSSadaf Ebrahimi 0, // use default stack size
152*b7893ccfSSadaf Ebrahimi (LPTHREAD_START_ROUTINE)func,
153*b7893ccfSSadaf Ebrahimi data, // thread function argument
154*b7893ccfSSadaf Ebrahimi 0, // use default creation flags
155*b7893ccfSSadaf Ebrahimi &threadID); // returns thread identifier
156*b7893ccfSSadaf Ebrahimi return (*thread != NULL);
157*b7893ccfSSadaf Ebrahimi }
test_platform_thread_join(test_platform_thread thread,void ** retval)158*b7893ccfSSadaf Ebrahimi static inline int test_platform_thread_join(test_platform_thread thread, void **retval) {
159*b7893ccfSSadaf Ebrahimi return WaitForSingleObject(thread, INFINITE);
160*b7893ccfSSadaf Ebrahimi }
161*b7893ccfSSadaf Ebrahimi
162*b7893ccfSSadaf Ebrahimi // Thread IDs:
163*b7893ccfSSadaf Ebrahimi typedef DWORD test_platform_thread_id;
test_platform_get_thread_id()164*b7893ccfSSadaf Ebrahimi static test_platform_thread_id test_platform_get_thread_id() { return GetCurrentThreadId(); }
165*b7893ccfSSadaf Ebrahimi
166*b7893ccfSSadaf Ebrahimi // Thread mutex:
167*b7893ccfSSadaf Ebrahimi typedef CRITICAL_SECTION test_platform_thread_mutex;
test_platform_thread_create_mutex(test_platform_thread_mutex * pMutex)168*b7893ccfSSadaf Ebrahimi static void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); }
test_platform_thread_lock_mutex(test_platform_thread_mutex * pMutex)169*b7893ccfSSadaf Ebrahimi static void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); }
test_platform_thread_unlock_mutex(test_platform_thread_mutex * pMutex)170*b7893ccfSSadaf Ebrahimi static void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); }
test_platform_thread_delete_mutex(test_platform_thread_mutex * pMutex)171*b7893ccfSSadaf Ebrahimi static void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); }
172*b7893ccfSSadaf Ebrahimi typedef CONDITION_VARIABLE test_platform_thread_cond;
test_platform_thread_init_cond(test_platform_thread_cond * pCond)173*b7893ccfSSadaf Ebrahimi static void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { InitializeConditionVariable(pCond); }
test_platform_thread_cond_wait(test_platform_thread_cond * pCond,test_platform_thread_mutex * pMutex)174*b7893ccfSSadaf Ebrahimi static void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) {
175*b7893ccfSSadaf Ebrahimi SleepConditionVariableCS(pCond, pMutex, INFINITE);
176*b7893ccfSSadaf Ebrahimi }
test_platform_thread_cond_broadcast(test_platform_thread_cond * pCond)177*b7893ccfSSadaf Ebrahimi static void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { WakeAllConditionVariable(pCond); }
178*b7893ccfSSadaf Ebrahimi #else // defined(_WIN32)
179*b7893ccfSSadaf Ebrahimi
180*b7893ccfSSadaf Ebrahimi #error The "test_common.h" file must be modified for this OS.
181*b7893ccfSSadaf Ebrahimi
182*b7893ccfSSadaf Ebrahimi // NOTE: In order to support another OS, an #elif needs to be added (above the
183*b7893ccfSSadaf Ebrahimi // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
184*b7893ccfSSadaf Ebrahimi // contents of this file must be created.
185*b7893ccfSSadaf Ebrahimi
186*b7893ccfSSadaf Ebrahimi // NOTE: Other OS-specific changes are also needed for this OS. Search for
187*b7893ccfSSadaf Ebrahimi // files with "WIN32" in it, as a quick way to find files that must be changed.
188*b7893ccfSSadaf Ebrahimi
189*b7893ccfSSadaf Ebrahimi #endif // defined(_WIN32)
190*b7893ccfSSadaf Ebrahimi
191*b7893ccfSSadaf Ebrahimi #endif // TEST_COMMON_H
192