blob: a6aaf24705684ababd88f25321c706f6bd43d5c1 [file] [log] [blame]
Karl Schultz929a1002016-02-04 11:33:21 -07001/*
Petr Kraus6bb16b42019-09-09 22:05:50 +02002 * Copyright (c) 2015-2019 The Khronos Group Inc.
3 * Copyright (c) 2015-2019 Valve Corporation
4 * Copyright (c) 2015-2019 LunarG, Inc.
Karl Schultz929a1002016-02-04 11:33:21 -07005 *
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
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060030#include <assert.h>
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -060031#include <stdbool.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060035
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/vk_sdk_platform.h>
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -060043#include <vulkan/vulkan.h>
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060044
Ian Elliottc11750d2015-10-30 13:24:12 -060045#ifdef _WIN32
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -060046#pragma warning(push)
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060047/*
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 */
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -060053#pragma warning(disable : 4251)
54#pragma warning(disable : 4275)
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060055#endif
Cody Northrop40354b82018-03-27 12:22:53 -060056
Mark Lobodzinski8bdb1f32018-03-27 17:14:34 -060057// GTest and Xlib collide due to redefinitions of "None" and "Bool"
58#ifdef VK_USE_PLATFORM_XLIB_KHR
59#pragma push_macro("None")
60#pragma push_macro("Bool")
61#undef None
62#undef Bool
63#endif
64
Cody Northrop40354b82018-03-27 12:22:53 -060065// Use the NDK's header on Android
66#ifndef __ANDROID__
Mark Lobodzinski42f743f2018-05-31 09:58:13 -060067#include "gtest/gtest.h"
Mark Lobodzinski8bdb1f32018-03-27 17:14:34 -060068#else
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -060069#include "gtest/gtest.h"
Mark Lobodzinski8bdb1f32018-03-27 17:14:34 -060070#endif
71
72// Redefine Xlib definitions
73#ifdef VK_USE_PLATFORM_XLIB_KHR
74#pragma pop_macro("Bool")
75#pragma pop_macro("None")
76#endif
Cody Northrop40354b82018-03-27 12:22:53 -060077
Ian Elliottc11750d2015-10-30 13:24:12 -060078#ifdef _WIN32
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -060079#pragma warning(pop)
Courtney Goeltzenleuchter58f3eff2015-10-07 13:28:58 -060080#endif
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060081#include "vktestbinding.h"
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060082
Petr Kraus6bb16b42019-09-09 22:05:50 +020083#define ASSERT_VK_SUCCESS(err) \
84 { \
85 const VkResult resolved_err = err; \
86 ASSERT_EQ(VK_SUCCESS, resolved_err) << vk_result_string(resolved_err); \
87 }
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060088
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -060089static inline const char *vk_result_string(VkResult err) {
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060090 switch (err) {
Mark Lobodzinski64318ba2017-01-26 13:34:13 -070091#define STR(r) \
92 case r: \
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -060093 return #r
94 STR(VK_SUCCESS);
95 STR(VK_NOT_READY);
96 STR(VK_TIMEOUT);
97 STR(VK_EVENT_SET);
98 STR(VK_EVENT_RESET);
99 STR(VK_ERROR_INITIALIZATION_FAILED);
100 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
101 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
102 STR(VK_ERROR_DEVICE_LOST);
103 STR(VK_ERROR_EXTENSION_NOT_PRESENT);
104 STR(VK_ERROR_LAYER_NOT_PRESENT);
105 STR(VK_ERROR_MEMORY_MAP_FAILED);
106 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -0600107#undef STR
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700108 default:
109 return "UNKNOWN_RESULT";
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -0600110 }
111}
112
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600113static inline void test_error_callback(const char *expr, const char *file, unsigned int line, const char *function) {
Chia-I Wua9a506a2014-12-27 22:04:00 +0800114 ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'";
115}
116
Karl Schultz2e5ed332017-12-12 10:33:01 -0500117#if defined(__linux__) || defined(__APPLE__)
Petr Kraus6bb16b42019-09-09 22:05:50 +0200118/* Linux-specific common code: */
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600119
120#include <pthread.h>
121
122// Threads:
123typedef pthread_t test_platform_thread;
124
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600125static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) {
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600126 pthread_attr_t thread_attr;
127 pthread_attr_init(&thread_attr);
128 return pthread_create(thread, &thread_attr, func, data);
129}
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600130static inline int test_platform_thread_join(test_platform_thread thread, void **retval) { return pthread_join(thread, retval); }
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600131
132// Thread IDs:
133typedef pthread_t test_platform_thread_id;
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600134static inline test_platform_thread_id test_platform_get_thread_id() { return pthread_self(); }
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600135
136// Thread mutex:
137typedef pthread_mutex_t test_platform_thread_mutex;
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600138static inline void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_init(pMutex, NULL); }
139static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_lock(pMutex); }
140static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_unlock(pMutex); }
141static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_destroy(pMutex); }
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600142typedef pthread_cond_t test_platform_thread_cond;
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600143static inline void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { pthread_cond_init(pCond, NULL); }
144static inline void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) {
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600145 pthread_cond_wait(pCond, pMutex);
146}
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600147static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { pthread_cond_broadcast(pCond); }
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600148
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700149#elif defined(_WIN32) // defined(__linux__)
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600150// Threads:
151typedef HANDLE test_platform_thread;
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600152static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) {
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600153 DWORD threadID;
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700154 *thread = CreateThread(NULL, // default security attributes
155 0, // use default stack size
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600156 (LPTHREAD_START_ROUTINE)func,
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700157 data, // thread function argument
158 0, // use default creation flags
159 &threadID); // returns thread identifier
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600160 return (*thread != NULL);
161}
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600162static inline int test_platform_thread_join(test_platform_thread thread, void **retval) {
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600163 return WaitForSingleObject(thread, INFINITE);
164}
165
166// Thread IDs:
167typedef DWORD test_platform_thread_id;
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600168static test_platform_thread_id test_platform_get_thread_id() { return GetCurrentThreadId(); }
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600169
170// Thread mutex:
171typedef CRITICAL_SECTION test_platform_thread_mutex;
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600172static void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); }
173static void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); }
174static void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); }
175static void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); }
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600176typedef CONDITION_VARIABLE test_platform_thread_cond;
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600177static void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { InitializeConditionVariable(pCond); }
178static void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) {
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600179 SleepConditionVariableCS(pCond, pMutex, INFINITE);
180}
Mark Lobodzinski6d4a6d92016-09-07 16:27:38 -0600181static void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { WakeAllConditionVariable(pCond); }
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700182#else // defined(_WIN32)
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600183
184#error The "test_common.h" file must be modified for this OS.
185
Petr Kraus6bb16b42019-09-09 22:05:50 +0200186// 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.
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600189
Petr Kraus6bb16b42019-09-09 22:05:50 +0200190// 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.
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600192
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700193#endif // defined(_WIN32)
Mike Stroyan4268d1f2015-07-13 14:45:35 -0600194
Mark Lobodzinski64318ba2017-01-26 13:34:13 -0700195#endif // TEST_COMMON_H