From 1dcfceb0905b02e9c097970faf2bd6dbf7266c3f Mon Sep 17 00:00:00 2001 From: Jeffrey Altman Date: Sun, 10 Apr 2016 16:26:08 -0500 Subject: [PATCH] lib/base: add tests for mutex and rwlock Add a basic set of tests for the HEIMDAL_MUTEX and HEIMDAL_RWLOCK abstraction using both static and dynamic initialization. Change-Id: Iaeb16e5dfcf00d29be7eaa4f2e6970c4f1268fb0 --- lib/base/test_base.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/lib/base/test_base.c b/lib/base/test_base.c index d276f8fc71..eb25444cd8 100644 --- a/lib/base/test_base.c +++ b/lib/base/test_base.c @@ -89,6 +89,64 @@ test_memory(void) return 0; } +static int +test_mutex(void) +{ + HEIMDAL_MUTEX m = HEIMDAL_MUTEX_INITIALIZER; + + HEIMDAL_MUTEX_lock(&m); + HEIMDAL_MUTEX_unlock(&m); + HEIMDAL_MUTEX_destroy(&m); + + HEIMDAL_MUTEX_init(&m); + HEIMDAL_MUTEX_lock(&m); + HEIMDAL_MUTEX_unlock(&m); + HEIMDAL_MUTEX_destroy(&m); + + return 0; +} + +static int +test_rwlock(void) +{ + HEIMDAL_RWLOCK l = HEIMDAL_RWLOCK_INITIALIZER; + + HEIMDAL_RWLOCK_rdlock(&l); + if (HEIMDAL_RWLOCK_trywrlock(&l)) + abort(); + HEIMDAL_RWLOCK_unlock(&l); + HEIMDAL_RWLOCK_wrlock(&l); + if (HEIMDAL_RWLOCK_tryrdlock(&l)) + abort(); + HEIMDAL_RWLOCK_unlock(&l); + if (!HEIMDAL_RWLOCK_trywrlock(&l)) + abort(); + HEIMDAL_RWLOCK_unlock(&l); + if (!HEIMDAL_RWLOCK_tryrdlock(&l)) + abort(); + HEIMDAL_RWLOCK_unlock(&l); + HEIMDAL_RWLOCK_destroy(&l); + + HEIMDAL_RWLOCK_init(&l); + HEIMDAL_RWLOCK_rdlock(&l); + if (HEIMDAL_RWLOCK_trywrlock(&l)) + abort(); + HEIMDAL_RWLOCK_unlock(&l); + HEIMDAL_RWLOCK_wrlock(&l); + if (HEIMDAL_RWLOCK_tryrdlock(&l)) + abort(); + HEIMDAL_RWLOCK_unlock(&l); + if (!HEIMDAL_RWLOCK_trywrlock(&l)) + abort(); + HEIMDAL_RWLOCK_unlock(&l); + if (!HEIMDAL_RWLOCK_tryrdlock(&l)) + abort(); + HEIMDAL_RWLOCK_unlock(&l); + HEIMDAL_RWLOCK_destroy(&l); + + return 0; +} + static int test_dict(void) { @@ -894,6 +952,8 @@ main(int argc, char **argv) int res = 0; res |= test_memory(); + res |= test_mutex(); + res |= test_rwlock(); res |= test_dict(); res |= test_auto_release(); res |= test_string();