Add basic tests for GRWLock
authorMatthias Clasen <mclasen@redhat.com>
Fri, 23 Sep 2011 02:43:29 +0000 (22:43 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 23 Sep 2011 02:45:47 +0000 (22:45 -0400)
Just basic api usage, no functional test cases yet.

glib/tests/Makefile.am
glib/tests/rwlock.c [new file with mode: 0644]

index fa7bfce..6a9ca57 100644 (file)
@@ -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 (file)
index 0000000..95a1dd1
--- /dev/null
@@ -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 <glib.h>
+
+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 ();
+}