Skip to content

Commit

Permalink
lib/bitmap: add tests for find_nth_bit()
Browse files Browse the repository at this point in the history
Add functional and performance tests for find_nth_bit().

Signed-off-by: Yury Norov <yury.norov@gmail.com>
  • Loading branch information
Yury Norov committed Sep 26, 2022
1 parent 3cea8d4 commit e3783c8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/find_bit_benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
47 changes: 45 additions & 2 deletions lib/test_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e3783c8

Please sign in to comment.