blob: 5c3a3f02dd29a1b041eac00f40f55e4163212bed [file] [log] [blame]
Courtney Goeltzenleuchtere099abd2014-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
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060010#include <vulkan.h>
Mark Lobodzinskieccbb372015-09-01 09:00:16 -060011#include <vk_sdk_platform.h>
Courtney Goeltzenleuchtere099abd2014-08-12 14:09:50 -060012
Courtney Goeltzenleuchter0abdb662015-10-07 13:28:58 -060013#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
Courtney Goeltzenleuchtere099abd2014-08-12 14:09:50 -060024#include "gtest/gtest.h"
Courtney Goeltzenleuchter5ec6c022014-09-01 16:33:55 -060025#include "gtest-1.7.0/include/gtest/gtest.h"
Courtney Goeltzenleuchter0abdb662015-10-07 13:28:58 -060026#ifdef WIN32
27#pragma warning( pop )
28#endif
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060029#include "vktestbinding.h"
Courtney Goeltzenleuchtere099abd2014-08-12 14:09:50 -060030
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060031#define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
Courtney Goeltzenleuchtere099abd2014-08-12 14:09:50 -060032
Courtney Goeltzenleuchter382489d2015-04-10 08:34:15 -060033static inline const char *vk_result_string(VkResult err)
Courtney Goeltzenleuchtere099abd2014-08-12 14:09:50 -060034{
35 switch (err) {
36#define STR(r) case r: return #r
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060037 STR(VK_SUCCESS);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060038 STR(VK_NOT_READY);
39 STR(VK_TIMEOUT);
40 STR(VK_EVENT_SET);
41 STR(VK_EVENT_RESET);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060042 STR(VK_ERROR_INITIALIZATION_FAILED);
Tony Barbour8205d902015-04-16 15:59:00 -060043 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
44 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060045 STR(VK_ERROR_DEVICE_LOST);
Courtney Goeltzenleuchterac544f32015-09-14 18:01:17 -060046 STR(VK_ERROR_EXTENSION_NOT_PRESENT);
47 STR(VK_ERROR_LAYER_NOT_PRESENT);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060048 STR(VK_ERROR_MEMORY_MAP_FAILED);
Courtney Goeltzenleuchter9cc421e2015-04-08 15:36:08 -060049 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
Courtney Goeltzenleuchtere099abd2014-08-12 14:09:50 -060050#undef STR
51 default: return "UNKNOWN_RESULT";
52 }
53}
54
Chia-I Wu9dac52d2014-12-27 22:04:00 +080055static inline void test_error_callback(const char *expr, const char *file,
56 unsigned int line, const char *function)
57{
58 ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'";
59}
60
Mike Stroyan7016f4f2015-07-13 14:45:35 -060061#if defined(__linux__)
62/* Linux-specific common code: */
63
64#include <pthread.h>
65
66// Threads:
67typedef pthread_t test_platform_thread;
68
69static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void*), void *data)
70{
71 pthread_attr_t thread_attr;
72 pthread_attr_init(&thread_attr);
73 return pthread_create(thread, &thread_attr, func, data);
74}
75static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
76{
77 return pthread_join(thread, retval);
78}
79
80// Thread IDs:
81typedef pthread_t test_platform_thread_id;
82static inline test_platform_thread_id test_platform_get_thread_id()
83{
84 return pthread_self();
85}
86
87// Thread mutex:
88typedef pthread_mutex_t test_platform_thread_mutex;
89static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
90{
91 pthread_mutex_init(pMutex, NULL);
92}
93static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
94{
95 pthread_mutex_lock(pMutex);
96}
97static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
98{
99 pthread_mutex_unlock(pMutex);
100}
101static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
102{
103 pthread_mutex_destroy(pMutex);
104}
105typedef pthread_cond_t test_platform_thread_cond;
106static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
107{
108 pthread_cond_init(pCond, NULL);
109}
110static inline void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
111{
112 pthread_cond_wait(pCond, pMutex);
113}
114static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond)
115{
116 pthread_cond_broadcast(pCond);
117}
118
119#elif defined(_WIN32) // defined(__linux__)
120/* Windows-specific common code: */
121#include <winsock2.h>
122#include <windows.h>
123
124// Threads:
125typedef HANDLE test_platform_thread;
126static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void *), void *data)
127{
128 DWORD threadID;
129 *thread = CreateThread(NULL, // default security attributes
130 0, // use default stack size
131 (LPTHREAD_START_ROUTINE)func,
132 data, // thread function argument
133 0, // use default creation flags
134 &threadID); // returns thread identifier
135 return (*thread != NULL);
136}
137static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
138{
139 return WaitForSingleObject(thread, INFINITE);
140}
141
142// Thread IDs:
143typedef DWORD test_platform_thread_id;
144static test_platform_thread_id test_platform_get_thread_id()
145{
146 return GetCurrentThreadId();
147}
148
149// Thread mutex:
150typedef CRITICAL_SECTION test_platform_thread_mutex;
151static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
152{
153 InitializeCriticalSection(pMutex);
154}
155static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
156{
157 EnterCriticalSection(pMutex);
158}
159static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
160{
161 LeaveCriticalSection(pMutex);
162}
163static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
164{
165 DeleteCriticalSection(pMutex);
166}
167typedef CONDITION_VARIABLE test_platform_thread_cond;
168static void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
169{
170 InitializeConditionVariable(pCond);
171}
172static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
173{
174 SleepConditionVariableCS(pCond, pMutex, INFINITE);
175}
176static void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond)
177{
178 WakeAllConditionVariable(pCond);
179}
180#else // defined(_WIN32)
181
182#error The "test_common.h" file must be modified for this OS.
183
184// NOTE: In order to support another OS, an #elif needs to be added (above the
185// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
186// contents of this file must be created.
187
188// NOTE: Other OS-specific changes are also needed for this OS. Search for
189// files with "WIN32" in it, as a quick way to find files that must be changed.
190
191#endif // defined(_WIN32)
192
Courtney Goeltzenleuchtere099abd2014-08-12 14:09:50 -0600193#endif // TEST_COMMON_H