David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Test cases for printf facility. |
| 3 | */ |
| 4 | |
| 5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 6 | |
| 7 | #include <linux/bitmap.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/printk.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/string.h> |
| 14 | |
| 15 | static unsigned total_tests __initdata; |
| 16 | static unsigned failed_tests __initdata; |
| 17 | |
| 18 | static char pbl_buffer[PAGE_SIZE] __initdata; |
| 19 | |
| 20 | |
| 21 | static bool __init |
| 22 | __check_eq_uint(const char *srcfile, unsigned int line, |
| 23 | const unsigned int exp_uint, unsigned int x) |
| 24 | { |
| 25 | if (exp_uint != x) { |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 26 | pr_err("[%s:%u] expected %u, got %u\n", |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 27 | srcfile, line, exp_uint, x); |
| 28 | return false; |
| 29 | } |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | static bool __init |
| 35 | __check_eq_bitmap(const char *srcfile, unsigned int line, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 36 | const unsigned long *exp_bmap, const unsigned long *bmap, |
| 37 | unsigned int nbits) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 38 | { |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 39 | if (!bitmap_equal(exp_bmap, bmap, nbits)) { |
| 40 | pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n", |
| 41 | srcfile, line, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 42 | nbits, exp_bmap, nbits, bmap); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | static bool __init |
| 49 | __check_eq_pbl(const char *srcfile, unsigned int line, |
| 50 | const char *expected_pbl, |
| 51 | const unsigned long *bitmap, unsigned int nbits) |
| 52 | { |
| 53 | snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap); |
| 54 | if (strcmp(expected_pbl, pbl_buffer)) { |
| 55 | pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n", |
| 56 | srcfile, line, |
| 57 | expected_pbl, pbl_buffer); |
| 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | static bool __init |
| 64 | __check_eq_u32_array(const char *srcfile, unsigned int line, |
| 65 | const u32 *exp_arr, unsigned int exp_len, |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 66 | const u32 *arr, unsigned int len) __used; |
| 67 | static bool __init |
| 68 | __check_eq_u32_array(const char *srcfile, unsigned int line, |
| 69 | const u32 *exp_arr, unsigned int exp_len, |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 70 | const u32 *arr, unsigned int len) |
| 71 | { |
| 72 | if (exp_len != len) { |
| 73 | pr_warn("[%s:%u] array length differ: expected %u, got %u\n", |
| 74 | srcfile, line, |
| 75 | exp_len, len); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if (memcmp(exp_arr, arr, len*sizeof(*arr))) { |
| 80 | pr_warn("[%s:%u] array contents differ\n", srcfile, line); |
| 81 | print_hex_dump(KERN_WARNING, " exp: ", DUMP_PREFIX_OFFSET, |
| 82 | 32, 4, exp_arr, exp_len*sizeof(*exp_arr), false); |
| 83 | print_hex_dump(KERN_WARNING, " got: ", DUMP_PREFIX_OFFSET, |
| 84 | 32, 4, arr, len*sizeof(*arr), false); |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | #define __expect_eq(suffix, ...) \ |
| 92 | ({ \ |
| 93 | int result = 0; \ |
| 94 | total_tests++; \ |
| 95 | if (!__check_eq_ ## suffix(__FILE__, __LINE__, \ |
| 96 | ##__VA_ARGS__)) { \ |
| 97 | failed_tests++; \ |
| 98 | result = 1; \ |
| 99 | } \ |
| 100 | result; \ |
| 101 | }) |
| 102 | |
| 103 | #define expect_eq_uint(...) __expect_eq(uint, ##__VA_ARGS__) |
| 104 | #define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__) |
| 105 | #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__) |
| 106 | #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__) |
| 107 | |
| 108 | static void __init test_zero_fill_copy(void) |
| 109 | { |
| 110 | DECLARE_BITMAP(bmap1, 1024); |
| 111 | DECLARE_BITMAP(bmap2, 1024); |
| 112 | |
| 113 | bitmap_zero(bmap1, 1024); |
| 114 | bitmap_zero(bmap2, 1024); |
| 115 | |
| 116 | /* single-word bitmaps */ |
| 117 | expect_eq_pbl("", bmap1, 23); |
| 118 | |
| 119 | bitmap_fill(bmap1, 19); |
| 120 | expect_eq_pbl("0-18", bmap1, 1024); |
| 121 | |
| 122 | bitmap_copy(bmap2, bmap1, 23); |
| 123 | expect_eq_pbl("0-18", bmap2, 1024); |
| 124 | |
| 125 | bitmap_fill(bmap2, 23); |
| 126 | expect_eq_pbl("0-22", bmap2, 1024); |
| 127 | |
| 128 | bitmap_copy(bmap2, bmap1, 23); |
| 129 | expect_eq_pbl("0-18", bmap2, 1024); |
| 130 | |
| 131 | bitmap_zero(bmap1, 23); |
| 132 | expect_eq_pbl("", bmap1, 1024); |
| 133 | |
| 134 | /* multi-word bitmaps */ |
| 135 | bitmap_zero(bmap1, 1024); |
| 136 | expect_eq_pbl("", bmap1, 1024); |
| 137 | |
| 138 | bitmap_fill(bmap1, 109); |
| 139 | expect_eq_pbl("0-108", bmap1, 1024); |
| 140 | |
| 141 | bitmap_copy(bmap2, bmap1, 1024); |
| 142 | expect_eq_pbl("0-108", bmap2, 1024); |
| 143 | |
| 144 | bitmap_fill(bmap2, 1024); |
| 145 | expect_eq_pbl("0-1023", bmap2, 1024); |
| 146 | |
| 147 | bitmap_copy(bmap2, bmap1, 1024); |
| 148 | expect_eq_pbl("0-108", bmap2, 1024); |
| 149 | |
| 150 | /* the following tests assume a 32- or 64-bit arch (even 128b |
| 151 | * if we care) |
| 152 | */ |
| 153 | |
| 154 | bitmap_fill(bmap2, 1024); |
| 155 | bitmap_copy(bmap2, bmap1, 109); /* ... but 0-padded til word length */ |
| 156 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); |
| 157 | |
| 158 | bitmap_fill(bmap2, 1024); |
| 159 | bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */ |
| 160 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); |
| 161 | |
| 162 | bitmap_zero(bmap2, 97); /* ... but 0-padded til word length */ |
| 163 | expect_eq_pbl("128-1023", bmap2, 1024); |
| 164 | } |
| 165 | |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 166 | #define PARSE_TIME 0x1 |
| 167 | |
| 168 | struct test_bitmap_parselist{ |
| 169 | const int errno; |
| 170 | const char *in; |
| 171 | const unsigned long *expected; |
| 172 | const int nbits; |
| 173 | const int flags; |
| 174 | }; |
| 175 | |
Yury Norov | 60ef690 | 2017-09-08 16:15:41 -0700 | [diff] [blame] | 176 | static const unsigned long exp[] __initconst = { |
| 177 | BITMAP_FROM_U64(1), |
| 178 | BITMAP_FROM_U64(2), |
| 179 | BITMAP_FROM_U64(0x0000ffff), |
| 180 | BITMAP_FROM_U64(0xffff0000), |
| 181 | BITMAP_FROM_U64(0x55555555), |
| 182 | BITMAP_FROM_U64(0xaaaaaaaa), |
| 183 | BITMAP_FROM_U64(0x11111111), |
| 184 | BITMAP_FROM_U64(0x22222222), |
| 185 | BITMAP_FROM_U64(0xffffffff), |
| 186 | BITMAP_FROM_U64(0xfffffffe), |
Geert Uytterhoeven | 8185f570 | 2017-09-13 16:28:20 -0700 | [diff] [blame] | 187 | BITMAP_FROM_U64(0x3333333311111111ULL), |
| 188 | BITMAP_FROM_U64(0xffffffff77777777ULL) |
Yury Norov | 60ef690 | 2017-09-08 16:15:41 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | static const unsigned long exp2[] __initconst = { |
Geert Uytterhoeven | 8185f570 | 2017-09-13 16:28:20 -0700 | [diff] [blame] | 192 | BITMAP_FROM_U64(0x3333333311111111ULL), |
| 193 | BITMAP_FROM_U64(0xffffffff77777777ULL) |
Yury Norov | 60ef690 | 2017-09-08 16:15:41 -0700 | [diff] [blame] | 194 | }; |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 195 | |
| 196 | static const struct test_bitmap_parselist parselist_tests[] __initconst = { |
Yury Norov | 60ef690 | 2017-09-08 16:15:41 -0700 | [diff] [blame] | 197 | #define step (sizeof(u64) / sizeof(unsigned long)) |
| 198 | |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 199 | {0, "0", &exp[0], 8, 0}, |
Yury Norov | 60ef690 | 2017-09-08 16:15:41 -0700 | [diff] [blame] | 200 | {0, "1", &exp[1 * step], 8, 0}, |
| 201 | {0, "0-15", &exp[2 * step], 32, 0}, |
| 202 | {0, "16-31", &exp[3 * step], 32, 0}, |
| 203 | {0, "0-31:1/2", &exp[4 * step], 32, 0}, |
| 204 | {0, "1-31:1/2", &exp[5 * step], 32, 0}, |
| 205 | {0, "0-31:1/4", &exp[6 * step], 32, 0}, |
| 206 | {0, "1-31:1/4", &exp[7 * step], 32, 0}, |
| 207 | {0, "0-31:4/4", &exp[8 * step], 32, 0}, |
| 208 | {0, "1-31:4/4", &exp[9 * step], 32, 0}, |
| 209 | {0, "0-31:1/4,32-63:2/4", &exp[10 * step], 64, 0}, |
| 210 | {0, "0-31:3/4,32-63:4/4", &exp[11 * step], 64, 0}, |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 211 | |
| 212 | {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0}, |
| 213 | |
| 214 | {0, "0-2047:128/256", NULL, 2048, PARSE_TIME}, |
| 215 | |
| 216 | {-EINVAL, "-1", NULL, 8, 0}, |
| 217 | {-EINVAL, "-0", NULL, 8, 0}, |
| 218 | {-EINVAL, "10-1", NULL, 8, 0}, |
| 219 | {-EINVAL, "0-31:10/1", NULL, 8, 0}, |
| 220 | }; |
| 221 | |
| 222 | static void __init test_bitmap_parselist(void) |
| 223 | { |
| 224 | int i; |
| 225 | int err; |
| 226 | cycles_t cycles; |
| 227 | DECLARE_BITMAP(bmap, 2048); |
| 228 | |
| 229 | for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) { |
| 230 | #define ptest parselist_tests[i] |
| 231 | |
| 232 | cycles = get_cycles(); |
| 233 | err = bitmap_parselist(ptest.in, bmap, ptest.nbits); |
| 234 | cycles = get_cycles() - cycles; |
| 235 | |
| 236 | if (err != ptest.errno) { |
| 237 | pr_err("test %d: input is %s, errno is %d, expected %d\n", |
| 238 | i, ptest.in, err, ptest.errno); |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | if (!err && ptest.expected |
| 243 | && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) { |
| 244 | pr_err("test %d: input is %s, result is 0x%lx, expected 0x%lx\n", |
| 245 | i, ptest.in, bmap[0], *ptest.expected); |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | if (ptest.flags & PARSE_TIME) |
| 250 | pr_err("test %d: input is '%s' OK, Time: %llu\n", |
| 251 | i, ptest.in, |
| 252 | (unsigned long long)cycles); |
| 253 | } |
| 254 | } |
| 255 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 256 | static void __init test_bitmap_arr32(void) |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 257 | { |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 258 | unsigned int nbits, next_bit, len = sizeof(exp) * 8; |
| 259 | u32 arr[sizeof(exp) / 4]; |
| 260 | DECLARE_BITMAP(bmap2, len); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 261 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 262 | memset(arr, 0xa5, sizeof(arr)); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 263 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 264 | for (nbits = 0; nbits < len; ++nbits) { |
| 265 | bitmap_to_arr32(arr, exp, nbits); |
| 266 | bitmap_from_arr32(bmap2, arr, nbits); |
| 267 | expect_eq_bitmap(bmap2, exp, nbits); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 268 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 269 | next_bit = find_next_bit(bmap2, |
| 270 | round_up(nbits, BITS_PER_LONG), nbits); |
| 271 | if (next_bit < round_up(nbits, BITS_PER_LONG)) |
| 272 | pr_err("bitmap_copy_arr32(nbits == %d:" |
| 273 | " tail is not safely cleared: %d\n", |
| 274 | nbits, next_bit); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 275 | |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 276 | if (nbits < len - 32) |
| 277 | expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)], |
| 278 | 0xa5a5a5a5); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 282 | static void noinline __init test_mem_optimisations(void) |
| 283 | { |
| 284 | DECLARE_BITMAP(bmap1, 1024); |
| 285 | DECLARE_BITMAP(bmap2, 1024); |
| 286 | unsigned int start, nbits; |
| 287 | |
| 288 | for (start = 0; start < 1024; start += 8) { |
| 289 | memset(bmap1, 0x5a, sizeof(bmap1)); |
| 290 | memset(bmap2, 0x5a, sizeof(bmap2)); |
| 291 | for (nbits = 0; nbits < 1024 - start; nbits += 8) { |
| 292 | bitmap_set(bmap1, start, nbits); |
| 293 | __bitmap_set(bmap2, start, nbits); |
| 294 | if (!bitmap_equal(bmap1, bmap2, 1024)) |
| 295 | printk("set not equal %d %d\n", start, nbits); |
| 296 | if (!__bitmap_equal(bmap1, bmap2, 1024)) |
| 297 | printk("set not __equal %d %d\n", start, nbits); |
| 298 | |
| 299 | bitmap_clear(bmap1, start, nbits); |
| 300 | __bitmap_clear(bmap2, start, nbits); |
| 301 | if (!bitmap_equal(bmap1, bmap2, 1024)) |
| 302 | printk("clear not equal %d %d\n", start, nbits); |
| 303 | if (!__bitmap_equal(bmap1, bmap2, 1024)) |
| 304 | printk("clear not __equal %d %d\n", start, |
| 305 | nbits); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 310 | static int __init test_bitmap_init(void) |
| 311 | { |
| 312 | test_zero_fill_copy(); |
Yury Norov | 3aa5688 | 2018-02-06 15:38:06 -0800 | [diff] [blame^] | 313 | test_bitmap_arr32(); |
Yury Norov | 6df0d46 | 2017-09-08 16:15:38 -0700 | [diff] [blame] | 314 | test_bitmap_parselist(); |
Matthew Wilcox | 3cc7812 | 2017-07-10 15:51:26 -0700 | [diff] [blame] | 315 | test_mem_optimisations(); |
David Decotigny | 5fd003f | 2016-02-19 09:24:00 -0500 | [diff] [blame] | 316 | |
| 317 | if (failed_tests == 0) |
| 318 | pr_info("all %u tests passed\n", total_tests); |
| 319 | else |
| 320 | pr_warn("failed %u out of %u tests\n", |
| 321 | failed_tests, total_tests); |
| 322 | |
| 323 | return failed_tests ? -EINVAL : 0; |
| 324 | } |
| 325 | |
| 326 | static void __exit test_bitmap_cleanup(void) |
| 327 | { |
| 328 | } |
| 329 | |
| 330 | module_init(test_bitmap_init); |
| 331 | module_exit(test_bitmap_cleanup); |
| 332 | |
| 333 | MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>"); |
| 334 | MODULE_LICENSE("GPL"); |