lib/bitmap: add tests for find_nth_bit()
authorYury Norov <yury.norov@gmail.com>
Sun, 18 Sep 2022 03:07:14 +0000 (20:07 -0700)
committerYury Norov <yury.norov@gmail.com>
Mon, 26 Sep 2022 19:19:12 +0000 (12:19 -0700)
Add functional and performance tests for find_nth_bit().

Signed-off-by: Yury Norov <yury.norov@gmail.com>
lib/find_bit_benchmark.c
lib/test_bitmap.c

index db904b5..1075458 100644 (file)
@@ -115,6 +115,22 @@ static int __init test_find_last_bit(const void *bitmap, unsigned long len)
        return 0;
 }
 
+static int __init test_find_nth_bit(const unsigned long *bitmap, unsigned long len)
+{
+       unsigned long l, n, w = bitmap_weight(bitmap, len);
+       ktime_t time;
+
+       time = ktime_get();
+       for (n = 0; n < w; n++) {
+               l = find_nth_bit(bitmap, len, n);
+               WARN_ON(l >= len);
+       }
+       time = ktime_get() - time;
+       pr_err("find_nth_bit:       %18llu ns, %6ld iterations\n", time, w);
+
+       return 0;
+}
+
 static int __init test_find_next_and_bit(const void *bitmap,
                const void *bitmap2, unsigned long len)
 {
@@ -142,6 +158,7 @@ static int __init find_bit_test(void)
        test_find_next_bit(bitmap, BITMAP_LEN);
        test_find_next_zero_bit(bitmap, BITMAP_LEN);
        test_find_last_bit(bitmap, BITMAP_LEN);
+       test_find_nth_bit(bitmap, BITMAP_LEN / 10);
 
        /*
         * test_find_first_bit() may take some time, so
@@ -164,6 +181,7 @@ static int __init find_bit_test(void)
        test_find_next_bit(bitmap, BITMAP_LEN);
        test_find_next_zero_bit(bitmap, BITMAP_LEN);
        test_find_last_bit(bitmap, BITMAP_LEN);
+       test_find_nth_bit(bitmap, BITMAP_LEN);
        test_find_first_bit(bitmap, BITMAP_LEN);
        test_find_first_and_bit(bitmap, bitmap2, BITMAP_LEN);
        test_find_next_and_bit(bitmap, bitmap2, BITMAP_LEN);
index 98754ff..da52dc7 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "../tools/testing/selftests/kselftest_module.h"
 
+#define EXP1_IN_BITS   (sizeof(exp1) * 8)
+
 KSTM_MODULE_GLOBALS();
 
 static char pbl_buffer[PAGE_SIZE] __initdata;
@@ -219,6 +221,47 @@ static void __init test_zero_clear(void)
        expect_eq_pbl("", bmap, 1024);
 }
 
+static void __init test_find_nth_bit(void)
+{
+       unsigned long b, bit, cnt = 0;
+       DECLARE_BITMAP(bmap, 64 * 3);
+
+       bitmap_zero(bmap, 64 * 3);
+       __set_bit(10, bmap);
+       __set_bit(20, bmap);
+       __set_bit(30, bmap);
+       __set_bit(40, bmap);
+       __set_bit(50, bmap);
+       __set_bit(60, bmap);
+       __set_bit(80, bmap);
+       __set_bit(123, bmap);
+
+       expect_eq_uint(10,  find_nth_bit(bmap, 64 * 3, 0));
+       expect_eq_uint(20,  find_nth_bit(bmap, 64 * 3, 1));
+       expect_eq_uint(30,  find_nth_bit(bmap, 64 * 3, 2));
+       expect_eq_uint(40,  find_nth_bit(bmap, 64 * 3, 3));
+       expect_eq_uint(50,  find_nth_bit(bmap, 64 * 3, 4));
+       expect_eq_uint(60,  find_nth_bit(bmap, 64 * 3, 5));
+       expect_eq_uint(80,  find_nth_bit(bmap, 64 * 3, 6));
+       expect_eq_uint(123, find_nth_bit(bmap, 64 * 3, 7));
+       expect_eq_uint(64 * 3, find_nth_bit(bmap, 64 * 3, 8));
+
+       expect_eq_uint(10,  find_nth_bit(bmap, 64 * 3 - 1, 0));
+       expect_eq_uint(20,  find_nth_bit(bmap, 64 * 3 - 1, 1));
+       expect_eq_uint(30,  find_nth_bit(bmap, 64 * 3 - 1, 2));
+       expect_eq_uint(40,  find_nth_bit(bmap, 64 * 3 - 1, 3));
+       expect_eq_uint(50,  find_nth_bit(bmap, 64 * 3 - 1, 4));
+       expect_eq_uint(60,  find_nth_bit(bmap, 64 * 3 - 1, 5));
+       expect_eq_uint(80,  find_nth_bit(bmap, 64 * 3 - 1, 6));
+       expect_eq_uint(123, find_nth_bit(bmap, 64 * 3 - 1, 7));
+       expect_eq_uint(64 * 3 - 1, find_nth_bit(bmap, 64 * 3 - 1, 8));
+
+       for_each_set_bit(bit, exp1, EXP1_IN_BITS) {
+               b = find_nth_bit(exp1, EXP1_IN_BITS, cnt++);
+               expect_eq_uint(b, bit);
+       }
+}
+
 static void __init test_fill_set(void)
 {
        DECLARE_BITMAP(bmap, 1024);
@@ -557,8 +600,6 @@ static void __init test_bitmap_parse(void)
        }
 }
 
-#define EXP1_IN_BITS   (sizeof(exp1) * 8)
-
 static void __init test_bitmap_arr32(void)
 {
        unsigned int nbits, next_bit;
@@ -952,6 +993,8 @@ static void __init selftest(void)
        test_bitmap_cut();
        test_bitmap_print_buf();
        test_bitmap_const_eval();
+
+       test_find_nth_bit();
 }
 
 KSTM_MODULE_LOADERS(test_bitmap);