Re: [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure

2024-10-11 Thread David Gow
On Wed, 4 Sept 2024 at 05:38, Diego Vieira
 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-k...@kernel.org/
>
> Signed-off-by: Diego Vieira 
> ---

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 

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 ..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 
> + */
> +#include 
> +
> +#include 
> +
> +#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);
> +}
> +
>

Re: [PATCH v2 1/1] lib/tests/kfifo_kunit.c: add tests for the kfifo structure

2024-10-01 Thread Rae Moar
On Tue, Sep 3, 2024 at 5:38 PM Diego Vieira
 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-k...@kernel.org/
>
> Signed-off-by: Diego Vieira 

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 ..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 
> + */
> +#include 
> +
> +#include 
> +
> +#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