mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] kfifo: extend KUnit coverage
@ 2026-12-02  3:17 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 ` [PATCH 2/2] kfifo: add tests for kfifo_avail and full-FIFO behavior Felix Huang via B4 Relay
  0 siblings, 2 replies; 3+ messages in thread
From: Felix Huang via B4 Relay @ 2026-12-02  3:17 UTC (permalink / raw)
  To: linux-kernel, Felix Huang

The first patch cleans up the existing kfifo KUnit tests without
changing their behavior.

The second patch checks that kfifo_avail() reports the expected free
space before and after a write and after a reset. It also checks that
a full FIFO rejects a write and accepts one after an element is read.

Signed-off-by: Felix Huang <akafelixab@gmail.com>
---
Felix Huang (2):
      kfifo: clean up KUnit test style
      kfifo: add tests for kfifo_avail and full-FIFO behavior

 lib/tests/kfifo_kunit.c | 97 ++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 76 insertions(+), 21 deletions(-)
---
base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
change-id: 20261201-kfifo-kunit-tests-9bdab23b598c

Best regards,
--  
Felix Huang <akafelixab@gmail.com>



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] kfifo: clean up KUnit test style
  2026-12-02  3:17 [PATCH 0/2] kfifo: extend KUnit coverage Felix Huang via B4 Relay
@ 2026-12-02  3:17 ` Felix Huang via B4 Relay
  2026-12-02  3:17 ` [PATCH 2/2] kfifo: add tests for kfifo_avail and full-FIFO behavior Felix Huang via B4 Relay
  1 sibling, 0 replies; 3+ messages in thread
From: Felix Huang via B4 Relay @ 2026-12-02  3:17 UTC (permalink / raw)
  To: linux-kernel, Felix Huang

From: Felix Huang <akafelixab@gmail.com>

Clean up the kfifo KUnit test to follow the kernel coding style.

Replace // comments with the preferred /* */ comment style and remove
comments that merely restate the following code. Move loop counter
declarations to the beginning of their functions and use ARRAY_SIZE()
instead of hardcoded element counts.

Also fix the misspelled test name "initiliaze" and update its
corresponding KUNIT_CASE() entry.

No functional change is intended.

Signed-off-by: Felix Huang <akafelixab@gmail.com>
---
 lib/tests/kfifo_kunit.c | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)

diff --git a/lib/tests/kfifo_kunit.c b/lib/tests/kfifo_kunit.c
index a85eedc31..dbef9d2c6 100644
--- a/lib/tests/kfifo_kunit.c
+++ b/lib/tests/kfifo_kunit.c
@@ -38,12 +38,13 @@ static void kfifo_test_define_should_define_an_empty_fifo(struct kunit *test)
 static void kfifo_test_len_should_ret_n_of_stored_elements(struct kunit *test)
 {
 	u8 buffer1[N_ELEMENTS];
-
-	for (int i = 0; i < N_ELEMENTS; i++)
-		buffer1[i] = i + 1;
+	int i;
 
 	DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
 
+	for (i = 0; i < N_ELEMENTS; i++)
+		buffer1[i] = i + 1;
+
 	KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
 
 	kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
@@ -60,19 +61,20 @@ static void kfifo_test_put_should_insert_and_get_should_pop(struct kunit *test)
 {
 	u8 out_data = 0;
 	int processed_elements;
+	int i;
 	u8 elements[] = { 3, 5, 11 };
 
 	DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
 
-	// If the fifo is empty, get returns 0
+	/* An empty FIFO returns no element. */
 	processed_elements = kfifo_get(&my_fifo, &out_data);
 	KUNIT_EXPECT_EQ(test, processed_elements, 0);
 	KUNIT_EXPECT_EQ(test, out_data, 0);
 
-	for (int i = 0; i < 3; i++)
+	for (i = 0; i < ARRAY_SIZE(elements); i++)
 		kfifo_put(&my_fifo, elements[i]);
 
-	for (int i = 0; i < 3; i++) {
+	for (i = 0; i < ARRAY_SIZE(elements); i++) {
 		processed_elements = kfifo_get(&my_fifo, &out_data);
 		KUNIT_EXPECT_EQ(test, processed_elements, 1);
 		KUNIT_EXPECT_EQ(test, out_data, elements[i]);
@@ -121,7 +123,7 @@ static void kfifo_test_dec_init_should_define_an_empty_fifo(struct kunit *test)
 
 	INIT_KFIFO(my_fifo);
 
-	// my_fifo is a struct with an inplace buffer
+	/* my_fifo is a struct with an inplace buffer */
 	KUNIT_EXPECT_FALSE(test, __is_kfifo_ptr(&my_fifo));
 
 	KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo));
@@ -129,16 +131,15 @@ static void kfifo_test_dec_init_should_define_an_empty_fifo(struct kunit *test)
 
 static void kfifo_test_define_should_equal_declare_init(struct kunit *test)
 {
-	// declare a variable my_fifo of type struct kfifo of u8
 	DECLARE_KFIFO(my_fifo1, u8, KFIFO_SIZE);
-	// initialize the my_fifo variable
 	INIT_KFIFO(my_fifo1);
 
-	// DEFINE_KFIFO declares the variable with the initial value
-	// essentially the same as calling DECLARE_KFIFO and INIT_KFIFO
+	/*
+	 * DEFINE_KFIFO() both declares and initializes the FIFO, equivalent to
+	 * DECLARE_KFIFO() followed by INIT_KFIFO().
+	 */
 	DEFINE_KFIFO(my_fifo2, u8, KFIFO_SIZE);
 
-	// my_fifo1 and my_fifo2 have the same size
 	KUNIT_EXPECT_EQ(test, sizeof(my_fifo1), sizeof(my_fifo2));
 	KUNIT_EXPECT_EQ(test, kfifo_initialized(&my_fifo1),
 			kfifo_initialized(&my_fifo2));
@@ -146,22 +147,20 @@ static void kfifo_test_define_should_equal_declare_init(struct kunit *test)
 			kfifo_is_empty(&my_fifo2));
 }
 
-static void kfifo_test_alloc_should_initiliaze_a_ptr_fifo(struct kunit *test)
+static void kfifo_test_alloc_should_initialize_a_ptr_fifo(struct kunit *test)
 {
 	int ret;
 	DECLARE_KFIFO_PTR(my_fifo, u8);
 
 	INIT_KFIFO(my_fifo);
 
-	// kfifo_initialized returns false signaling the buffer pointer is NULL
+	/* kfifo_initialized returns false signaling the buffer pointer is NULL */
 	KUNIT_EXPECT_FALSE(test, kfifo_initialized(&my_fifo));
 
-	// kfifo_alloc allocates the buffer
 	ret = kfifo_alloc(&my_fifo, KFIFO_SIZE, GFP_KERNEL);
 	KUNIT_EXPECT_EQ_MSG(test, ret, 0, "Memory allocation should succeed");
 	KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo));
 
-	// kfifo_free frees the buffer
 	kfifo_free(&my_fifo);
 }
 
@@ -172,7 +171,7 @@ static void kfifo_test_peek_should_not_remove_elements(struct kunit *test)
 
 	DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
 
-	// If the fifo is empty, peek returns 0
+	/* If the fifo is empty, peek returns 0 */
 	processed_elements = kfifo_peek(&my_fifo, &out_data);
 	KUNIT_EXPECT_EQ(test, processed_elements, 0);
 
@@ -188,9 +187,11 @@ static void kfifo_test_peek_should_not_remove_elements(struct kunit *test)
 
 	KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
 
-	// Using peek doesn't remove the element
-	// so the read element and the fifo length
-	// remains the same
+	/*
+	 * Using peek doesn't remove the element
+	 * so the read element and the fifo length
+	 * remains the same
+	 */
 	processed_elements = kfifo_peek(&my_fifo, &out_data);
 	KUNIT_EXPECT_EQ(test, processed_elements, 1);
 	KUNIT_EXPECT_EQ(test, out_data, 3);
@@ -207,7 +208,7 @@ static struct kunit_case kfifo_test_cases[] = {
 	KUNIT_CASE(kfifo_test_out_should_pop_multiple_elements),
 	KUNIT_CASE(kfifo_test_dec_init_should_define_an_empty_fifo),
 	KUNIT_CASE(kfifo_test_define_should_equal_declare_init),
-	KUNIT_CASE(kfifo_test_alloc_should_initiliaze_a_ptr_fifo),
+	KUNIT_CASE(kfifo_test_alloc_should_initialize_a_ptr_fifo),
 	KUNIT_CASE(kfifo_test_peek_should_not_remove_elements),
 	{},
 };

-- 
2.56.1



^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] kfifo: add tests for kfifo_avail and full-FIFO behavior
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Felix Huang via B4 Relay @ 2026-12-02  3:17 UTC (permalink / raw)
  To: linux-kernel, Felix Huang

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02  3:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 2/2] kfifo: add tests for kfifo_avail and full-FIFO behavior Felix Huang via B4 Relay

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®