blob: 2e48170d18d341f88df7f6ab857c2dd752f89068 [file] [log] [blame]
Karl Schultzb8328c02016-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 Ashburn4f80d672016-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 Schultzb8328c02016-02-04 11:33:21 -07009 *
Jon Ashburn4f80d672016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Karl Schultzb8328c02016-02-04 11:33:21 -070011 *
Jon Ashburn4f80d672016-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 Schultzb8328c02016-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 Goeltzenleuchter382f3c72014-08-12 14:09:50 -060027#ifndef TEST_COMMON_H
28#define TEST_COMMON_H
29
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060030#include <assert.h>
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -060031#include <stdbool.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060035
Ian Elliott329fb922015-10-30 13:24:12 -060036#ifdef _WIN32
Mark Lobodzinski383bd832015-11-24 12:04:15 -070037#define NOMINMAX
38// WinSock2.h must be included *BEFORE* windows.h
39#include <winsock2.h>
Ian Elliott329fb922015-10-30 13:24:12 -060040#endif
Mark Lobodzinski06e36262015-11-25 13:26:15 -070041
David Pinedoa5036622015-11-06 12:54:48 -070042#include <vulkan/vk_sdk_platform.h>
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -060043#include <vulkan/vulkan.h>
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060044
Ian Elliott329fb922015-10-30 13:24:12 -060045#ifdef _WIN32
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -060046#pragma warning(push)
Courtney Goeltzenleuchter94e54002015-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 Lobodzinski30b6bc42016-09-07 16:27:38 -060053#pragma warning(disable : 4251)
54#pragma warning(disable : 4275)
Courtney Goeltzenleuchter94e54002015-10-07 13:28:58 -060055#endif
Cody Northrop1c28bfe2018-03-27 12:22:53 -060056
57// Use the NDK's header on Android
58#ifndef __ANDROID__
Courtney Goeltzenleuchter722f0132014-09-01 16:33:55 -060059#include "gtest-1.7.0/include/gtest/gtest.h"
Cody Northrop1c28bfe2018-03-27 12:22:53 -060060#endif
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -060061#include "gtest/gtest.h"
Cody Northrop1c28bfe2018-03-27 12:22:53 -060062
Ian Elliott329fb922015-10-30 13:24:12 -060063#ifdef _WIN32
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -060064#pragma warning(pop)
Courtney Goeltzenleuchter94e54002015-10-07 13:28:58 -060065#endif
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060066#include "vktestbinding.h"
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060067
Courtney Goeltzenleuchter0b018602015-04-08 15:36:08 -060068#define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060069
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -060070static inline const char *vk_result_string(VkResult err) {
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060071 switch (err) {
Mark Lobodzinskie2859eb2017-01-26 13:34:13 -070072#define STR(r) \
73 case r: \
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -060074 return #r
75 STR(VK_SUCCESS);
76 STR(VK_NOT_READY);
77 STR(VK_TIMEOUT);
78 STR(VK_EVENT_SET);
79 STR(VK_EVENT_RESET);
80 STR(VK_ERROR_INITIALIZATION_FAILED);
81 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
82 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
83 STR(VK_ERROR_DEVICE_LOST);
84 STR(VK_ERROR_EXTENSION_NOT_PRESENT);
85 STR(VK_ERROR_LAYER_NOT_PRESENT);
86 STR(VK_ERROR_MEMORY_MAP_FAILED);
87 STR(VK_ERROR_INCOMPATIBLE_DRIVER);
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060088#undef STR
Mark Lobodzinskie2859eb2017-01-26 13:34:13 -070089 default:
90 return "UNKNOWN_RESULT";
Courtney Goeltzenleuchter382f3c72014-08-12 14:09:50 -060091 }
92}
93
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -060094static inline void test_error_callback(const char *expr, const char *file, unsigned int line, const char *function) {
Chia-I Wu45c863f2014-12-27 22:04:00 +080095 ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'";
96}
97
Karl Schultzd81ebfb2017-12-12 10:33:01 -050098#if defined(__linux__) || defined(__APPLE__)
Dave Houltonf031f072018-02-06 17:49:16 -070099 /* Linux-specific common code: */
Mike Stroyanf04d0732015-07-13 14:45:35 -0600100
101#include <pthread.h>
102
103// Threads:
104typedef pthread_t test_platform_thread;
105
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600106static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) {
Mike Stroyanf04d0732015-07-13 14:45:35 -0600107 pthread_attr_t thread_attr;
108 pthread_attr_init(&thread_attr);
109 return pthread_create(thread, &thread_attr, func, data);
110}
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600111static inline int test_platform_thread_join(test_platform_thread thread, void **retval) { return pthread_join(thread, retval); }
Mike Stroyanf04d0732015-07-13 14:45:35 -0600112
113// Thread IDs:
114typedef pthread_t test_platform_thread_id;
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600115static inline test_platform_thread_id test_platform_get_thread_id() { return pthread_self(); }
Mike Stroyanf04d0732015-07-13 14:45:35 -0600116
117// Thread mutex:
118typedef pthread_mutex_t test_platform_thread_mutex;
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600119static inline void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_init(pMutex, NULL); }
120static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_lock(pMutex); }
121static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_unlock(pMutex); }
122static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_destroy(pMutex); }
Mike Stroyanf04d0732015-07-13 14:45:35 -0600123typedef pthread_cond_t test_platform_thread_cond;
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600124static inline void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { pthread_cond_init(pCond, NULL); }
125static inline void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) {
Mike Stroyanf04d0732015-07-13 14:45:35 -0600126 pthread_cond_wait(pCond, pMutex);
127}
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600128static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { pthread_cond_broadcast(pCond); }
Mike Stroyanf04d0732015-07-13 14:45:35 -0600129
Mark Lobodzinskie2859eb2017-01-26 13:34:13 -0700130#elif defined(_WIN32) // defined(__linux__)
Mike Stroyanf04d0732015-07-13 14:45:35 -0600131// Threads:
132typedef HANDLE test_platform_thread;
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600133static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) {
Mike Stroyanf04d0732015-07-13 14:45:35 -0600134 DWORD threadID;
Mark Lobodzinskie2859eb2017-01-26 13:34:13 -0700135 *thread = CreateThread(NULL, // default security attributes
136 0, // use default stack size
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600137 (LPTHREAD_START_ROUTINE)func,
Mark Lobodzinskie2859eb2017-01-26 13:34:13 -0700138 data, // thread function argument
139 0, // use default creation flags
140 &threadID); // returns thread identifier
Mike Stroyanf04d0732015-07-13 14:45:35 -0600141 return (*thread != NULL);
142}
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600143static inline int test_platform_thread_join(test_platform_thread thread, void **retval) {
Mike Stroyanf04d0732015-07-13 14:45:35 -0600144 return WaitForSingleObject(thread, INFINITE);
145}
146
147// Thread IDs:
148typedef DWORD test_platform_thread_id;
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600149static test_platform_thread_id test_platform_get_thread_id() { return GetCurrentThreadId(); }
Mike Stroyanf04d0732015-07-13 14:45:35 -0600150
151// Thread mutex:
152typedef CRITICAL_SECTION test_platform_thread_mutex;
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600153static void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); }
154static void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); }
155static void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); }
156static void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); }
Mike Stroyanf04d0732015-07-13 14:45:35 -0600157typedef CONDITION_VARIABLE test_platform_thread_cond;
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600158static void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { InitializeConditionVariable(pCond); }
159static void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) {
Mike Stroyanf04d0732015-07-13 14:45:35 -0600160 SleepConditionVariableCS(pCond, pMutex, INFINITE);
161}
Mark Lobodzinski30b6bc42016-09-07 16:27:38 -0600162static void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { WakeAllConditionVariable(pCond); }
Mark Lobodzinskie2859eb2017-01-26 13:34:13 -0700163#else // defined(_WIN32)
Mike Stroyanf04d0732015-07-13 14:45:35 -0600164
165#error The "test_common.h" file must be modified for this OS.
166
Dave Houltonf031f072018-02-06 17:49:16 -0700167 // NOTE: In order to support another OS, an #elif needs to be added (above the
168 // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
169 // contents of this file must be created.
Mike Stroyanf04d0732015-07-13 14:45:35 -0600170
Dave Houltonf031f072018-02-06 17:49:16 -0700171 // NOTE: Other OS-specific changes are also needed for this OS. Search for
172 // files with "WIN32" in it, as a quick way to find files that must be changed.
Mike Stroyanf04d0732015-07-13 14:45:35 -0600173
Mark Lobodzinskie2859eb2017-01-26 13:34:13 -0700174#endif // defined(_WIN32)
Mike Stroyanf04d0732015-07-13 14:45:35 -0600175
Mark Lobodzinskie2859eb2017-01-26 13:34:13 -0700176#endif // TEST_COMMON_H