linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Tamir Duberstein <tamird@gmail.com>
To: David Gow <davidgow@google.com>,
	John Hubbard <jhubbard@nvidia.com>,
	 Andrew Morton <akpm@linux-foundation.org>,
	 Geert Uytterhoeven <geert@linux-m68k.org>,
	 Madhavan Srinivasan <maddy@linux.ibm.com>,
	 Michael Ellerman <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	 Christophe Leroy <christophe.leroy@csgroup.eu>,
	 Naveen N Rao <naveen@kernel.org>,
	Yury Norov <yury.norov@gmail.com>,
	 Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Shuah Khan <shuah@kernel.org>,  Kees Cook <kees@kernel.org>
Cc: Muhammad Usama Anjum <usama.anjum@collabora.com>,
	 linux-kernel@vger.kernel.org, linux-m68k@lists.linux-m68k.org,
	 linuxppc-dev@lists.ozlabs.org, linux-kselftest@vger.kernel.org,
	 Tamir Duberstein <tamird@gmail.com>
Subject: [PATCH 3/3] bitmap: break kunit into test cases
Date: Fri, 07 Feb 2025 15:14:04 -0500	[thread overview]
Message-ID: <20250207-bitmap-kunit-convert-v1-3-c520675343b6@gmail.com> (raw)
In-Reply-To: <20250207-bitmap-kunit-convert-v1-0-c520675343b6@gmail.com>

Move some tests into `bitmap_test_cases` and parameterize
`test_bitmap_print_buf`. This gives us nicer output in the event of a
failure.

Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 lib/bitmap_kunit.c | 182 ++++++++++++++++++++++++++---------------------------
 1 file changed, 89 insertions(+), 93 deletions(-)

diff --git a/lib/bitmap_kunit.c b/lib/bitmap_kunit.c
index 0605228288d6..f7b90f6d5f49 100644
--- a/lib/bitmap_kunit.c
+++ b/lib/bitmap_kunit.c
@@ -17,8 +17,6 @@
 static char pbl_buffer[PAGE_SIZE];
 static char print_buf[PAGE_SIZE * 2];
 
-static struct kunit *kunittest;
-
 #define tc_err(fmt, ...) \
 	KUNIT_FAIL(kunittest, fmt, ##__VA_ARGS__)
 
@@ -96,7 +94,7 @@ static const unsigned long exp3_1_0[] = {
 
 #define expect_eq_uint(x, y)		expect_eq_ulong((unsigned int)(x), (unsigned int)(y))
 
-static void test_zero_clear(void)
+static void test_zero_clear(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bmap, 1024);
 
@@ -125,7 +123,7 @@ static void test_zero_clear(void)
 	expect_eq_pbl("", bmap, 1024);
 }
 
-static void test_find_nth_bit(void)
+static void test_find_nth_bit(struct kunit *kunittest)
 {
 	unsigned long b, bit, cnt = 0;
 	DECLARE_BITMAP(bmap, 64 * 3);
@@ -166,7 +164,7 @@ static void test_find_nth_bit(void)
 	}
 }
 
-static void test_fill_set(void)
+static void test_fill_set(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bmap, 1024);
 
@@ -195,7 +193,7 @@ static void test_fill_set(void)
 	expect_eq_pbl("0-1023", bmap, 1024);
 }
 
-static void test_copy(void)
+static void test_copy(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bmap1, 1024);
 	DECLARE_BITMAP(bmap2, 1024);
@@ -234,7 +232,7 @@ static void test_copy(void)
 	expect_eq_pbl("0-108,128-1023", bmap2, 1024);
 }
 
-static void test_bitmap_region(void)
+static void test_bitmap_region(struct kunit *kunittest)
 {
 	int pos, order;
 
@@ -259,7 +257,7 @@ static void test_bitmap_region(void)
 
 #define EXP2_IN_BITS	(sizeof(exp2) * 8)
 
-static void test_replace(void)
+static void test_replace(struct kunit *kunittest)
 {
 	unsigned int nbits = 64;
 	unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG);
@@ -300,7 +298,7 @@ static const unsigned long sg_scatter_exp[] = {
 	BITMAP_FROM_U64(0x000000000000021aULL),
 };
 
-static void test_bitmap_sg(void)
+static void test_bitmap_sg(struct kunit *kunittest)
 {
 	unsigned int nbits = 64;
 	DECLARE_BITMAP(bmap_gather, 100);
@@ -421,7 +419,7 @@ static const struct test_bitmap_parselist parselist_tests[] = {
 
 };
 
-static void test_bitmap_parselist(void)
+static void test_bitmap_parselist(struct kunit *kunittest)
 {
 	int i;
 	int err;
@@ -457,7 +455,7 @@ static void test_bitmap_parselist(void)
 	}
 }
 
-static void test_bitmap_printlist(void)
+static void test_bitmap_printlist(struct kunit *kunittest)
 {
 	unsigned long *bmap = kmalloc(PAGE_SIZE, GFP_KERNEL);
 	char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
@@ -533,7 +531,7 @@ static const struct test_bitmap_parselist parse_tests[] = {
 #undef step
 };
 
-static void test_bitmap_parse(void)
+static void test_bitmap_parse(struct kunit *kunittest)
 {
 	int i;
 	int err;
@@ -568,7 +566,7 @@ static void test_bitmap_parse(void)
 	}
 }
 
-static void test_bitmap_arr32(void)
+static void test_bitmap_arr32(struct kunit *kunittest)
 {
 	unsigned int nbits, next_bit;
 	u32 arr[EXP1_IN_BITS / 32];
@@ -594,7 +592,7 @@ static void test_bitmap_arr32(void)
 	}
 }
 
-static void test_bitmap_arr64(void)
+static void test_bitmap_arr64(struct kunit *kunittest)
 {
 	unsigned int nbits, next_bit;
 	u64 arr[EXP1_IN_BITS / 64];
@@ -626,7 +624,7 @@ static void test_bitmap_arr64(void)
 	}
 }
 
-static noinline void test_mem_optimisations(void)
+static noinline void test_mem_optimisations(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bmap1, 1024);
 	DECLARE_BITMAP(bmap2, 1024);
@@ -669,7 +667,7 @@ static const unsigned char clump_exp[] = {
 	0x05,	/* non-adjacent 2 bits set */
 };
 
-static void test_for_each_set_clump8(void)
+static void test_for_each_set_clump8(struct kunit *kunittest)
 {
 #define CLUMP_EXP_NUMBITS 64
 	DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS);
@@ -691,7 +689,7 @@ static void test_for_each_set_clump8(void)
 		expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump);
 }
 
-static void test_for_each_set_bit_wrap(void)
+static void test_for_each_set_bit_wrap(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -716,7 +714,7 @@ static void test_for_each_set_bit_wrap(void)
 	}
 }
 
-static void test_for_each_set_bit(void)
+static void test_for_each_set_bit(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -738,7 +736,7 @@ static void test_for_each_set_bit(void)
 	expect_eq_bitmap(orig, copy, 500);
 }
 
-static void test_for_each_set_bit_from(void)
+static void test_for_each_set_bit_from(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -768,7 +766,7 @@ static void test_for_each_set_bit_from(void)
 	}
 }
 
-static void test_for_each_clear_bit(void)
+static void test_for_each_clear_bit(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -790,7 +788,7 @@ static void test_for_each_clear_bit(void)
 	expect_eq_bitmap(orig, copy, 500);
 }
 
-static void test_for_each_clear_bit_from(void)
+static void test_for_each_clear_bit_from(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -820,7 +818,7 @@ static void test_for_each_clear_bit_from(void)
 	}
 }
 
-static void test_for_each_set_bitrange(void)
+static void test_for_each_set_bitrange(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -842,7 +840,7 @@ static void test_for_each_set_bitrange(void)
 	expect_eq_bitmap(orig, copy, 500);
 }
 
-static void test_for_each_clear_bitrange(void)
+static void test_for_each_clear_bitrange(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -864,7 +862,7 @@ static void test_for_each_clear_bitrange(void)
 	expect_eq_bitmap(orig, copy, 500);
 }
 
-static void test_for_each_set_bitrange_from(void)
+static void test_for_each_set_bitrange_from(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -894,7 +892,7 @@ static void test_for_each_set_bitrange_from(void)
 	}
 }
 
-static void test_for_each_clear_bitrange_from(void)
+static void test_for_each_clear_bitrange_from(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(orig, 500);
 	DECLARE_BITMAP(copy, 500);
@@ -965,7 +963,7 @@ static struct test_bitmap_cut test_cut[] = {
 	},
 };
 
-static void test_bitmap_cut(void)
+static void test_bitmap_cut(struct kunit *kunittest)
 {
 	unsigned long b[5], *in = &b[1], *out = &b[0];	/* Partial overlap */
 	int i;
@@ -986,8 +984,14 @@ struct test_bitmap_print {
 	unsigned long nbits;
 	const char *mask;
 	const char *list;
+	const char *name;
 };
 
+static void param_to_desc(const struct test_bitmap_print *param, char *desc)
+{
+	strscpy(desc, param->name, KUNIT_PARAM_DESC_SIZE);
+}
+
 static const unsigned long small_bitmap[] = {
 	BITMAP_FROM_U64(0x3333333311111111ULL),
 };
@@ -1082,35 +1086,34 @@ static const char large_list[] = /* more than 4KB */
 	"49,2552-2553,2556-2557\n";
 
 static const struct test_bitmap_print test_print[] = {
-	{ small_bitmap, sizeof(small_bitmap) * BITS_PER_BYTE, small_mask, small_list },
-	{ large_bitmap, sizeof(large_bitmap) * BITS_PER_BYTE, large_mask, large_list },
+	{ small_bitmap, sizeof(small_bitmap) * BITS_PER_BYTE, small_mask, small_list, "small" },
+	{ large_bitmap, sizeof(large_bitmap) * BITS_PER_BYTE, large_mask, large_list, "large" },
 };
 
-static void test_bitmap_print_buf(void)
+KUNIT_ARRAY_PARAM(test_print, test_print, param_to_desc);
+
+static void test_bitmap_print_buf(struct kunit *kunittest)
 {
-	int i;
+	int n;
 
-	for (i = 0; i < ARRAY_SIZE(test_print); i++) {
-		const struct test_bitmap_print *t = &test_print[i];
-		int n;
+	const struct test_bitmap_print *t = kunittest->param_value;
 
-		n = bitmap_print_bitmask_to_buf(print_buf, t->bitmap, t->nbits,
-						0, 2 * PAGE_SIZE);
-		expect_eq_uint(strlen(t->mask) + 1, n);
-		expect_eq_str(t->mask, print_buf, n);
+	n = bitmap_print_bitmask_to_buf(print_buf, t->bitmap, t->nbits,
+					0, 2 * PAGE_SIZE);
+	expect_eq_uint(strlen(t->mask) + 1, n);
+	expect_eq_str(t->mask, print_buf, n);
 
+	n = bitmap_print_list_to_buf(print_buf, t->bitmap, t->nbits,
+					0, 2 * PAGE_SIZE);
+	expect_eq_uint(strlen(t->list) + 1, n);
+	expect_eq_str(t->list, print_buf, n);
+
+	/* test by non-zero offset */
+	if (strlen(t->list) > PAGE_SIZE) {
 		n = bitmap_print_list_to_buf(print_buf, t->bitmap, t->nbits,
-					     0, 2 * PAGE_SIZE);
-		expect_eq_uint(strlen(t->list) + 1, n);
-		expect_eq_str(t->list, print_buf, n);
-
-		/* test by non-zero offset */
-		if (strlen(t->list) > PAGE_SIZE) {
-			n = bitmap_print_list_to_buf(print_buf, t->bitmap, t->nbits,
-						     PAGE_SIZE, PAGE_SIZE);
-			expect_eq_uint(strlen(t->list) + 1 - PAGE_SIZE, n);
-			expect_eq_str(t->list + PAGE_SIZE, print_buf, n);
-		}
+						PAGE_SIZE, PAGE_SIZE);
+		expect_eq_uint(strlen(t->list) + 1 - PAGE_SIZE, n);
+		expect_eq_str(t->list + PAGE_SIZE, print_buf, n);
 	}
 }
 
@@ -1118,7 +1121,7 @@ static void test_bitmap_print_buf(void)
  * FIXME: Clang breaks compile-time evaluations when KASAN and GCOV are enabled.
  * To workaround it, GCOV is force-disabled in Makefile for this configuration.
  */
-static void test_bitmap_const_eval(void)
+static void test_bitmap_const_eval(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bitmap, BITS_PER_LONG);
 	unsigned long initvar = BIT(2);
@@ -1186,7 +1189,7 @@ static void test_bitmap_const_eval(void)
 /*
  * Helper function to test bitmap_write() overwriting the chosen byte pattern.
  */
-static void test_bitmap_write_helper(const char *pattern)
+static void test_bitmap_write_helper(struct kunit *kunittest, const char *pattern)
 {
 	DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
 	DECLARE_BITMAP(exp_bitmap, TEST_BIT_LEN);
@@ -1240,7 +1243,7 @@ static void test_bitmap_write_helper(const char *pattern)
 	}
 }
 
-static void test_bitmap_read_write(void)
+static void test_bitmap_read_write(struct kunit *kunittest)
 {
 	unsigned char *pattern[3] = {"", "all:1/2", "all"};
 	DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
@@ -1286,10 +1289,10 @@ static void test_bitmap_read_write(void)
 	}
 
 	for (pi = 0; pi < ARRAY_SIZE(pattern); pi++)
-		test_bitmap_write_helper(pattern[pi]);
+		test_bitmap_write_helper(kunittest, pattern[pi]);
 }
 
-static void test_bitmap_read_perf(void)
+static void test_bitmap_read_perf(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
 	unsigned int cnt, nbits, i;
@@ -1312,10 +1315,10 @@ static void test_bitmap_read_perf(void)
 		}
 	}
 	time = ktime_get() - time;
-	kunit_info(kunittest, "Time spent in %s:\t%llu\n", __func__, time);
+	kunit_info(kunittest, "Time spent:\t%llu\n", time);
 }
 
-static void test_bitmap_write_perf(void)
+static void test_bitmap_write_perf(struct kunit *kunittest)
 {
 	DECLARE_BITMAP(bitmap, TEST_BIT_LEN);
 	unsigned int cnt, nbits, i;
@@ -1334,49 +1337,42 @@ static void test_bitmap_write_perf(void)
 		}
 	}
 	time = ktime_get() - time;
-	kunit_info(kunittest, "Time spent in %s:\t%llu\n", __func__, time);
+	kunit_info(kunittest, "Time spent:\t%llu\n", time);
 }
 
 #undef TEST_BIT_LEN
 
-static void bitmap_test(struct kunit *test)
-{
-	kunittest = test;
-
-	test_zero_clear();
-	test_fill_set();
-	test_copy();
-	test_bitmap_region();
-	test_replace();
-	test_bitmap_sg();
-	test_bitmap_arr32();
-	test_bitmap_arr64();
-	test_bitmap_parse();
-	test_bitmap_parselist();
-	test_bitmap_printlist();
-	test_mem_optimisations();
-	test_bitmap_cut();
-	test_bitmap_print_buf();
-	test_bitmap_const_eval();
-	test_bitmap_read_write();
-	test_bitmap_read_perf();
-	test_bitmap_write_perf();
-
-	test_find_nth_bit();
-	test_for_each_set_bit();
-	test_for_each_set_bit_from();
-	test_for_each_clear_bit();
-	test_for_each_clear_bit_from();
-	test_for_each_set_bitrange();
-	test_for_each_clear_bitrange();
-	test_for_each_set_bitrange_from();
-	test_for_each_clear_bitrange_from();
-	test_for_each_set_clump8();
-	test_for_each_set_bit_wrap();
-}
-
 static struct kunit_case bitmap_test_cases[] = {
-	KUNIT_CASE(bitmap_test),
+	KUNIT_CASE(test_zero_clear),
+	KUNIT_CASE(test_fill_set),
+	KUNIT_CASE(test_copy),
+	KUNIT_CASE(test_bitmap_region),
+	KUNIT_CASE(test_replace),
+	KUNIT_CASE(test_bitmap_sg),
+	KUNIT_CASE(test_bitmap_arr32),
+	KUNIT_CASE(test_bitmap_arr64),
+	KUNIT_CASE(test_bitmap_parse),
+	KUNIT_CASE(test_bitmap_parselist),
+	KUNIT_CASE(test_bitmap_printlist),
+	KUNIT_CASE(test_mem_optimisations),
+	KUNIT_CASE(test_bitmap_cut),
+	KUNIT_CASE_PARAM(test_bitmap_print_buf, test_print_gen_params),
+	KUNIT_CASE(test_bitmap_const_eval),
+	KUNIT_CASE(test_bitmap_read_write),
+	KUNIT_CASE(test_bitmap_read_perf),
+	KUNIT_CASE(test_bitmap_write_perf),
+
+	KUNIT_CASE(test_find_nth_bit),
+	KUNIT_CASE(test_for_each_set_bit),
+	KUNIT_CASE(test_for_each_set_bit_from),
+	KUNIT_CASE(test_for_each_clear_bit),
+	KUNIT_CASE(test_for_each_clear_bit_from),
+	KUNIT_CASE(test_for_each_set_bitrange),
+	KUNIT_CASE(test_for_each_clear_bitrange),
+	KUNIT_CASE(test_for_each_set_bitrange_from),
+	KUNIT_CASE(test_for_each_clear_bitrange_from),
+	KUNIT_CASE(test_for_each_set_clump8),
+	KUNIT_CASE(test_for_each_set_bit_wrap),
 	{}
 };
 

-- 
2.48.1



  parent reply	other threads:[~2025-02-07 20:14 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-07 20:14 [PATCH 0/3] bitmap: convert self-test to KUnit Tamir Duberstein
2025-02-07 20:14 ` [PATCH 1/3] bitmap: remove _check_eq_u32_array Tamir Duberstein
2025-02-08  9:07   ` David Gow
2025-02-08 18:00   ` Yury Norov
2025-02-10  5:22   ` Muhammad Usama Anjum
2025-02-07 20:14 ` [PATCH 2/3] bitmap: convert self-test to KUnit Tamir Duberstein
2025-02-08  9:07   ` David Gow
2025-02-10  5:22   ` Muhammad Usama Anjum
2025-02-10  7:42   ` Geert Uytterhoeven
2025-02-07 20:14 ` Tamir Duberstein [this message]
2025-02-08  9:07   ` [PATCH 3/3] bitmap: break kunit into test cases David Gow
2025-02-08 12:33     ` Tamir Duberstein
2025-02-10  5:23   ` Muhammad Usama Anjum
2025-02-08  9:07 ` [PATCH 0/3] bitmap: convert self-test to KUnit David Gow
2025-02-08 17:53 ` Yury Norov
2025-02-08 18:42   ` Tamir Duberstein
2025-02-10  7:54   ` Geert Uytterhoeven
2025-02-10 19:35     ` distro support for CONFIG_KUNIT: " John Hubbard
2025-02-10 19:46       ` David Hildenbrand
2025-02-10 23:10         ` Nico Pache
2025-02-10 23:41           ` John Hubbard
2025-02-10 20:20       ` Yury Norov
2025-02-10 22:01         ` Tamir Duberstein
2025-02-11  7:51         ` David Gow
2025-03-21 16:53           ` Tamir Duberstein
2025-03-21 18:32             ` Yury Norov
2025-03-21 19:47               ` Tamir Duberstein

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=20250207-bitmap-kunit-convert-v1-3-c520675343b6@gmail.com \
    --to=tamird@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=christophe.leroy@csgroup.eu \
    --cc=davidgow@google.com \
    --cc=geert@linux-m68k.org \
    --cc=jhubbard@nvidia.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-m68k@lists.linux-m68k.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=naveen@kernel.org \
    --cc=npiggin@gmail.com \
    --cc=shuah@kernel.org \
    --cc=usama.anjum@collabora.com \
    --cc=yury.norov@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;
as well as URLs for NNTP newsgroup(s).