diff mbox series

[v2,2/3] list: test: Add a test for list_is_head()

Message ID 20220208040122.695258-2-davidgow@google.com (mailing list archive)
State New
Headers show
Series [v2,1/3] list: test: Add test for list_del_init_careful() | expand

Commit Message

David Gow Feb. 8, 2022, 4:01 a.m. UTC
list_is_head() was added recently[1], and didn't have a KUnit test. The
implementation is trivial, so it's not a particularly exciting test, but
it'd be nice to get back to full coverage of the list functions.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/list.h?id=0425473037db40d9e322631f2d4dc6ef51f97e88

Signed-off-by: David Gow <davidgow@google.com>
---

Changes since v1:
https://lore.kernel.org/linux-kselftest/20220205061539.273330-2-davidgow@google.com/
- Test both non-head elements of the same list and head elements of
  different lists.

---
 lib/list-test.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Daniel Latypov Feb. 8, 2022, 8:40 p.m. UTC | #1
On Mon, Feb 7, 2022 at 8:02 PM David Gow <davidgow@google.com> wrote:
>
> list_is_head() was added recently[1], and didn't have a KUnit test. The
> implementation is trivial, so it's not a particularly exciting test, but
> it'd be nice to get back to full coverage of the list functions.
>
> [1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/list.h?id=0425473037db40d9e322631f2d4dc6ef51f97e88
>
> Signed-off-by: David Gow <davidgow@google.com>

Acked-by: Daniel Latypov <dlatypov@google.com>

One very optional suggestion below.

> ---
>
> Changes since v1:
> https://lore.kernel.org/linux-kselftest/20220205061539.273330-2-davidgow@google.com/
> - Test both non-head elements of the same list and head elements of
>   different lists.
>
> ---
>  lib/list-test.c | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
>
> diff --git a/lib/list-test.c b/lib/list-test.c
> index 976e9ae1f3c5..1960615d1a9f 100644
> --- a/lib/list-test.c
> +++ b/lib/list-test.c
> @@ -252,6 +252,23 @@ static void list_test_list_bulk_move_tail(struct kunit *test)
>         KUNIT_EXPECT_EQ(test, i, 2);
>  }
>
> +static void list_test_list_is_head(struct kunit *test)
> +{
> +       struct list_head a, b, c;
> +
> +       /* Two lists: [a] -> b, [c] */
> +       INIT_LIST_HEAD(&a);
> +       INIT_LIST_HEAD(&c);
> +       list_add_tail(&b, &a);
> +
> +       KUNIT_EXPECT_TRUE(test, list_is_head(&a, &a));
> +       /* Non-head element of same list */
> +       KUNIT_EXPECT_FALSE(test, list_is_head(&a, &b));
> +       /* Head element of different list */
> +       KUNIT_EXPECT_FALSE(test, list_is_head(&a, &c));

very optional,
  KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &c), "Head of a
different list");
It goes over 80 char, so probably needs to be line-wrapped, and thus
doesn't reduce # of lines.

Given the simplicity of this function (checks that its args are
equal), I highly doubt it should ever fail, and so better error
messages aren't really much of a bonus.
Brendan Higgins Feb. 8, 2022, 9:46 p.m. UTC | #2
On Mon, Feb 7, 2022 at 11:02 PM David Gow <davidgow@google.com> wrote:
>
> list_is_head() was added recently[1], and didn't have a KUnit test. The
> implementation is trivial, so it's not a particularly exciting test, but
> it'd be nice to get back to full coverage of the list functions.
>
> [1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/include/linux/list.h?id=0425473037db40d9e322631f2d4dc6ef51f97e88
>
> Signed-off-by: David Gow <davidgow@google.com>

Acked-by: Brendan Higgins <brendanhiggins@google.com>
diff mbox series

Patch

diff --git a/lib/list-test.c b/lib/list-test.c
index 976e9ae1f3c5..1960615d1a9f 100644
--- a/lib/list-test.c
+++ b/lib/list-test.c
@@ -252,6 +252,23 @@  static void list_test_list_bulk_move_tail(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, i, 2);
 }
 
+static void list_test_list_is_head(struct kunit *test)
+{
+	struct list_head a, b, c;
+
+	/* Two lists: [a] -> b, [c] */
+	INIT_LIST_HEAD(&a);
+	INIT_LIST_HEAD(&c);
+	list_add_tail(&b, &a);
+
+	KUNIT_EXPECT_TRUE(test, list_is_head(&a, &a));
+	/* Non-head element of same list */
+	KUNIT_EXPECT_FALSE(test, list_is_head(&a, &b));
+	/* Head element of different list */
+	KUNIT_EXPECT_FALSE(test, list_is_head(&a, &c));
+}
+
+
 static void list_test_list_is_first(struct kunit *test)
 {
 	struct list_head a, b;
@@ -729,6 +746,7 @@  static struct kunit_case list_test_cases[] = {
 	KUNIT_CASE(list_test_list_move),
 	KUNIT_CASE(list_test_list_move_tail),
 	KUNIT_CASE(list_test_list_bulk_move_tail),
+	KUNIT_CASE(list_test_list_is_head),
 	KUNIT_CASE(list_test_list_is_first),
 	KUNIT_CASE(list_test_list_is_last),
 	KUNIT_CASE(list_test_list_empty),