From 295af777e4c251559f15502e6082f540a7a0f325 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 22 Sep 2011 21:55:43 -0400 Subject: [PATCH] Add trivial tests for GMutex and GRecMutex Not testing any mutual exclusion with threads yet, just basic api use. This is already enough to reveal g_rec_mutex_trylock as broken... --- glib/tests/Makefile.am | 6 +++++ glib/tests/mutex.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ glib/tests/rec-mutex.c | 52 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 glib/tests/mutex.c create mode 100644 glib/tests/rec-mutex.c diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am index 6bb8ca3..fa7bfce 100644 --- a/glib/tests/Makefile.am +++ b/glib/tests/Makefile.am @@ -200,6 +200,12 @@ atomic_LDADD = $(progs_ldadd) TEST_PROGS += bitlock bitlock_LDADD = $(progs_ldadd) +TEST_PROGS += mutex +mutex_LDADD = $(progs_ldadd) + +TEST_PROGS += rec-mutex +rec_mutex_LDADD = $(progs_ldadd) + # some testing of gtester funcitonality XMLLINT=xmllint gtester-xmllint-check: # check testreport xml with xmllint if present diff --git a/glib/tests/mutex.c b/glib/tests/mutex.c new file mode 100644 index 0000000..27fe4e7 --- /dev/null +++ b/glib/tests/mutex.c @@ -0,0 +1,63 @@ +#include + +static void +test_mutex1 (void) +{ + GMutex mutex; + + g_mutex_init (&mutex); + g_mutex_lock (&mutex); + g_mutex_unlock (&mutex); + g_mutex_clear (&mutex); +} + +static void +test_mutex2 (void) +{ + GMutex mutex = G_MUTEX_INIT; + + g_mutex_lock (&mutex); + g_mutex_unlock (&mutex); + g_mutex_clear (&mutex); +} + +static void +test_mutex3 (void) +{ + GMutex *mutex; + + mutex = g_mutex_new (); + g_mutex_lock (mutex); + g_mutex_unlock (mutex); + g_mutex_free (mutex); +} + +static void +test_mutex4 (void) +{ + GMutex mutex = G_MUTEX_INIT; + gboolean ret; + + ret = g_mutex_trylock (&mutex); + g_assert (ret); + + ret = g_mutex_trylock (&mutex); + /* no guarantees that mutex is recursive, so could return 0 or 1 */ + + g_mutex_unlock (&mutex); + g_mutex_clear (&mutex); +} + + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/thread/mutex1", test_mutex1); + g_test_add_func ("/thread/mutex2", test_mutex2); + g_test_add_func ("/thread/mutex3", test_mutex3); + g_test_add_func ("/thread/mutex4", test_mutex4); + + return g_test_run (); +} diff --git a/glib/tests/rec-mutex.c b/glib/tests/rec-mutex.c new file mode 100644 index 0000000..2b82ceb --- /dev/null +++ b/glib/tests/rec-mutex.c @@ -0,0 +1,52 @@ +#include + +static void +test_rec_mutex1 (void) +{ + GRecMutex mutex; + + g_rec_mutex_init (&mutex); + g_rec_mutex_lock (&mutex); + g_rec_mutex_unlock (&mutex); + g_rec_mutex_clear (&mutex); +} + +static void +test_rec_mutex2 (void) +{ + GRecMutex mutex = G_REC_MUTEX_INIT; + + g_rec_mutex_lock (&mutex); + g_rec_mutex_unlock (&mutex); + g_rec_mutex_clear (&mutex); +} + +static void +test_rec_mutex3 (void) +{ + GRecMutex mutex = G_REC_MUTEX_INIT; + gboolean ret; + + ret = g_rec_mutex_trylock (&mutex); + g_assert (ret); + + ret = g_rec_mutex_trylock (&mutex); + g_assert (ret); + + g_rec_mutex_unlock (&mutex); + g_rec_mutex_clear (&mutex); + g_rec_mutex_clear (&mutex); +} + + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/thread/rec-mutex1", test_rec_mutex1); + g_test_add_func ("/thread/rec-mutex2", test_rec_mutex2); + g_test_add_func ("/thread/rec-mutex3", test_rec_mutex3); + + return g_test_run (); +} -- 2.7.4