[glib] Add trivial tests for GMutex and GRecMutex
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] Add trivial tests for GMutex and GRecMutex
- Date: Fri, 23 Sep 2011 01:59:29 +0000 (UTC)
commit 295af777e4c251559f15502e6082f540a7a0f325
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Sep 22 21:55:43 2011 -0400
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(+), 0 deletions(-)
---
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 <glib.h>
+
+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 <glib.h>
+
+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 ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]