1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020 Intel Corporation 4 * Copyright (C) 2026 Ryota Sakamoto <sakamo.ryota@gmail.com> 5 */ 6 7 #include <linux/bitops.h> 8 #include <linux/module.h> 9 #include <kunit/test.h> 10 11 /* use an enum because that's the most common BITMAP usage */ 12 enum bitops_fun { 13 BITOPS_4 = 4, 14 BITOPS_7 = 7, 15 BITOPS_11 = 11, 16 BITOPS_31 = 31, 17 BITOPS_88 = 88, 18 BITOPS_LENGTH = 256 19 }; 20 21 struct bitops_test_case { 22 const char *str; 23 const long nr; 24 }; 25 26 static struct bitops_test_case bitops_cases[] = { 27 { 28 .str = "BITOPS_4", 29 .nr = BITOPS_4, 30 }, 31 { 32 .str = "BITOPS_7", 33 .nr = BITOPS_7, 34 }, 35 { 36 .str = "BITOPS_11", 37 .nr = BITOPS_11, 38 }, 39 { 40 .str = "BITOPS_31", 41 .nr = BITOPS_31, 42 }, 43 { 44 .str = "BITOPS_88", 45 .nr = BITOPS_88, 46 }, 47 }; 48 49 KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str); 50 51 static void test_set_bit_clear_bit(struct kunit *test) 52 { 53 const struct bitops_test_case *params = test->param_value; 54 DECLARE_BITMAP(bitmap, BITOPS_LENGTH); 55 int bit_set; 56 57 bitmap_zero(bitmap, BITOPS_LENGTH); 58 59 set_bit(params->nr, bitmap); 60 KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap)); 61 62 clear_bit(params->nr, bitmap); 63 KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap)); 64 65 bit_set = find_first_bit(bitmap, BITOPS_LENGTH); 66 KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH); 67 } 68 69 struct order_test_case { 70 const char *str; 71 const unsigned int count; 72 const int expected; 73 }; 74 75 static struct order_test_case order_test_cases[] = { 76 {"0x00000003", 0x00000003, 2}, 77 {"0x00000004", 0x00000004, 2}, 78 {"0x00001fff", 0x00001fff, 13}, 79 {"0x00002000", 0x00002000, 13}, 80 {"0x50000000", 0x50000000, 31}, 81 {"0x80000000", 0x80000000, 31}, 82 {"0x80003000", 0x80003000, 32}, 83 }; 84 85 KUNIT_ARRAY_PARAM_DESC(order, order_test_cases, str); 86 87 static void test_get_count_order(struct kunit *test) 88 { 89 const struct order_test_case *params = test->param_value; 90 91 KUNIT_EXPECT_EQ(test, get_count_order(params->count), params->expected); 92 KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected); 93 } 94 95 #ifdef CONFIG_64BIT 96 struct order_long_test_case { 97 const char *str; 98 const unsigned long count; 99 const int expected; 100 }; 101 102 static struct order_long_test_case order_long_test_cases[] = { 103 {"0x0000000300000000", 0x0000000300000000, 34}, 104 {"0x0000000400000000", 0x0000000400000000, 34}, 105 {"0x00001fff00000000", 0x00001fff00000000, 45}, 106 {"0x0000200000000000", 0x0000200000000000, 45}, 107 {"0x5000000000000000", 0x5000000000000000, 63}, 108 {"0x8000000000000000", 0x8000000000000000, 63}, 109 {"0x8000300000000000", 0x8000300000000000, 64}, 110 }; 111 112 KUNIT_ARRAY_PARAM_DESC(order_long, order_long_test_cases, str); 113 114 static void test_get_count_order_long(struct kunit *test) 115 { 116 const struct order_long_test_case *params = test->param_value; 117 118 KUNIT_EXPECT_EQ(test, get_count_order_long(params->count), params->expected); 119 } 120 #endif 121 122 static struct kunit_case bitops_test_cases[] = { 123 KUNIT_CASE_PARAM(test_set_bit_clear_bit, bitops_gen_params), 124 KUNIT_CASE_PARAM(test_get_count_order, order_gen_params), 125 #ifdef CONFIG_64BIT 126 KUNIT_CASE_PARAM(test_get_count_order_long, order_long_gen_params), 127 #endif 128 {}, 129 }; 130 131 static struct kunit_suite bitops_test_suite = { 132 .name = "bitops", 133 .test_cases = bitops_test_cases, 134 }; 135 136 kunit_test_suite(bitops_test_suite); 137 138 MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>"); 139 MODULE_AUTHOR("Wei Yang <richard.weiyang@gmail.com>"); 140 MODULE_AUTHOR("Ryota Sakamoto <sakamo.ryota@gmail.com>"); 141 MODULE_LICENSE("GPL"); 142 MODULE_DESCRIPTION("Bit testing module"); 143