Blame view
lib/test_string.c
3.84 KB
09c434b8a treewide: Add SPD... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
d6b28e099 lib: add module s... |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <linux/module.h> #include <linux/printk.h> #include <linux/slab.h> #include <linux/string.h> static __init int memset16_selftest(void) { unsigned i, j, k; u16 v, *p; p = kmalloc(256 * 2 * 2, GFP_KERNEL); if (!p) return -1; for (i = 0; i < 256; i++) { for (j = 0; j < 256; j++) { memset(p, 0xa1, 256 * 2 * sizeof(v)); memset16(p + i, 0xb1b2, j); for (k = 0; k < 512; k++) { v = p[k]; if (k < i) { if (v != 0xa1a1) goto fail; } else if (k < i + j) { if (v != 0xb1b2) goto fail; } else { if (v != 0xa1a1) goto fail; } } } } fail: kfree(p); if (i < 256) |
33d6e0ff6 lib/test_string.c... |
39 |
return (i << 24) | (j << 16) | k | 0x8000; |
d6b28e099 lib: add module s... |
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
return 0; } static __init int memset32_selftest(void) { unsigned i, j, k; u32 v, *p; p = kmalloc(256 * 2 * 4, GFP_KERNEL); if (!p) return -1; for (i = 0; i < 256; i++) { for (j = 0; j < 256; j++) { memset(p, 0xa1, 256 * 2 * sizeof(v)); memset32(p + i, 0xb1b2b3b4, j); for (k = 0; k < 512; k++) { v = p[k]; if (k < i) { if (v != 0xa1a1a1a1) goto fail; } else if (k < i + j) { if (v != 0xb1b2b3b4) goto fail; } else { if (v != 0xa1a1a1a1) goto fail; } } } } fail: kfree(p); if (i < 256) |
33d6e0ff6 lib/test_string.c... |
75 |
return (i << 24) | (j << 16) | k | 0x8000; |
d6b28e099 lib: add module s... |
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
return 0; } static __init int memset64_selftest(void) { unsigned i, j, k; u64 v, *p; p = kmalloc(256 * 2 * 8, GFP_KERNEL); if (!p) return -1; for (i = 0; i < 256; i++) { for (j = 0; j < 256; j++) { memset(p, 0xa1, 256 * 2 * sizeof(v)); memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j); for (k = 0; k < 512; k++) { v = p[k]; if (k < i) { if (v != 0xa1a1a1a1a1a1a1a1ULL) goto fail; } else if (k < i + j) { if (v != 0xb1b2b3b4b5b6b7b8ULL) goto fail; } else { if (v != 0xa1a1a1a1a1a1a1a1ULL) goto fail; } } } } fail: kfree(p); if (i < 256) |
33d6e0ff6 lib/test_string.c... |
111 |
return (i << 24) | (j << 16) | k | 0x8000; |
d6b28e099 lib: add module s... |
112 113 |
return 0; } |
d1a5dc5e6 lib/test_string.c... |
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
static __init int strchr_selftest(void) { const char *test_string = "abcdefghijkl"; const char *empty_string = ""; char *result; int i; for (i = 0; i < strlen(test_string) + 1; i++) { result = strchr(test_string, test_string[i]); if (result - test_string != i) return i + 'a'; } result = strchr(empty_string, '\0'); if (result != empty_string) return 0x101; result = strchr(empty_string, 'a'); if (result) return 0x102; result = strchr(test_string, 'z'); if (result) return 0x103; return 0; } static __init int strnchr_selftest(void) { const char *test_string = "abcdefghijkl"; const char *empty_string = ""; char *result; int i, j; for (i = 0; i < strlen(test_string) + 1; i++) { for (j = 0; j < strlen(test_string) + 2; j++) { result = strnchr(test_string, j, test_string[i]); if (j <= i) { if (!result) continue; return ((i + 'a') << 8) | j; } if (result - test_string != i) return ((i + 'a') << 8) | j; } } result = strnchr(empty_string, 0, '\0'); if (result) return 0x10001; result = strnchr(empty_string, 1, '\0'); if (result != empty_string) return 0x10002; result = strnchr(empty_string, 1, 'a'); if (result) return 0x10003; result = strnchr(NULL, 0, '\0'); if (result) return 0x10004; return 0; } |
d6b28e099 lib: add module s... |
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
static __init int string_selftest_init(void) { int test, subtest; test = 1; subtest = memset16_selftest(); if (subtest) goto fail; test = 2; subtest = memset32_selftest(); if (subtest) goto fail; test = 3; subtest = memset64_selftest(); if (subtest) goto fail; |
d1a5dc5e6 lib/test_string.c... |
198 199 200 201 202 203 204 205 206 |
test = 4; subtest = strchr_selftest(); if (subtest) goto fail; test = 5; subtest = strnchr_selftest(); if (subtest) goto fail; |
d6b28e099 lib: add module s... |
207 208 209 210 211 212 213 214 215 216 217 |
pr_info("String selftests succeeded "); return 0; fail: pr_crit("String selftest failure %d.%08x ", test, subtest); return 0; } module_init(string_selftest_init); MODULE_LICENSE("GPL v2"); |