lib/test_bitmap.c: add test for bitmap_parselist()
[sfrench/cifs-2.6.git] / lib / test_bitmap.c
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) {
26                 pr_warn("[%s:%u] expected %u, got %u\n",
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,
36                   const unsigned long *exp_bmap, unsigned int exp_nbits,
37                   const unsigned long *bmap, unsigned int nbits)
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)) {
46                 pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
47                         srcfile, line,
48                         exp_nbits, exp_bmap, nbits, bmap);
49                 return false;
50         }
51         return true;
52 }
53
54 static bool __init
55 __check_eq_pbl(const char *srcfile, unsigned int line,
56                const char *expected_pbl,
57                const unsigned long *bitmap, unsigned int nbits)
58 {
59         snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
60         if (strcmp(expected_pbl, pbl_buffer)) {
61                 pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
62                         srcfile, line,
63                         expected_pbl, pbl_buffer);
64                 return false;
65         }
66         return true;
67 }
68
69 static bool __init
70 __check_eq_u32_array(const char *srcfile, unsigned int line,
71                      const u32 *exp_arr, unsigned int exp_len,
72                      const u32 *arr, unsigned int len)
73 {
74         if (exp_len != len) {
75                 pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
76                         srcfile, line,
77                         exp_len, len);
78                 return false;
79         }
80
81         if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
82                 pr_warn("[%s:%u] array contents differ\n", srcfile, line);
83                 print_hex_dump(KERN_WARNING, "  exp:  ", DUMP_PREFIX_OFFSET,
84                                32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
85                 print_hex_dump(KERN_WARNING, "  got:  ", DUMP_PREFIX_OFFSET,
86                                32, 4, arr, len*sizeof(*arr), false);
87                 return false;
88         }
89
90         return true;
91 }
92
93 #define __expect_eq(suffix, ...)                                        \
94         ({                                                              \
95                 int result = 0;                                         \
96                 total_tests++;                                          \
97                 if (!__check_eq_ ## suffix(__FILE__, __LINE__,          \
98                                            ##__VA_ARGS__)) {            \
99                         failed_tests++;                                 \
100                         result = 1;                                     \
101                 }                                                       \
102                 result;                                                 \
103         })
104
105 #define expect_eq_uint(...)             __expect_eq(uint, ##__VA_ARGS__)
106 #define expect_eq_bitmap(...)           __expect_eq(bitmap, ##__VA_ARGS__)
107 #define expect_eq_pbl(...)              __expect_eq(pbl, ##__VA_ARGS__)
108 #define expect_eq_u32_array(...)        __expect_eq(u32_array, ##__VA_ARGS__)
109
110 static void __init test_zero_fill_copy(void)
111 {
112         DECLARE_BITMAP(bmap1, 1024);
113         DECLARE_BITMAP(bmap2, 1024);
114
115         bitmap_zero(bmap1, 1024);
116         bitmap_zero(bmap2, 1024);
117
118         /* single-word bitmaps */
119         expect_eq_pbl("", bmap1, 23);
120
121         bitmap_fill(bmap1, 19);
122         expect_eq_pbl("0-18", bmap1, 1024);
123
124         bitmap_copy(bmap2, bmap1, 23);
125         expect_eq_pbl("0-18", bmap2, 1024);
126
127         bitmap_fill(bmap2, 23);
128         expect_eq_pbl("0-22", bmap2, 1024);
129
130         bitmap_copy(bmap2, bmap1, 23);
131         expect_eq_pbl("0-18", bmap2, 1024);
132
133         bitmap_zero(bmap1, 23);
134         expect_eq_pbl("", bmap1, 1024);
135
136         /* multi-word bitmaps */
137         bitmap_zero(bmap1, 1024);
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);
144         expect_eq_pbl("0-108", bmap2, 1024);
145
146         bitmap_fill(bmap2, 1024);
147         expect_eq_pbl("0-1023", bmap2, 1024);
148
149         bitmap_copy(bmap2, bmap1, 1024);
150         expect_eq_pbl("0-108", bmap2, 1024);
151
152         /* the following tests assume a 32- or 64-bit arch (even 128b
153          * if we care)
154          */
155
156         bitmap_fill(bmap2, 1024);
157         bitmap_copy(bmap2, bmap1, 109);  /* ... but 0-padded til word length */
158         expect_eq_pbl("0-108,128-1023", bmap2, 1024);
159
160         bitmap_fill(bmap2, 1024);
161         bitmap_copy(bmap2, bmap1, 97);  /* ... but aligned on word length */
162         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 }
167
168 #define PARSE_TIME 0x1
169
170 struct test_bitmap_parselist{
171         const int errno;
172         const char *in;
173         const unsigned long *expected;
174         const int nbits;
175         const int flags;
176 };
177
178 static const unsigned long exp[] = {1, 2, 0x0000ffff, 0xffff0000, 0x55555555,
179                                 0xaaaaaaaa, 0x11111111, 0x22222222, 0xffffffff,
180                                 0xfffffffe, 0x3333333311111111, 0xffffffff77777777};
181 static const unsigned long exp2[] = {0x3333333311111111, 0xffffffff77777777};
182
183 static const struct test_bitmap_parselist parselist_tests[] __initconst = {
184         {0, "0",                        &exp[0], 8, 0},
185         {0, "1",                        &exp[1], 8, 0},
186         {0, "0-15",                     &exp[2], 32, 0},
187         {0, "16-31",                    &exp[3], 32, 0},
188         {0, "0-31:1/2",                 &exp[4], 32, 0},
189         {0, "1-31:1/2",                 &exp[5], 32, 0},
190         {0, "0-31:1/4",                 &exp[6], 32, 0},
191         {0, "1-31:1/4",                 &exp[7], 32, 0},
192         {0, "0-31:4/4",                 &exp[8], 32, 0},
193         {0, "1-31:4/4",                 &exp[9], 32, 0},
194         {0, "0-31:1/4,32-63:2/4",       &exp[10], 64, 0},
195         {0, "0-31:3/4,32-63:4/4",       &exp[11], 64, 0},
196
197         {0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4",  exp2, 128, 0},
198
199         {0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
200
201         {-EINVAL, "-1", NULL, 8, 0},
202         {-EINVAL, "-0", NULL, 8, 0},
203         {-EINVAL, "10-1", NULL, 8, 0},
204         {-EINVAL, "0-31:10/1", NULL, 8, 0},
205 };
206
207 static void __init test_bitmap_parselist(void)
208 {
209         int i;
210         int err;
211         cycles_t cycles;
212         DECLARE_BITMAP(bmap, 2048);
213
214         for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
215 #define ptest parselist_tests[i]
216
217                 cycles = get_cycles();
218                 err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
219                 cycles = get_cycles() - cycles;
220
221                 if (err != ptest.errno) {
222                         pr_err("test %d: input is %s, errno is %d, expected %d\n",
223                                         i, ptest.in, err, ptest.errno);
224                         continue;
225                 }
226
227                 if (!err && ptest.expected
228                          && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
229                         pr_err("test %d: input is %s, result is 0x%lx, expected 0x%lx\n",
230                                         i, ptest.in, bmap[0], *ptest.expected);
231                         continue;
232                 }
233
234                 if (ptest.flags & PARSE_TIME)
235                         pr_err("test %d: input is '%s' OK, Time: %llu\n",
236                                         i, ptest.in,
237                                         (unsigned long long)cycles);
238         }
239 }
240
241 static void __init test_bitmap_u32_array_conversions(void)
242 {
243         DECLARE_BITMAP(bmap1, 1024);
244         DECLARE_BITMAP(bmap2, 1024);
245         u32 exp_arr[32], arr[32];
246         unsigned nbits;
247
248         for (nbits = 0 ; nbits < 257 ; ++nbits) {
249                 const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32);
250                 unsigned int i, rv;
251
252                 bitmap_zero(bmap1, nbits);
253                 bitmap_set(bmap1, nbits, 1024 - nbits);  /* garbage */
254
255                 memset(arr, 0xff, sizeof(arr));
256                 rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
257                 expect_eq_uint(nbits, rv);
258
259                 memset(exp_arr, 0xff, sizeof(exp_arr));
260                 memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
261                 expect_eq_u32_array(exp_arr, 32, arr, 32);
262
263                 bitmap_fill(bmap2, 1024);
264                 rv = bitmap_from_u32array(bmap2, nbits, arr, used_u32s);
265                 expect_eq_uint(nbits, rv);
266                 expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
267
268                 for (i = 0 ; i < nbits ; ++i) {
269                         /*
270                          * test conversion bitmap -> u32[]
271                          */
272
273                         bitmap_zero(bmap1, 1024);
274                         __set_bit(i, bmap1);
275                         bitmap_set(bmap1, nbits, 1024 - nbits);  /* garbage */
276
277                         memset(arr, 0xff, sizeof(arr));
278                         rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
279                         expect_eq_uint(nbits, rv);
280
281                         /* 1st used u32 words contain expected bit set, the
282                          * remaining words are left unchanged (0xff)
283                          */
284                         memset(exp_arr, 0xff, sizeof(exp_arr));
285                         memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
286                         exp_arr[i/32] = (1U<<(i%32));
287                         expect_eq_u32_array(exp_arr, 32, arr, 32);
288
289
290                         /* same, with longer array to fill
291                          */
292                         memset(arr, 0xff, sizeof(arr));
293                         rv = bitmap_to_u32array(arr, 32, bmap1, nbits);
294                         expect_eq_uint(nbits, rv);
295
296                         /* 1st used u32 words contain expected bit set, the
297                          * remaining words are all 0s
298                          */
299                         memset(exp_arr, 0, sizeof(exp_arr));
300                         exp_arr[i/32] = (1U<<(i%32));
301                         expect_eq_u32_array(exp_arr, 32, arr, 32);
302
303                         /*
304                          * test conversion u32[] -> bitmap
305                          */
306
307                         /* the 1st nbits of bmap2 are identical to
308                          * bmap1, the remaining bits of bmap2 are left
309                          * unchanged (all 1s)
310                          */
311                         bitmap_fill(bmap2, 1024);
312                         rv = bitmap_from_u32array(bmap2, nbits,
313                                                   exp_arr, used_u32s);
314                         expect_eq_uint(nbits, rv);
315
316                         expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
317
318                         /* same, with more bits to fill
319                          */
320                         memset(arr, 0xff, sizeof(arr));  /* garbage */
321                         memset(arr, 0, used_u32s*sizeof(u32));
322                         arr[i/32] = (1U<<(i%32));
323
324                         bitmap_fill(bmap2, 1024);
325                         rv = bitmap_from_u32array(bmap2, 1024, arr, used_u32s);
326                         expect_eq_uint(used_u32s*32, rv);
327
328                         /* the 1st nbits of bmap2 are identical to
329                          * bmap1, the remaining bits of bmap2 are cleared
330                          */
331                         bitmap_zero(bmap1, 1024);
332                         __set_bit(i, bmap1);
333                         expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
334
335
336                         /*
337                          * test short conversion bitmap -> u32[] (1
338                          * word too short)
339                          */
340                         if (used_u32s > 1) {
341                                 bitmap_zero(bmap1, 1024);
342                                 __set_bit(i, bmap1);
343                                 bitmap_set(bmap1, nbits,
344                                            1024 - nbits);  /* garbage */
345                                 memset(arr, 0xff, sizeof(arr));
346
347                                 rv = bitmap_to_u32array(arr, used_u32s - 1,
348                                                         bmap1, nbits);
349                                 expect_eq_uint((used_u32s - 1)*32, rv);
350
351                                 /* 1st used u32 words contain expected
352                                  * bit set, the remaining words are
353                                  * left unchanged (0xff)
354                                  */
355                                 memset(exp_arr, 0xff, sizeof(exp_arr));
356                                 memset(exp_arr, 0,
357                                        (used_u32s-1)*sizeof(*exp_arr));
358                                 if ((i/32) < (used_u32s - 1))
359                                         exp_arr[i/32] = (1U<<(i%32));
360                                 expect_eq_u32_array(exp_arr, 32, arr, 32);
361                         }
362
363                         /*
364                          * test short conversion u32[] -> bitmap (3
365                          * bits too short)
366                          */
367                         if (nbits > 3) {
368                                 memset(arr, 0xff, sizeof(arr));  /* garbage */
369                                 memset(arr, 0, used_u32s*sizeof(*arr));
370                                 arr[i/32] = (1U<<(i%32));
371
372                                 bitmap_zero(bmap1, 1024);
373                                 rv = bitmap_from_u32array(bmap1, nbits - 3,
374                                                           arr, used_u32s);
375                                 expect_eq_uint(nbits - 3, rv);
376
377                                 /* we are expecting the bit < nbits -
378                                  * 3 (none otherwise), and the rest of
379                                  * bmap1 unchanged (0-filled)
380                                  */
381                                 bitmap_zero(bmap2, 1024);
382                                 if (i < nbits - 3)
383                                         __set_bit(i, bmap2);
384                                 expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
385
386                                 /* do the same with bmap1 initially
387                                  * 1-filled
388                                  */
389
390                                 bitmap_fill(bmap1, 1024);
391                                 rv = bitmap_from_u32array(bmap1, nbits - 3,
392                                                          arr, used_u32s);
393                                 expect_eq_uint(nbits - 3, rv);
394
395                                 /* we are expecting the bit < nbits -
396                                  * 3 (none otherwise), and the rest of
397                                  * bmap1 unchanged (1-filled)
398                                  */
399                                 bitmap_zero(bmap2, 1024);
400                                 if (i < nbits - 3)
401                                         __set_bit(i, bmap2);
402                                 bitmap_set(bmap2, nbits-3, 1024 - nbits + 3);
403                                 expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
404                         }
405                 }
406         }
407 }
408
409 static void noinline __init test_mem_optimisations(void)
410 {
411         DECLARE_BITMAP(bmap1, 1024);
412         DECLARE_BITMAP(bmap2, 1024);
413         unsigned int start, nbits;
414
415         for (start = 0; start < 1024; start += 8) {
416                 memset(bmap1, 0x5a, sizeof(bmap1));
417                 memset(bmap2, 0x5a, sizeof(bmap2));
418                 for (nbits = 0; nbits < 1024 - start; nbits += 8) {
419                         bitmap_set(bmap1, start, nbits);
420                         __bitmap_set(bmap2, start, nbits);
421                         if (!bitmap_equal(bmap1, bmap2, 1024))
422                                 printk("set not equal %d %d\n", start, nbits);
423                         if (!__bitmap_equal(bmap1, bmap2, 1024))
424                                 printk("set not __equal %d %d\n", start, nbits);
425
426                         bitmap_clear(bmap1, start, nbits);
427                         __bitmap_clear(bmap2, start, nbits);
428                         if (!bitmap_equal(bmap1, bmap2, 1024))
429                                 printk("clear not equal %d %d\n", start, nbits);
430                         if (!__bitmap_equal(bmap1, bmap2, 1024))
431                                 printk("clear not __equal %d %d\n", start,
432                                                                         nbits);
433                 }
434         }
435 }
436
437 static int __init test_bitmap_init(void)
438 {
439         test_zero_fill_copy();
440         test_bitmap_u32_array_conversions();
441         test_bitmap_parselist();
442         test_mem_optimisations();
443
444         if (failed_tests == 0)
445                 pr_info("all %u tests passed\n", total_tests);
446         else
447                 pr_warn("failed %u out of %u tests\n",
448                         failed_tests, total_tests);
449
450         return failed_tests ? -EINVAL : 0;
451 }
452
453 static void __exit test_bitmap_cleanup(void)
454 {
455 }
456
457 module_init(test_bitmap_init);
458 module_exit(test_bitmap_cleanup);
459
460 MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
461 MODULE_LICENSE("GPL");