.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | | - * Test cases for printf facility. |
---|
| 3 | + * Test cases for bitmap API. |
---|
3 | 4 | */ |
---|
4 | 5 | |
---|
5 | 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
---|
.. | .. |
---|
11 | 12 | #include <linux/printk.h> |
---|
12 | 13 | #include <linux/slab.h> |
---|
13 | 14 | #include <linux/string.h> |
---|
| 15 | +#include <linux/uaccess.h> |
---|
14 | 16 | |
---|
15 | | -static unsigned total_tests __initdata; |
---|
16 | | -static unsigned failed_tests __initdata; |
---|
| 17 | +#include "../tools/testing/selftests/kselftest_module.h" |
---|
| 18 | + |
---|
| 19 | +KSTM_MODULE_GLOBALS(); |
---|
17 | 20 | |
---|
18 | 21 | static char pbl_buffer[PAGE_SIZE] __initdata; |
---|
19 | 22 | |
---|
| 23 | +static const unsigned long exp1[] __initconst = { |
---|
| 24 | + BITMAP_FROM_U64(1), |
---|
| 25 | + BITMAP_FROM_U64(2), |
---|
| 26 | + BITMAP_FROM_U64(0x0000ffff), |
---|
| 27 | + BITMAP_FROM_U64(0xffff0000), |
---|
| 28 | + BITMAP_FROM_U64(0x55555555), |
---|
| 29 | + BITMAP_FROM_U64(0xaaaaaaaa), |
---|
| 30 | + BITMAP_FROM_U64(0x11111111), |
---|
| 31 | + BITMAP_FROM_U64(0x22222222), |
---|
| 32 | + BITMAP_FROM_U64(0xffffffff), |
---|
| 33 | + BITMAP_FROM_U64(0xfffffffe), |
---|
| 34 | + BITMAP_FROM_U64(0x3333333311111111ULL), |
---|
| 35 | + BITMAP_FROM_U64(0xffffffff77777777ULL), |
---|
| 36 | + BITMAP_FROM_U64(0), |
---|
| 37 | +}; |
---|
| 38 | + |
---|
| 39 | +static const unsigned long exp2[] __initconst = { |
---|
| 40 | + BITMAP_FROM_U64(0x3333333311111111ULL), |
---|
| 41 | + BITMAP_FROM_U64(0xffffffff77777777ULL), |
---|
| 42 | +}; |
---|
| 43 | + |
---|
| 44 | +/* Fibonacci sequence */ |
---|
| 45 | +static const unsigned long exp2_to_exp3_mask[] __initconst = { |
---|
| 46 | + BITMAP_FROM_U64(0x008000020020212eULL), |
---|
| 47 | +}; |
---|
| 48 | +/* exp3_0_1 = (exp2[0] & ~exp2_to_exp3_mask) | (exp2[1] & exp2_to_exp3_mask) */ |
---|
| 49 | +static const unsigned long exp3_0_1[] __initconst = { |
---|
| 50 | + BITMAP_FROM_U64(0x33b3333311313137ULL), |
---|
| 51 | +}; |
---|
| 52 | +/* exp3_1_0 = (exp2[1] & ~exp2_to_exp3_mask) | (exp2[0] & exp2_to_exp3_mask) */ |
---|
| 53 | +static const unsigned long exp3_1_0[] __initconst = { |
---|
| 54 | + BITMAP_FROM_U64(0xff7fffff77575751ULL), |
---|
| 55 | +}; |
---|
20 | 56 | |
---|
21 | 57 | static bool __init |
---|
22 | 58 | __check_eq_uint(const char *srcfile, unsigned int line, |
---|
.. | .. |
---|
88 | 124 | return true; |
---|
89 | 125 | } |
---|
90 | 126 | |
---|
| 127 | +static bool __init __check_eq_clump8(const char *srcfile, unsigned int line, |
---|
| 128 | + const unsigned int offset, |
---|
| 129 | + const unsigned int size, |
---|
| 130 | + const unsigned char *const clump_exp, |
---|
| 131 | + const unsigned long *const clump) |
---|
| 132 | +{ |
---|
| 133 | + unsigned long exp; |
---|
| 134 | + |
---|
| 135 | + if (offset >= size) { |
---|
| 136 | + pr_warn("[%s:%u] bit offset for clump out-of-bounds: expected less than %u, got %u\n", |
---|
| 137 | + srcfile, line, size, offset); |
---|
| 138 | + return false; |
---|
| 139 | + } |
---|
| 140 | + |
---|
| 141 | + exp = clump_exp[offset / 8]; |
---|
| 142 | + if (!exp) { |
---|
| 143 | + pr_warn("[%s:%u] bit offset for zero clump: expected nonzero clump, got bit offset %u with clump value 0", |
---|
| 144 | + srcfile, line, offset); |
---|
| 145 | + return false; |
---|
| 146 | + } |
---|
| 147 | + |
---|
| 148 | + if (*clump != exp) { |
---|
| 149 | + pr_warn("[%s:%u] expected clump value of 0x%lX, got clump value of 0x%lX", |
---|
| 150 | + srcfile, line, exp, *clump); |
---|
| 151 | + return false; |
---|
| 152 | + } |
---|
| 153 | + |
---|
| 154 | + return true; |
---|
| 155 | +} |
---|
| 156 | + |
---|
91 | 157 | #define __expect_eq(suffix, ...) \ |
---|
92 | 158 | ({ \ |
---|
93 | 159 | int result = 0; \ |
---|
.. | .. |
---|
104 | 170 | #define expect_eq_bitmap(...) __expect_eq(bitmap, ##__VA_ARGS__) |
---|
105 | 171 | #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__) |
---|
106 | 172 | #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__) |
---|
| 173 | +#define expect_eq_clump8(...) __expect_eq(clump8, ##__VA_ARGS__) |
---|
107 | 174 | |
---|
108 | 175 | static void __init test_zero_clear(void) |
---|
109 | 176 | { |
---|
.. | .. |
---|
202 | 269 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); |
---|
203 | 270 | } |
---|
204 | 271 | |
---|
205 | | -#define PARSE_TIME 0x1 |
---|
| 272 | +#define EXP2_IN_BITS (sizeof(exp2) * 8) |
---|
| 273 | + |
---|
| 274 | +static void __init test_replace(void) |
---|
| 275 | +{ |
---|
| 276 | + unsigned int nbits = 64; |
---|
| 277 | + unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG); |
---|
| 278 | + DECLARE_BITMAP(bmap, 1024); |
---|
| 279 | + |
---|
| 280 | + BUILD_BUG_ON(EXP2_IN_BITS < nbits * 2); |
---|
| 281 | + |
---|
| 282 | + bitmap_zero(bmap, 1024); |
---|
| 283 | + bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits); |
---|
| 284 | + expect_eq_bitmap(bmap, exp3_0_1, nbits); |
---|
| 285 | + |
---|
| 286 | + bitmap_zero(bmap, 1024); |
---|
| 287 | + bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits); |
---|
| 288 | + expect_eq_bitmap(bmap, exp3_1_0, nbits); |
---|
| 289 | + |
---|
| 290 | + bitmap_fill(bmap, 1024); |
---|
| 291 | + bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits); |
---|
| 292 | + expect_eq_bitmap(bmap, exp3_0_1, nbits); |
---|
| 293 | + |
---|
| 294 | + bitmap_fill(bmap, 1024); |
---|
| 295 | + bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits); |
---|
| 296 | + expect_eq_bitmap(bmap, exp3_1_0, nbits); |
---|
| 297 | +} |
---|
| 298 | + |
---|
| 299 | +#define PARSE_TIME 0x1 |
---|
| 300 | +#define NO_LEN 0x2 |
---|
206 | 301 | |
---|
207 | 302 | struct test_bitmap_parselist{ |
---|
208 | 303 | const int errno; |
---|
.. | .. |
---|
212 | 307 | const int flags; |
---|
213 | 308 | }; |
---|
214 | 309 | |
---|
215 | | -static const unsigned long exp[] __initconst = { |
---|
216 | | - BITMAP_FROM_U64(1), |
---|
217 | | - BITMAP_FROM_U64(2), |
---|
218 | | - BITMAP_FROM_U64(0x0000ffff), |
---|
219 | | - BITMAP_FROM_U64(0xffff0000), |
---|
220 | | - BITMAP_FROM_U64(0x55555555), |
---|
221 | | - BITMAP_FROM_U64(0xaaaaaaaa), |
---|
222 | | - BITMAP_FROM_U64(0x11111111), |
---|
223 | | - BITMAP_FROM_U64(0x22222222), |
---|
224 | | - BITMAP_FROM_U64(0xffffffff), |
---|
225 | | - BITMAP_FROM_U64(0xfffffffe), |
---|
226 | | - BITMAP_FROM_U64(0x3333333311111111ULL), |
---|
227 | | - BITMAP_FROM_U64(0xffffffff77777777ULL) |
---|
228 | | -}; |
---|
229 | | - |
---|
230 | | -static const unsigned long exp2[] __initconst = { |
---|
231 | | - BITMAP_FROM_U64(0x3333333311111111ULL), |
---|
232 | | - BITMAP_FROM_U64(0xffffffff77777777ULL) |
---|
233 | | -}; |
---|
234 | | - |
---|
235 | 310 | static const struct test_bitmap_parselist parselist_tests[] __initconst = { |
---|
236 | 311 | #define step (sizeof(u64) / sizeof(unsigned long)) |
---|
237 | 312 | |
---|
238 | | - {0, "0", &exp[0], 8, 0}, |
---|
239 | | - {0, "1", &exp[1 * step], 8, 0}, |
---|
240 | | - {0, "0-15", &exp[2 * step], 32, 0}, |
---|
241 | | - {0, "16-31", &exp[3 * step], 32, 0}, |
---|
242 | | - {0, "0-31:1/2", &exp[4 * step], 32, 0}, |
---|
243 | | - {0, "1-31:1/2", &exp[5 * step], 32, 0}, |
---|
244 | | - {0, "0-31:1/4", &exp[6 * step], 32, 0}, |
---|
245 | | - {0, "1-31:1/4", &exp[7 * step], 32, 0}, |
---|
246 | | - {0, "0-31:4/4", &exp[8 * step], 32, 0}, |
---|
247 | | - {0, "1-31:4/4", &exp[9 * step], 32, 0}, |
---|
248 | | - {0, "0-31:1/4,32-63:2/4", &exp[10 * step], 64, 0}, |
---|
249 | | - {0, "0-31:3/4,32-63:4/4", &exp[11 * step], 64, 0}, |
---|
| 313 | + {0, "0", &exp1[0], 8, 0}, |
---|
| 314 | + {0, "1", &exp1[1 * step], 8, 0}, |
---|
| 315 | + {0, "0-15", &exp1[2 * step], 32, 0}, |
---|
| 316 | + {0, "16-31", &exp1[3 * step], 32, 0}, |
---|
| 317 | + {0, "0-31:1/2", &exp1[4 * step], 32, 0}, |
---|
| 318 | + {0, "1-31:1/2", &exp1[5 * step], 32, 0}, |
---|
| 319 | + {0, "0-31:1/4", &exp1[6 * step], 32, 0}, |
---|
| 320 | + {0, "1-31:1/4", &exp1[7 * step], 32, 0}, |
---|
| 321 | + {0, "0-31:4/4", &exp1[8 * step], 32, 0}, |
---|
| 322 | + {0, "1-31:4/4", &exp1[9 * step], 32, 0}, |
---|
| 323 | + {0, "0-31:1/4,32-63:2/4", &exp1[10 * step], 64, 0}, |
---|
| 324 | + {0, "0-31:3/4,32-63:4/4", &exp1[11 * step], 64, 0}, |
---|
| 325 | + {0, " ,, 0-31:3/4 ,, 32-63:4/4 ,, ", &exp1[11 * step], 64, 0}, |
---|
250 | 326 | |
---|
251 | 327 | {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4", exp2, 128, 0}, |
---|
252 | 328 | |
---|
253 | 329 | {0, "0-2047:128/256", NULL, 2048, PARSE_TIME}, |
---|
| 330 | + |
---|
| 331 | + {0, "", &exp1[12 * step], 8, 0}, |
---|
| 332 | + {0, "\n", &exp1[12 * step], 8, 0}, |
---|
| 333 | + {0, ",, ,, , , ,", &exp1[12 * step], 8, 0}, |
---|
| 334 | + {0, " , ,, , , ", &exp1[12 * step], 8, 0}, |
---|
| 335 | + {0, " , ,, , , \n", &exp1[12 * step], 8, 0}, |
---|
254 | 336 | |
---|
255 | 337 | {-EINVAL, "-1", NULL, 8, 0}, |
---|
256 | 338 | {-EINVAL, "-0", NULL, 8, 0}, |
---|
257 | 339 | {-EINVAL, "10-1", NULL, 8, 0}, |
---|
258 | 340 | {-EINVAL, "0-31:", NULL, 8, 0}, |
---|
259 | 341 | {-EINVAL, "0-31:0", NULL, 8, 0}, |
---|
| 342 | + {-EINVAL, "0-31:0/", NULL, 8, 0}, |
---|
260 | 343 | {-EINVAL, "0-31:0/0", NULL, 8, 0}, |
---|
261 | 344 | {-EINVAL, "0-31:1/0", NULL, 8, 0}, |
---|
262 | 345 | {-EINVAL, "0-31:10/1", NULL, 8, 0}, |
---|
| 346 | + {-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0}, |
---|
| 347 | + |
---|
| 348 | + {-EINVAL, "a-31", NULL, 8, 0}, |
---|
| 349 | + {-EINVAL, "0-a1", NULL, 8, 0}, |
---|
| 350 | + {-EINVAL, "a-31:10/1", NULL, 8, 0}, |
---|
| 351 | + {-EINVAL, "0-31:a/1", NULL, 8, 0}, |
---|
| 352 | + {-EINVAL, "0-\n", NULL, 8, 0}, |
---|
| 353 | + |
---|
263 | 354 | }; |
---|
264 | 355 | |
---|
265 | 356 | static void __init test_bitmap_parselist(void) |
---|
266 | 357 | { |
---|
267 | 358 | int i; |
---|
268 | 359 | int err; |
---|
269 | | - cycles_t cycles; |
---|
| 360 | + ktime_t time; |
---|
270 | 361 | DECLARE_BITMAP(bmap, 2048); |
---|
271 | 362 | |
---|
272 | 363 | for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) { |
---|
273 | 364 | #define ptest parselist_tests[i] |
---|
274 | 365 | |
---|
275 | | - cycles = get_cycles(); |
---|
| 366 | + time = ktime_get(); |
---|
276 | 367 | err = bitmap_parselist(ptest.in, bmap, ptest.nbits); |
---|
277 | | - cycles = get_cycles() - cycles; |
---|
| 368 | + time = ktime_get() - time; |
---|
278 | 369 | |
---|
279 | 370 | if (err != ptest.errno) { |
---|
280 | | - pr_err("test %d: input is %s, errno is %d, expected %d\n", |
---|
| 371 | + pr_err("parselist: %d: input is %s, errno is %d, expected %d\n", |
---|
281 | 372 | i, ptest.in, err, ptest.errno); |
---|
282 | 373 | continue; |
---|
283 | 374 | } |
---|
284 | 375 | |
---|
285 | 376 | if (!err && ptest.expected |
---|
286 | 377 | && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) { |
---|
287 | | - pr_err("test %d: input is %s, result is 0x%lx, expected 0x%lx\n", |
---|
288 | | - i, ptest.in, bmap[0], *ptest.expected); |
---|
| 378 | + pr_err("parselist: %d: input is %s, result is 0x%lx, expected 0x%lx\n", |
---|
| 379 | + i, ptest.in, bmap[0], |
---|
| 380 | + *ptest.expected); |
---|
289 | 381 | continue; |
---|
290 | 382 | } |
---|
291 | 383 | |
---|
292 | 384 | if (ptest.flags & PARSE_TIME) |
---|
293 | | - pr_err("test %d: input is '%s' OK, Time: %llu\n", |
---|
294 | | - i, ptest.in, |
---|
295 | | - (unsigned long long)cycles); |
---|
| 385 | + pr_err("parselist: %d: input is '%s' OK, Time: %llu\n", |
---|
| 386 | + i, ptest.in, time); |
---|
| 387 | + |
---|
| 388 | +#undef ptest |
---|
296 | 389 | } |
---|
297 | 390 | } |
---|
298 | 391 | |
---|
299 | | -#define EXP_BYTES (sizeof(exp) * 8) |
---|
| 392 | +static const unsigned long parse_test[] __initconst = { |
---|
| 393 | + BITMAP_FROM_U64(0), |
---|
| 394 | + BITMAP_FROM_U64(1), |
---|
| 395 | + BITMAP_FROM_U64(0xdeadbeef), |
---|
| 396 | + BITMAP_FROM_U64(0x100000000ULL), |
---|
| 397 | +}; |
---|
| 398 | + |
---|
| 399 | +static const unsigned long parse_test2[] __initconst = { |
---|
| 400 | + BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xdeadbeef), |
---|
| 401 | + BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xbaadf00ddeadbeef), |
---|
| 402 | + BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0x0badf00ddeadbeef), |
---|
| 403 | +}; |
---|
| 404 | + |
---|
| 405 | +static const struct test_bitmap_parselist parse_tests[] __initconst = { |
---|
| 406 | + {0, "", &parse_test[0 * step], 32, 0}, |
---|
| 407 | + {0, " ", &parse_test[0 * step], 32, 0}, |
---|
| 408 | + {0, "0", &parse_test[0 * step], 32, 0}, |
---|
| 409 | + {0, "0\n", &parse_test[0 * step], 32, 0}, |
---|
| 410 | + {0, "1", &parse_test[1 * step], 32, 0}, |
---|
| 411 | + {0, "deadbeef", &parse_test[2 * step], 32, 0}, |
---|
| 412 | + {0, "1,0", &parse_test[3 * step], 33, 0}, |
---|
| 413 | + {0, "deadbeef,\n,0,1", &parse_test[2 * step], 96, 0}, |
---|
| 414 | + |
---|
| 415 | + {0, "deadbeef,1,0", &parse_test2[0 * 2 * step], 96, 0}, |
---|
| 416 | + {0, "baadf00d,deadbeef,1,0", &parse_test2[1 * 2 * step], 128, 0}, |
---|
| 417 | + {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, 0}, |
---|
| 418 | + {0, "badf00d,deadbeef,1,0", &parse_test2[2 * 2 * step], 124, NO_LEN}, |
---|
| 419 | + {0, " badf00d,deadbeef,1,0 ", &parse_test2[2 * 2 * step], 124, 0}, |
---|
| 420 | + {0, " , badf00d,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0}, |
---|
| 421 | + {0, " , badf00d, ,, ,,deadbeef,1,0 , ", &parse_test2[2 * 2 * step], 124, 0}, |
---|
| 422 | + |
---|
| 423 | + {-EINVAL, "goodfood,deadbeef,1,0", NULL, 128, 0}, |
---|
| 424 | + {-EOVERFLOW, "3,0", NULL, 33, 0}, |
---|
| 425 | + {-EOVERFLOW, "123badf00d,deadbeef,1,0", NULL, 128, 0}, |
---|
| 426 | + {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 90, 0}, |
---|
| 427 | + {-EOVERFLOW, "fbadf00d,deadbeef,1,0", NULL, 95, 0}, |
---|
| 428 | + {-EOVERFLOW, "badf00d,deadbeef,1,0", NULL, 100, 0}, |
---|
| 429 | +#undef step |
---|
| 430 | +}; |
---|
| 431 | + |
---|
| 432 | +static void __init test_bitmap_parse(void) |
---|
| 433 | +{ |
---|
| 434 | + int i; |
---|
| 435 | + int err; |
---|
| 436 | + ktime_t time; |
---|
| 437 | + DECLARE_BITMAP(bmap, 2048); |
---|
| 438 | + |
---|
| 439 | + for (i = 0; i < ARRAY_SIZE(parse_tests); i++) { |
---|
| 440 | + struct test_bitmap_parselist test = parse_tests[i]; |
---|
| 441 | + size_t len = test.flags & NO_LEN ? UINT_MAX : strlen(test.in); |
---|
| 442 | + |
---|
| 443 | + time = ktime_get(); |
---|
| 444 | + err = bitmap_parse(test.in, len, bmap, test.nbits); |
---|
| 445 | + time = ktime_get() - time; |
---|
| 446 | + |
---|
| 447 | + if (err != test.errno) { |
---|
| 448 | + pr_err("parse: %d: input is %s, errno is %d, expected %d\n", |
---|
| 449 | + i, test.in, err, test.errno); |
---|
| 450 | + continue; |
---|
| 451 | + } |
---|
| 452 | + |
---|
| 453 | + if (!err && test.expected |
---|
| 454 | + && !__bitmap_equal(bmap, test.expected, test.nbits)) { |
---|
| 455 | + pr_err("parse: %d: input is %s, result is 0x%lx, expected 0x%lx\n", |
---|
| 456 | + i, test.in, bmap[0], |
---|
| 457 | + *test.expected); |
---|
| 458 | + continue; |
---|
| 459 | + } |
---|
| 460 | + |
---|
| 461 | + if (test.flags & PARSE_TIME) |
---|
| 462 | + pr_err("parse: %d: input is '%s' OK, Time: %llu\n", |
---|
| 463 | + i, test.in, time); |
---|
| 464 | + } |
---|
| 465 | +} |
---|
| 466 | + |
---|
| 467 | +#define EXP1_IN_BITS (sizeof(exp1) * 8) |
---|
300 | 468 | |
---|
301 | 469 | static void __init test_bitmap_arr32(void) |
---|
302 | 470 | { |
---|
303 | 471 | unsigned int nbits, next_bit; |
---|
304 | | - u32 arr[sizeof(exp) / 4]; |
---|
305 | | - DECLARE_BITMAP(bmap2, EXP_BYTES); |
---|
| 472 | + u32 arr[EXP1_IN_BITS / 32]; |
---|
| 473 | + DECLARE_BITMAP(bmap2, EXP1_IN_BITS); |
---|
306 | 474 | |
---|
307 | 475 | memset(arr, 0xa5, sizeof(arr)); |
---|
308 | 476 | |
---|
309 | | - for (nbits = 0; nbits < EXP_BYTES; ++nbits) { |
---|
310 | | - bitmap_to_arr32(arr, exp, nbits); |
---|
| 477 | + for (nbits = 0; nbits < EXP1_IN_BITS; ++nbits) { |
---|
| 478 | + bitmap_to_arr32(arr, exp1, nbits); |
---|
311 | 479 | bitmap_from_arr32(bmap2, arr, nbits); |
---|
312 | | - expect_eq_bitmap(bmap2, exp, nbits); |
---|
| 480 | + expect_eq_bitmap(bmap2, exp1, nbits); |
---|
313 | 481 | |
---|
314 | 482 | next_bit = find_next_bit(bmap2, |
---|
315 | 483 | round_up(nbits, BITS_PER_LONG), nbits); |
---|
.. | .. |
---|
318 | 486 | " tail is not safely cleared: %d\n", |
---|
319 | 487 | nbits, next_bit); |
---|
320 | 488 | |
---|
321 | | - if (nbits < EXP_BYTES - 32) |
---|
| 489 | + if (nbits < EXP1_IN_BITS - 32) |
---|
322 | 490 | expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)], |
---|
323 | 491 | 0xa5a5a5a5); |
---|
324 | 492 | } |
---|
.. | .. |
---|
361 | 529 | } |
---|
362 | 530 | } |
---|
363 | 531 | |
---|
364 | | -static int __init test_bitmap_init(void) |
---|
| 532 | +static const unsigned char clump_exp[] __initconst = { |
---|
| 533 | + 0x01, /* 1 bit set */ |
---|
| 534 | + 0x02, /* non-edge 1 bit set */ |
---|
| 535 | + 0x00, /* zero bits set */ |
---|
| 536 | + 0x38, /* 3 bits set across 4-bit boundary */ |
---|
| 537 | + 0x38, /* Repeated clump */ |
---|
| 538 | + 0x0F, /* 4 bits set */ |
---|
| 539 | + 0xFF, /* all bits set */ |
---|
| 540 | + 0x05, /* non-adjacent 2 bits set */ |
---|
| 541 | +}; |
---|
| 542 | + |
---|
| 543 | +static void __init test_for_each_set_clump8(void) |
---|
| 544 | +{ |
---|
| 545 | +#define CLUMP_EXP_NUMBITS 64 |
---|
| 546 | + DECLARE_BITMAP(bits, CLUMP_EXP_NUMBITS); |
---|
| 547 | + unsigned int start; |
---|
| 548 | + unsigned long clump; |
---|
| 549 | + |
---|
| 550 | + /* set bitmap to test case */ |
---|
| 551 | + bitmap_zero(bits, CLUMP_EXP_NUMBITS); |
---|
| 552 | + bitmap_set(bits, 0, 1); /* 0x01 */ |
---|
| 553 | + bitmap_set(bits, 9, 1); /* 0x02 */ |
---|
| 554 | + bitmap_set(bits, 27, 3); /* 0x28 */ |
---|
| 555 | + bitmap_set(bits, 35, 3); /* 0x28 */ |
---|
| 556 | + bitmap_set(bits, 40, 4); /* 0x0F */ |
---|
| 557 | + bitmap_set(bits, 48, 8); /* 0xFF */ |
---|
| 558 | + bitmap_set(bits, 56, 1); /* 0x05 - part 1 */ |
---|
| 559 | + bitmap_set(bits, 58, 1); /* 0x05 - part 2 */ |
---|
| 560 | + |
---|
| 561 | + for_each_set_clump8(start, clump, bits, CLUMP_EXP_NUMBITS) |
---|
| 562 | + expect_eq_clump8(start, CLUMP_EXP_NUMBITS, clump_exp, &clump); |
---|
| 563 | +} |
---|
| 564 | + |
---|
| 565 | +struct test_bitmap_cut { |
---|
| 566 | + unsigned int first; |
---|
| 567 | + unsigned int cut; |
---|
| 568 | + unsigned int nbits; |
---|
| 569 | + unsigned long in[4]; |
---|
| 570 | + unsigned long expected[4]; |
---|
| 571 | +}; |
---|
| 572 | + |
---|
| 573 | +static struct test_bitmap_cut test_cut[] = { |
---|
| 574 | + { 0, 0, 8, { 0x0000000aUL, }, { 0x0000000aUL, }, }, |
---|
| 575 | + { 0, 0, 32, { 0xdadadeadUL, }, { 0xdadadeadUL, }, }, |
---|
| 576 | + { 0, 3, 8, { 0x000000aaUL, }, { 0x00000015UL, }, }, |
---|
| 577 | + { 3, 3, 8, { 0x000000aaUL, }, { 0x00000012UL, }, }, |
---|
| 578 | + { 0, 1, 32, { 0xa5a5a5a5UL, }, { 0x52d2d2d2UL, }, }, |
---|
| 579 | + { 0, 8, 32, { 0xdeadc0deUL, }, { 0x00deadc0UL, }, }, |
---|
| 580 | + { 1, 1, 32, { 0x5a5a5a5aUL, }, { 0x2d2d2d2cUL, }, }, |
---|
| 581 | + { 0, 15, 32, { 0xa5a5a5a5UL, }, { 0x00014b4bUL, }, }, |
---|
| 582 | + { 0, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, }, |
---|
| 583 | + { 15, 15, 32, { 0xa5a5a5a5UL, }, { 0x000125a5UL, }, }, |
---|
| 584 | + { 15, 16, 32, { 0xa5a5a5a5UL, }, { 0x0000a5a5UL, }, }, |
---|
| 585 | + { 16, 15, 32, { 0xa5a5a5a5UL, }, { 0x0001a5a5UL, }, }, |
---|
| 586 | + |
---|
| 587 | + { BITS_PER_LONG, BITS_PER_LONG, BITS_PER_LONG, |
---|
| 588 | + { 0xa5a5a5a5UL, 0xa5a5a5a5UL, }, |
---|
| 589 | + { 0xa5a5a5a5UL, 0xa5a5a5a5UL, }, |
---|
| 590 | + }, |
---|
| 591 | + { 1, BITS_PER_LONG - 1, BITS_PER_LONG, |
---|
| 592 | + { 0xa5a5a5a5UL, 0xa5a5a5a5UL, }, |
---|
| 593 | + { 0x00000001UL, 0x00000001UL, }, |
---|
| 594 | + }, |
---|
| 595 | + |
---|
| 596 | + { 0, BITS_PER_LONG * 2, BITS_PER_LONG * 2 + 1, |
---|
| 597 | + { 0xa5a5a5a5UL, 0x00000001UL, 0x00000001UL, 0x00000001UL }, |
---|
| 598 | + { 0x00000001UL, }, |
---|
| 599 | + }, |
---|
| 600 | + { 16, BITS_PER_LONG * 2 + 1, BITS_PER_LONG * 2 + 1 + 16, |
---|
| 601 | + { 0x0000ffffUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL, 0x5a5a5a5aUL }, |
---|
| 602 | + { 0x2d2dffffUL, }, |
---|
| 603 | + }, |
---|
| 604 | +}; |
---|
| 605 | + |
---|
| 606 | +static void __init test_bitmap_cut(void) |
---|
| 607 | +{ |
---|
| 608 | + unsigned long b[5], *in = &b[1], *out = &b[0]; /* Partial overlap */ |
---|
| 609 | + int i; |
---|
| 610 | + |
---|
| 611 | + for (i = 0; i < ARRAY_SIZE(test_cut); i++) { |
---|
| 612 | + struct test_bitmap_cut *t = &test_cut[i]; |
---|
| 613 | + |
---|
| 614 | + memcpy(in, t->in, sizeof(t->in)); |
---|
| 615 | + |
---|
| 616 | + bitmap_cut(out, in, t->first, t->cut, t->nbits); |
---|
| 617 | + |
---|
| 618 | + expect_eq_bitmap(t->expected, out, t->nbits); |
---|
| 619 | + } |
---|
| 620 | +} |
---|
| 621 | + |
---|
| 622 | +static void __init selftest(void) |
---|
365 | 623 | { |
---|
366 | 624 | test_zero_clear(); |
---|
367 | 625 | test_fill_set(); |
---|
368 | 626 | test_copy(); |
---|
| 627 | + test_replace(); |
---|
369 | 628 | test_bitmap_arr32(); |
---|
| 629 | + test_bitmap_parse(); |
---|
370 | 630 | test_bitmap_parselist(); |
---|
371 | 631 | test_mem_optimisations(); |
---|
372 | | - |
---|
373 | | - if (failed_tests == 0) |
---|
374 | | - pr_info("all %u tests passed\n", total_tests); |
---|
375 | | - else |
---|
376 | | - pr_warn("failed %u out of %u tests\n", |
---|
377 | | - failed_tests, total_tests); |
---|
378 | | - |
---|
379 | | - return failed_tests ? -EINVAL : 0; |
---|
| 632 | + test_for_each_set_clump8(); |
---|
| 633 | + test_bitmap_cut(); |
---|
380 | 634 | } |
---|
381 | 635 | |
---|
382 | | -static void __exit test_bitmap_cleanup(void) |
---|
383 | | -{ |
---|
384 | | -} |
---|
385 | | - |
---|
386 | | -module_init(test_bitmap_init); |
---|
387 | | -module_exit(test_bitmap_cleanup); |
---|
388 | | - |
---|
| 636 | +KSTM_MODULE_LOADERS(test_bitmap); |
---|
389 | 637 | MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>"); |
---|
390 | 638 | MODULE_LICENSE("GPL"); |
---|