Skip to contentSkip to conversation

list: test: Add test for list_del_init_careful()

PATCH Superseded David Gow 2 participants 4 messages Feb 9, 2022

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

David Gow

author
#0 lore raw

PATCH v3 1/3 list: test: Add test for list_del_init_careful()

The list_del_init_careful() function was added[1] after the list KUnit test. Add a very basic test to cover it.

Note that this test only covers the single-threaded behaviour (which matches list_del_init()), as is already the case with the test for list_empty_careful().

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c6fe44d96fc1536af5b11cd859686453d1b7bfd1

---

Changes since v2:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Fix the test calling list_del_init() instead of list_del_init_careful()
- Improve the comment noting we only test single-threaded behaviour.

Changes since v1:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Patch 1/3 unchanged

---
 lib/list-test.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
lib/list-test.c +20 -0
index ee09505df16f..f82a3c7788b8 100644
@@ -161,6 +161,25 @@ static void list_test_list_del_init(struct kunit *test)
 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
 }
 
+static void list_test_list_del_init_careful(struct kunit *test)
+{
+	/* NOTE: This test only checks the behaviour of this function in
+	 * isolation. It does not verify memory model guarantees. */
+	struct list_head a, b;
+	LIST_HEAD(list);
+
+	list_add_tail(&a, &list);
+	list_add_tail(&b, &list);
+
+	/* before: [list] -> a -> b */
+	list_del_init_careful(&a);
+	/* after: [list] -> b, a initialised */
+
+	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
+	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
+	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
+}
+
 static void list_test_list_move(struct kunit *test)
 {
 	struct list_head a, b;
@@ -707,6 +726,7 @@ static struct kunit_case list_test_cases[] = {
 	KUNIT_CASE(list_test_list_replace_init),
 	KUNIT_CASE(list_test_list_swap),
 	KUNIT_CASE(list_test_list_del_init),
+	KUNIT_CASE(list_test_list_del_init_careful),
 	KUNIT_CASE(list_test_list_move),
 	KUNIT_CASE(list_test_list_move_tail),
 	KUNIT_CASE(list_test_list_bulk_move_tail),
Signature · 1 line
2.35.0.263.gb82422642f-goog
author
#1 lore raw

PATCH v3 2/3 list: test: Add a test for list_is_head()

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

---

Changes since v2:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Use the _MSG variants of the assert macros, as suggested by Daniel Latypov.
Changes since v1:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Test both non-head elements of the same list and head elements of different lists.

---
 lib/list-test.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
lib/list-test.c +19 -0
index f82a3c7788b8..19f13059439b 100644
@@ -253,6 +253,24 @@ 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_MSG(test, list_is_head(&a, &a),
+		"Head element of same list");
+	KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &b),
+		"Non-head element of same list");
+	KUNIT_EXPECT_FALSE_MSG(test, list_is_head(&a, &c),
+		"Head element of different list");
+}
+
+
 static void list_test_list_is_first(struct kunit *test)
 {
 	struct list_head a, b;
@@ -730,6 +748,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),
Signature · 1 line
2.35.0.263.gb82422642f-goog
author
#2 lore raw

PATCH v3 3/3 list: test: Add a test for list_entry_is_head()

In reply to David Gow

The list_entry_is_head() macro was added[1] after the list KUnit tests, so wasn't tested. Add a new KUnit test to complete the set.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e130816164e244b692921de49771eeb28205152d

---

Changes since v2:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Use the _MSG variants of the assert macros, as suggested by Daniel Latypov.
Changes since v1:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Rework the test entirely to better match the improved list_is_head() test.

---
 lib/list-test.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
lib/list-test.c +21 -0
index 19f13059439b..869caae14b10 100644
@@ -548,6 +548,26 @@ static void list_test_list_entry(struct kunit *test)
 				struct list_test_struct, list));
 }
 
+static void list_test_list_entry_is_head(struct kunit *test)
+{
+	struct list_test_struct test_struct1, test_struct2, test_struct3;
+
+	INIT_LIST_HEAD(&test_struct1.list);
+	INIT_LIST_HEAD(&test_struct3.list);
+
+	list_add_tail(&test_struct2.list, &test_struct1.list);
+
+	KUNIT_EXPECT_TRUE_MSG(test,
+		list_entry_is_head((&test_struct1), &test_struct1.list, list),
+		"Head element of same list");
+	KUNIT_EXPECT_FALSE_MSG(test,
+		list_entry_is_head((&test_struct2), &test_struct1.list, list),
+		"Non-head element of same list");
+	KUNIT_EXPECT_FALSE_MSG(test,
+		list_entry_is_head((&test_struct3), &test_struct1.list, list),
+		"Head element of different list");
+}
+
 static void list_test_list_first_entry(struct kunit *test)
 {
 	struct list_test_struct test_struct1, test_struct2;
@@ -763,6 +783,7 @@ static struct kunit_case list_test_cases[] = {
 	KUNIT_CASE(list_test_list_splice_init),
 	KUNIT_CASE(list_test_list_splice_tail_init),
 	KUNIT_CASE(list_test_list_entry),
+	KUNIT_CASE(list_test_list_entry_is_head),
 	KUNIT_CASE(list_test_list_first_entry),
 	KUNIT_CASE(list_test_list_last_entry),
 	KUNIT_CASE(list_test_list_first_entry_or_null),
Signature · 1 line
2.35.0.263.gb82422642f-goog
Reviewed-by on v3 1/3
#3 lore raw
In reply to David Gow
Quoted 8 lines · David Gow

The list_del_init_careful() function was added[1] after the list KUnit test. Add a very basic test to cover it.

Note that this test only covers the single-threaded behaviour (which matches list_del_init()), as is already the case with the test for list_empty_careful().

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c6fe44d96fc1536af5b11cd859686453d1b7bfd1

Now the negative tests make more sense, thanks!

to the entire series.

Quoted 57 lines

---

Changes since v2:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Fix the test calling list_del_init() instead of list_del_init_careful()
- Improve the comment noting we only test single-threaded behaviour.

Changes since v1:
https://lore.kernel.org/linux-kselftest/[email protected]/
- Patch 1/3 unchanged

---
 lib/list-test.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
lib/list-test.c +20 -0
index ee09505df16f..f82a3c7788b8 100644
@@ -161,6 +161,25 @@ static void list_test_list_del_init(struct kunit *test)
 	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
 }
 
+static void list_test_list_del_init_careful(struct kunit *test)
+{
+	/* NOTE: This test only checks the behaviour of this function in
+	 * isolation. It does not verify memory model guarantees. */
+	struct list_head a, b;
+	LIST_HEAD(list);
+
+	list_add_tail(&a, &list);
+	list_add_tail(&b, &list);
+
+	/* before: [list] -> a -> b */
+	list_del_init_careful(&a);
+	/* after: [list] -> b, a initialised */
+
+	KUNIT_EXPECT_PTR_EQ(test, list.next, &b);
+	KUNIT_EXPECT_PTR_EQ(test, b.prev, &list);
+	KUNIT_EXPECT_TRUE(test, list_empty_careful(&a));
+}
+
 static void list_test_list_move(struct kunit *test)
 {
 	struct list_head a, b;
@@ -707,6 +726,7 @@ static struct kunit_case list_test_cases[] = {
 	KUNIT_CASE(list_test_list_replace_init),
 	KUNIT_CASE(list_test_list_swap),
 	KUNIT_CASE(list_test_list_del_init),
+	KUNIT_CASE(list_test_list_del_init_careful),
 	KUNIT_CASE(list_test_list_move),
 	KUNIT_CASE(list_test_list_move_tail),
 	KUNIT_CASE(list_test_list_bulk_move_tail),
Signature · 1 line
2.35.0.263.gb82422642f-goog
Signature · 2 lines
With Best Regards,
Andy Shevchenko