diff options
Diffstat (limited to 'lib/test_bitmap.c')
-rw-r--r-- | lib/test_bitmap.c | 295 |
1 files changed, 96 insertions, 199 deletions
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index aa1f2669bdd5..b3f235baa05d 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) { |
@@ -107,7 +105,65 @@ __check_eq_u32_array(const char *srcfile, unsigned int line, | |||
107 | #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__) | 105 | #define expect_eq_pbl(...) __expect_eq(pbl, ##__VA_ARGS__) |
108 | #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__) | 106 | #define expect_eq_u32_array(...) __expect_eq(u32_array, ##__VA_ARGS__) |
109 | 107 | ||
110 | static void __init test_zero_fill_copy(void) | 108 | static void __init test_zero_clear(void) |
109 | { | ||
110 | DECLARE_BITMAP(bmap, 1024); | ||
111 | |||
112 | /* Known way to set all bits */ | ||
113 | memset(bmap, 0xff, 128); | ||
114 | |||
115 | expect_eq_pbl("0-22", bmap, 23); | ||
116 | expect_eq_pbl("0-1023", bmap, 1024); | ||
117 | |||
118 | /* single-word bitmaps */ | ||
119 | bitmap_clear(bmap, 0, 9); | ||
120 | expect_eq_pbl("9-1023", bmap, 1024); | ||
121 | |||
122 | bitmap_zero(bmap, 35); | ||
123 | expect_eq_pbl("64-1023", bmap, 1024); | ||
124 | |||
125 | /* cross boundaries operations */ | ||
126 | bitmap_clear(bmap, 79, 19); | ||
127 | expect_eq_pbl("64-78,98-1023", bmap, 1024); | ||
128 | |||
129 | bitmap_zero(bmap, 115); | ||
130 | expect_eq_pbl("128-1023", bmap, 1024); | ||
131 | |||
132 | /* Zeroing entire area */ | ||
133 | bitmap_zero(bmap, 1024); | ||
134 | expect_eq_pbl("", bmap, 1024); | ||
135 | } | ||
136 | |||
137 | static void __init test_fill_set(void) | ||
138 | { | ||
139 | DECLARE_BITMAP(bmap, 1024); | ||
140 | |||
141 | /* Known way to clear all bits */ | ||
142 | memset(bmap, 0x00, 128); | ||
143 | |||
144 | expect_eq_pbl("", bmap, 23); | ||
145 | expect_eq_pbl("", bmap, 1024); | ||
146 | |||
147 | /* single-word bitmaps */ | ||
148 | bitmap_set(bmap, 0, 9); | ||
149 | expect_eq_pbl("0-8", bmap, 1024); | ||
150 | |||
151 | bitmap_fill(bmap, 35); | ||
152 | expect_eq_pbl("0-63", bmap, 1024); | ||
153 | |||
154 | /* cross boundaries operations */ | ||
155 | bitmap_set(bmap, 79, 19); | ||
156 | expect_eq_pbl("0-63,79-97", bmap, 1024); | ||
157 | |||
158 | bitmap_fill(bmap, 115); | ||
159 | expect_eq_pbl("0-127", bmap, 1024); | ||
160 | |||
161 | /* Zeroing entire area */ | ||
162 | bitmap_fill(bmap, 1024); | ||
163 | expect_eq_pbl("0-1023", bmap, 1024); | ||
164 | } | ||
165 | |||
166 | static void __init test_copy(void) | ||
111 | { | 167 | { |
112 | DECLARE_BITMAP(bmap1, 1024); | 168 | DECLARE_BITMAP(bmap1, 1024); |
113 | DECLARE_BITMAP(bmap2, 1024); | 169 | DECLARE_BITMAP(bmap2, 1024); |
@@ -116,36 +172,20 @@ static void __init test_zero_fill_copy(void) | |||
116 | bitmap_zero(bmap2, 1024); | 172 | bitmap_zero(bmap2, 1024); |
117 | 173 | ||
118 | /* single-word bitmaps */ | 174 | /* single-word bitmaps */ |
119 | expect_eq_pbl("", bmap1, 23); | 175 | bitmap_set(bmap1, 0, 19); |
120 | |||
121 | bitmap_fill(bmap1, 19); | ||
122 | expect_eq_pbl("0-18", bmap1, 1024); | ||
123 | |||
124 | bitmap_copy(bmap2, bmap1, 23); | 176 | bitmap_copy(bmap2, bmap1, 23); |
125 | expect_eq_pbl("0-18", bmap2, 1024); | 177 | expect_eq_pbl("0-18", bmap2, 1024); |
126 | 178 | ||
127 | bitmap_fill(bmap2, 23); | 179 | bitmap_set(bmap2, 0, 23); |
128 | expect_eq_pbl("0-22", bmap2, 1024); | ||
129 | |||
130 | bitmap_copy(bmap2, bmap1, 23); | 180 | bitmap_copy(bmap2, bmap1, 23); |
131 | expect_eq_pbl("0-18", bmap2, 1024); | 181 | expect_eq_pbl("0-18", bmap2, 1024); |
132 | 182 | ||
133 | bitmap_zero(bmap1, 23); | ||
134 | expect_eq_pbl("", bmap1, 1024); | ||
135 | |||
136 | /* multi-word bitmaps */ | 183 | /* multi-word bitmaps */ |
137 | bitmap_zero(bmap1, 1024); | 184 | bitmap_set(bmap1, 0, 109); |
138 | expect_eq_pbl("", bmap1, 1024); | ||
139 | |||
140 | bitmap_fill(bmap1, 109); | ||
141 | expect_eq_pbl("0-108", bmap1, 1024); | ||
142 | |||
143 | bitmap_copy(bmap2, bmap1, 1024); | 185 | bitmap_copy(bmap2, bmap1, 1024); |
144 | expect_eq_pbl("0-108", bmap2, 1024); | 186 | expect_eq_pbl("0-108", bmap2, 1024); |
145 | 187 | ||
146 | bitmap_fill(bmap2, 1024); | 188 | bitmap_fill(bmap2, 1024); |
147 | expect_eq_pbl("0-1023", bmap2, 1024); | ||
148 | |||
149 | bitmap_copy(bmap2, bmap1, 1024); | 189 | bitmap_copy(bmap2, bmap1, 1024); |
150 | expect_eq_pbl("0-108", bmap2, 1024); | 190 | expect_eq_pbl("0-108", bmap2, 1024); |
151 | 191 | ||
@@ -160,9 +200,6 @@ static void __init test_zero_fill_copy(void) | |||
160 | bitmap_fill(bmap2, 1024); | 200 | bitmap_fill(bmap2, 1024); |
161 | bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */ | 201 | bitmap_copy(bmap2, bmap1, 97); /* ... but aligned on word length */ |
162 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); | 202 | expect_eq_pbl("0-108,128-1023", bmap2, 1024); |
163 | |||
164 | bitmap_zero(bmap2, 97); /* ... but 0-padded til word length */ | ||
165 | expect_eq_pbl("128-1023", bmap2, 1024); | ||
166 | } | 203 | } |
167 | 204 | ||
168 | #define PARSE_TIME 0x1 | 205 | #define PARSE_TIME 0x1 |
@@ -255,171 +292,29 @@ static void __init test_bitmap_parselist(void) | |||
255 | } | 292 | } |
256 | } | 293 | } |
257 | 294 | ||
258 | static void __init test_bitmap_u32_array_conversions(void) | 295 | static void __init test_bitmap_arr32(void) |
259 | { | 296 | { |
260 | DECLARE_BITMAP(bmap1, 1024); | 297 | unsigned int nbits, next_bit, len = sizeof(exp) * 8; |
261 | DECLARE_BITMAP(bmap2, 1024); | 298 | u32 arr[sizeof(exp) / 4]; |
262 | u32 exp_arr[32], arr[32]; | 299 | DECLARE_BITMAP(bmap2, len); |
263 | unsigned nbits; | 300 | |
264 | 301 | memset(arr, 0xa5, sizeof(arr)); | |
265 | for (nbits = 0 ; nbits < 257 ; ++nbits) { | 302 | |
266 | const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32); | 303 | for (nbits = 0; nbits < len; ++nbits) { |
267 | unsigned int i, rv; | 304 | bitmap_to_arr32(arr, exp, nbits); |
268 | 305 | bitmap_from_arr32(bmap2, arr, nbits); | |
269 | bitmap_zero(bmap1, nbits); | 306 | expect_eq_bitmap(bmap2, exp, nbits); |
270 | bitmap_set(bmap1, nbits, 1024 - nbits); /* garbage */ | 307 | |
271 | 308 | next_bit = find_next_bit(bmap2, | |
272 | memset(arr, 0xff, sizeof(arr)); | 309 | round_up(nbits, BITS_PER_LONG), nbits); |
273 | rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits); | 310 | if (next_bit < round_up(nbits, BITS_PER_LONG)) |
274 | expect_eq_uint(nbits, rv); | 311 | pr_err("bitmap_copy_arr32(nbits == %d:" |
275 | 312 | " tail is not safely cleared: %d\n", | |
276 | memset(exp_arr, 0xff, sizeof(exp_arr)); | 313 | nbits, next_bit); |
277 | memset(exp_arr, 0, used_u32s*sizeof(*exp_arr)); | 314 | |
278 | expect_eq_u32_array(exp_arr, 32, arr, 32); | 315 | if (nbits < len - 32) |
279 | 316 | expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)], | |
280 | bitmap_fill(bmap2, 1024); | 317 | 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 | } | 318 | } |
424 | } | 319 | } |
425 | 320 | ||
@@ -453,8 +348,10 @@ static void noinline __init test_mem_optimisations(void) | |||
453 | 348 | ||
454 | static int __init test_bitmap_init(void) | 349 | static int __init test_bitmap_init(void) |
455 | { | 350 | { |
456 | test_zero_fill_copy(); | 351 | test_zero_clear(); |
457 | test_bitmap_u32_array_conversions(); | 352 | test_fill_set(); |
353 | test_copy(); | ||
354 | test_bitmap_arr32(); | ||
458 | test_bitmap_parselist(); | 355 | test_bitmap_parselist(); |
459 | test_mem_optimisations(); | 356 | test_mem_optimisations(); |
460 | 357 | ||