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...
This commit is contained in:
Matthias Clasen 2011-09-22 21:55:43 -04:00
parent 9958909fb2
commit 295af777e4
3 changed files with 121 additions and 0 deletions

View File

@ -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

63
glib/tests/mutex.c Normal file
View File

@ -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 ();
}

52
glib/tests/rec-mutex.c Normal file
View File

@ -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 ();
}