blob: 7b2f148c2a7ea037cccb5b7f75ed53c16e441bfb [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 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -06006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Karl Schultz929a1002016-02-04 11:33:21 -07009 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Karl Schultz929a1002016-02-04 11:33:21 -070011 *
Jon Ashburn3ebf1252016-04-19 11:30:31 -060012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Karl Schultz929a1002016-02-04 11:33:21 -070017 *
18 * Author: Chia-I Wu <olvaffe@gmail.com>
19 * Author: Chris Forbes <chrisf@ijw.co.nz>
20 * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
21 * Author: Mark Lobodzinski <mark@lunarg.com>
22 * Author: Mike Stroyan <mike@LunarG.com>
23 * Author: Tobin Ehlis <tobine@google.com>
24 * Author: Tony Barbour <tony@LunarG.com>
25 */
26
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060027#ifndef TEST_COMMON_H
28#define TEST_COMMON_H
29
30#include <stdlib.h>
31#include <stdio.h>
32#include <stdbool.h>
33#include <string.h>
34#include <assert.h>
35
Ian Elliottc11750d2015-10-30 13:24:12 -060036#ifdef _WIN32
Mark Lobodzinski450e4632015-11-24 12:04:15 -070037#define NOMINMAX
38// WinSock2.h must be included *BEFORE* windows.h
39#include <winsock2.h>
Ian Elliottc11750d2015-10-30 13:24:12 -060040#endif
Mark Lobodzinskifaa90812015-11-25 13:26:15 -070041
David Pinedo9316d3b2015-11-06 12:54:48 -070042#include <vulkan/vulkan.h>
43#include <vulkan/vk_sdk_platform.h>
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060044
Ian Elliottc11750d2015-10-30 13:24:12 -060045#ifdef _WIN32
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060046#pragma warning( push )
47/*
48 warnings 4251 and 4275 have to do with potential dll-interface mismatch
49 between library (gtest) and users. Since we build the gtest library
50 as part of the test build we know that the dll-interface will match and
51 can disable these warnings.
52 */
53#pragma warning(disable: 4251)
54#pragma warning(disable: 4275)
55#endif
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060056#include "gtest/gtest.h"
Courtney Goeltzenleuchter5b804932014-09-01 16:33:55 -060057#include "gtest-1.7.0/include/gtest/gtest.h"
Ian Elliottc11750d2015-10-30 13:24:12 -060058#ifdef _WIN32
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060059#pragma warning( pop )
60#endif
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060061#include "vktestbinding.h"
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060062
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060063#define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060064
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060065static inline const char *vk_result_string(VkResult err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060066{
67 switch (err) {
68#define STR(r) case r: return #r
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060069 STR(VK_SUCCESS);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060070 STR(VK_NOT_READY);
71 STR(VK_TIMEOUT);
72 STR(VK_EVENT_SET);
73 STR(VK_EVENT_RESET);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060074 STR(VK_ERROR_INITIALIZATION_FAILED);
Tony Barbourd1c35722015-04-16 15:59:00 -060075 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
76 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060077 STR(VK_ERROR_DEVICE_LOST);
Courtney Goeltzenleuchter55659b72015-09-14 18:01:17 -060078 STR(VK_ERROR_EXTENSION_NOT_PRESENT);
79 STR(VK_ERROR_LAYER_NOT_PRESENT);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060080 STR(VK_ERROR_MEMORY_MAP_FAILED);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060081 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060082#undef STR
83 default: return "UNKNOWN_RESULT";
84 }
85}
86
Chia-I Wua9a506a2014-12-27 22:04:00 +080087static inline void test_error_callback(const char *expr, const char *file,
88 unsigned int line, const char *function)
89{
90 ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'";
91}
92
Mike Stroyan4268d1f2015-07-13 14:45:35 -060093#if defined(__linux__)
94/* Linux-specific common code: */
95
96#include <pthread.h>
97
98// Threads:
99typedef pthread_t test_platform_thread;
100
101static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void*), void *data)
102{
103 pthread_attr_t thread_attr;
104 pthread_attr_init(&thread_attr);
105 return pthread_create(thread, &thread_attr, func, data);
106}
107static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
108{
109 return pthread_join(thread, retval);
110}
111
112// Thread IDs:
113typedef pthread_t test_platform_thread_id;
114static inline test_platform_thread_id test_platform_get_thread_id()
115{
116 return pthread_self();
117}
118
119// Thread mutex:
120typedef pthread_mutex_t test_platform_thread_mutex;
121static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
122{
123 pthread_mutex_init(pMutex, NULL);
124}
125static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
126{
127 pthread_mutex_lock(pMutex);
128}
129static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
130{
131 pthread_mutex_unlock(pMutex);
132}
133static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
134{
135 pthread_mutex_destroy(pMutex);
136}
137typedef pthread_cond_t test_platform_thread_cond;
138static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
139{
140 pthread_cond_init(pCond, NULL);
141}
142static inline void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
143{
144 pthread_cond_wait(pCond, pMutex);
145}
146static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond)
147{
148 pthread_cond_broadcast(pCond);
149}
150
151#elif defined(_WIN32) // defined(__linux__)
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600152// Threads:
153typedef HANDLE test_platform_thread;
154static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void *), void *data)
155{
156 DWORD threadID;
157 *thread = CreateThread(NULL, // default security attributes
158 0, // use default stack size
159 (LPTHREAD_START_ROUTINE)func,
160 data, // thread function argument
161 0, // use default creation flags
162 &threadID); // returns thread identifier
163 return (*thread != NULL);
164}
165static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
166{
167 return WaitForSingleObject(thread, INFINITE);
168}
169
170// Thread IDs:
171typedef DWORD test_platform_thread_id;
172static test_platform_thread_id test_platform_get_thread_id()
173{
174 return GetCurrentThreadId();
175}
176
177// Thread mutex:
178typedef CRITICAL_SECTION test_platform_thread_mutex;
179static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
180{
181 InitializeCriticalSection(pMutex);
182}
183static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
184{
185 EnterCriticalSection(pMutex);
186}
187static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
188{
189 LeaveCriticalSection(pMutex);
190}
191static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
192{
193 DeleteCriticalSection(pMutex);
194}
195typedef CONDITION_VARIABLE test_platform_thread_cond;
196static void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
197{
198 InitializeConditionVariable(pCond);
199}
200static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
201{
202 SleepConditionVariableCS(pCond, pMutex, INFINITE);
203}
204static void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond)
205{
206 WakeAllConditionVariable(pCond);
207}
208#else // defined(_WIN32)
209
210#error The "test_common.h" file must be modified for this OS.
211
212// NOTE: In order to support another OS, an #elif needs to be added (above the
213// "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
214// contents of this file must be created.
215
216// NOTE: Other OS-specific changes are also needed for this OS. Search for
217// files with "WIN32" in it, as a quick way to find files that must be changed.
218
219#endif // defined(_WIN32)
220
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -0600221#endif // TEST_COMMON_H