Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/module.h>
0003 #include <linux/printk.h>
0004 #include <linux/slab.h>
0005 #include <linux/string.h>
0006 
0007 static __init int memset16_selftest(void)
0008 {
0009     unsigned i, j, k;
0010     u16 v, *p;
0011 
0012     p = kmalloc(256 * 2 * 2, GFP_KERNEL);
0013     if (!p)
0014         return -1;
0015 
0016     for (i = 0; i < 256; i++) {
0017         for (j = 0; j < 256; j++) {
0018             memset(p, 0xa1, 256 * 2 * sizeof(v));
0019             memset16(p + i, 0xb1b2, j);
0020             for (k = 0; k < 512; k++) {
0021                 v = p[k];
0022                 if (k < i) {
0023                     if (v != 0xa1a1)
0024                         goto fail;
0025                 } else if (k < i + j) {
0026                     if (v != 0xb1b2)
0027                         goto fail;
0028                 } else {
0029                     if (v != 0xa1a1)
0030                         goto fail;
0031                 }
0032             }
0033         }
0034     }
0035 
0036 fail:
0037     kfree(p);
0038     if (i < 256)
0039         return (i << 24) | (j << 16) | k | 0x8000;
0040     return 0;
0041 }
0042 
0043 static __init int memset32_selftest(void)
0044 {
0045     unsigned i, j, k;
0046     u32 v, *p;
0047 
0048     p = kmalloc(256 * 2 * 4, GFP_KERNEL);
0049     if (!p)
0050         return -1;
0051 
0052     for (i = 0; i < 256; i++) {
0053         for (j = 0; j < 256; j++) {
0054             memset(p, 0xa1, 256 * 2 * sizeof(v));
0055             memset32(p + i, 0xb1b2b3b4, j);
0056             for (k = 0; k < 512; k++) {
0057                 v = p[k];
0058                 if (k < i) {
0059                     if (v != 0xa1a1a1a1)
0060                         goto fail;
0061                 } else if (k < i + j) {
0062                     if (v != 0xb1b2b3b4)
0063                         goto fail;
0064                 } else {
0065                     if (v != 0xa1a1a1a1)
0066                         goto fail;
0067                 }
0068             }
0069         }
0070     }
0071 
0072 fail:
0073     kfree(p);
0074     if (i < 256)
0075         return (i << 24) | (j << 16) | k | 0x8000;
0076     return 0;
0077 }
0078 
0079 static __init int memset64_selftest(void)
0080 {
0081     unsigned i, j, k;
0082     u64 v, *p;
0083 
0084     p = kmalloc(256 * 2 * 8, GFP_KERNEL);
0085     if (!p)
0086         return -1;
0087 
0088     for (i = 0; i < 256; i++) {
0089         for (j = 0; j < 256; j++) {
0090             memset(p, 0xa1, 256 * 2 * sizeof(v));
0091             memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
0092             for (k = 0; k < 512; k++) {
0093                 v = p[k];
0094                 if (k < i) {
0095                     if (v != 0xa1a1a1a1a1a1a1a1ULL)
0096                         goto fail;
0097                 } else if (k < i + j) {
0098                     if (v != 0xb1b2b3b4b5b6b7b8ULL)
0099                         goto fail;
0100                 } else {
0101                     if (v != 0xa1a1a1a1a1a1a1a1ULL)
0102                         goto fail;
0103                 }
0104             }
0105         }
0106     }
0107 
0108 fail:
0109     kfree(p);
0110     if (i < 256)
0111         return (i << 24) | (j << 16) | k | 0x8000;
0112     return 0;
0113 }
0114 
0115 static __init int strchr_selftest(void)
0116 {
0117     const char *test_string = "abcdefghijkl";
0118     const char *empty_string = "";
0119     char *result;
0120     int i;
0121 
0122     for (i = 0; i < strlen(test_string) + 1; i++) {
0123         result = strchr(test_string, test_string[i]);
0124         if (result - test_string != i)
0125             return i + 'a';
0126     }
0127 
0128     result = strchr(empty_string, '\0');
0129     if (result != empty_string)
0130         return 0x101;
0131 
0132     result = strchr(empty_string, 'a');
0133     if (result)
0134         return 0x102;
0135 
0136     result = strchr(test_string, 'z');
0137     if (result)
0138         return 0x103;
0139 
0140     return 0;
0141 }
0142 
0143 static __init int strnchr_selftest(void)
0144 {
0145     const char *test_string = "abcdefghijkl";
0146     const char *empty_string = "";
0147     char *result;
0148     int i, j;
0149 
0150     for (i = 0; i < strlen(test_string) + 1; i++) {
0151         for (j = 0; j < strlen(test_string) + 2; j++) {
0152             result = strnchr(test_string, j, test_string[i]);
0153             if (j <= i) {
0154                 if (!result)
0155                     continue;
0156                 return ((i + 'a') << 8) | j;
0157             }
0158             if (result - test_string != i)
0159                 return ((i + 'a') << 8) | j;
0160         }
0161     }
0162 
0163     result = strnchr(empty_string, 0, '\0');
0164     if (result)
0165         return 0x10001;
0166 
0167     result = strnchr(empty_string, 1, '\0');
0168     if (result != empty_string)
0169         return 0x10002;
0170 
0171     result = strnchr(empty_string, 1, 'a');
0172     if (result)
0173         return 0x10003;
0174 
0175     result = strnchr(NULL, 0, '\0');
0176     if (result)
0177         return 0x10004;
0178 
0179     return 0;
0180 }
0181 
0182 static __init int strspn_selftest(void)
0183 {
0184     static const struct strspn_test {
0185         const char str[16];
0186         const char accept[16];
0187         const char reject[16];
0188         unsigned a;
0189         unsigned r;
0190     } tests[] __initconst = {
0191         { "foobar", "", "", 0, 6 },
0192         { "abba", "abc", "ABBA", 4, 4 },
0193         { "abba", "a", "b", 1, 1 },
0194         { "", "abc", "abc", 0, 0},
0195     };
0196     const struct strspn_test *s = tests;
0197     size_t i, res;
0198 
0199     for (i = 0; i < ARRAY_SIZE(tests); ++i, ++s) {
0200         res = strspn(s->str, s->accept);
0201         if (res != s->a)
0202             return 0x100 + 2*i;
0203         res = strcspn(s->str, s->reject);
0204         if (res != s->r)
0205             return 0x100 + 2*i + 1;
0206     }
0207     return 0;
0208 }
0209 
0210 static __exit void string_selftest_remove(void)
0211 {
0212 }
0213 
0214 static __init int string_selftest_init(void)
0215 {
0216     int test, subtest;
0217 
0218     test = 1;
0219     subtest = memset16_selftest();
0220     if (subtest)
0221         goto fail;
0222 
0223     test = 2;
0224     subtest = memset32_selftest();
0225     if (subtest)
0226         goto fail;
0227 
0228     test = 3;
0229     subtest = memset64_selftest();
0230     if (subtest)
0231         goto fail;
0232 
0233     test = 4;
0234     subtest = strchr_selftest();
0235     if (subtest)
0236         goto fail;
0237 
0238     test = 5;
0239     subtest = strnchr_selftest();
0240     if (subtest)
0241         goto fail;
0242 
0243     test = 6;
0244     subtest = strspn_selftest();
0245     if (subtest)
0246         goto fail;
0247 
0248     pr_info("String selftests succeeded\n");
0249     return 0;
0250 fail:
0251     pr_crit("String selftest failure %d.%08x\n", test, subtest);
0252     return 0;
0253 }
0254 
0255 module_init(string_selftest_init);
0256 module_exit(string_selftest_remove);
0257 MODULE_LICENSE("GPL v2");