Linux 4.19-rc7
[linux-2.6/btrfs-unstable.git] / lib / test_string.c
blob0fcdb82dca8667604a0a0756803b7c7914fc3aa1
1 #include <linux/module.h>
2 #include <linux/printk.h>
3 #include <linux/slab.h>
4 #include <linux/string.h>
6 static __init int memset16_selftest(void)
8 unsigned i, j, k;
9 u16 v, *p;
11 p = kmalloc(256 * 2 * 2, GFP_KERNEL);
12 if (!p)
13 return -1;
15 for (i = 0; i < 256; i++) {
16 for (j = 0; j < 256; j++) {
17 memset(p, 0xa1, 256 * 2 * sizeof(v));
18 memset16(p + i, 0xb1b2, j);
19 for (k = 0; k < 512; k++) {
20 v = p[k];
21 if (k < i) {
22 if (v != 0xa1a1)
23 goto fail;
24 } else if (k < i + j) {
25 if (v != 0xb1b2)
26 goto fail;
27 } else {
28 if (v != 0xa1a1)
29 goto fail;
35 fail:
36 kfree(p);
37 if (i < 256)
38 return (i << 24) | (j << 16) | k;
39 return 0;
42 static __init int memset32_selftest(void)
44 unsigned i, j, k;
45 u32 v, *p;
47 p = kmalloc(256 * 2 * 4, GFP_KERNEL);
48 if (!p)
49 return -1;
51 for (i = 0; i < 256; i++) {
52 for (j = 0; j < 256; j++) {
53 memset(p, 0xa1, 256 * 2 * sizeof(v));
54 memset32(p + i, 0xb1b2b3b4, j);
55 for (k = 0; k < 512; k++) {
56 v = p[k];
57 if (k < i) {
58 if (v != 0xa1a1a1a1)
59 goto fail;
60 } else if (k < i + j) {
61 if (v != 0xb1b2b3b4)
62 goto fail;
63 } else {
64 if (v != 0xa1a1a1a1)
65 goto fail;
71 fail:
72 kfree(p);
73 if (i < 256)
74 return (i << 24) | (j << 16) | k;
75 return 0;
78 static __init int memset64_selftest(void)
80 unsigned i, j, k;
81 u64 v, *p;
83 p = kmalloc(256 * 2 * 8, GFP_KERNEL);
84 if (!p)
85 return -1;
87 for (i = 0; i < 256; i++) {
88 for (j = 0; j < 256; j++) {
89 memset(p, 0xa1, 256 * 2 * sizeof(v));
90 memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
91 for (k = 0; k < 512; k++) {
92 v = p[k];
93 if (k < i) {
94 if (v != 0xa1a1a1a1a1a1a1a1ULL)
95 goto fail;
96 } else if (k < i + j) {
97 if (v != 0xb1b2b3b4b5b6b7b8ULL)
98 goto fail;
99 } else {
100 if (v != 0xa1a1a1a1a1a1a1a1ULL)
101 goto fail;
107 fail:
108 kfree(p);
109 if (i < 256)
110 return (i << 24) | (j << 16) | k;
111 return 0;
114 static __init int string_selftest_init(void)
116 int test, subtest;
118 test = 1;
119 subtest = memset16_selftest();
120 if (subtest)
121 goto fail;
123 test = 2;
124 subtest = memset32_selftest();
125 if (subtest)
126 goto fail;
128 test = 3;
129 subtest = memset64_selftest();
130 if (subtest)
131 goto fail;
133 pr_info("String selftests succeeded\n");
134 return 0;
135 fail:
136 pr_crit("String selftest failure %d.%08x\n", test, subtest);
137 return 0;
140 module_init(string_selftest_init);
141 MODULE_LICENSE("GPL v2");