Skip to content

Commit

Permalink
lib/base: add tests for mutex and rwlock
Browse files Browse the repository at this point in the history
Add a basic set of tests for the HEIMDAL_MUTEX and HEIMDAL_RWLOCK
abstraction using both static and dynamic initialization.

Change-Id: Iaeb16e5dfcf00d29be7eaa4f2e6970c4f1268fb0
  • Loading branch information
jaltman committed Apr 10, 2016
1 parent 1953b0b commit 1dcfceb
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions lib/base/test_base.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1dcfceb

Please sign in to comment.