mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Sohaib Mohamed <sohaib.amhmd@gmail.com>,
	Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH master 2/2] test: self: add range.h test
Date: Sat,  1 Nov 2025 11:55:39 +0100	[thread overview]
Message-ID: <20251101105542.3830943-2-a.fatoum@barebox.org> (raw)
In-Reply-To: <20251101105542.3830943-1-a.fatoum@barebox.org>

Due to the subtle bug in range.h breaking the MMU code, it's probably a
good idea to have some tests that exercise the corner cases of the
range.h utility functions.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 test/self/Kconfig  |   6 +++
 test/self/Makefile |   1 +
 test/self/range.c  | 105 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 test/self/range.c

diff --git a/test/self/Kconfig b/test/self/Kconfig
index fa2911fa649a..f46253afc034 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -28,6 +28,7 @@ config SELFTEST_AUTORUN
 
 config SELFTEST_ENABLE_ALL
 	bool "Enable all self-tests"
+	select SELFTEST_RANGE
 	select SELFTEST_PRINTF
 	select SELFTEST_MALLOC
 	select SELFTEST_PROGRESS_NOTIFIER
@@ -50,6 +51,11 @@ config SELFTEST_ENABLE_ALL
 	help
 	  Selects all self-tests compatible with current configuration
 
+config SELFTEST_RANGE
+	bool "range.h selftest"
+	help
+	  Tests range functions
+
 config SELFTEST_MALLOC
 	bool "malloc() selftest"
 	help
diff --git a/test/self/Makefile b/test/self/Makefile
index 3d74bf9e98ad..124607b375d8 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 obj-$(CONFIG_SELFTEST) += core.o
+obj-$(CONFIG_SELFTEST_RANGE) += range.o
 obj-$(CONFIG_SELFTEST_MALLOC) += malloc.o
 obj-$(CONFIG_SELFTEST_PRINTF) += printf.o
 CFLAGS_printf.o += -Wno-format-security -Wno-format
diff --git a/test/self/range.c b/test/self/range.c
new file mode 100644
index 000000000000..82b19239eac8
--- /dev/null
+++ b/test/self/range.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) "range: " fmt
+
+#include <common.h>
+#include <bselftest.h>
+#include <linux/types.h>
+#include <linux/sizes.h>
+#include <stdio.h>
+#include <range.h>
+
+BSELFTEST_GLOBALS();
+
+static void __expect_bool(const char *msg, bool got, bool want)
+{
+	total_tests++;
+
+	if (got != want) {
+		failed_tests++;
+		pr_err("FAIL: %s — got %d want %d\n", msg, got, want);
+	}
+}
+
+#define expect_bool(got, want, type, a, b, c, d) do {		\
+	char _buf[256];						\
+	snprintf(_buf, sizeof(_buf), "%d: " type		\
+		 " [%#llx, %#llx] vs [%#llx, %#llx]", __LINE__,	\
+		 (u64)(a), (u64)(b), (u64)(c), (u64)(d));	\
+	__expect_bool(_buf, got, want);				\
+} while (0)
+
+#define TEST_INCL(a, b, c, d, expect)				\
+	expect_bool(region_overlap_end_inclusive(a, b, c, d),	\
+		    expect, "incl", a, b, c, d);
+
+#define TEST_EXCL(a, b, c, d, expect)				\
+	expect_bool(region_overlap_end_exclusive(a, b, c, d),	\
+		    expect, "excl", a, b, c, d);
+
+#define TEST_SIZE(a, lena, b, lenb, expect) \
+	expect_bool(region_overlap_size(a, lena, b, lenb),	\
+		    expect, "size", a, lena, b, lenb)
+
+static void test_region_overlap(void)
+{
+	TEST_INCL(0u, 5u, 5u, 10u, true);
+	TEST_INCL(0u, 4u, 5u, 10u, false);
+	TEST_INCL(10u, 20u, 12u, 15u, true);
+	TEST_INCL(20u, 10u, 12u, 15u, false);
+
+	TEST_EXCL(0ull, 5ull, 5ull, 10ull, false);
+	TEST_EXCL(0u, 6u, 5u, 10u, true);
+	TEST_EXCL(5u, 5u, 5u, 10u, false);
+	TEST_EXCL(5u, 10u, 7u, 7u, false);
+	TEST_EXCL(3u, 8u, 3u, 8u, true);
+	TEST_EXCL(0u, 0u, 0u, 1u, true);
+	TEST_EXCL(0xFFFFFF00, 0xFFFFFF10,
+		  0xFFFFF000, 0xFFFFFFF0, true);
+	TEST_EXCL(0u, 1u, 1u, 1u, false);
+
+	TEST_SIZE(0u, 0u, 0u, 10u, false);
+	TEST_SIZE(0u, 10u, 5u, 0u, false);
+	TEST_SIZE(0u, 5u, 5u, 5u, false);
+	TEST_SIZE(0u, 6u, 5u, 5u, true);
+	TEST_SIZE(0u, U64_MAX, 123u, 1u, true);
+
+	TEST_EXCL(0u, 0u, 0u, 0u, true);
+	TEST_EXCL(0ull, 0ull, 0ull, 0ull, true);
+
+
+	TEST_EXCL(0xF0000000u, 0u,
+		  (u32)(SZ_4G + SZ_1G), (u32)(SZ_4G + SZ_2G), false);
+	TEST_EXCL(0xF0000000ul, 0ul,
+		  (ulong)(SZ_4G + SZ_1G), (ulong)(SZ_4G + SZ_2G),
+		  sizeof(ulong) == sizeof(u64));
+	TEST_EXCL(0xF0000000ull, 0ull,
+		  SZ_4G + SZ_1G, SZ_4G + SZ_2G, true);
+
+	TEST_EXCL(1u, 1u, 1u, 1u, false);
+
+	TEST_EXCL(0xFFFFFFFEu, 0u, 0u, 1u, false);
+
+	TEST_EXCL(U32_MAX / 2, U32_MAX + 1,
+		  U32_MAX / 2 + 0x10, U32_MAX / 2 + 0x100, true);
+
+	TEST_SIZE(1u, U64_MAX, 1u, 1u, true);
+	TEST_SIZE(1u, U64_MAX, 10u, 10u, true);
+
+	TEST_SIZE(0xFFFFFFFFFFFFFFE0ull, 0x20ull,
+		  0xFFFFFFFFFFFFFFFFull, 0x0ull, false);
+
+	TEST_SIZE(0xFFFFFFFFFFFFFFE0ull, 0x20ull,
+		  0xFFFFFFFFFFFFFFFFull, 0x0ull, false);
+
+	TEST_SIZE(0xFFFFFFFFFFFFFFE0ull, 0x20ull,
+		  0xFFFFFFFFFFFFFFFCull, 0x1ull, true);
+
+	TEST_SIZE(0xFFFFFFFFFFFFFFE0ull, 0x20ull,
+		  0xFFFFFFFFFFFFFFFEull, 0x1ull, true);
+
+	TEST_SIZE(0xFFFFFFFFFFFFFFE0ull, 0x20ull,
+		  0ull, 10ull, false);
+}
+
+bselftest(core, test_region_overlap);
-- 
2.47.3




  reply	other threads:[~2025-11-01 10:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-01 10:55 [PATCH master 1/2] range: fix corner cases when exclusive end is zero Ahmad Fatoum
2025-11-01 10:55 ` Ahmad Fatoum [this message]
2025-11-01 11:07 ` [PATCH] fixup! " Ahmad Fatoum

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=20251101105542.3830943-2-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    --cc=sohaib.amhmd@gmail.com \
    /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