Home | History | Annotate | Download | only in tests
      1 /*
      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  * 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
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * 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.
     17  *
     18  * Author: Chia-I Wu <olvaffe (at) gmail.com>
     19  * Author: Chris Forbes <chrisf (at) ijw.co.nz>
     20  * Author: Courtney Goeltzenleuchter <courtney (at) LunarG.com>
     21  * Author: Mark Lobodzinski <mark (at) lunarg.com>
     22  * Author: Mike Stroyan <mike (at) LunarG.com>
     23  * Author: Tobin Ehlis <tobine (at) google.com>
     24  * Author: Tony Barbour <tony (at) LunarG.com>
     25  */
     26 
     27 #ifndef TEST_COMMON_H
     28 #define TEST_COMMON_H
     29 
     30 #include <assert.h>
     31 #include <stdbool.h>
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <string.h>
     35 
     36 #ifdef _WIN32
     37 #define NOMINMAX
     38 // WinSock2.h must be included *BEFORE* windows.h
     39 #include <winsock2.h>
     40 #endif
     41 
     42 #include <vulkan/vk_sdk_platform.h>
     43 #include <vulkan/vulkan.h>
     44 
     45 #ifdef _WIN32
     46 #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
     56 #include "gtest-1.7.0/include/gtest/gtest.h"
     57 #include "gtest/gtest.h"
     58 #ifdef _WIN32
     59 #pragma warning(pop)
     60 #endif
     61 #include "vktestbinding.h"
     62 
     63 #define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
     64 
     65 static inline const char *vk_result_string(VkResult err) {
     66     switch (err) {
     67 #define STR(r) \
     68     case r:    \
     69         return #r
     70         STR(VK_SUCCESS);
     71         STR(VK_NOT_READY);
     72         STR(VK_TIMEOUT);
     73         STR(VK_EVENT_SET);
     74         STR(VK_EVENT_RESET);
     75         STR(VK_ERROR_INITIALIZATION_FAILED);
     76         STR(VK_ERROR_OUT_OF_HOST_MEMORY);
     77         STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
     78         STR(VK_ERROR_DEVICE_LOST);
     79         STR(VK_ERROR_EXTENSION_NOT_PRESENT);
     80         STR(VK_ERROR_LAYER_NOT_PRESENT);
     81         STR(VK_ERROR_MEMORY_MAP_FAILED);
     82         STR(VK_ERROR_INCOMPATIBLE_DRIVER);
     83 #undef STR
     84         default:
     85             return "UNKNOWN_RESULT";
     86     }
     87 }
     88 
     89 static inline void test_error_callback(const char *expr, const char *file, unsigned int line, const char *function) {
     90     ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'";
     91 }
     92 
     93 #if defined(__linux__)
     94     /* Linux-specific common code: */
     95 
     96 #include <pthread.h>
     97 
     98 // Threads:
     99 typedef pthread_t test_platform_thread;
    100 
    101 static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) {
    102     pthread_attr_t thread_attr;
    103     pthread_attr_init(&thread_attr);
    104     return pthread_create(thread, &thread_attr, func, data);
    105 }
    106 static inline int test_platform_thread_join(test_platform_thread thread, void **retval) { return pthread_join(thread, retval); }
    107 
    108 // Thread IDs:
    109 typedef pthread_t test_platform_thread_id;
    110 static inline test_platform_thread_id test_platform_get_thread_id() { return pthread_self(); }
    111 
    112 // Thread mutex:
    113 typedef pthread_mutex_t test_platform_thread_mutex;
    114 static inline void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_init(pMutex, NULL); }
    115 static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_lock(pMutex); }
    116 static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_unlock(pMutex); }
    117 static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { pthread_mutex_destroy(pMutex); }
    118 typedef pthread_cond_t test_platform_thread_cond;
    119 static inline void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { pthread_cond_init(pCond, NULL); }
    120 static inline void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) {
    121     pthread_cond_wait(pCond, pMutex);
    122 }
    123 static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { pthread_cond_broadcast(pCond); }
    124 
    125 #elif defined(_WIN32)  // defined(__linux__)
    126 // Threads:
    127 typedef HANDLE test_platform_thread;
    128 static inline int test_platform_thread_create(test_platform_thread *thread, void *(*func)(void *), void *data) {
    129     DWORD threadID;
    130     *thread = CreateThread(NULL,  // default security attributes
    131                            0,     // use default stack size
    132                            (LPTHREAD_START_ROUTINE)func,
    133                            data,        // thread function argument
    134                            0,           // use default creation flags
    135                            &threadID);  // returns thread identifier
    136     return (*thread != NULL);
    137 }
    138 static inline int test_platform_thread_join(test_platform_thread thread, void **retval) {
    139     return WaitForSingleObject(thread, INFINITE);
    140 }
    141 
    142 // Thread IDs:
    143 typedef DWORD test_platform_thread_id;
    144 static test_platform_thread_id test_platform_get_thread_id() { return GetCurrentThreadId(); }
    145 
    146 // Thread mutex:
    147 typedef CRITICAL_SECTION test_platform_thread_mutex;
    148 static void test_platform_thread_create_mutex(test_platform_thread_mutex *pMutex) { InitializeCriticalSection(pMutex); }
    149 static void test_platform_thread_lock_mutex(test_platform_thread_mutex *pMutex) { EnterCriticalSection(pMutex); }
    150 static void test_platform_thread_unlock_mutex(test_platform_thread_mutex *pMutex) { LeaveCriticalSection(pMutex); }
    151 static void test_platform_thread_delete_mutex(test_platform_thread_mutex *pMutex) { DeleteCriticalSection(pMutex); }
    152 typedef CONDITION_VARIABLE test_platform_thread_cond;
    153 static void test_platform_thread_init_cond(test_platform_thread_cond *pCond) { InitializeConditionVariable(pCond); }
    154 static void test_platform_thread_cond_wait(test_platform_thread_cond *pCond, test_platform_thread_mutex *pMutex) {
    155     SleepConditionVariableCS(pCond, pMutex, INFINITE);
    156 }
    157 static void test_platform_thread_cond_broadcast(test_platform_thread_cond *pCond) { WakeAllConditionVariable(pCond); }
    158 #else                  // defined(_WIN32)
    159 
    160 #error The "test_common.h" file must be modified for this OS.
    161 
    162     // NOTE: In order to support another OS, an #elif needs to be added (above the
    163     // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the
    164     // contents of this file must be created.
    165 
    166     // NOTE: Other OS-specific changes are also needed for this OS.  Search for
    167     // files with "WIN32" in it, as a quick way to find files that must be changed.
    168 
    169 #endif  // defined(_WIN32)
    170 
    171 #endif  // TEST_COMMON_H
    172