test_string.c 4.6 KB

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