blob: 5d396168b087e7aa8df1706bd57f8d1301fc027c [file] [log] [blame]
Karl Schultz929a1002016-02-04 11:33:21 -07001/*
2 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
Karl Schultz929a1002016-02-04 11:33:21 -070016 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 *
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23 * USE OR OTHER DEALINGS IN THE MATERIALS.
24 *
25 * Author: Chia-I Wu <olvaffe@gmail.com>
26 * Author: Chris Forbes <chrisf@ijw.co.nz>
27 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
28 * Author: Mark Lobodzinski <mark@lunarg.com>
29 * Author: Mike Stroyan <mike@LunarG.com>
30 * Author: Tobin Ehlis <tobine@google.com>
31 * Author: Tony Barbour <tony@LunarG.com>
32 */
33
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060034#ifndef TEST_COMMON_H
35#define TEST_COMMON_H
36
37#include <stdlib.h>
38#include <stdio.h>
39#include <stdbool.h>
40#include <string.h>
41#include <assert.h>
42
Ian Elliottc11750d2015-10-30 13:24:12 -060043#ifdef _WIN32
Mark Lobodzinski450e4632015-11-24 12:04:15 -070044#define NOMINMAX
45// WinSock2.h must be included *BEFORE* windows.h
46#include <winsock2.h>
Ian Elliottc11750d2015-10-30 13:24:12 -060047#endif
Mark Lobodzinskifaa90812015-11-25 13:26:15 -070048
David Pinedo9316d3b2015-11-06 12:54:48 -070049#include <vulkan/vulkan.h>
50#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060051
Ian Elliottc11750d2015-10-30 13:24:12 -060052#ifdef _WIN32
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060053#pragma warning( push )
54/*
55 warnings 4251 and 4275 have to do with potential dll-interface mismatch
56 between library (gtest) and users. Since we build the gtest library
57 as part of the test build we know that the dll-interface will match and
58 can disable these warnings.
59 */
60#pragma warning(disable: 4251)
61#pragma warning(disable: 4275)
62#endif
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060063#include "gtest/gtest.h"
Courtney Goeltzenleuchter5b804932014-09-01 16:33:55 -060064#include "gtest-1.7.0/include/gtest/gtest.h"
Ian Elliottc11750d2015-10-30 13:24:12 -060065#ifdef _WIN32
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060066#pragma warning( pop )
67#endif
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060068#include "vktestbinding.h"
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060069
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060070#define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060071
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060072static inline const char *vk_result_string(VkResult err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060073{
74 switch (err) {
75#define STR(r) case r: return #r
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060076 STR(VK_SUCCESS);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060077 STR(VK_NOT_READY);
78 STR(VK_TIMEOUT);
79 STR(VK_EVENT_SET);
80 STR(VK_EVENT_RESET);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060081 STR(VK_ERROR_INITIALIZATION_FAILED);
Tony Barbourd1c35722015-04-16 15:59:00 -060082 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
83 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060084 STR(VK_ERROR_DEVICE_LOST);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -060085 STR(VK_ERROR_EXTENSION_NOT_PRESENT);
86 STR(VK_ERROR_LAYER_NOT_PRESENT);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060087 STR(VK_ERROR_MEMORY_MAP_FAILED);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060088 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060089#undef STR
90 default: return "UNKNOWN_RESULT";
91 }
92}
93
Chia-I Wua9a506a2014-12-27 22:04:00 +080094static inline void test_error_callback(const char *expr, const char *file,
95 unsigned int line, const char *function)
96{
97 ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'";
98}
99
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600100#if defined(__linux__)
101/* Linux-specific common code: */
102
103#include <pthread.h>
104
105// Threads:
106typedef pthread_t test_platform_thread;
107
108static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void*), void *data)
109{
110 pthread_attr_t thread_attr;
111 pthread_attr_init(&thread_attr);
112 return pthread_create(thread, &thread_attr, func, data);
113}
114static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
115{
116 return pthread_join(thread, retval);
117}
118
119// Thread IDs:
120typedef pthread_t test_platform_thread_id;
121static inline test_platform_thread_id test_platform_get_thread_id()
122{
123 return pthread_self();
124}
125
126// Thread mutex:
127typedef pthread_mutex_t test_platform_thread_mutex;
128static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
129{
130 pthread_mutex_init(pMutex, NULL);
131}
132static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
133{
134 pthread_mutex_lock(pMutex);
135}
136static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
137{
138 pthread_mutex_unlock(pMutex);
139}
140static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
141{
142 pthread_mutex_destroy(pMutex);
143}
144typedef pthread_cond_t test_platform_thread_cond;
145static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
146{
147 pthread_cond_init(pCond, NULL);
148}
149static inline void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
150{
151 pthread_cond_wait(pCond, pMutex);
152}
153static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond)
154{
155 pthread_cond_broadcast(pCond);
156}
157
158#elif defined(_WIN32) // defined(__linux__)
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600159// Threads:
160typedef HANDLE test_platform_thread;
161static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void *), void *data)
162{
163 DWORD threadID;
164 *thread = CreateThread(NULL, // default security attributes
165 0, // use default stack size
166 (LPTHREAD_START_ROUTINE)func,
167 data, // thread function argument
168 0, // use default creation flags
169 &threadID); // returns thread identifier
170 return (*thread != NULL);
171}
172static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
173{
174 return WaitForSingleObject(thread, INFINITE);
175}
176
177// Thread IDs:
178typedef DWORD test_platform_thread_id;
179static test_platform_thread_id test_platform_get_thread_id()
180{
181 return GetCurrentThreadId();
182}
183
184// Thread mutex:
185typedef CRITICAL_SECTION test_platform_thread_mutex;
186static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
187{
188 InitializeCriticalSection(pMutex);
189}
190static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
191{
192 EnterCriticalSection(pMutex);
193}
194static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
195{
196 LeaveCriticalSection(pMutex);
197}
198static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
199{
200 DeleteCriticalSection(pMutex);
201}
202typedef CONDITION_VARIABLE test_platform_thread_cond;
203static void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
204{
205 InitializeConditionVariable(pCond);
206}
207static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
208{
209 SleepConditionVariableCS(pCond, pMutex, INFINITE);
210}
211static void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond)
212{
213 WakeAllConditionVariable(pCond);
214}
215#else // defined(_WIN32)
216
217#error The "test_common.h" file must be modified for this OS.
218
219// NOTE: In order to support another OS, an #elif needs to be added (above the
220// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
221// contents of this file must be created.
222
223// NOTE: Other OS-specific changes are also needed for this OS. Search for
224// files with "WIN32" in it, as a quick way to find files that must be changed.
225
226#endif // defined(_WIN32)
227
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -0600228#endif // TEST_COMMON_H