git: a707b81ccb31 - main - tests: Add a buf_ring test

From: Andrew Turner <andrew_at_FreeBSD.org>
Date: Mon, 30 Sep 2024 12:23:43 UTC
The branch main has been updated by andrew:

URL: https://cgit.FreeBSD.org/src/commit/?id=a707b81ccb31e621f2e0325de150d730ed717419

commit a707b81ccb31e621f2e0325de150d730ed717419
Author:     Andrew Turner <andrew@FreeBSD.org>
AuthorDate: 2024-09-27 15:13:25 +0000
Commit:     Andrew Turner <andrew@FreeBSD.org>
CommitDate: 2024-09-30 12:04:23 +0000

    tests: Add a buf_ring test
    
    Add a simple test for sys/buf_ring.h. This enqueues and dequeues items
    in the buf_ring to ensure the simple operations are correct.
    
    There are no checks for multiple threads operating on buf_ring_enqueue
    or buf_ring_dequeue_mc as they can be probabilistic and depend on the
    architecture memory model.
    
    Reviewed by:    markj
    Sponsored by:   Arm Ltd
    Differential Revision:  https://reviews.freebsd.org/D46147
---
 tests/sys/sys/Makefile        |   1 +
 tests/sys/sys/buf_ring_test.c | 126 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 127 insertions(+)

diff --git a/tests/sys/sys/Makefile b/tests/sys/sys/Makefile
index 8d9cd552d9d2..40060911856f 100644
--- a/tests/sys/sys/Makefile
+++ b/tests/sys/sys/Makefile
@@ -5,6 +5,7 @@ TESTSDIR=	${TESTSBASE}/sys/sys
 ATF_TESTS_C=	arb_test \
 		bitset_test \
 		bitstring_test \
+		buf_ring_test \
 		qmath_test \
 		rb_test \
 		splay_test \
diff --git a/tests/sys/sys/buf_ring_test.c b/tests/sys/sys/buf_ring_test.c
new file mode 100644
index 000000000000..5ee1b1cc094b
--- /dev/null
+++ b/tests/sys/sys/buf_ring_test.c
@@ -0,0 +1,126 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2024 Arm Ltd
+ */
+
+#include <sys/types.h>
+
+#include <machine/cpu.h>
+#include <machine/cpufunc.h>
+
+#include <errno.h>
+
+#include <atf-c.h>
+
+static void critical_enter(void);
+static void critical_exit(void);
+
+#include <sys/buf_ring.h>
+
+static void
+critical_enter(void)
+{
+}
+
+static void
+critical_exit(void)
+{
+}
+
+static void *
+buf_ring_dequeue_peek(struct buf_ring *br)
+{
+	void *val;
+
+	val = buf_ring_peek(br);
+	if (val != NULL)
+		buf_ring_advance_sc(br);
+	return (val);
+}
+
+static void *
+buf_ring_dequeue_peek_clear_sc(struct buf_ring *br)
+{
+	void *val;
+
+	val = buf_ring_peek_clear_sc(br);
+	if (val != NULL)
+		buf_ring_advance_sc(br);
+	return (val);
+}
+
+#define	MC_SC_TEST(dequeue_func)					\
+ATF_TC_WITHOUT_HEAD(dequeue_func);					\
+ATF_TC_BODY(dequeue_func, tc)						\
+{									\
+	struct buf_ring *br;						\
+									\
+	br = buf_ring_alloc(4);						\
+	ATF_REQUIRE_MSG(br != NULL, "buf_ring_alloc returned NULL");	\
+									\
+	ATF_REQUIRE(dequeue_func(br) == NULL);				\
+	ATF_REQUIRE(buf_ring_count(br) == 0);				\
+	ATF_REQUIRE(!buf_ring_full(br));				\
+	ATF_REQUIRE(buf_ring_empty(br));				\
+									\
+	/* Try filling the buf_ring */					\
+	ATF_REQUIRE(buf_ring_enqueue(br, (void *)1) == 0);		\
+	ATF_REQUIRE(buf_ring_enqueue(br, (void *)2) == 0);		\
+	ATF_REQUIRE(buf_ring_enqueue(br, (void *)3) == 0);		\
+	ATF_REQUIRE(buf_ring_enqueue(br, (void *)4) == ENOBUFS);	\
+									\
+	ATF_REQUIRE(buf_ring_count(br) == 3);				\
+	ATF_REQUIRE(buf_ring_full(br));					\
+	ATF_REQUIRE(!buf_ring_empty(br));				\
+									\
+	/* Partially empty it */					\
+	ATF_REQUIRE(dequeue_func(br) == (void *)1);			\
+	ATF_REQUIRE(dequeue_func(br) == (void *)2);			\
+									\
+	ATF_REQUIRE(buf_ring_count(br) == 1);				\
+	ATF_REQUIRE(!buf_ring_full(br));				\
+	ATF_REQUIRE(!buf_ring_empty(br));				\
+									\
+	/* Add more items */						\
+	ATF_REQUIRE(buf_ring_enqueue(br, (void *)5) == 0);		\
+	ATF_REQUIRE(buf_ring_count(br) == 2);				\
+									\
+	/* Finish emptying it */					\
+	ATF_REQUIRE(dequeue_func(br) == (void *)3);			\
+	ATF_REQUIRE(dequeue_func(br) == (void *)5);			\
+	ATF_REQUIRE(dequeue_func(br) == NULL);				\
+									\
+	ATF_REQUIRE(buf_ring_count(br) == 0);				\
+	ATF_REQUIRE(!buf_ring_full(br));				\
+	ATF_REQUIRE(buf_ring_empty(br));				\
+									\
+	for (uintptr_t i = 0; i < 8; i++) {				\
+		ATF_REQUIRE(buf_ring_enqueue(br, (void *)(i + 100)) == 0);  \
+		ATF_REQUIRE(buf_ring_enqueue(br, (void *)(i + 200)) == 0);  \
+		ATF_REQUIRE(buf_ring_enqueue(br, (void *)(i + 300)) == 0);  \
+		ATF_REQUIRE(buf_ring_count(br) == 3);			\
+		ATF_REQUIRE(dequeue_func(br) == (void *)(i + 100));	\
+		ATF_REQUIRE(dequeue_func(br) == (void *)(i + 200));	\
+		ATF_REQUIRE(dequeue_func(br) == (void *)(i + 300));	\
+									\
+		ATF_REQUIRE(!buf_ring_full(br));			\
+		ATF_REQUIRE(buf_ring_empty(br));			\
+	}								\
+									\
+	buf_ring_free(br);						\
+}
+
+MC_SC_TEST(buf_ring_dequeue_sc)
+MC_SC_TEST(buf_ring_dequeue_mc)
+MC_SC_TEST(buf_ring_dequeue_peek)
+MC_SC_TEST(buf_ring_dequeue_peek_clear_sc)
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, buf_ring_dequeue_sc);
+	ATF_TP_ADD_TC(tp, buf_ring_dequeue_mc);
+	ATF_TP_ADD_TC(tp, buf_ring_dequeue_peek);
+	ATF_TP_ADD_TC(tp, buf_ring_dequeue_peek_clear_sc);
+	return (atf_no_error());
+}