From ff139135372cb9d8d88fba19d553f6b68b3ffa37 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 22 Sep 2011 22:43:29 -0400 Subject: [PATCH] Add basic tests for GRWLock Just basic api usage, no functional test cases yet. --- glib/tests/Makefile.am | 3 ++ glib/tests/rwlock.c | 77 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 glib/tests/rwlock.c diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am index fa7bfcee3..6a9ca572f 100644 --- a/glib/tests/Makefile.am +++ b/glib/tests/Makefile.am @@ -206,6 +206,9 @@ mutex_LDADD = $(progs_ldadd) TEST_PROGS += rec-mutex rec_mutex_LDADD = $(progs_ldadd) +TEST_PROGS += rwlock +rwlock_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/rwlock.c b/glib/tests/rwlock.c new file mode 100644 index 000000000..95a1dd1db --- /dev/null +++ b/glib/tests/rwlock.c @@ -0,0 +1,77 @@ +/* Unit tests for GRWLock + * Copyright (C) 2011 Red Hat, Inc + * Author: Matthias Clasen + * + * This work is provided "as is"; redistribution and modification + * in whole or in part, in any medium, physical or electronic is + * permitted without restriction. + * + * This work is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In no event shall the authors or contributors be liable for any + * direct, indirect, incidental, special, exemplary, or consequential + * damages (including, but not limited to, procurement of substitute + * goods or services; loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether + * in contract, strict liability, or tort (including negligence or + * otherwise) arising in any way out of the use of this software, even + * if advised of the possibility of such damage. + */ + +#include + +static void +test_rwlock1 (void) +{ + GRWLock lock; + + g_rw_lock_init (&lock); + g_rw_lock_writer_lock (&lock); + g_rw_lock_writer_unlock (&lock); + g_rw_lock_writer_lock (&lock); + g_rw_lock_writer_unlock (&lock); + g_rw_lock_clear (&lock); +} + +static void +test_rwlock2 (void) +{ + GRWLock lock = G_RW_LOCK_INIT; + + g_rw_lock_writer_lock (&lock); + g_rw_lock_writer_unlock (&lock); + g_rw_lock_writer_lock (&lock); + g_rw_lock_writer_unlock (&lock); + g_rw_lock_clear (&lock); +} + +static void +test_rwlock3 (void) +{ + GRWLock lock = G_RW_LOCK_INIT; + gboolean ret; + + ret = g_rw_lock_writer_trylock (&lock); + g_assert (ret); + ret = g_rw_lock_writer_trylock (&lock); + g_assert (!ret); + + g_rw_lock_writer_unlock (&lock); + + g_rw_lock_clear (&lock); +} + + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/thread/rwlock1", test_rwlock1); + g_test_add_func ("/thread/rwlock2", test_rwlock2); + g_test_add_func ("/thread/rwlock3", test_rwlock3); + + return g_test_run (); +}