From: Felix Huang via B4 Relay <devnull+akafelixab.gmail.com@kernel.org>
To: linux-kernel@vger.kernel.org, Felix Huang <akafelixab@gmail.com>
Subject: [PATCH 2/2] kfifo: add tests for kfifo_avail and full-FIFO behavior
Date: Tue, 01 Dec 2026 22:17:22 -0500 [thread overview]
Message-ID: <20261201-kfifo-kunit-tests-v1-2-8d2a4ff45ad4@gmail.com> (raw)
In-Reply-To: <20261201-kfifo-kunit-tests-v1-0-8d2a4ff45ad4@gmail.com>
From: Felix Huang <akafelixab@gmail.com>
Add KUnit tests covering kfifo_avail() and the behavior of a full
FIFO.
Verify that kfifo_avail() reports the remaining writable capacity as
elements are added and removed. Also verify that a full FIFO rejects
further kfifo_put() calls and that reading an element makes space
available again.
Signed-off-by: Felix Huang <akafelixab@gmail.com>
---
lib/tests/kfifo_kunit.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/lib/tests/kfifo_kunit.c b/lib/tests/kfifo_kunit.c
index dbef9d2c6..eecc20a67 100644
--- a/lib/tests/kfifo_kunit.c
+++ b/lib/tests/kfifo_kunit.c
@@ -199,6 +199,58 @@ static void kfifo_test_peek_should_not_remove_elements(struct kunit *test)
KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
}
+static void kfifo_test_avail_tracks_free_space(struct kunit *test)
+{
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
+ KUNIT_EXPECT_EQ(test, kfifo_avail(&my_fifo), KFIFO_SIZE);
+ KUNIT_EXPECT_FALSE(test, kfifo_is_full(&my_fifo));
+
+ KUNIT_ASSERT_EQ(test, kfifo_put(&my_fifo, 0), 1);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 1);
+ KUNIT_EXPECT_EQ(test, kfifo_avail(&my_fifo), KFIFO_SIZE - 1);
+ KUNIT_EXPECT_FALSE(test, kfifo_is_full(&my_fifo));
+
+ kfifo_reset(&my_fifo);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
+ KUNIT_EXPECT_EQ(test, kfifo_avail(&my_fifo), KFIFO_SIZE);
+ KUNIT_EXPECT_FALSE(test, kfifo_is_full(&my_fifo));
+}
+
+static void kfifo_test_full_fifo_rejects_put(struct kunit *test)
+{
+ u8 value;
+
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ for (int i = 0; i < KFIFO_SIZE; i++)
+ kfifo_put(&my_fifo, i);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), KFIFO_SIZE);
+ KUNIT_EXPECT_EQ(test, kfifo_avail(&my_fifo), 0);
+ KUNIT_EXPECT_TRUE(test, kfifo_is_full(&my_fifo));
+
+ KUNIT_EXPECT_EQ(test, kfifo_put(&my_fifo, U8_MAX), 0);
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), KFIFO_SIZE);
+ KUNIT_EXPECT_EQ(test, kfifo_avail(&my_fifo), 0);
+
+ KUNIT_ASSERT_EQ(test, kfifo_get(&my_fifo, &value), 1);
+ KUNIT_EXPECT_EQ(test, value, 0);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), KFIFO_SIZE - 1);
+ KUNIT_EXPECT_EQ(test, kfifo_avail(&my_fifo), 1);
+ KUNIT_EXPECT_FALSE(test, kfifo_is_full(&my_fifo));
+
+ KUNIT_EXPECT_EQ(test, kfifo_put(&my_fifo, U8_MAX), 1);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), KFIFO_SIZE);
+ KUNIT_EXPECT_EQ(test, kfifo_avail(&my_fifo), 0);
+ KUNIT_EXPECT_TRUE(test, kfifo_is_full(&my_fifo));
+}
+
static struct kunit_case kfifo_test_cases[] = {
KUNIT_CASE(kfifo_test_reset_should_clear_the_fifo),
KUNIT_CASE(kfifo_test_define_should_define_an_empty_fifo),
@@ -210,6 +262,8 @@ static struct kunit_case kfifo_test_cases[] = {
KUNIT_CASE(kfifo_test_define_should_equal_declare_init),
KUNIT_CASE(kfifo_test_alloc_should_initialize_a_ptr_fifo),
KUNIT_CASE(kfifo_test_peek_should_not_remove_elements),
+ KUNIT_CASE(kfifo_test_avail_tracks_free_space),
+ KUNIT_CASE(kfifo_test_full_fifo_rejects_put),
{},
};
--
2.56.1
prev parent reply other threads:[~2026-12-02 3:15 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 3:17 [PATCH 0/2] kfifo: extend KUnit coverage Felix Huang via B4 Relay
2026-12-02 3:17 ` [PATCH 1/2] kfifo: clean up KUnit test style Felix Huang via B4 Relay
2026-12-02 3:17 ` Felix Huang via B4 Relay [this message]
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=20261201-kfifo-kunit-tests-v1-2-8d2a4ff45ad4@gmail.com \
--to=devnull+akafelixab.gmail.com@kernel.org \
--cc=akafelixab@gmail.com \
--cc=linux-kernel@vger.kernel.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
all inboxes | Powered by JetHome®