blob: 13f99207ade4b0445cd9131aa3e9827967a8bbe5 [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
Mark Lobodzinski450e4632015-11-24 12:04:15 -070011#define NOMINMAX
12// WinSock2.h must be included *BEFORE* windows.h
13#include <winsock2.h>
Ian Elliottc11750d2015-10-30 13:24:12 -060014#endif
Mark Lobodzinskifaa90812015-11-25 13:26:15 -070015
David Pinedo9316d3b2015-11-06 12:54:48 -070016#include <vulkan/vulkan.h>
17#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060018
Ian Elliottc11750d2015-10-30 13:24:12 -060019#ifdef _WIN32
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060020#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 Goeltzenleuchterb7f50502014-08-12 14:09:50 -060030#include "gtest/gtest.h"
Courtney Goeltzenleuchter5b804932014-09-01 16:33:55 -060031#include "gtest-1.7.0/include/gtest/gtest.h"
Ian Elliottc11750d2015-10-30 13:24:12 -060032#ifdef _WIN32
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060033#pragma warning( pop )
34#endif
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060035#include "vktestbinding.h"
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060036
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060037#define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060038
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060039static inline const char *vk_result_string(VkResult err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060040{
41 switch (err) {
42#define STR(r) case r: return #r
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060043 STR(VK_SUCCESS);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060044 STR(VK_NOT_READY);
45 STR(VK_TIMEOUT);
46 STR(VK_EVENT_SET);
47 STR(VK_EVENT_RESET);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060048 STR(VK_ERROR_INITIALIZATION_FAILED);
Tony Barbourd1c35722015-04-16 15:59:00 -060049 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
50 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060051 STR(VK_ERROR_DEVICE_LOST);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -060052 STR(VK_ERROR_EXTENSION_NOT_PRESENT);
53 STR(VK_ERROR_LAYER_NOT_PRESENT);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060054 STR(VK_ERROR_MEMORY_MAP_FAILED);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060055 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060056#undef STR
57 default: return "UNKNOWN_RESULT";
58 }
59}
60
Chia-I Wua9a506a2014-12-27 22:04:00 +080061static 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 Stroyan4268d1f2015-07-13 14:45:35 -060067#if defined(__linux__)
68/* Linux-specific common code: */
69
70#include <pthread.h>
71
72// Threads:
73typedef pthread_t test_platform_thread;
74
75static 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}
81static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
82{
83 return pthread_join(thread, retval);
84}
85
86// Thread IDs:
87typedef pthread_t test_platform_thread_id;
88static inline test_platform_thread_id test_platform_get_thread_id()
89{
90 return pthread_self();
91}
92
93// Thread mutex:
94typedef pthread_mutex_t test_platform_thread_mutex;
95static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
96{
97 pthread_mutex_init(pMutex, NULL);
98}
99static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
100{
101 pthread_mutex_lock(pMutex);
102}
103static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
104{
105 pthread_mutex_unlock(pMutex);
106}
107static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
108{
109 pthread_mutex_destroy(pMutex);
110}
111typedef pthread_cond_t test_platform_thread_cond;
112static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
113{
114 pthread_cond_init(pCond, NULL);
115}
116static 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}
120static 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 Stroyan4268d1f2015-07-13 14:45:35 -0600126// Threads:
127typedef HANDLE test_platform_thread;
128static 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}
139static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
140{
141 return WaitForSingleObject(thread, INFINITE);
142}
143
144// Thread IDs:
145typedef DWORD test_platform_thread_id;
146static test_platform_thread_id test_platform_get_thread_id()
147{
148 return GetCurrentThreadId();
149}
150
151// Thread mutex:
152typedef CRITICAL_SECTION test_platform_thread_mutex;
153static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
154{
155 InitializeCriticalSection(pMutex);
156}
157static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
158{
159 EnterCriticalSection(pMutex);
160}
161static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
162{
163 LeaveCriticalSection(pMutex);
164}
165static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
166{
167 DeleteCriticalSection(pMutex);
168}
169typedef CONDITION_VARIABLE test_platform_thread_cond;
170static void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
171{
172 InitializeConditionVariable(pCond);
173}
174static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
175{
176 SleepConditionVariableCS(pCond, pMutex, INFINITE);
177}
178static 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 Goeltzenleuchterb7f50502014-08-12 14:09:50 -0600195#endif // TEST_COMMON_H