From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 14/20] test: self: add simple IDR test
Date: Wed, 22 Nov 2023 18:29:45 +0100 [thread overview]
Message-ID: <20231122172951.376531-15-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20231122172951.376531-1-a.fatoum@pengutronix.de>
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@pengutronix.de>
---
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
next prev parent reply other threads:[~2023-11-22 17:42 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-22 17:29 [PATCH v2 00/20] prepare for porting OP-TEE communication support Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 01/20] include: provide linux/errno.h Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 02/20] include: add linux/refcount.h Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 03/20] bitops: split off linux/bits.h Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 04/20] include: import <linux/instruction_pointer.h> Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 05/20] asm-generic: split off typeconfused readl and friends Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 06/20] asm-generic: migrate relaxed helpers into asm-generic/io.h Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 07/20] include: add linux/io.h with strict prototypes Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 08/20] include: import Linux word-at-a-time.h Ahmad Fatoum
2023-11-23 8:16 ` [PATCH] fixup! " Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 09/20] string: implement strscpy Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 10/20] of: add CONFIG_OF for Linux compatibility Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 11/20] include: asm-generic/atomic.h: define atomic_cmpxchg Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 12/20] kbuild: build barebox for -std=gnu11 Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 13/20] include: linux/idr.h: implement more Linux API Ahmad Fatoum
2023-11-30 20:37 ` Sascha Hauer
2023-11-22 17:29 ` Ahmad Fatoum [this message]
2023-11-22 17:29 ` [PATCH v2 15/20] include: implement dev_warn_once and friends Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 16/20] include: add blocking notifier aliases Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 17/20] include: add Linux ktime API Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 18/20] of: constify string pointed to by struct of_device_id::compatible Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 19/20] of: define of_devices_ensure_probed_by_compatible Ahmad Fatoum
2023-11-22 17:29 ` [PATCH v2 20/20] include: add linux/device.h wrapper around driver.h Ahmad Fatoum
2023-11-23 14:50 ` [PATCH v2 00/20] prepare for porting OP-TEE communication support Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231122172951.376531-15-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox