diff mbox series

[13/14] lib/bitmap: add tests for bitmap_{get,set}_bits()

Message ID 20231009151026.66145-14-aleksander.lobakin@intel.com (mailing list archive)
State Superseded
Headers show
Series ip_tunnel: convert __be16 tunnel flags to bitmaps | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain_full }}
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-4 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-3 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-2 fail Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-7 success Logs for veristat
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Guessed tree name to be net-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 1362 this patch: 1362
netdev/cc_maintainers success CCed 3 of 3 maintainers
netdev/build_clang success Errors and warnings before: 1387 this patch: 1387
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 1387 this patch: 1387
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 89 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-PR fail PR summary
bpf/vmtest-bpf-VM_Test-6 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-7 success Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-VM_Test-0 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-1 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-8 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-3 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-2 success Logs for build for s390x with gcc
bpf/vmtest-bpf-VM_Test-4 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-9 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-10 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-11 success Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-VM_Test-12 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-13 fail Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-14 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-15 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-VM_Test-16 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-18 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-19 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-21 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-22 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-23 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-24 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-VM_Test-25 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-VM_Test-26 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-VM_Test-27 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-28 success Logs for veristat

Commit Message

Alexander Lobakin Oct. 9, 2023, 3:10 p.m. UTC
bitmap_{get,set}_value8() is now bitmap_{get,set}_bits(). The former
didn't have any dedicated tests in the bitmap test suite.
Add a pack of test cases with variable offset, width, and value to set
(for _set_bits()), randomly generated with the only limitation:
``offset % 32 + width <= 32``, to make sure the tests won't fail or
trigger kernel warnings on 32-bit platforms. For _get_bits(), compare
the return values with the expected ones, calculated and saved manually.
For _set_bit(), do several modifications of the source bitmap and then
compare against the expected resulting one, also pre-calculated.

Reviewed-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Signed-off-by: Alexander Lobakin <aleksander.lobakin@intel.com>
---
 lib/test_bitmap.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
diff mbox series

Patch

diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c
index cbf1b9611616..6037b66fd30a 100644
--- a/lib/test_bitmap.c
+++ b/lib/test_bitmap.c
@@ -1161,6 +1161,82 @@  static void __init test_bitmap_print_buf(void)
 	}
 }
 
+struct getset_test {
+	u16	offset;
+	u16	width;
+	union {
+		u32	expect;
+		u32	value;
+	};
+};
+
+#define GETSET_TEST(o, w, v) {	\
+	.offset	= (o),		\
+	.width	= (w),		\
+	.value	= (v),		\
+}
+
+static const unsigned long getset_src[] __initconst = {
+	BITMAP_FROM_U64(0x4329c918b472468eULL),
+	BITMAP_FROM_U64(0xb2c20a622474a119ULL),
+	BITMAP_FROM_U64(0x3a08cb5591cea40dULL),
+	BITMAP_FROM_U64(0xc9a7550384e145f8ULL),
+};
+
+static const struct getset_test get_bits_test[] __initconst = {
+	GETSET_TEST(208, 16, 0x84e1),
+	GETSET_TEST(104, 8, 0xa),
+	GETSET_TEST(224, 32, 0xc9a75503),
+	GETSET_TEST(64, 16, 0xa119),
+	GETSET_TEST(169, 1, 0x1),
+	GETSET_TEST(144, 8, 0xce),
+	GETSET_TEST(80, 4, 0x4),
+	GETSET_TEST(24, 4, 0x4),
+};
+
+static const struct getset_test set_bits_test[] __initconst = {
+	GETSET_TEST(56, 4, 0xa),
+	GETSET_TEST(80, 16, 0xb17a),
+	GETSET_TEST(112, 8, 0x1b),
+	GETSET_TEST(0, 32, 0xe8a555f2),
+	GETSET_TEST(16, 2, 0),
+	GETSET_TEST(72, 8, 0x7d),
+	GETSET_TEST(47, 1, 0),
+	GETSET_TEST(160, 16, 0x1622),
+};
+
+static const unsigned long getset_out[] __initconst = {
+	BITMAP_FROM_U64(0x4a294918e8a455f2ULL),
+	BITMAP_FROM_U64(0xb21b0a62b17a7d19ULL),
+	BITMAP_FROM_U64(0x3a08162291cea40dULL),
+	BITMAP_FROM_U64(0xc9a7550384e145f8ULL),
+};
+
+#define GETSET_TEST_BITS	BYTES_TO_BITS(sizeof(getset_out))
+
+static void __init test_bitmap_getset_bits(void)
+{
+	DECLARE_BITMAP(out, GETSET_TEST_BITS);
+
+	for (u32 i = 0; i < ARRAY_SIZE(get_bits_test); i++) {
+		const struct getset_test *test = &get_bits_test[i];
+		u32 val;
+
+		val = bitmap_get_bits(getset_src, test->offset, test->width);
+		expect_eq_uint(test->expect, val);
+	}
+
+	bitmap_copy(out, getset_src, GETSET_TEST_BITS);
+
+	for (u32 i = 0; i < ARRAY_SIZE(set_bits_test); i++) {
+		const struct getset_test *test = &set_bits_test[i];
+
+		bitmap_set_bits(out, test->offset, test->value, test->width);
+	}
+
+	expect_eq_bitmap(getset_out, out, GETSET_TEST_BITS);
+}
+
 /*
  * FIXME: Clang breaks compile-time evaluations when KASAN and GCOV are enabled.
  * To workaround it, GCOV is force-disabled in Makefile for this configuration.
@@ -1238,6 +1314,7 @@  static void __init selftest(void)
 	test_mem_optimisations();
 	test_bitmap_cut();
 	test_bitmap_print_buf();
+	test_bitmap_getset_bits();
 	test_bitmap_const_eval();
 
 	test_find_nth_bit();