[PATCH v2 14/20] test: self: add simple IDR test

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The barebox implementation for IDR is very rudimentary compared to the
kernel, consisting just of a linked list compared to Linux' radix tree.

Nevertheless, there is potential for wrong implementation, so add a
self test to verify its operation.

Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx>
---
v1 -> v2:
  - new patch
---
 test/self/Kconfig  |   5 ++
 test/self/Makefile |   1 +
 test/self/idr.c    | 119 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 125 insertions(+)
 create mode 100644 test/self/idr.c

diff --git a/test/self/Kconfig b/test/self/Kconfig
index 5850dc95973b..ace01accda7e 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -43,6 +43,7 @@ config SELFTEST_ENABLE_ALL
 	select SELFTEST_SETJMP if ARCH_HAS_SJLJ
 	select SELFTEST_REGULATOR if REGULATOR && OFDEVICE
 	select SELFTEST_TEST_COMMAND if CMD_TEST
+	select SELFTEST_IDR
 	help
 	  Selects all self-tests compatible with current configuration
 
@@ -107,4 +108,8 @@ config SELFTEST_TEST_COMMAND
 	bool "test command selftest"
 	depends on CMD_TEST
 
+config SELFTEST_IDR
+	bool "idr selftest"
+	select IDR
+
 endif
diff --git a/test/self/Makefile b/test/self/Makefile
index c9ecb459c2d3..51131474f333 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -16,6 +16,7 @@ obj-$(CONFIG_SELFTEST_STRING) += string.o
 obj-$(CONFIG_SELFTEST_SETJMP) += setjmp.o
 obj-$(CONFIG_SELFTEST_REGULATOR) += regulator.o test_regulator.dtbo.o
 obj-$(CONFIG_SELFTEST_TEST_COMMAND) += test_command.o
+obj-$(CONFIG_SELFTEST_IDR) += idr.o
 
 ifdef REGENERATE_RSATOC
 
diff --git a/test/self/idr.c b/test/self/idr.c
new file mode 100644
index 000000000000..3d23141e0f03
--- /dev/null
+++ b/test/self/idr.c
@@ -0,0 +1,119 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <printk.h>
+#include <linux/idr.h>
+#include <bselftest.h>
+
+BSELFTEST_GLOBALS();
+
+#define __expect(cond, fmt, ...) ({ \
+	bool __cond = (cond); \
+	total_tests++; \
+	\
+	if (!__cond) { \
+		failed_tests++; \
+		printf("%s failed at %s:%d " fmt "\n", \
+			#cond, __func__, __LINE__, ##__VA_ARGS__); \
+	} \
+	__cond; \
+})
+
+#define expect(ret, ...) __expect((ret), __VA_ARGS__)
+
+static int cmp[3] = { 7, 1, 2};
+static int sorted_cmp[3] = { 1, 2, 7};
+
+static int test_idr_for_each(int id, void *p, void *data)
+{
+	expect(data == &cmp[2]);
+	expect(*(int *)p == id);
+
+	return id == 1 ? 0 : -1;
+}
+
+static int count_idr(int id, void *p, void *data)
+{
+	int *count = data;
+
+	++*count;
+
+	return 0;
+}
+
+static void test_idr(void)
+{
+	void *ptr;
+	int id, count;
+
+	DEFINE_IDR(idr);
+
+	expect(idr_is_empty(&idr));
+
+	expect(!idr_find(&idr, cmp[0]));
+
+	id = idr_alloc_one(&idr, &cmp[0], cmp[0]);
+	expect(id == cmp[0]);
+
+	expect(!idr_is_empty(&idr));
+
+	ptr = idr_find(&idr, cmp[0]);
+	expect(ptr);
+	expect(ptr == &cmp[0]);
+
+	id = idr_alloc_one(&idr, &cmp[1], cmp[1]);
+	expect(id == cmp[1]);
+
+	id = idr_alloc_one(&idr, &cmp[2], cmp[2]);
+	expect(id == cmp[2]);
+
+	count = 0;
+
+	idr_for_each_entry(&idr, ptr, id) {
+		expect(id  ==  sorted_cmp[count]);
+		expect(*(int *)ptr == sorted_cmp[count]);
+
+		count++;
+
+	}
+
+	expect(count == 3);
+
+	expect(idr_for_each(&idr, test_idr_for_each, &cmp[2]) == -1);
+
+	count = 0;
+	expect(idr_for_each(&idr, count_idr, &count) == 0);
+	expect(count == 3);
+
+	idr_remove(&idr, 1);
+
+	count = 0;
+	expect(idr_for_each(&idr, count_idr, &count) == 0);
+	expect(count == 2);
+
+	idr_remove(&idr, 7);
+
+	count = 0;
+	expect(idr_for_each(&idr, count_idr, &count) == 0);
+	expect(count == 1);
+
+	idr_remove(&idr, 2);
+
+	count = 0;
+	expect(idr_for_each(&idr, count_idr, &count) == 0);
+	expect(count == 0);
+
+	expect(idr_is_empty(&idr));
+
+	idr_alloc_one(&idr, &cmp[0], cmp[0]);
+	idr_alloc_one(&idr, &cmp[1], cmp[1]);
+	idr_alloc_one(&idr, &cmp[2], cmp[2]);
+
+	expect(!idr_is_empty(&idr));
+
+	idr_destroy(&idr);
+
+	expect(idr_is_empty(&idr));
+}
+bselftest(core, test_idr);
-- 
2.39.2





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux