diff options
Diffstat (limited to 'lib/test_bitmap.c')
-rw-r--r-- | lib/test_bitmap.c | 206 |
1 files changed, 31 insertions, 175 deletions
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index aa1f2669bdd5..de7ef2996a07 100644 --- a/lib/test_bitmap.c +++ b/lib/test_bitmap.c | |||
@@ -23,7 +23,7 @@ __check_eq_uint(const char *srcfile, unsigned int line, | |||
23 | const unsigned int exp_uint, unsigned int x) | 23 | const unsigned int exp_uint, unsigned int x) |
24 | { | 24 | { |
25 | if (exp_uint != x) { | 25 | if (exp_uint != x) { |
26 | pr_warn("[%s:%u] expected %u, got %u\n", | 26 | pr_err("[%s:%u] expected %u, got %u\n", |
27 | srcfile, line, exp_uint, x); | 27 | srcfile, line, exp_uint, x); |
28 | return false; | 28 | return false; |
29 | } | 29 | } |
@@ -33,19 +33,13 @@ __check_eq_uint(const char *srcfile, unsigned int line, | |||
33 | 33 | ||
34 | static bool __init | 34 | static bool __init |
35 | __check_eq_bitmap(const char *srcfile, unsigned int line, | 35 | __check_eq_bitmap(const char *srcfile, unsigned int line, |
36 | const unsigned long *exp_bmap, unsigned int exp_nbits, | 36 | const unsigned long *exp_bmap, const unsigned long *bmap, |
37 | const unsigned long *bmap, unsigned int nbits) | 37 | unsigned int nbits) |
38 | { | 38 | { |
39 | if (exp_nbits != nbits) { | ||
40 | pr_warn("[%s:%u] bitmap length mismatch: expected %u, got %u\n", | ||
41 | srcfile, line, exp_nbits, nbits); | ||
42 | return false; | ||
43 | } | ||
44 | |||
45 | if (!bitmap_equal(exp_bmap, bmap, nbits)) { | 39 | if (!bitmap_equal(exp_bmap, bmap, nbits)) { |
46 | pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n", | 40 | pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n", |
47 | srcfile, line, | 41 | srcfile, line, |
48 | exp_nbits, exp_bmap, nbits, bmap); | 42 | nbits, exp_bmap, nbits, bmap); |
49 | return false; | 43 | return false; |
50 | } | 44 | } |
51 | return true; | 45 | return true; |
@@ -69,6 +63,10 @@ __check_eq_pbl(const char *srcfile, unsigned int line, | |||
69 | static bool __init | 63 | static bool __init |
70 | __check_eq_u32_array(const char *srcfile, unsigned int line, | 64 | __check_eq_u32_array(const char *srcfile, unsigned int line, |
71 | const u32 *exp_arr, unsigned int exp_len, | 65 | const u32 *exp_arr, unsigned int exp_len, |
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, | ||
72 | const u32 *arr, unsigned int len) | 70 | const u32 *arr, unsigned int len) |
73 | { | 71 | { |
74 | if (exp_len != len) { | 72 | if (exp_len != len) { |
@@ -255,171 +253,29 @@ static void __init test_bitmap_parselist(void) | |||
255 | } | 253 | } |
256 | } | 254 | } |
257 | 255 | ||
258 | static void __init test_bitmap_u32_array_conversions(void) | 256 | static void __init test_bitmap_arr32(void) |
259 | { | 257 | { |
260 | DECLARE_BITMAP(bmap1, 1024); | 258 | unsigned int nbits, next_bit, len = sizeof(exp) * 8; |
261 | DECLARE_BITMAP(bmap2, 1024); | 259 | u32 arr[sizeof(exp) / 4]; |
262 | u32 exp_arr[32], arr[32]; | 260 | DECLARE_BITMAP(bmap2, len); |
263 | unsigned nbits; | 261 | |
264 | 262 | memset(arr, 0xa5, sizeof(arr)); | |
265 | for (nbits = 0 ; nbits < 257 ; ++nbits) { | 263 | |
266 | const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32); | 264 | for (nbits = 0; nbits < len; ++nbits) { |
267 | unsigned int i, rv; | 265 | bitmap_to_arr32(arr, exp, nbits); |
268 | 266 | bitmap_from_arr32(bmap2, arr, nbits); | |
269 | bitmap_zero(bmap1, nbits); | 267 | expect_eq_bitmap(bmap2, exp, nbits); |
270 | bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */ | 268 | |
271 | 269 | next_bit = find_next_bit(bmap2, | |
272 | memset(arr, 0xff, sizeof(arr)); | 270 | round_up(nbits, BITS_PER_LONG), nbits); |
273 | rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits); | 271 | if (next_bit < round_up(nbits, BITS_PER_LONG)) |
274 | expect_eq_uint(nbits, rv); | 272 | pr_err("bitmap_copy_arr32(nbits == %d:" |
275 | 273 | " tail is not safely cleared: %d\n", | |
276 | memset(exp_arr, 0xff, sizeof(exp_arr)); | 274 | nbits, next_bit); |
277 | memset(exp_arr, 0, used_u32s*sizeof(*exp_arr)); | 275 | |
278 | expect_eq_u32_array(exp_arr, 32, arr, 32); | 276 | if (nbits < len - 32) |
279 | 277 | expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)], | |
280 | bitmap_fill(bmap2, 1024); | 278 | 0xa5a5a5a5); |
281 | rv = bitmap_from_u32array(bmap2, nbits, arr, used_u32s); | ||
282 | expect_eq_uint(nbits, rv); | ||
283 | expect_eq_bitmap(bmap1, 1024, bmap2, 1024); | ||
284 | |||
285 | for (i = 0 ; i < nbits ; ++i) { | ||
286 | /* | ||
287 | * test conversion bitmap -> u32[] | ||
288 | */ | ||
289 | |||
290 | bitmap_zero(bmap1, 1024); | ||
291 | __set_bit(i, bmap1); | ||
292 | bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */ | ||
293 | |||
294 | memset(arr, 0xff, sizeof(arr)); | ||
295 | rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits); | ||
296 | expect_eq_uint(nbits, rv); | ||
297 | |||
298 | /* 1st used u32 words contain expected bit set, the | ||
299 | * remaining words are left unchanged (0xff) | ||
300 | */ | ||
301 | memset(exp_arr, 0xff, sizeof(exp_arr)); | ||
302 | memset(exp_arr, 0, used_u32s*sizeof(*exp_arr)); | ||
303 | exp_arr[i/32] = (1U<<(i%32)); | ||
304 | expect_eq_u32_array(exp_arr, 32, arr, 32); | ||
305 | |||
306 | |||
307 | /* same, with longer array to fill | ||
308 | */ | ||
309 | memset(arr, 0xff, sizeof(arr)); | ||
310 | rv = bitmap_to_u32array(arr, 32, bmap1, nbits); | ||
311 | expect_eq_uint(nbits, rv); | ||
312 | |||
313 | /* 1st used u32 words contain expected bit set, the | ||
314 | * remaining words are all 0s | ||
315 | */ | ||
316 | memset(exp_arr, 0, sizeof(exp_arr)); | ||
317 | exp_arr[i/32] = (1U<<(i%32)); | ||
318 | expect_eq_u32_array(exp_arr, 32, arr, 32); | ||
319 | |||
320 | /* | ||
321 | * test conversion u32[] -> bitmap | ||
322 | */ | ||
323 | |||
324 | /* the 1st nbits of bmap2 are identical to | ||
325 | * bmap1, the remaining bits of bmap2 are left | ||
326 | * unchanged (all 1s) | ||
327 | */ | ||
328 | bitmap_fill(bmap2, 1024); | ||
329 | rv = bitmap_from_u32array(bmap2, nbits, | ||
330 | exp_arr, used_u32s); | ||
331 | expect_eq_uint(nbits, rv); | ||
332 | |||
333 | expect_eq_bitmap(bmap1, 1024, bmap2, 1024); | ||
334 | |||
335 | /* same, with more bits to fill | ||
336 | */ | ||
337 | memset(arr, 0xff, sizeof(arr)); /* garbage */ | ||
338 | memset(arr, 0, used_u32s*sizeof(u32)); | ||
339 | arr[i/32] = (1U<<(i%32)); | ||
340 | |||
341 | bitmap_fill(bmap2, 1024); | ||
342 | rv = bitmap_from_u32array(bmap2, 1024, arr, used_u32s); | ||
343 | expect_eq_uint(used_u32s*32, rv); | ||
344 | |||
345 | /* the 1st nbits of bmap2 are identical to | ||
346 | * bmap1, the remaining bits of bmap2 are cleared | ||
347 | */ | ||
348 | bitmap_zero(bmap1, 1024); | ||
349 | __set_bit(i, bmap1); | ||
350 | expect_eq_bitmap(bmap1, 1024, bmap2, 1024); | ||
351 | |||
352 | |||
353 | /* | ||
354 | * test short conversion bitmap -> u32[] (1 | ||
355 | * word too short) | ||
356 | */ | ||
357 | if (used_u32s > 1) { | ||
358 | bitmap_zero(bmap1, 1024); | ||
359 | __set_bit(i, bmap1); | ||
360 | bitmap_set(bmap1, nbits, | ||
361 | 1024 - nbits); /* garbage */ | ||
362 | memset(arr, 0xff, sizeof(arr)); | ||
363 | |||
364 | rv = bitmap_to_u32array(arr, used_u32s - 1, | ||
365 | bmap1, nbits); | ||
366 | expect_eq_uint((used_u32s - 1)*32, rv); | ||
367 | |||
368 | /* 1st used u32 words contain expected | ||
369 | * bit set, the remaining words are | ||
370 | * left unchanged (0xff) | ||
371 | */ | ||
372 | memset(exp_arr, 0xff, sizeof(exp_arr)); | ||
373 | memset(exp_arr, 0, | ||
374 | (used_u32s-1)*sizeof(*exp_arr)); | ||
375 | if ((i/32) < (used_u32s - 1)) | ||
376 | exp_arr[i/32] = (1U<<(i%32)); | ||
377 | expect_eq_u32_array(exp_arr, 32, arr, 32); | ||
378 | } | ||
379 | |||
380 | /* | ||
381 | * test short conversion u32[] -> bitmap (3 | ||
382 | * bits too short) | ||
383 | */ | ||
384 | if (nbits > 3) { | ||
385 | memset(arr, 0xff, sizeof(arr)); /* garbage */ | ||
386 | memset(arr, 0, used_u32s*sizeof(*arr)); | ||
387 | arr[i/32] = (1U<<(i%32)); | ||
388 | |||
389 | bitmap_zero(bmap1, 1024); | ||
390 | rv = bitmap_from_u32array(bmap1, nbits - 3, | ||
391 | arr, used_u32s); | ||
392 | expect_eq_uint(nbits - 3, rv); | ||
393 | |||
394 | /* we are expecting the bit < nbits - | ||
395 | * 3 (none otherwise), and the rest of | ||
396 | * bmap1 unchanged (0-filled) | ||
397 | */ | ||
398 | bitmap_zero(bmap2, 1024); | ||
399 | if (i < nbits - 3) | ||
400 | __set_bit(i, bmap2); | ||
401 | expect_eq_bitmap(bmap2, 1024, bmap1, 1024); | ||
402 | |||
403 | /* do the same with bmap1 initially | ||
404 | * 1-filled | ||
405 | */ | ||
406 | |||
407 | bitmap_fill(bmap1, 1024); | ||
408 | rv = bitmap_from_u32array(bmap1, nbits - 3, | ||
409 | arr, used_u32s); | ||
410 | expect_eq_uint(nbits - 3, rv); | ||
411 | |||
412 | /* we are expecting the bit < nbits - | ||
413 | * 3 (none otherwise), and the rest of | ||
414 | * bmap1 unchanged (1-filled) | ||
415 | */ | ||
416 | bitmap_zero(bmap2, 1024); | ||
417 | if (i < nbits - 3) | ||
418 | __set_bit(i, bmap2); | ||
419 | bitmap_set(bmap2, nbits-3, 1024 - nbits + 3); | ||
420 | expect_eq_bitmap(bmap2, 1024, bmap1, 1024); | ||
421 | } | ||
422 | } | ||
423 | } | 279 | } |
424 | } | 280 | } |
425 | 281 | ||
@@ -454,7 +310,7 @@ static void noinline __init test_mem_optimisations(void) | |||
454 | static int __init test_bitmap_init(void) | 310 | static int __init test_bitmap_init(void) |
455 | { | 311 | { |
456 | test_zero_fill_copy(); | 312 | test_zero_fill_copy(); |
457 | test_bitmap_u32_array_conversions(); | 313 | test_bitmap_arr32(); |
458 | test_bitmap_parselist(); | 314 | test_bitmap_parselist(); |
459 | test_mem_optimisations(); | 315 | test_mem_optimisations(); |
460 | 316 | ||