Back to home page

LXR

 
 

    


0001 /*
0002  * Test cases for printf facility.
0003  */
0004 
0005 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0006 
0007 #include <linux/bitmap.h>
0008 #include <linux/init.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/printk.h>
0012 #include <linux/slab.h>
0013 #include <linux/string.h>
0014 
0015 static unsigned total_tests __initdata;
0016 static unsigned failed_tests __initdata;
0017 
0018 static char pbl_buffer[PAGE_SIZE] __initdata;
0019 
0020 
0021 static bool __init
0022 __check_eq_uint(const char *srcfile, unsigned int line,
0023         const unsigned int exp_uint, unsigned int x)
0024 {
0025     if (exp_uint != x) {
0026         pr_warn("[%s:%u] expected %u, got %u\n",
0027             srcfile, line, exp_uint, x);
0028         return false;
0029     }
0030     return true;
0031 }
0032 
0033 
0034 static bool __init
0035 __check_eq_bitmap(const char *srcfile, unsigned int line,
0036           const unsigned long *exp_bmap, unsigned int exp_nbits,
0037           const unsigned long *bmap, unsigned int nbits)
0038 {
0039     if (exp_nbits != nbits) {
0040         pr_warn("[%s:%u] bitmap length mismatch: expected %u, got %u\n",
0041             srcfile, line, exp_nbits, nbits);
0042         return false;
0043     }
0044 
0045     if (!bitmap_equal(exp_bmap, bmap, nbits)) {
0046         pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
0047             srcfile, line,
0048             exp_nbits, exp_bmap, nbits, bmap);
0049         return false;
0050     }
0051     return true;
0052 }
0053 
0054 static bool __init
0055 __check_eq_pbl(const char *srcfile, unsigned int line,
0056            const char *expected_pbl,
0057            const unsigned long *bitmap, unsigned int nbits)
0058 {
0059     snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
0060     if (strcmp(expected_pbl, pbl_buffer)) {
0061         pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
0062             srcfile, line,
0063             expected_pbl, pbl_buffer);
0064         return false;
0065     }
0066     return true;
0067 }
0068 
0069 static bool __init
0070 __check_eq_u32_array(const char *srcfile, unsigned int line,
0071              const u32 *exp_arr, unsigned int exp_len,
0072              const u32 *arr, unsigned int len)
0073 {
0074     if (exp_len != len) {
0075         pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
0076             srcfile, line,
0077             exp_len, len);
0078         return false;
0079     }
0080 
0081     if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
0082         pr_warn("[%s:%u] array contents differ\n", srcfile, line);
0083         print_hex_dump(KERN_WARNING, "  exp:  ", DUMP_PREFIX_OFFSET,
0084                    32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
0085         print_hex_dump(KERN_WARNING, "  got:  ", DUMP_PREFIX_OFFSET,
0086                    32, 4, arr, len*sizeof(*arr), false);
0087         return false;
0088     }
0089 
0090     return true;
0091 }
0092 
0093 #define __expect_eq(suffix, ...)                    \
0094     ({                              \
0095         int result = 0;                     \
0096         total_tests++;                      \
0097         if (!__check_eq_ ## suffix(__FILE__, __LINE__,      \
0098                        ##__VA_ARGS__)) {        \
0099             failed_tests++;                 \
0100             result = 1;                 \
0101         }                           \
0102         result;                         \
0103     })
0104 
0105 #define expect_eq_uint(...)     __expect_eq(uint, ##__VA_ARGS__)
0106 #define expect_eq_bitmap(...)       __expect_eq(bitmap, ##__VA_ARGS__)
0107 #define expect_eq_pbl(...)      __expect_eq(pbl, ##__VA_ARGS__)
0108 #define expect_eq_u32_array(...)    __expect_eq(u32_array, ##__VA_ARGS__)
0109 
0110 static void __init test_zero_fill_copy(void)
0111 {
0112     DECLARE_BITMAP(bmap1, 1024);
0113     DECLARE_BITMAP(bmap2, 1024);
0114 
0115     bitmap_zero(bmap1, 1024);
0116     bitmap_zero(bmap2, 1024);
0117 
0118     /* single-word bitmaps */
0119     expect_eq_pbl("", bmap1, 23);
0120 
0121     bitmap_fill(bmap1, 19);
0122     expect_eq_pbl("0-18", bmap1, 1024);
0123 
0124     bitmap_copy(bmap2, bmap1, 23);
0125     expect_eq_pbl("0-18", bmap2, 1024);
0126 
0127     bitmap_fill(bmap2, 23);
0128     expect_eq_pbl("0-22", bmap2, 1024);
0129 
0130     bitmap_copy(bmap2, bmap1, 23);
0131     expect_eq_pbl("0-18", bmap2, 1024);
0132 
0133     bitmap_zero(bmap1, 23);
0134     expect_eq_pbl("", bmap1, 1024);
0135 
0136     /* multi-word bitmaps */
0137     bitmap_zero(bmap1, 1024);
0138     expect_eq_pbl("", bmap1, 1024);
0139 
0140     bitmap_fill(bmap1, 109);
0141     expect_eq_pbl("0-108", bmap1, 1024);
0142 
0143     bitmap_copy(bmap2, bmap1, 1024);
0144     expect_eq_pbl("0-108", bmap2, 1024);
0145 
0146     bitmap_fill(bmap2, 1024);
0147     expect_eq_pbl("0-1023", bmap2, 1024);
0148 
0149     bitmap_copy(bmap2, bmap1, 1024);
0150     expect_eq_pbl("0-108", bmap2, 1024);
0151 
0152     /* the following tests assume a 32- or 64-bit arch (even 128b
0153      * if we care)
0154      */
0155 
0156     bitmap_fill(bmap2, 1024);
0157     bitmap_copy(bmap2, bmap1, 109);  /* ... but 0-padded til word length */
0158     expect_eq_pbl("0-108,128-1023", bmap2, 1024);
0159 
0160     bitmap_fill(bmap2, 1024);
0161     bitmap_copy(bmap2, bmap1, 97);  /* ... but aligned on word length */
0162     expect_eq_pbl("0-108,128-1023", bmap2, 1024);
0163 
0164     bitmap_zero(bmap2, 97);  /* ... but 0-padded til word length */
0165     expect_eq_pbl("128-1023", bmap2, 1024);
0166 }
0167 
0168 static void __init test_bitmap_u32_array_conversions(void)
0169 {
0170     DECLARE_BITMAP(bmap1, 1024);
0171     DECLARE_BITMAP(bmap2, 1024);
0172     u32 exp_arr[32], arr[32];
0173     unsigned nbits;
0174 
0175     for (nbits = 0 ; nbits < 257 ; ++nbits) {
0176         const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32);
0177         unsigned int i, rv;
0178 
0179         bitmap_zero(bmap1, nbits);
0180         bitmap_set(bmap1, nbits, 1024 - nbits);  /* garbage */
0181 
0182         memset(arr, 0xff, sizeof(arr));
0183         rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
0184         expect_eq_uint(nbits, rv);
0185 
0186         memset(exp_arr, 0xff, sizeof(exp_arr));
0187         memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
0188         expect_eq_u32_array(exp_arr, 32, arr, 32);
0189 
0190         bitmap_fill(bmap2, 1024);
0191         rv = bitmap_from_u32array(bmap2, nbits, arr, used_u32s);
0192         expect_eq_uint(nbits, rv);
0193         expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
0194 
0195         for (i = 0 ; i < nbits ; ++i) {
0196             /*
0197              * test conversion bitmap -> u32[]
0198              */
0199 
0200             bitmap_zero(bmap1, 1024);
0201             __set_bit(i, bmap1);
0202             bitmap_set(bmap1, nbits, 1024 - nbits);  /* garbage */
0203 
0204             memset(arr, 0xff, sizeof(arr));
0205             rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
0206             expect_eq_uint(nbits, rv);
0207 
0208             /* 1st used u32 words contain expected bit set, the
0209              * remaining words are left unchanged (0xff)
0210              */
0211             memset(exp_arr, 0xff, sizeof(exp_arr));
0212             memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
0213             exp_arr[i/32] = (1U<<(i%32));
0214             expect_eq_u32_array(exp_arr, 32, arr, 32);
0215 
0216 
0217             /* same, with longer array to fill
0218              */
0219             memset(arr, 0xff, sizeof(arr));
0220             rv = bitmap_to_u32array(arr, 32, bmap1, nbits);
0221             expect_eq_uint(nbits, rv);
0222 
0223             /* 1st used u32 words contain expected bit set, the
0224              * remaining words are all 0s
0225              */
0226             memset(exp_arr, 0, sizeof(exp_arr));
0227             exp_arr[i/32] = (1U<<(i%32));
0228             expect_eq_u32_array(exp_arr, 32, arr, 32);
0229 
0230             /*
0231              * test conversion u32[] -> bitmap
0232              */
0233 
0234             /* the 1st nbits of bmap2 are identical to
0235              * bmap1, the remaining bits of bmap2 are left
0236              * unchanged (all 1s)
0237              */
0238             bitmap_fill(bmap2, 1024);
0239             rv = bitmap_from_u32array(bmap2, nbits,
0240                           exp_arr, used_u32s);
0241             expect_eq_uint(nbits, rv);
0242 
0243             expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
0244 
0245             /* same, with more bits to fill
0246              */
0247             memset(arr, 0xff, sizeof(arr));  /* garbage */
0248             memset(arr, 0, used_u32s*sizeof(u32));
0249             arr[i/32] = (1U<<(i%32));
0250 
0251             bitmap_fill(bmap2, 1024);
0252             rv = bitmap_from_u32array(bmap2, 1024, arr, used_u32s);
0253             expect_eq_uint(used_u32s*32, rv);
0254 
0255             /* the 1st nbits of bmap2 are identical to
0256              * bmap1, the remaining bits of bmap2 are cleared
0257              */
0258             bitmap_zero(bmap1, 1024);
0259             __set_bit(i, bmap1);
0260             expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
0261 
0262 
0263             /*
0264              * test short conversion bitmap -> u32[] (1
0265              * word too short)
0266              */
0267             if (used_u32s > 1) {
0268                 bitmap_zero(bmap1, 1024);
0269                 __set_bit(i, bmap1);
0270                 bitmap_set(bmap1, nbits,
0271                        1024 - nbits);  /* garbage */
0272                 memset(arr, 0xff, sizeof(arr));
0273 
0274                 rv = bitmap_to_u32array(arr, used_u32s - 1,
0275                             bmap1, nbits);
0276                 expect_eq_uint((used_u32s - 1)*32, rv);
0277 
0278                 /* 1st used u32 words contain expected
0279                  * bit set, the remaining words are
0280                  * left unchanged (0xff)
0281                  */
0282                 memset(exp_arr, 0xff, sizeof(exp_arr));
0283                 memset(exp_arr, 0,
0284                        (used_u32s-1)*sizeof(*exp_arr));
0285                 if ((i/32) < (used_u32s - 1))
0286                     exp_arr[i/32] = (1U<<(i%32));
0287                 expect_eq_u32_array(exp_arr, 32, arr, 32);
0288             }
0289 
0290             /*
0291              * test short conversion u32[] -> bitmap (3
0292              * bits too short)
0293              */
0294             if (nbits > 3) {
0295                 memset(arr, 0xff, sizeof(arr));  /* garbage */
0296                 memset(arr, 0, used_u32s*sizeof(*arr));
0297                 arr[i/32] = (1U<<(i%32));
0298 
0299                 bitmap_zero(bmap1, 1024);
0300                 rv = bitmap_from_u32array(bmap1, nbits - 3,
0301                               arr, used_u32s);
0302                 expect_eq_uint(nbits - 3, rv);
0303 
0304                 /* we are expecting the bit < nbits -
0305                  * 3 (none otherwise), and the rest of
0306                  * bmap1 unchanged (0-filled)
0307                  */
0308                 bitmap_zero(bmap2, 1024);
0309                 if (i < nbits - 3)
0310                     __set_bit(i, bmap2);
0311                 expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
0312 
0313                 /* do the same with bmap1 initially
0314                  * 1-filled
0315                  */
0316 
0317                 bitmap_fill(bmap1, 1024);
0318                 rv = bitmap_from_u32array(bmap1, nbits - 3,
0319                              arr, used_u32s);
0320                 expect_eq_uint(nbits - 3, rv);
0321 
0322                 /* we are expecting the bit < nbits -
0323                  * 3 (none otherwise), and the rest of
0324                  * bmap1 unchanged (1-filled)
0325                  */
0326                 bitmap_zero(bmap2, 1024);
0327                 if (i < nbits - 3)
0328                     __set_bit(i, bmap2);
0329                 bitmap_set(bmap2, nbits-3, 1024 - nbits + 3);
0330                 expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
0331             }
0332         }
0333     }
0334 }
0335 
0336 static int __init test_bitmap_init(void)
0337 {
0338     test_zero_fill_copy();
0339     test_bitmap_u32_array_conversions();
0340 
0341     if (failed_tests == 0)
0342         pr_info("all %u tests passed\n", total_tests);
0343     else
0344         pr_warn("failed %u out of %u tests\n",
0345             failed_tests, total_tests);
0346 
0347     return failed_tests ? -EINVAL : 0;
0348 }
0349 
0350 static void __exit test_bitmap_cleanup(void)
0351 {
0352 }
0353 
0354 module_init(test_bitmap_init);
0355 module_exit(test_bitmap_cleanup);
0356 
0357 MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
0358 MODULE_LICENSE("GPL");