Skip to contentSkip to conversation

lib/list-test: add a test for the 'list' doubly linked list

PATCH Superseded David Gow 2 participants 2 messages Oct 22, 2019 – Oct 23, 2019

Subsystems (2)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

David Gow

author
#0 lore raw

PATCH v5 1/1 lib/list-test: add a test for the 'list' doubly linked list

Add a KUnit test for the kernel doubly linked list implementation in include/linux/list.h

Each test case (list_test_x) is focused on testing the behaviour of the list function/macro 'x'. None of the tests pass invalid lists to these macros, and so should behave identically with DEBUG_LIST enabled and disabled.

Note that, at present, it only tests the list_ types (not the singly-linked hlist_), and does not yet test all of the list_for_each_entry* macros (and some related things like list_prepare_entry).

---
v5 replaces the use of KUNIT_ASSERT_NOT_ERR_OR_NULL() -- to check the return value from kzalloc() and kmalloc() -- with the __GFP_NOFAIL arugment. (Both in the list_test_list_init test.)

Earlier versions of the test can be found:
v4: https://lore.kernel.org/linux-kselftest/[email protected]/
v3: https://lore.kernel.org/linux-kselftest/[email protected]/
v2: https://lore.kernel.org/linux-kselftest/[email protected]/
v1: https://lore.kernel.org/linux-kselftest/[email protected]/

 MAINTAINERS       |   5 +
 lib/Kconfig.debug |  18 ++
 lib/Makefile      |   3 +
 lib/list-test.c   | 738 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 764 insertions(+)
 create mode 100644 lib/list-test.c
MAINTAINERS +5 -0
index 7ef985e01457..7ced1b69a3d3 100644
@@ -9504,6 +9504,11 @@ F:	Documentation/misc-devices/lis3lv02d.rst
 F:	drivers/misc/lis3lv02d/
 F:	drivers/platform/x86/hp_accel.c
 
+LIST KUNIT TEST
+M:	David Gow <[email protected]>
+S:	Maintained
+F:	lib/list-test.c
+
 LIVE PATCHING
 M:	Josh Poimboeuf <[email protected]>
 M:	Jiri Kosina <[email protected]>
lib/Kconfig.debug +18 -0
index a3017a5dadcd..7991b78eb1f3 100644
@@ -1961,6 +1961,24 @@ config SYSCTL_KUNIT_TEST
 
 	  If unsure, say N.
 
+config LIST_KUNIT_TEST
+	bool "KUnit Test for Kernel Linked-list structures"
+	depends on KUNIT
+	help
+	  This builds the linked list KUnit test suite.
+	  It tests that the API and basic functionality of the list_head type
+	  and associated macros.
+	
+	  KUnit tests run during boot and output the results to the debug log
+	  in TAP format (http://testanything.org/). Only useful for kernel devs
+	  running the KUnit test harness, and not intended for inclusion into a
+	  production build.
+
+	  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 TEST_UDELAY
 	tristate "udelay test driver"
 	help
lib/Makefile +3 -0
index bba1fd5485f7..890e581d00c4 100644
@@ -292,3 +292,6 @@ obj-$(CONFIG_GENERIC_LIB_MULDI3) += muldi3.o
 obj-$(CONFIG_GENERIC_LIB_CMPDI2) += cmpdi2.o
 obj-$(CONFIG_GENERIC_LIB_UCMPDI2) += ucmpdi2.o
 obj-$(CONFIG_OBJAGG) += objagg.o
+
+# KUnit tests
+obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
lib/list-test.c new file +738 -0

738 lines not on this page · raw message

Signature · 1 line
2.23.0.866.gb869b98d4c-goog
Reviewed-by Tested-by on v5 1/1
#1 lore raw
Quoted 8 lines · David Gow' via KUnit Development

Add a KUnit test for the kernel doubly linked list implementation in include/linux/list.h

Each test case (list_test_x) is focused on testing the behaviour of the list function/macro 'x'. None of the tests pass invalid lists to these macros, and so should behave identically with DEBUG_LIST enabled and disabled.

Quoted 6 lines

Note that, at present, it only tests the list_ types (not the singly-linked hlist_), and does not yet test all of the list_for_each_entry* macros (and some related things like list_prepare_entry).

I think I already had a "Reviewed-by and a Tested-by" on this patch. Please make sure to apply the footers to subsequent versions of a patch in the future.

Quoted 24 lines

---
v5 replaces the use of KUNIT_ASSERT_NOT_ERR_OR_NULL() -- to check the return value from kzalloc() and kmalloc() -- with the __GFP_NOFAIL arugment. (Both in the list_test_list_init test.)

Earlier versions of the test can be found:
v4: https://lore.kernel.org/linux-kselftest/[email protected]/
v3: https://lore.kernel.org/linux-kselftest/[email protected]/
v2: https://lore.kernel.org/linux-kselftest/[email protected]/
v1: https://lore.kernel.org/linux-kselftest/[email protected]/

 MAINTAINERS       |   5 +
 lib/Kconfig.debug |  18 ++
 lib/Makefile      |   3 +
 lib/list-test.c   | 738 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 764 insertions(+)
 create mode 100644 lib/list-test.c
MAINTAINERS +0 -0
index 7ef985e01457..7ced1b69a3d3 100644

MAINTAINERS F: Documentation/misc-devices/lis3lv02d.rst · line 9510

Quoted 7 lines
MAINTAINERS +4 -0
@@ -9504,6 +9504,11 @@ F:	Documentation/misc-devices/lis3lv02d.rst
 F:	drivers/misc/lis3lv02d/
 F:	drivers/platform/x86/hp_accel.c
 
+LIST KUNIT TEST
+M:	David Gow <[email protected]>
+S:	Maintained
+F:	lib/list-test.c

Probably want to have a "mailing list" line. Something like:
"""
L: [email protected]
L: [email protected]
"""

Quoted 10 lines
MAINTAINERS +1 -0
+
 LIVE PATCHING
 M:	Josh Poimboeuf <[email protected]>
 M:	Jiri Kosina <[email protected]>
lib/Kconfig.debug +0 -0
index a3017a5dadcd..7991b78eb1f3 100644
@@ -1961,6 +1961,24 @@ config SYSCTL_KUNIT_TEST
 

lib/Kconfig.debug config SYSCTL_KUNIT_TEST · line 1971

Quoted 10 lines
lib/Kconfig.debug +8 -0
 	  If unsure, say N.
 
+config LIST_KUNIT_TEST
+	bool "KUnit Test for Kernel Linked-list structures"
+	depends on KUNIT
+	help
+	  This builds the linked list KUnit test suite.
+	  It tests that the API and basic functionality of the list_head type
+	  and associated macros.
+	

nit: unnecessary tab.

Quoted 13 lines
lib/Kconfig.debug +10 -0
+	  KUnit tests run during boot and output the results to the debug log
+	  in TAP format (http://testanything.org/). Only useful for kernel devs
+	  running the KUnit test harness, and not intended for inclusion into a
+	  production build.
+
+	  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 TEST_UDELAY
 	tristate "udelay test driver"
 	help

[...]

Quoted 5 lines
lib/list-test.c new file +0 -0
new file mode 100644
index 000000000000..a6d17647e309

[...]

lib/list-test.c line 510

Quoted 5 lines
lib/list-test.c new file +5 -0
+static void list_test_list_entry(struct kunit *test)
+{
+	struct list_test_struct test_struct;
+
+	KUNIT_EXPECT_PTR_EQ(test, &test_struct, list_entry(&(test_struct.list), struct list_test_struct, list));

nit: here and elsewhere: over 80 chars.

Quoted 13 lines
lib/list-test.c new file +13 -0
+}
+
+static void list_test_list_first_entry(struct kunit *test)
+{
+	struct list_test_struct test_struct1, test_struct2;
+	LIST_HEAD(list);
+
+	list_add_tail(&test_struct1.list, &list);
+	list_add_tail(&test_struct2.list, &list);
+
+
+	KUNIT_EXPECT_PTR_EQ(test, &test_struct1, list_first_entry(&list, struct list_test_struct, list));
+}

[...]

Quoted 8 lines
lib/list-test.c new file +8 -0
+static void list_test_list_for_each_entry(struct kunit *test)
+{
+	struct list_test_struct entries[5], *cur;
+	static LIST_HEAD(list);
+	int i = 0;
+
+	for (i = 0; i < 5; ++i) {
+		entries[i].data = i;

lib/list-test.c line 668

Quoted 10 lines
lib/list-test.c new file +10 -0
+		list_add_tail(&entries[i].list, &list);
+	}
+
+	i = 0;
+
+	list_for_each_entry(cur, &list, list) {
+		KUNIT_EXPECT_EQ(test, cur->data, i);
+		i++;
+	}
+	

nit: another unnecessary tab. Looks like you should probably run checkpatch.

Quoted 13 lines
lib/list-test.c new file +13 -0
+	KUNIT_EXPECT_EQ(test, i, 5);
+}
+
+static void list_test_list_for_each_entry_reverse(struct kunit *test)
+{
+	struct list_test_struct entries[5], *cur;
+	static LIST_HEAD(list);
+	int i = 0;
+
+	for (i = 0; i < 5; ++i) {
+		entries[i].data = i;
+		list_add_tail(&entries[i].list, &list);
+	}

lib/list-test.c line 691

Quoted 10 lines
lib/list-test.c new file +10 -0
+
+	i = 4;
+
+	list_for_each_entry_reverse(cur, &list, list) {
+		KUNIT_EXPECT_EQ(test, cur->data, i);
+		i--;
+	}
+	
+	KUNIT_EXPECT_EQ(test, i, -1);
+}

[...]

Cheers