quasi-glib: add test of all defined functions

use c++ 17 for library and test
This commit is contained in:
alexey.lysiuk 2023-02-23 15:15:52 +02:00
parent a9c3840dc2
commit 40ce7088f4
2 changed files with 103 additions and 1 deletions

View file

@ -1,5 +1,12 @@
cmake_minimum_required(VERSION 3.1) cmake_minimum_required(VERSION 3.1)
project(quasi-glib) project(quasi-glib)
add_library(quasi-glib quasi-glib.cpp) add_library(quasi-glib quasi-glib.cpp)
set_property(TARGET quasi-glib PROPERTY CXX_STANDARD 11) set_property(TARGET quasi-glib PROPERTY CXX_STANDARD 17)
install(TARGETS quasi-glib) install(TARGETS quasi-glib)
add_executable(quasi-glib-test quasi-glib-test.cpp)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB REQUIRED glib-2.0)
set_property(TARGET quasi-glib-test PROPERTY CXX_STANDARD 17)
target_include_directories(quasi-glib-test PRIVATE ${GLIB_INCLUDE_DIRS})
target_link_libraries(quasi-glib-test PRIVATE quasi-glib)

View file

@ -0,0 +1,95 @@
#include <assert.h>
#include <glib.h>
constexpr int REC_MUTEX_LOCK_COUNT = 4;
struct ThreadData
{
GCond* cond;
GMutex* mutex;
GRecMutex* recMutex;
GPrivate* tls;
bool flag;
};
static void* ThreadFunc(void* arg)
{
ThreadData* threadData = reinterpret_cast<ThreadData*>(arg);
GPrivate* tls = threadData->tls;
{
assert(g_private_get(tls) == nullptr);
const gpointer TLS_REF_VALUE = gpointer(0xFEDCBA0987654321);
g_private_set(tls, TLS_REF_VALUE);
assert(g_private_get(tls) == TLS_REF_VALUE);
}
GMutex* mutex = threadData->mutex;
g_mutex_lock(mutex);
threadData->flag = true;
g_cond_signal(threadData->cond);
g_mutex_unlock(mutex);
GRecMutex* recMutex = threadData->recMutex;
g_rec_mutex_lock(recMutex);
g_rec_mutex_unlock(recMutex);
return nullptr;
}
int main()
{
g_clear_error(nullptr);
g_return_if_fail_warning("", "", "");
assert(g_file_test(nullptr, GFileTest(0)) == 0);
assert(g_get_monotonic_time() > 0);
GRecMutex recMutex;
g_rec_mutex_init(&recMutex);
for (int i = 0; i < REC_MUTEX_LOCK_COUNT; ++i)
g_rec_mutex_lock(&recMutex);
GCond cond;
g_cond_init(&cond);
GMutex mutex;
g_mutex_init(&mutex);
GPrivate tls = G_PRIVATE_INIT(nullptr);
{
assert(g_private_get(&tls) == nullptr);
const gpointer TLS_REF_VALUE = gpointer(0x1234567890ABCDEF);
g_private_set(&tls, TLS_REF_VALUE);
assert(g_private_get(&tls) == TLS_REF_VALUE);
}
ThreadData threadData = { &cond, &mutex, &recMutex, &tls, false };
GThread* thread = g_thread_try_new("", ThreadFunc, &threadData, nullptr);
assert(thread);
g_usleep(10'000);
g_mutex_lock(&mutex);
while (!threadData.flag)
g_cond_wait(&cond, &mutex);
g_mutex_unlock(&mutex);
g_cond_broadcast(&cond);
g_cond_clear(&cond);
for (int i = 0; i < REC_MUTEX_LOCK_COUNT; ++i)
g_rec_mutex_unlock(&recMutex);
g_thread_join(thread);
g_thread_unref(thread);
g_mutex_clear(&mutex);
g_rec_mutex_clear(&recMutex);
}