Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 7 May 2025 12:36:00 GMT
From:      Olivier Certner <olce@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 0285c2428780 - main - queue(3): Add a test for STAILQ_REVERSE()
Message-ID:  <202505071236.547Ca0XO073574@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by olce:

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

commit 0285c2428780cdb94bfb7d2dc1806a4bd129324e
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-05-05 09:50:28 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-05-07 12:34:39 +0000

    queue(3): Add a test for STAILQ_REVERSE()
    
    Reviewed by:    kib, markj
    MFC after:      1 week
    MFC with:       e8286eb29516 ("sys/queue.h: add STAILQ_REVERSE")
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D50165
---
 tests/sys/sys/queue_test.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/tests/sys/sys/queue_test.c b/tests/sys/sys/queue_test.c
index 75974ad8d792..7f8738751b85 100644
--- a/tests/sys/sys/queue_test.c
+++ b/tests/sys/sys/queue_test.c
@@ -165,6 +165,34 @@ ATF_TC_BODY(type ## _split_after_and_concat, tc)			\
 	type ## _destroy(type);						\
 }
 
+#define QUEUE_TESTS_CHECK_REVERSED(type, TYPE)				\
+/*									\
+ * Checks that some tailq/list is reversed.				\
+ */									\
+static void								\
+type ## _check_reversed(const struct type ## _ids *const type,		\
+    const u_int nb, const u_int id_shift)				\
+{									\
+	struct type ## _id_elem *e;					\
+	u_int i = 0;							\
+									\
+	TYPE ## _FOREACH(e, type, ie_entry) {				\
+		const u_int expected_id = nb - 1 - i + id_shift;	\
+									\
+		ATF_REQUIRE_MSG(i < nb,					\
+		    #type " %p has more than %u elements",		\
+		    type, nb);						\
+		ATF_REQUIRE_MSG(e->ie_id == expected_id,		\
+		    #type " %p element %p, idx %u: Found ID %u, "	\
+		    "expected %u",					\
+		    type, e, i, e->ie_id, expected_id);			\
+		++i;							\
+	}								\
+	ATF_REQUIRE_MSG(i == nb,					\
+	    #type " %p has only %u elements, expected %u",		\
+	    type, i, nb);						\
+}
+
 /*
  * Paper over the *_CONCAT() signature differences.
  */
@@ -216,11 +244,40 @@ ATF_TC_BODY(type ## _split_after_and_concat, tc)			\
  * Meat.
  */
 
+/* Common tests. */
 QUEUE_TESTS_COMMON(tailq, TAILQ);
 QUEUE_TESTS_COMMON(list, LIST);
 QUEUE_TESTS_COMMON(stailq, STAILQ);
 QUEUE_TESTS_COMMON(slist, SLIST);
 
+/* STAILQ_REVERSE(). */
+QUEUE_TESTS_CHECK_REVERSED(stailq, STAILQ);
+ATF_TC(stailq_reverse);
+ATF_TC_HEAD(stailq_reverse, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test STAILQ_REVERSE");
+}
+ATF_TC_BODY(stailq_reverse, tc)
+{
+	const u_int size = 100;
+	struct stailq_ids *const stailq = stailq_create(size, 0);
+	struct stailq_ids *const empty_stailq = stailq_create(0, 0);
+	const struct stailq_id_elem *last;
+
+	stailq_check(stailq, size, 0);
+	STAILQ_REVERSE(stailq, stailq_id_elem, ie_entry);
+	stailq_check_reversed(stailq, size, 0);
+	last = STAILQ_LAST(stailq, stailq_id_elem, ie_entry);
+	ATF_REQUIRE_MSG(last->ie_id == 0,
+	    "Last element of stailq %p has id %u, expected 0",
+	    stailq, last->ie_id);
+	stailq_destroy(stailq);
+
+	STAILQ_REVERSE(empty_stailq, stailq_id_elem, ie_entry);
+	stailq_check(empty_stailq, 0, 0);
+	stailq_destroy(empty_stailq);
+}
+
 /*
  * Main.
  */
@@ -230,6 +287,7 @@ ATF_TP_ADD_TCS(tp)
 	QUEUE_TESTS_REGISTRATION(tp, list);
 	QUEUE_TESTS_REGISTRATION(tp, stailq);
 	QUEUE_TESTS_REGISTRATION(tp, slist);
+	ATF_TP_ADD_TC(tp, stailq_reverse);
 
 	return (atf_no_error());
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202505071236.547Ca0XO073574>