blob: 3f32653df120bdd39d587fc743ac697535355553 [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 *
16 * The Materials are Confidential Information as defined by the Khronos
17 * Membership Agreement until designated non-confidential by Khronos, at which
18 * point this condition clause shall be removed.
19 *
20 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 *
24 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
26 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
27 * USE OR OTHER DEALINGS IN THE MATERIALS.
28 *
29 * Author: Chia-I Wu <olvaffe@gmail.com>
30 * Author: Chris Forbes <chrisf@ijw.co.nz>
31 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
32 * Author: Mark Lobodzinski <mark@lunarg.com>
33 * Author: Mike Stroyan <mike@LunarG.com>
34 * Author: Tobin Ehlis <tobine@google.com>
35 * Author: Tony Barbour <tony@LunarG.com>
36 */
37
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060038#ifndef TEST_COMMON_H
39#define TEST_COMMON_H
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <stdbool.h>
44#include <string.h>
45#include <assert.h>
46
Ian Elliottc11750d2015-10-30 13:24:12 -060047#ifdef _WIN32
Mark Lobodzinski450e4632015-11-24 12:04:15 -070048#define NOMINMAX
49// WinSock2.h must be included *BEFORE* windows.h
50#include <winsock2.h>
Ian Elliottc11750d2015-10-30 13:24:12 -060051#endif
Mark Lobodzinskifaa90812015-11-25 13:26:15 -070052
David Pinedo9316d3b2015-11-06 12:54:48 -070053#include <vulkan/vulkan.h>
54#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060055
Ian Elliottc11750d2015-10-30 13:24:12 -060056#ifdef _WIN32
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060057#pragma warning( push )
58/*
59 warnings 4251 and 4275 have to do with potential dll-interface mismatch
60 between library (gtest) and users. Since we build the gtest library
61 as part of the test build we know that the dll-interface will match and
62 can disable these warnings.
63 */
64#pragma warning(disable: 4251)
65#pragma warning(disable: 4275)
66#endif
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060067#include "gtest/gtest.h"
Courtney Goeltzenleuchter5b804932014-09-01 16:33:55 -060068#include "gtest-1.7.0/include/gtest/gtest.h"
Ian Elliottc11750d2015-10-30 13:24:12 -060069#ifdef _WIN32
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060070#pragma warning( pop )
71#endif
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060072#include "vktestbinding.h"
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060073
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060074#define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060075
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060076static inline const char *vk_result_string(VkResult err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060077{
78 switch (err) {
79#define STR(r) case r: return #r
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060080 STR(VK_SUCCESS);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060081 STR(VK_NOT_READY);
82 STR(VK_TIMEOUT);
83 STR(VK_EVENT_SET);
84 STR(VK_EVENT_RESET);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060085 STR(VK_ERROR_INITIALIZATION_FAILED);
Tony Barbourd1c35722015-04-16 15:59:00 -060086 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
87 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060088 STR(VK_ERROR_DEVICE_LOST);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -060089 STR(VK_ERROR_EXTENSION_NOT_PRESENT);
90 STR(VK_ERROR_LAYER_NOT_PRESENT);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060091 STR(VK_ERROR_MEMORY_MAP_FAILED);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060092 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060093#undef STR
94 default: return "UNKNOWN_RESULT";
95 }
96}
97
Chia-I Wua9a506a2014-12-27 22:04:00 +080098static inline void test_error_callback(const char *expr, const char *file,
99 unsigned int line, const char *function)
100{
101 ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'";
102}
103
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600104#if defined(__linux__)
105/* Linux-specific common code: */
106
107#include <pthread.h>
108
109// Threads:
110typedef pthread_t test_platform_thread;
111
112static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void*), void *data)
113{
114 pthread_attr_t thread_attr;
115 pthread_attr_init(&thread_attr);
116 return pthread_create(thread, &thread_attr, func, data);
117}
118static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
119{
120 return pthread_join(thread, retval);
121}
122
123// Thread IDs:
124typedef pthread_t test_platform_thread_id;
125static inline test_platform_thread_id test_platform_get_thread_id()
126{
127 return pthread_self();
128}
129
130// Thread mutex:
131typedef pthread_mutex_t test_platform_thread_mutex;
132static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
133{
134 pthread_mutex_init(pMutex, NULL);
135}
136static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
137{
138 pthread_mutex_lock(pMutex);
139}
140static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
141{
142 pthread_mutex_unlock(pMutex);
143}
144static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
145{
146 pthread_mutex_destroy(pMutex);
147}
148typedef pthread_cond_t test_platform_thread_cond;
149static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
150{
151 pthread_cond_init(pCond, NULL);
152}
153static inline void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
154{
155 pthread_cond_wait(pCond, pMutex);
156}
157static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond)
158{
159 pthread_cond_broadcast(pCond);
160}
161
162#elif defined(_WIN32) // defined(__linux__)
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600163// Threads:
164typedef HANDLE test_platform_thread;
165static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void *), void *data)
166{
167 DWORD threadID;
168 *thread = CreateThread(NULL, // default security attributes
169 0, // use default stack size
170 (LPTHREAD_START_ROUTINE)func,
171 data, // thread function argument
172 0, // use default creation flags
173 &threadID); // returns thread identifier
174 return (*thread != NULL);
175}
176static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
177{
178 return WaitForSingleObject(thread, INFINITE);
179}
180
181// Thread IDs:
182typedef DWORD test_platform_thread_id;
183static test_platform_thread_id test_platform_get_thread_id()
184{
185 return GetCurrentThreadId();
186}
187
188// Thread mutex:
189typedef CRITICAL_SECTION test_platform_thread_mutex;
190static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
191{
192 InitializeCriticalSection(pMutex);
193}
194static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
195{
196 EnterCriticalSection(pMutex);
197}
198static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
199{
200 LeaveCriticalSection(pMutex);
201}
202static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
203{
204 DeleteCriticalSection(pMutex);
205}
206typedef CONDITION_VARIABLE test_platform_thread_cond;
207static void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
208{
209 InitializeConditionVariable(pCond);
210}
211static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
212{
213 SleepConditionVariableCS(pCond, pMutex, INFINITE);
214}
215static void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond)
216{
217 WakeAllConditionVariable(pCond);
218}
219#else // defined(_WIN32)
220
221#error The "test_common.h" file must be modified for this OS.
222
223// NOTE: In order to support another OS, an #elif needs to be added (above the
224// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
225// contents of this file must be created.
226
227// NOTE: Other OS-specific changes are also needed for this OS. Search for
228// files with "WIN32" in it, as a quick way to find files that must be changed.
229
230#endif // defined(_WIN32)
231
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -0600232#endif // TEST_COMMON_H