]>
git.proxmox.com Git - mirror_ubuntu-kernels.git/blob - lib/test_string.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
3 #include <linux/printk.h>
4 #include <linux/slab.h>
5 #include <linux/string.h>
7 static __init
int memset16_selftest(void)
12 p
= kmalloc(256 * 2 * 2, GFP_KERNEL
);
16 for (i
= 0; i
< 256; i
++) {
17 for (j
= 0; j
< 256; j
++) {
18 memset(p
, 0xa1, 256 * 2 * sizeof(v
));
19 memset16(p
+ i
, 0xb1b2, j
);
20 for (k
= 0; k
< 512; k
++) {
25 } else if (k
< i
+ j
) {
39 return (i
<< 24) | (j
<< 16) | k
| 0x8000;
43 static __init
int memset32_selftest(void)
48 p
= kmalloc(256 * 2 * 4, GFP_KERNEL
);
52 for (i
= 0; i
< 256; i
++) {
53 for (j
= 0; j
< 256; j
++) {
54 memset(p
, 0xa1, 256 * 2 * sizeof(v
));
55 memset32(p
+ i
, 0xb1b2b3b4, j
);
56 for (k
= 0; k
< 512; k
++) {
61 } else if (k
< i
+ j
) {
75 return (i
<< 24) | (j
<< 16) | k
| 0x8000;
79 static __init
int memset64_selftest(void)
84 p
= kmalloc(256 * 2 * 8, GFP_KERNEL
);
88 for (i
= 0; i
< 256; i
++) {
89 for (j
= 0; j
< 256; j
++) {
90 memset(p
, 0xa1, 256 * 2 * sizeof(v
));
91 memset64(p
+ i
, 0xb1b2b3b4b5b6b7b8ULL
, j
);
92 for (k
= 0; k
< 512; k
++) {
95 if (v
!= 0xa1a1a1a1a1a1a1a1ULL
)
97 } else if (k
< i
+ j
) {
98 if (v
!= 0xb1b2b3b4b5b6b7b8ULL
)
101 if (v
!= 0xa1a1a1a1a1a1a1a1ULL
)
111 return (i
<< 24) | (j
<< 16) | k
| 0x8000;
115 static __init
int strchr_selftest(void)
117 const char *test_string
= "abcdefghijkl";
118 const char *empty_string
= "";
122 for (i
= 0; i
< strlen(test_string
) + 1; i
++) {
123 result
= strchr(test_string
, test_string
[i
]);
124 if (result
- test_string
!= i
)
128 result
= strchr(empty_string
, '\0');
129 if (result
!= empty_string
)
132 result
= strchr(empty_string
, 'a');
136 result
= strchr(test_string
, 'z');
143 static __init
int strnchr_selftest(void)
145 const char *test_string
= "abcdefghijkl";
146 const char *empty_string
= "";
150 for (i
= 0; i
< strlen(test_string
) + 1; i
++) {
151 for (j
= 0; j
< strlen(test_string
) + 2; j
++) {
152 result
= strnchr(test_string
, j
, test_string
[i
]);
156 return ((i
+ 'a') << 8) | j
;
158 if (result
- test_string
!= i
)
159 return ((i
+ 'a') << 8) | j
;
163 result
= strnchr(empty_string
, 0, '\0');
167 result
= strnchr(empty_string
, 1, '\0');
168 if (result
!= empty_string
)
171 result
= strnchr(empty_string
, 1, 'a');
175 result
= strnchr(NULL
, 0, '\0');
182 static __init
int strspn_selftest(void)
184 static const struct strspn_test
{
186 const char accept
[16];
187 const char reject
[16];
190 } tests
[] __initconst
= {
191 { "foobar", "", "", 0, 6 },
192 { "abba", "abc", "ABBA", 4, 4 },
193 { "abba", "a", "b", 1, 1 },
194 { "", "abc", "abc", 0, 0},
196 const struct strspn_test
*s
= tests
;
199 for (i
= 0; i
< ARRAY_SIZE(tests
); ++i
, ++s
) {
200 res
= strspn(s
->str
, s
->accept
);
203 res
= strcspn(s
->str
, s
->reject
);
205 return 0x100 + 2*i
+ 1;
210 static __exit
void string_selftest_remove(void)
214 static __init
int string_selftest_init(void)
219 subtest
= memset16_selftest();
224 subtest
= memset32_selftest();
229 subtest
= memset64_selftest();
234 subtest
= strchr_selftest();
239 subtest
= strnchr_selftest();
244 subtest
= strspn_selftest();
248 pr_info("String selftests succeeded\n");
251 pr_crit("String selftest failure %d.%08x\n", test
, subtest
);
255 module_init(string_selftest_init
);
256 module_exit(string_selftest_remove
);
257 MODULE_LICENSE("GPL v2");