* [PATCH v2 0/1] Add KUnit tests for kfifo
@ 2024-09-03 21:36 Diego Vieira
2024-09-03 21:36 ` [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure Diego Vieira
2024-09-21 23:15 ` [PATCH v2 0/1] Add KUnit tests for kfifo Vinicius Peixoto
0 siblings, 2 replies; 6+ messages in thread
From: Diego Vieira @ 2024-09-03 21:36 UTC (permalink / raw)
To: Andrew Morton, linux-kernel, Brendan Higgins, David Gow, Rae Moar,
linux-kselftest, kunit-dev
Cc: n, andrealmeid, vinicius, diego.daniel.professional
Hi all,
This is part of a hackathon organized by LKCAMP [1], focused on writing
tests using KUnit. We reached out a while ago asking for advice on what would
be a useful contribution [2] and ended up choosing data structures that did
not yet have tests.
This patch series depends on the patch that moves the KUnit tests on lib/
into lib/tests/ [3].
This patch adds tests for the kfifo data structure, defined in
include/linux/kfifo.h, and is inspired by the KUnit tests for the doubly
linked list in lib/tests/list-test.c (previously at lib/list-test.c) [4].
[1] https://lkcamp.dev/about/
[2] https://lore.kernel.org/all/Zktnt7rjKryTh9-N@arch/
[3] https://lore.kernel.org/all/20240720181025.work.002-kees@kernel.org/
[4] https://elixir.bootlin.com/linux/latest/source/lib/list-test.c
---
Changes in v2:
- Add MODULE_DESCRIPTION()
- Move the tests from lib/kfifo-test.c to lib/tests/kfifo_kunit.c
Diego Vieira (1):
lib/tests/kfifo_kunit.c: add tests for the kfifo structure
lib/Kconfig.debug | 14 +++
lib/tests/Makefile | 1 +
lib/tests/kfifo_kunit.c | 224 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 239 insertions(+)
create mode 100644 lib/tests/kfifo_kunit.c
--
2.34.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure
2024-09-03 21:36 [PATCH v2 0/1] Add KUnit tests for kfifo Diego Vieira
@ 2024-09-03 21:36 ` Diego Vieira
2024-10-01 20:45 ` Rae Moar
2024-10-11 7:22 ` David Gow
2024-09-21 23:15 ` [PATCH v2 0/1] Add KUnit tests for kfifo Vinicius Peixoto
1 sibling, 2 replies; 6+ messages in thread
From: Diego Vieira @ 2024-09-03 21:36 UTC (permalink / raw)
To: Andrew Morton, linux-kernel, Brendan Higgins, David Gow, Rae Moar,
linux-kselftest, kunit-dev
Cc: n, andrealmeid, vinicius, diego.daniel.professional
Add KUnit tests for the kfifo data structure.
They test the vast majority of macros defined in the kfifo
header (include/linux/kfifo.h).
These are inspired by the existing tests for the doubly
linked list in lib/tests/list-test.c (previously at lib/list-test.c) [1].
Note that this patch depends on the patch that moves the KUnit tests on
lib/ into lib/tests/ [2].
[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/list-test.c?h=v6.11-rc6
[2] https://lore.kernel.org/all/20240720181025.work.002-kees@kernel.org/
Signed-off-by: Diego Vieira <diego.daniel.professional@gmail.com>
---
lib/Kconfig.debug | 14 +++
lib/tests/Makefile | 1 +
lib/tests/kfifo_kunit.c | 224 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 239 insertions(+)
create mode 100644 lib/tests/kfifo_kunit.c
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 59b6765d86b8..d7a4b996d731 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2646,6 +2646,20 @@ config SYSCTL_KUNIT_TEST
If unsure, say N.
+config KFIFO_KUNIT_TEST
+ tristate "KUnit Test for the generic kernel FIFO implementation" if !KUNIT_ALL_TESTS
+ depends on KUNIT
+ default KUNIT_ALL_TESTS
+ help
+ This builds the generic FIFO implementation KUnit test suite.
+ It tests that the API and basic functionality of the kfifo type
+ and associated macros.
+
+ For more information on KUnit and unit tests in general please refer
+ to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
+
config LIST_KUNIT_TEST
tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS
depends on KUNIT
diff --git a/lib/tests/Makefile b/lib/tests/Makefile
index c6a14cc8663e..42699c7ee638 100644
--- a/lib/tests/Makefile
+++ b/lib/tests/Makefile
@@ -22,6 +22,7 @@ obj-$(CONFIG_TEST_IOV_ITER) += kunit_iov_iter.o
obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o
obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
+obj-$(CONFIG_KFIFO_KUNIT_TEST) += kfifo_kunit.o
obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
diff --git a/lib/tests/kfifo_kunit.c b/lib/tests/kfifo_kunit.c
new file mode 100644
index 000000000000..a85eedc3195a
--- /dev/null
+++ b/lib/tests/kfifo_kunit.c
@@ -0,0 +1,224 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit test for the generic kernel FIFO implementation.
+ *
+ * Copyright (C) 2024 Diego Vieira <diego.daniel.professional@gmail.com>
+ */
+#include <kunit/test.h>
+
+#include <linux/kfifo.h>
+
+#define KFIFO_SIZE 32
+#define N_ELEMENTS 5
+
+static void kfifo_test_reset_should_clear_the_fifo(struct kunit *test)
+{
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ kfifo_put(&my_fifo, 1);
+ kfifo_put(&my_fifo, 2);
+ kfifo_put(&my_fifo, 3);
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
+
+ kfifo_reset(&my_fifo);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
+ KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
+}
+
+static void kfifo_test_define_should_define_an_empty_fifo(struct kunit *test)
+{
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo));
+ KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
+}
+
+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;
+
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
+
+ kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS);
+
+ kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS * 2);
+
+ kfifo_reset(&my_fifo);
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
+}
+
+static void kfifo_test_put_should_insert_and_get_should_pop(struct kunit *test)
+{
+ u8 out_data = 0;
+ int processed_elements;
+ u8 elements[] = { 3, 5, 11 };
+
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ // If the fifo is empty, get returns 0
+ 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++)
+ kfifo_put(&my_fifo, elements[i]);
+
+ for (int i = 0; i < 3; i++) {
+ processed_elements = kfifo_get(&my_fifo, &out_data);
+ KUNIT_EXPECT_EQ(test, processed_elements, 1);
+ KUNIT_EXPECT_EQ(test, out_data, elements[i]);
+ }
+}
+
+static void kfifo_test_in_should_insert_multiple_elements(struct kunit *test)
+{
+ u8 in_buffer[] = { 11, 25, 65 };
+ u8 out_data;
+ int processed_elements;
+
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ kfifo_in(&my_fifo, in_buffer, 3);
+
+ for (int i = 0; i < 3; i++) {
+ processed_elements = kfifo_get(&my_fifo, &out_data);
+ KUNIT_EXPECT_EQ(test, processed_elements, 1);
+ KUNIT_EXPECT_EQ(test, out_data, in_buffer[i]);
+ }
+}
+
+static void kfifo_test_out_should_pop_multiple_elements(struct kunit *test)
+{
+ u8 in_buffer[] = { 11, 25, 65 };
+ u8 out_buffer[3];
+ int copied_elements;
+
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ for (int i = 0; i < 3; i++)
+ kfifo_put(&my_fifo, in_buffer[i]);
+
+ copied_elements = kfifo_out(&my_fifo, out_buffer, 3);
+ KUNIT_EXPECT_EQ(test, copied_elements, 3);
+
+ for (int i = 0; i < 3; i++)
+ KUNIT_EXPECT_EQ(test, out_buffer[i], in_buffer[i]);
+ KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
+}
+
+static void kfifo_test_dec_init_should_define_an_empty_fifo(struct kunit *test)
+{
+ DECLARE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ INIT_KFIFO(my_fifo);
+
+ // 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));
+}
+
+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(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));
+ KUNIT_EXPECT_EQ(test, kfifo_is_empty(&my_fifo1),
+ kfifo_is_empty(&my_fifo2));
+}
+
+static void kfifo_test_alloc_should_initiliaze_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
+ 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);
+}
+
+static void kfifo_test_peek_should_not_remove_elements(struct kunit *test)
+{
+ u8 out_data;
+ int processed_elements;
+
+ DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
+
+ // If the fifo is empty, peek returns 0
+ processed_elements = kfifo_peek(&my_fifo, &out_data);
+ KUNIT_EXPECT_EQ(test, processed_elements, 0);
+
+ kfifo_put(&my_fifo, 3);
+ kfifo_put(&my_fifo, 5);
+ kfifo_put(&my_fifo, 11);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
+
+ processed_elements = kfifo_peek(&my_fifo, &out_data);
+ KUNIT_EXPECT_EQ(test, processed_elements, 1);
+ KUNIT_EXPECT_EQ(test, out_data, 3);
+
+ 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
+ processed_elements = kfifo_peek(&my_fifo, &out_data);
+ KUNIT_EXPECT_EQ(test, processed_elements, 1);
+ KUNIT_EXPECT_EQ(test, out_data, 3);
+
+ KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
+}
+
+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),
+ KUNIT_CASE(kfifo_test_len_should_ret_n_of_stored_elements),
+ KUNIT_CASE(kfifo_test_put_should_insert_and_get_should_pop),
+ KUNIT_CASE(kfifo_test_in_should_insert_multiple_elements),
+ 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_peek_should_not_remove_elements),
+ {},
+};
+
+static struct kunit_suite kfifo_test_module = {
+ .name = "kfifo",
+ .test_cases = kfifo_test_cases,
+};
+
+kunit_test_suites(&kfifo_test_module);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Diego Vieira <diego.daniel.professional@gmail.com>");
+MODULE_DESCRIPTION("KUnit test for the kernel FIFO");
--
2.34.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 0/1] Add KUnit tests for kfifo
2024-09-03 21:36 [PATCH v2 0/1] Add KUnit tests for kfifo Diego Vieira
2024-09-03 21:36 ` [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure Diego Vieira
@ 2024-09-21 23:15 ` Vinicius Peixoto
2024-10-03 21:41 ` Shuah Khan
1 sibling, 1 reply; 6+ messages in thread
From: Vinicius Peixoto @ 2024-09-21 23:15 UTC (permalink / raw)
To: Diego Vieira, Andrew Morton, linux-kernel, Brendan Higgins,
David Gow, Rae Moar, linux-kselftest, kunit-dev
Cc: n, andrealmeid, vinicius, ~lkcamp/patches
Hi all,
On 9/3/24 18:36, Diego Vieira wrote:
> Hi all,
>
> This is part of a hackathon organized by LKCAMP [1], focused on writing
> tests using KUnit. We reached out a while ago asking for advice on what would
> be a useful contribution [2] and ended up choosing data structures that did
> not yet have tests.
>
> This patch series depends on the patch that moves the KUnit tests on lib/
> into lib/tests/ [3].
>
> This patch adds tests for the kfifo data structure, defined in
> include/linux/kfifo.h, and is inspired by the KUnit tests for the doubly
> linked list in lib/tests/list-test.c (previously at lib/list-test.c) [4].
>
> [1] https://lkcamp.dev/about/
> [2] https://lore.kernel.org/all/Zktnt7rjKryTh9-N@arch/
> [3] https://lore.kernel.org/all/20240720181025.work.002-kees@kernel.org/
> [4] https://elixir.bootlin.com/linux/latest/source/lib/list-test.c
>
> ---
> Changes in v2:
> - Add MODULE_DESCRIPTION()
> - Move the tests from lib/kfifo-test.c to lib/tests/kfifo_kunit.c
>
> Diego Vieira (1):
> lib/tests/kfifo_kunit.c: add tests for the kfifo structure
>
> lib/Kconfig.debug | 14 +++
> lib/tests/Makefile | 1 +
> lib/tests/kfifo_kunit.c | 224 ++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 239 insertions(+)
> create mode 100644 lib/tests/kfifo_kunit.c
>
Gentle ping, is there any chance could we get some opinions on this? :-)
I know that this patch is quite big, plus LPC just ended and people are
probably very busy, but we would really appreciate some feedback on this
one. Thanks in advance!
Vinicius
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure
2024-09-03 21:36 ` [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure Diego Vieira
@ 2024-10-01 20:45 ` Rae Moar
2024-10-11 7:22 ` David Gow
1 sibling, 0 replies; 6+ messages in thread
From: Rae Moar @ 2024-10-01 20:45 UTC (permalink / raw)
To: Diego Vieira
Cc: Andrew Morton, linux-kernel, Brendan Higgins, David Gow,
linux-kselftest, kunit-dev, n, andrealmeid, vinicius
On Tue, Sep 3, 2024 at 5:38 PM Diego Vieira
<diego.daniel.professional@gmail.com> wrote:
>
> Add KUnit tests for the kfifo data structure.
> They test the vast majority of macros defined in the kfifo
> header (include/linux/kfifo.h).
>
> These are inspired by the existing tests for the doubly
> linked list in lib/tests/list-test.c (previously at lib/list-test.c) [1].
>
> Note that this patch depends on the patch that moves the KUnit tests on
> lib/ into lib/tests/ [2].
>
> [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/list-test.c?h=v6.11-rc6
> [2] https://lore.kernel.org/all/20240720181025.work.002-kees@kernel.org/
>
> Signed-off-by: Diego Vieira <diego.daniel.professional@gmail.com>
Hello!
Thanks for creating this test suite! The suite is looking good and
passing the tests! Sorry for the delay in reviewing this. We have been
a bit busy due to LPC.
I have some comments, see below.
Thanks again!
-Rae
> ---
> lib/Kconfig.debug | 14 +++
> lib/tests/Makefile | 1 +
> lib/tests/kfifo_kunit.c | 224 ++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 239 insertions(+)
> create mode 100644 lib/tests/kfifo_kunit.c
>
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 59b6765d86b8..d7a4b996d731 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -2646,6 +2646,20 @@ config SYSCTL_KUNIT_TEST
>
> If unsure, say N.
>
> +config KFIFO_KUNIT_TEST
> + tristate "KUnit Test for the generic kernel FIFO implementation" if !KUNIT_ALL_TESTS
> + depends on KUNIT
> + default KUNIT_ALL_TESTS
> + help
> + This builds the generic FIFO implementation KUnit test suite.
> + It tests that the API and basic functionality of the kfifo type
> + and associated macros.
> +
> + For more information on KUnit and unit tests in general please refer
> + to the KUnit documentation in Documentation/dev-tools/kunit/.
> +
> + If unsure, say N.
> +
> config LIST_KUNIT_TEST
> tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS
> depends on KUNIT
> diff --git a/lib/tests/Makefile b/lib/tests/Makefile
> index c6a14cc8663e..42699c7ee638 100644
> --- a/lib/tests/Makefile
> +++ b/lib/tests/Makefile
> @@ -22,6 +22,7 @@ obj-$(CONFIG_TEST_IOV_ITER) += kunit_iov_iter.o
> obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o
> obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
> obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
> +obj-$(CONFIG_KFIFO_KUNIT_TEST) += kfifo_kunit.o
> obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
> obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
> obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
> diff --git a/lib/tests/kfifo_kunit.c b/lib/tests/kfifo_kunit.c
> new file mode 100644
> index 000000000000..a85eedc3195a
> --- /dev/null
> +++ b/lib/tests/kfifo_kunit.c
> @@ -0,0 +1,224 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * KUnit test for the generic kernel FIFO implementation.
> + *
> + * Copyright (C) 2024 Diego Vieira <diego.daniel.professional@gmail.com>
> + */
> +#include <kunit/test.h>
> +
> +#include <linux/kfifo.h>
> +
> +#define KFIFO_SIZE 32
I would prefer if we test on at least one other size of kfifo struct
if possible.
> +#define N_ELEMENTS 5
> +
> +static void kfifo_test_reset_should_clear_the_fifo(struct kunit *test)
> +{
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + kfifo_put(&my_fifo, 1);
> + kfifo_put(&my_fifo, 2);
> + kfifo_put(&my_fifo, 3);
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
> +
> + kfifo_reset(&my_fifo);
> +
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
> + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
> +}
> +
> +static void kfifo_test_define_should_define_an_empty_fifo(struct kunit *test)
So an overall comment I have is that I notice you are testing specific
situations. Above it is: "define should define an empty fifo".
However, for API test suites like this we tend to structure test cases
by method or groups of methods. So for example, one case testing the
kfifo_peek method and one case testing the initialization functions. I
think I would recommend that for your test suite because it helps to
ensure every method is being checked for correctness and I think it
would help shorten some of the more verbose test case names. As an
example for these API test suites, I recommend looking at the KUnit
list or hashtable test suites.
So this case would become kfifo_test_define and would test the
definitions and declarations of the kfifo struct. So you could combine
it with what you test in
kfifo_test_dec_init_should_define_an_empty_fifo and
kfifo_test_define_should_equal_declare_init. Or for a second example,
the case above would become kfifo_test_reset.
Finally, I would also recommend bringing the tests for
definitions/initilizations to the top of the test suite. I recommend
building from the basics and working up from there (if a complex test
crashes the kernel before simple tests are run you may never see that
the basic test case failed, pointing you to the problem).
I realize this comment requires some reworking and moving around
however, many of your methods are already somewhat structured around a
method. And then I would just recommend thinking through any edge
cases needed to test for each method.
> +{
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo));
> + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
> +}
> +
> +static void kfifo_test_len_should_ret_n_of_stored_elements(struct kunit *test)
This could be kfifo_test_len.
> +{
> + u8 buffer1[N_ELEMENTS];
> +
> + for (int i = 0; i < N_ELEMENTS; i++)
> + buffer1[i] = i + 1;
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
> +
> + kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS);
> +
> + kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS * 2);
> +
> + kfifo_reset(&my_fifo);
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
> +}
> +
> +static void kfifo_test_put_should_insert_and_get_should_pop(struct kunit *test)
This could be split into two test cases for put and get or together as
kfifo_test_put_get.
> +{
> + u8 out_data = 0;
> + int processed_elements;
> + u8 elements[] = { 3, 5, 11 };
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + // If the fifo is empty, get returns 0
> + 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++)
> + kfifo_put(&my_fifo, elements[i]);
> +
> + for (int i = 0; i < 3; i++) {
> + processed_elements = kfifo_get(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 1);
> + KUNIT_EXPECT_EQ(test, out_data, elements[i]);
> + }
> +}
> +
> +static void kfifo_test_in_should_insert_multiple_elements(struct kunit *test)
This could become kfifo_test_in.
> +{
> + u8 in_buffer[] = { 11, 25, 65 };
> + u8 out_data;
> + int processed_elements;
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + kfifo_in(&my_fifo, in_buffer, 3);
> +
> + for (int i = 0; i < 3; i++) {
> + processed_elements = kfifo_get(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 1);
> + KUNIT_EXPECT_EQ(test, out_data, in_buffer[i]);
> + }
> +}
> +
> +static void kfifo_test_out_should_pop_multiple_elements(struct kunit *test)
This could become kfifo_test_out.
> +{
> + u8 in_buffer[] = { 11, 25, 65 };
> + u8 out_buffer[3];
> + int copied_elements;
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + for (int i = 0; i < 3; i++)
> + kfifo_put(&my_fifo, in_buffer[i]);
> +
> + copied_elements = kfifo_out(&my_fifo, out_buffer, 3);
> + KUNIT_EXPECT_EQ(test, copied_elements, 3);
> +
> + for (int i = 0; i < 3; i++)
> + KUNIT_EXPECT_EQ(test, out_buffer[i], in_buffer[i]);
> + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
> +}
> +
> +static void kfifo_test_dec_init_should_define_an_empty_fifo(struct kunit *test)
> +{
> + DECLARE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + INIT_KFIFO(my_fifo);
> +
> + // 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));
> +}
> +
> +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(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));
> + KUNIT_EXPECT_EQ(test, kfifo_is_empty(&my_fifo1),
> + kfifo_is_empty(&my_fifo2));
> +}
> +
> +static void kfifo_test_alloc_should_initiliaze_a_ptr_fifo(struct kunit *test)
This could become kfifo_test_alloc or kfifo_test_declare_ptr.
> +{
> + int ret;
> + DECLARE_KFIFO_PTR(my_fifo, u8);
> +
> + INIT_KFIFO(my_fifo);
> +
> + // 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);
> +}
> +
> +static void kfifo_test_peek_should_not_remove_elements(struct kunit *test)
So this could become kfifo_test_peek.
> +{
> + u8 out_data;
> + int processed_elements;
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + // If the fifo is empty, peek returns 0
> + processed_elements = kfifo_peek(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 0);
> +
> + kfifo_put(&my_fifo, 3);
> + kfifo_put(&my_fifo, 5);
> + kfifo_put(&my_fifo, 11);
> +
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
> +
> + processed_elements = kfifo_peek(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 1);
> + KUNIT_EXPECT_EQ(test, out_data, 3);
> +
> + 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
> + processed_elements = kfifo_peek(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 1);
> + KUNIT_EXPECT_EQ(test, out_data, 3);
> +
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
> +}
> +
> +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),
> + KUNIT_CASE(kfifo_test_len_should_ret_n_of_stored_elements),
> + KUNIT_CASE(kfifo_test_put_should_insert_and_get_should_pop),
> + KUNIT_CASE(kfifo_test_in_should_insert_multiple_elements),
> + 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_peek_should_not_remove_elements),
Just a final comment here: I notice that you are missing some of the
methods from the API. I would love it if you could include a case on
the size methods (kfifo_size, esize, recsize), the other length
methods (kfifo_avail, kfifo_is_full), kfifo_skip, kfifo_peek_len, and
kfifo_out_peek.
> + {},
> +};
> +
> +static struct kunit_suite kfifo_test_module = {
> + .name = "kfifo",
> + .test_cases = kfifo_test_cases,
> +};
> +
> +kunit_test_suites(&kfifo_test_module);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Diego Vieira <diego.daniel.professional@gmail.com>");
> +MODULE_DESCRIPTION("KUnit test for the kernel FIFO");
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20240903213649.21467-2-diego.daniel.professional%40gmail.com.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 0/1] Add KUnit tests for kfifo
2024-09-21 23:15 ` [PATCH v2 0/1] Add KUnit tests for kfifo Vinicius Peixoto
@ 2024-10-03 21:41 ` Shuah Khan
0 siblings, 0 replies; 6+ messages in thread
From: Shuah Khan @ 2024-10-03 21:41 UTC (permalink / raw)
To: Vinicius Peixoto, Diego Vieira, Andrew Morton, linux-kernel,
Brendan Higgins, David Gow, Rae Moar, linux-kselftest, kunit-dev
Cc: n, andrealmeid, vinicius, ~lkcamp/patches, Shuah Khan
On 9/21/24 17:15, Vinicius Peixoto wrote:
> Hi all,
>
> On 9/3/24 18:36, Diego Vieira wrote:
>> Hi all,
>>
>> This is part of a hackathon organized by LKCAMP [1], focused on writing
>> tests using KUnit. We reached out a while ago asking for advice on what would
>> be a useful contribution [2] and ended up choosing data structures that did
>> not yet have tests.
>>
>> This patch series depends on the patch that moves the KUnit tests on lib/
>> into lib/tests/ [3].
>>
>> This patch adds tests for the kfifo data structure, defined in
>> include/linux/kfifo.h, and is inspired by the KUnit tests for the doubly
>> linked list in lib/tests/list-test.c (previously at lib/list-test.c) [4].
>>
>> [1] https://lkcamp.dev/about/
>> [2] https://lore.kernel.org/all/Zktnt7rjKryTh9-N@arch/
>> [3] https://lore.kernel.org/all/20240720181025.work.002-kees@kernel.org/
>> [4] https://elixir.bootlin.com/linux/latest/source/lib/list-test.c
>>
>> ---
>> Changes in v2:
>> - Add MODULE_DESCRIPTION()
>> - Move the tests from lib/kfifo-test.c to lib/tests/kfifo_kunit.c
>>
>> Diego Vieira (1):
>> lib/tests/kfifo_kunit.c: add tests for the kfifo structure
>>
>> lib/Kconfig.debug | 14 +++
>> lib/tests/Makefile | 1 +
>> lib/tests/kfifo_kunit.c | 224 ++++++++++++++++++++++++++++++++++++++++
>> 3 files changed, 239 insertions(+)
>> create mode 100644 lib/tests/kfifo_kunit.c
>>
>
> Gentle ping, is there any chance could we get some opinions on this? :-)
>
> I know that this patch is quite big, plus LPC just ended and people are probably very busy, but we would really appreciate some feedback on this one. Thanks in advance!
>
Which repo is this based on?
thanks,
-- Shuah
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure
2024-09-03 21:36 ` [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure Diego Vieira
2024-10-01 20:45 ` Rae Moar
@ 2024-10-11 7:22 ` David Gow
1 sibling, 0 replies; 6+ messages in thread
From: David Gow @ 2024-10-11 7:22 UTC (permalink / raw)
To: Diego Vieira
Cc: Andrew Morton, linux-kernel, Brendan Higgins, Rae Moar,
linux-kselftest, kunit-dev, n, andrealmeid, vinicius
[-- Attachment #1: Type: text/plain, Size: 11674 bytes --]
On Wed, 4 Sept 2024 at 05:38, Diego Vieira
<diego.daniel.professional@gmail.com> wrote:
>
> Add KUnit tests for the kfifo data structure.
> They test the vast majority of macros defined in the kfifo
> header (include/linux/kfifo.h).
>
> These are inspired by the existing tests for the doubly
> linked list in lib/tests/list-test.c (previously at lib/list-test.c) [1].
>
> Note that this patch depends on the patch that moves the KUnit tests on
> lib/ into lib/tests/ [2].
>
> [1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/lib/list-test.c?h=v6.11-rc6
> [2] https://lore.kernel.org/all/20240720181025.work.002-kees@kernel.org/
>
> Signed-off-by: Diego Vieira <diego.daniel.professional@gmail.com>
> ---
Hi Diego,
Sorry for the delay: as you surmised, things have been very busy.
I think this patch is pretty good as-is, though (as Rae notes) there
are some places it could be improved and/or extended. It's
nevertheless worth having even in its current state, IMO, so this is:
Reviewed-by: David Gow <davidgow@google.com>
I'd like to accept it as-is for now, though, as I'm collating and
rebasing patches for lib/ tests which depend on the renaming to get
added to mm-nonmm-unstable (so we can avoid merge conflicts). If you
want to add extra test cases (or rearrange them within the file),
those may be best suited as a follow-up patch.
Thanks,
-- David
> lib/Kconfig.debug | 14 +++
> lib/tests/Makefile | 1 +
> lib/tests/kfifo_kunit.c | 224 ++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 239 insertions(+)
> create mode 100644 lib/tests/kfifo_kunit.c
>
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 59b6765d86b8..d7a4b996d731 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -2646,6 +2646,20 @@ config SYSCTL_KUNIT_TEST
>
> If unsure, say N.
>
> +config KFIFO_KUNIT_TEST
> + tristate "KUnit Test for the generic kernel FIFO implementation" if !KUNIT_ALL_TESTS
> + depends on KUNIT
> + default KUNIT_ALL_TESTS
> + help
> + This builds the generic FIFO implementation KUnit test suite.
> + It tests that the API and basic functionality of the kfifo type
> + and associated macros.
> +
> + For more information on KUnit and unit tests in general please refer
> + to the KUnit documentation in Documentation/dev-tools/kunit/.
> +
> + If unsure, say N.
> +
> config LIST_KUNIT_TEST
> tristate "KUnit Test for Kernel Linked-list structures" if !KUNIT_ALL_TESTS
> depends on KUNIT
> diff --git a/lib/tests/Makefile b/lib/tests/Makefile
> index c6a14cc8663e..42699c7ee638 100644
> --- a/lib/tests/Makefile
> +++ b/lib/tests/Makefile
> @@ -22,6 +22,7 @@ obj-$(CONFIG_TEST_IOV_ITER) += kunit_iov_iter.o
> obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o
> obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
> obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
> +obj-$(CONFIG_KFIFO_KUNIT_TEST) += kfifo_kunit.o
> obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
> obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
> obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
> diff --git a/lib/tests/kfifo_kunit.c b/lib/tests/kfifo_kunit.c
> new file mode 100644
> index 000000000000..a85eedc3195a
> --- /dev/null
> +++ b/lib/tests/kfifo_kunit.c
> @@ -0,0 +1,224 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * KUnit test for the generic kernel FIFO implementation.
> + *
> + * Copyright (C) 2024 Diego Vieira <diego.daniel.professional@gmail.com>
> + */
> +#include <kunit/test.h>
> +
> +#include <linux/kfifo.h>
> +
> +#define KFIFO_SIZE 32
> +#define N_ELEMENTS 5
> +
> +static void kfifo_test_reset_should_clear_the_fifo(struct kunit *test)
> +{
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + kfifo_put(&my_fifo, 1);
> + kfifo_put(&my_fifo, 2);
> + kfifo_put(&my_fifo, 3);
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
> +
> + kfifo_reset(&my_fifo);
> +
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
> + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
> +}
> +
> +static void kfifo_test_define_should_define_an_empty_fifo(struct kunit *test)
> +{
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + KUNIT_EXPECT_TRUE(test, kfifo_initialized(&my_fifo));
> + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
> +}
> +
> +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;
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
> +
> + kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS);
> +
> + kfifo_in(&my_fifo, buffer1, N_ELEMENTS);
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), N_ELEMENTS * 2);
> +
> + kfifo_reset(&my_fifo);
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 0);
> +}
> +
> +static void kfifo_test_put_should_insert_and_get_should_pop(struct kunit *test)
> +{
> + u8 out_data = 0;
> + int processed_elements;
> + u8 elements[] = { 3, 5, 11 };
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + // If the fifo is empty, get returns 0
> + 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++)
> + kfifo_put(&my_fifo, elements[i]);
> +
> + for (int i = 0; i < 3; i++) {
> + processed_elements = kfifo_get(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 1);
> + KUNIT_EXPECT_EQ(test, out_data, elements[i]);
> + }
> +}
> +
> +static void kfifo_test_in_should_insert_multiple_elements(struct kunit *test)
> +{
> + u8 in_buffer[] = { 11, 25, 65 };
> + u8 out_data;
> + int processed_elements;
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + kfifo_in(&my_fifo, in_buffer, 3);
> +
> + for (int i = 0; i < 3; i++) {
> + processed_elements = kfifo_get(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 1);
> + KUNIT_EXPECT_EQ(test, out_data, in_buffer[i]);
> + }
> +}
> +
> +static void kfifo_test_out_should_pop_multiple_elements(struct kunit *test)
> +{
> + u8 in_buffer[] = { 11, 25, 65 };
> + u8 out_buffer[3];
> + int copied_elements;
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + for (int i = 0; i < 3; i++)
> + kfifo_put(&my_fifo, in_buffer[i]);
> +
> + copied_elements = kfifo_out(&my_fifo, out_buffer, 3);
> + KUNIT_EXPECT_EQ(test, copied_elements, 3);
> +
> + for (int i = 0; i < 3; i++)
> + KUNIT_EXPECT_EQ(test, out_buffer[i], in_buffer[i]);
> + KUNIT_EXPECT_TRUE(test, kfifo_is_empty(&my_fifo));
> +}
> +
> +static void kfifo_test_dec_init_should_define_an_empty_fifo(struct kunit *test)
> +{
> + DECLARE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + INIT_KFIFO(my_fifo);
> +
> + // 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));
> +}
> +
> +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(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));
> + KUNIT_EXPECT_EQ(test, kfifo_is_empty(&my_fifo1),
> + kfifo_is_empty(&my_fifo2));
> +}
> +
> +static void kfifo_test_alloc_should_initiliaze_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
> + 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);
> +}
> +
> +static void kfifo_test_peek_should_not_remove_elements(struct kunit *test)
> +{
> + u8 out_data;
> + int processed_elements;
> +
> + DEFINE_KFIFO(my_fifo, u8, KFIFO_SIZE);
> +
> + // If the fifo is empty, peek returns 0
> + processed_elements = kfifo_peek(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 0);
> +
> + kfifo_put(&my_fifo, 3);
> + kfifo_put(&my_fifo, 5);
> + kfifo_put(&my_fifo, 11);
> +
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
> +
> + processed_elements = kfifo_peek(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 1);
> + KUNIT_EXPECT_EQ(test, out_data, 3);
> +
> + 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
> + processed_elements = kfifo_peek(&my_fifo, &out_data);
> + KUNIT_EXPECT_EQ(test, processed_elements, 1);
> + KUNIT_EXPECT_EQ(test, out_data, 3);
> +
> + KUNIT_EXPECT_EQ(test, kfifo_len(&my_fifo), 3);
> +}
> +
> +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),
> + KUNIT_CASE(kfifo_test_len_should_ret_n_of_stored_elements),
> + KUNIT_CASE(kfifo_test_put_should_insert_and_get_should_pop),
> + KUNIT_CASE(kfifo_test_in_should_insert_multiple_elements),
> + 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_peek_should_not_remove_elements),
> + {},
> +};
> +
> +static struct kunit_suite kfifo_test_module = {
> + .name = "kfifo",
> + .test_cases = kfifo_test_cases,
> +};
> +
> +kunit_test_suites(&kfifo_test_module);
> +
> +MODULE_LICENSE("GPL");
> +MODULE_AUTHOR("Diego Vieira <diego.daniel.professional@gmail.com>");
> +MODULE_DESCRIPTION("KUnit test for the kernel FIFO");
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20240903213649.21467-2-diego.daniel.professional%40gmail.com.
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 5294 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-10-11 7:22 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-03 21:36 [PATCH v2 0/1] Add KUnit tests for kfifo Diego Vieira
2024-09-03 21:36 ` [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure Diego Vieira
2024-10-01 20:45 ` Rae Moar
2024-10-11 7:22 ` David Gow
2024-09-21 23:15 ` [PATCH v2 0/1] Add KUnit tests for kfifo Vinicius Peixoto
2024-10-03 21:41 ` Shuah Khan
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).