OpenSBI Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ivan Orlov <ivan.orlov0322@gmail.com>
To: opensbi@lists.infradead.org
Subject: [PATCH 3/3] lib: tests: Add test for spinlocks
Date: Tue, 23 Apr 2024 16:52:45 +0100	[thread overview]
Message-ID: <20240423155245.14541-4-ivan.orlov0322@gmail.com> (raw)
In-Reply-To: <20240423155245.14541-1-ivan.orlov0322@gmail.com>

Implement the test which covers some of the functions from the
`riscv_locks.h` file. This test consists of 3 test cases:

1) For lock/unlock functions
2) Unsuccessful trylock (the lock was previously taken)
3) Successful trylock (the lock is free and can be taken)

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
---
 lib/sbi/tests/objects.mk         |  3 +++
 lib/sbi/tests/riscv_locks_test.c | 41 ++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100644 lib/sbi/tests/riscv_locks_test.c

diff --git a/lib/sbi/tests/objects.mk b/lib/sbi/tests/objects.mk
index ba588dc..8f27289 100644
--- a/lib/sbi/tests/objects.mk
+++ b/lib/sbi/tests/objects.mk
@@ -9,3 +9,6 @@ libsbi-objs-$(CONFIG_SBIUNIT) += tests/sbi_console_test.o
 
 carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += atomic_test_suite
 libsbi-objs-$(CONFIG_SBIUNIT) += tests/riscv_atomic_test.o
+
+carray-sbi_unit_tests-$(CONFIG_SBIUNIT) += locks_test_suite
+libsbi-objs-$(CONFIG_SBIUNIT) += tests/riscv_locks_test.o
diff --git a/lib/sbi/tests/riscv_locks_test.c b/lib/sbi/tests/riscv_locks_test.c
new file mode 100644
index 0000000..7894c4e
--- /dev/null
+++ b/lib/sbi/tests/riscv_locks_test.c
@@ -0,0 +1,41 @@
+#include <sbi/sbi_unit_test.h>
+#include <sbi/riscv_locks.h>
+
+static spinlock_t test_lock = SPIN_LOCK_INITIALIZER;
+
+static void spin_lock_test(struct sbiunit_test_case *test)
+{
+	/* We don't want to accidentally get locked */
+	SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock));
+
+	spin_lock(&test_lock);
+	SBIUNIT_EXPECT(test, spin_lock_check(&test_lock));
+	spin_unlock(&test_lock);
+
+	SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock));
+}
+
+static void spin_trylock_fail(struct sbiunit_test_case *test)
+{
+	/* We don't want to accidentally get locked */
+	SBIUNIT_ASSERT(test, !spin_lock_check(&test_lock));
+
+	spin_lock(&test_lock);
+	SBIUNIT_EXPECT(test, !spin_trylock(&test_lock));
+	spin_unlock(&test_lock);
+}
+
+static void spin_trylock_success(struct sbiunit_test_case *test)
+{
+	SBIUNIT_EXPECT(test, spin_trylock(&test_lock));
+	spin_unlock(&test_lock);
+}
+
+static struct sbiunit_test_case locks_test_cases[] = {
+	SBIUNIT_TEST_CASE(spin_lock_test),
+	SBIUNIT_TEST_CASE(spin_trylock_fail),
+	SBIUNIT_TEST_CASE(spin_trylock_success),
+	SBIUNIT_END_CASE,
+};
+
+SBIUNIT_TEST_SUITE(locks_test_suite, locks_test_cases);
-- 
2.34.1



  parent reply	other threads:[~2024-04-23 15:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-23 15:52 [PATCH 0/3] Add two tests and improve SBIUnit Ivan Orlov
2024-04-23 15:52 ` [PATCH 1/3] lib: tests: Add test suite init function Ivan Orlov
2024-05-07  6:02   ` Anup Patel
2024-04-23 15:52 ` [PATCH 2/3] lib: tests: Add test for atomic_t Ivan Orlov
2024-05-07  6:02   ` Anup Patel
2024-04-23 15:52 ` Ivan Orlov [this message]
2024-05-07  6:02   ` [PATCH 3/3] lib: tests: Add test for spinlocks Anup Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240423155245.14541-4-ivan.orlov0322@gmail.com \
    --to=ivan.orlov0322@gmail.com \
    --cc=opensbi@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox