lkml.org 
[lkml]   [2013]   [Jan]   [31]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH 3/7] liblock: mutex test suite
Date
This is a rather simple and basic test suite to test common locking
issues.

Beyond tests, it also shows how to use the library.

Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
---
tools/lib/liblock/run_tests.sh | 16 ++++++++++++++++
tools/lib/liblock/tests/AA.c | 16 ++++++++++++++++
tools/lib/liblock/tests/ABBA.c | 16 ++++++++++++++++
tools/lib/liblock/tests/ABBCCA.c | 18 ++++++++++++++++++
tools/lib/liblock/tests/ABBCCDDA.c | 20 ++++++++++++++++++++
tools/lib/liblock/tests/ABCABC.c | 18 ++++++++++++++++++
tools/lib/liblock/tests/ABCDBCDA.c | 20 ++++++++++++++++++++
tools/lib/liblock/tests/ABCDBDDA.c | 20 ++++++++++++++++++++
tools/lib/liblock/tests/common.h | 11 +++++++++++
tools/lib/liblock/tests/unlock_balance.c | 15 +++++++++++++++
10 files changed, 170 insertions(+)
create mode 100755 tools/lib/liblock/run_tests.sh
create mode 100644 tools/lib/liblock/tests/AA.c
create mode 100644 tools/lib/liblock/tests/ABBA.c
create mode 100644 tools/lib/liblock/tests/ABBCCA.c
create mode 100644 tools/lib/liblock/tests/ABBCCDDA.c
create mode 100644 tools/lib/liblock/tests/ABCABC.c
create mode 100644 tools/lib/liblock/tests/ABCDBCDA.c
create mode 100644 tools/lib/liblock/tests/ABCDBDDA.c
create mode 100644 tools/lib/liblock/tests/common.h
create mode 100644 tools/lib/liblock/tests/unlock_balance.c

diff --git a/tools/lib/liblock/run_tests.sh b/tools/lib/liblock/run_tests.sh
new file mode 100755
index 0000000..bbf5281
--- /dev/null
+++ b/tools/lib/liblock/run_tests.sh
@@ -0,0 +1,16 @@
+#! /bin/bash
+
+make &> /dev/null
+
+for i in `ls tests/*.c`; do
+ testname=$(basename -s .c "$i")
+ gcc -o tests/$testname -lpthread $i liblock.a -Iinclude &> /dev/null
+ echo -ne "$testname... "
+ if [ $(timeout 1 ./tests/$testname | wc -l) -gt 0 ]; then
+ echo "PASSED!"
+ else
+ echo "FAILED!"
+ fi
+ rm tests/$testname
+done
+
diff --git a/tools/lib/liblock/tests/AA.c b/tools/lib/liblock/tests/AA.c
new file mode 100644
index 0000000..558109c
--- /dev/null
+++ b/tools/lib/liblock/tests/AA.c
@@ -0,0 +1,16 @@
+#include <liblock/mutex.h>
+
+void main(void)
+{
+ liblock_pthread_mutex_t a, b;
+
+ liblock_init();
+ liblock_set_thread();
+
+ liblock_pthread_mutex_init(&a, NULL);
+ liblock_pthread_mutex_init(&b, NULL);
+
+ liblock_pthread_mutex_lock(&a);
+ liblock_pthread_mutex_unlock(&b);
+ liblock_pthread_mutex_lock(&a);
+}
diff --git a/tools/lib/liblock/tests/ABBA.c b/tools/lib/liblock/tests/ABBA.c
new file mode 100644
index 0000000..b5070fd
--- /dev/null
+++ b/tools/lib/liblock/tests/ABBA.c
@@ -0,0 +1,16 @@
+#include <liblock/mutex.h>
+#include "common.h"
+
+void main(void)
+{
+ liblock_pthread_mutex_t a, b;
+
+ liblock_init();
+ liblock_set_thread();
+
+ liblock_pthread_mutex_init(&a, NULL);
+ liblock_pthread_mutex_init(&b, NULL);
+
+ LOCK_UNLOCK_2(a, b);
+ LOCK_UNLOCK_2(b, a);
+}
diff --git a/tools/lib/liblock/tests/ABBCCA.c b/tools/lib/liblock/tests/ABBCCA.c
new file mode 100644
index 0000000..5fc5fa2
--- /dev/null
+++ b/tools/lib/liblock/tests/ABBCCA.c
@@ -0,0 +1,18 @@
+#include <liblock/mutex.h>
+#include "common.h"
+
+void main(void)
+{
+ liblock_pthread_mutex_t a, b, c;
+
+ liblock_init();
+ liblock_set_thread();
+
+ liblock_pthread_mutex_init(&a, NULL);
+ liblock_pthread_mutex_init(&b, NULL);
+ liblock_pthread_mutex_init(&c, NULL);
+
+ LOCK_UNLOCK_2(a, b);
+ LOCK_UNLOCK_2(b, c);
+ LOCK_UNLOCK_2(c, a);
+}
diff --git a/tools/lib/liblock/tests/ABBCCDDA.c b/tools/lib/liblock/tests/ABBCCDDA.c
new file mode 100644
index 0000000..5cf05bb
--- /dev/null
+++ b/tools/lib/liblock/tests/ABBCCDDA.c
@@ -0,0 +1,20 @@
+#include <liblock/mutex.h>
+#include "common.h"
+
+void main(void)
+{
+ liblock_pthread_mutex_t a, b, c, d;
+
+ liblock_init();
+ liblock_set_thread();
+
+ liblock_pthread_mutex_init(&a, NULL);
+ liblock_pthread_mutex_init(&b, NULL);
+ liblock_pthread_mutex_init(&c, NULL);
+ liblock_pthread_mutex_init(&d, NULL);
+
+ LOCK_UNLOCK_2(a, b);
+ LOCK_UNLOCK_2(b, c);
+ LOCK_UNLOCK_2(c, d);
+ LOCK_UNLOCK_2(d, a);
+}
diff --git a/tools/lib/liblock/tests/ABCABC.c b/tools/lib/liblock/tests/ABCABC.c
new file mode 100644
index 0000000..eab71ff
--- /dev/null
+++ b/tools/lib/liblock/tests/ABCABC.c
@@ -0,0 +1,18 @@
+#include <liblock/mutex.h>
+#include "common.h"
+
+void main(void)
+{
+ liblock_pthread_mutex_t a, b, c;
+
+ liblock_init();
+ liblock_set_thread();
+
+ liblock_pthread_mutex_init(&a, NULL);
+ liblock_pthread_mutex_init(&b, NULL);
+ liblock_pthread_mutex_init(&c, NULL);
+
+ LOCK_UNLOCK_2(a, b);
+ LOCK_UNLOCK_2(c, a);
+ LOCK_UNLOCK_2(b, c);
+}
diff --git a/tools/lib/liblock/tests/ABCDBCDA.c b/tools/lib/liblock/tests/ABCDBCDA.c
new file mode 100644
index 0000000..3250f0f
--- /dev/null
+++ b/tools/lib/liblock/tests/ABCDBCDA.c
@@ -0,0 +1,20 @@
+#include <liblock/mutex.h>
+#include "common.h"
+
+void main(void)
+{
+ liblock_pthread_mutex_t a, b, c, d;
+
+ liblock_init();
+ liblock_set_thread();
+
+ liblock_pthread_mutex_init(&a, NULL);
+ liblock_pthread_mutex_init(&b, NULL);
+ liblock_pthread_mutex_init(&c, NULL);
+ liblock_pthread_mutex_init(&d, NULL);
+
+ LOCK_UNLOCK_2(a, b);
+ LOCK_UNLOCK_2(c, d);
+ LOCK_UNLOCK_2(b, c);
+ LOCK_UNLOCK_2(d, a);
+}
diff --git a/tools/lib/liblock/tests/ABCDBDDA.c b/tools/lib/liblock/tests/ABCDBDDA.c
new file mode 100644
index 0000000..b49c5d9
--- /dev/null
+++ b/tools/lib/liblock/tests/ABCDBDDA.c
@@ -0,0 +1,20 @@
+#include <liblock/mutex.h>
+#include "common.h"
+
+void main(void)
+{
+ liblock_pthread_mutex_t a, b, c, d;
+
+ liblock_init();
+ liblock_set_thread();
+
+ liblock_pthread_mutex_init(&a, NULL);
+ liblock_pthread_mutex_init(&b, NULL);
+ liblock_pthread_mutex_init(&c, NULL);
+ liblock_pthread_mutex_init(&d, NULL);
+
+ LOCK_UNLOCK_2(a, b);
+ LOCK_UNLOCK_2(c, d);
+ LOCK_UNLOCK_2(b, d);
+ LOCK_UNLOCK_2(d, a);
+}
diff --git a/tools/lib/liblock/tests/common.h b/tools/lib/liblock/tests/common.h
new file mode 100644
index 0000000..d5f6d5e
--- /dev/null
+++ b/tools/lib/liblock/tests/common.h
@@ -0,0 +1,11 @@
+#ifndef _LIBLOCK_TEST_COMMON_H
+#define _LIBLOCK_TEST_COMMON_H
+
+#define LOCK_UNLOCK_2(a, b) \
+ liblock_pthread_mutex_lock(&(a)); \
+ liblock_pthread_mutex_lock(&(b)); \
+ liblock_pthread_mutex_unlock(&(b)); \
+ liblock_pthread_mutex_unlock(&(a));
+
+
+#endif
diff --git a/tools/lib/liblock/tests/unlock_balance.c b/tools/lib/liblock/tests/unlock_balance.c
new file mode 100644
index 0000000..e68ca99
--- /dev/null
+++ b/tools/lib/liblock/tests/unlock_balance.c
@@ -0,0 +1,15 @@
+#include <liblock/mutex.h>
+
+void main(void)
+{
+ liblock_pthread_mutex_t a;
+
+ liblock_init();
+ liblock_set_thread();
+
+ liblock_pthread_mutex_init(&a, NULL);
+
+ liblock_pthread_mutex_lock(&a);
+ liblock_pthread_mutex_unlock(&a);
+ liblock_pthread_mutex_unlock(&a);
+}
--
1.8.1.1


\
 
 \ /
  Last update: 2013-02-01 06:41    [W:0.077 / U:0.480 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site