test-strchr.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* Test STRCHR functions.
  2. Copyright (C) 1999-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #define TEST_MAIN
  16. #ifndef WIDE
  17. # ifdef USE_FOR_STRCHRNUL
  18. # define TEST_NAME "strchrnul"
  19. # else
  20. # define TEST_NAME "strchr"
  21. # endif /* !USE_FOR_STRCHRNUL */
  22. #else
  23. # ifdef USE_FOR_STRCHRNUL
  24. # define TEST_NAME "wcschrnul"
  25. # else
  26. # define TEST_NAME "wcschr"
  27. # endif /* !USE_FOR_STRCHRNUL */
  28. #endif /* WIDE */
  29. #include "test-string.h"
  30. #ifndef WIDE
  31. # ifdef USE_FOR_STRCHRNUL
  32. # define STRCHR strchrnul
  33. # else
  34. # define STRCHR strchr
  35. # endif /* !USE_FOR_STRCHRNUL */
  36. # define STRLEN strlen
  37. # define CHAR char
  38. # define BIG_CHAR CHAR_MAX
  39. # define MIDDLE_CHAR 127
  40. # define SMALL_CHAR 23
  41. # define UCHAR unsigned char
  42. # define L(s) s
  43. #else
  44. # include <wchar.h>
  45. # ifdef USE_FOR_STRCHRNUL
  46. # define STRCHR wcschrnul
  47. # else
  48. # define STRCHR wcschr
  49. # endif /* !USE_FOR_STRCHRNUL */
  50. # define STRLEN wcslen
  51. # define CHAR wchar_t
  52. # define BIG_CHAR WCHAR_MAX
  53. # define MIDDLE_CHAR 1121
  54. # define SMALL_CHAR 851
  55. # define UCHAR wchar_t
  56. # define L(s) L ## s
  57. #endif /* WIDE */
  58. #ifdef USE_FOR_STRCHRNUL
  59. # define NULLRET(endptr) endptr
  60. #else
  61. # define NULLRET(endptr) NULL
  62. #endif /* !USE_FOR_STRCHRNUL */
  63. typedef CHAR *(*proto_t) (const CHAR *, int);
  64. IMPL (STRCHR, 1)
  65. /* Also check the generic implementation. */
  66. #undef STRCHR
  67. #undef static_weak_alias
  68. #define static_weak_alias(a, b)
  69. #undef libc_hidden_builtin_def
  70. #define libc_hidden_builtin_def(a)
  71. #undef libc_hidden_def
  72. #define libc_hidden_def(a)
  73. #undef libc_hidden_weak
  74. #define libc_hidden_weak(a)
  75. #ifndef WIDE
  76. # define STRCHRNUL __strchrnul_default
  77. # include "string/strchrnul.c"
  78. # ifndef USE_FOR_STRCHRNUL
  79. # define STRCHR __strchr_default
  80. # include "string/strchr.c"
  81. # define STRCHR_DEFAULT STRCHR
  82. # else
  83. # define STRCHR_DEFAULT STRCHRNUL
  84. # endif
  85. #else
  86. # ifndef USE_FOR_STRCHRNUL
  87. # define WCSCHR __wcschr_default
  88. # include "wcsmbs/wcschr.c"
  89. # define STRCHR_DEFAULT WCSCHR
  90. # else
  91. # define WCSCHRNUL __wcschrnul_default
  92. # include "wcsmbs/wcschrnul.c"
  93. # define STRCHR_DEFAULT WCSCHRNUL
  94. # endif
  95. #endif
  96. IMPL (STRCHR_DEFAULT, 1)
  97. static int
  98. check_result (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
  99. {
  100. CHAR *res = CALL (impl, s, c);
  101. if (res != exp_res)
  102. {
  103. error (0, 0, "Wrong result in function %s(%p) %#x %p %p", impl->name, s,
  104. c, res, exp_res);
  105. ret = 1;
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. static int
  111. do_one_test (impl_t *impl, const CHAR *s, int c, const CHAR *exp_res)
  112. {
  113. return check_result (impl, s, c, exp_res);
  114. }
  115. static void
  116. do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
  117. /* For wcschr: align here means align not in bytes,
  118. but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
  119. len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
  120. {
  121. size_t i;
  122. CHAR *result;
  123. CHAR *buf = (CHAR *) buf1;
  124. align &= 127;
  125. if ((align + len) * sizeof (CHAR) >= page_size)
  126. return;
  127. for (i = 0; i < len; ++i)
  128. {
  129. buf[align + i] = 32 + 23 * i % max_char;
  130. if (buf[align + i] == seek_char)
  131. buf[align + i] = seek_char + 1;
  132. else if (buf[align + i] == 0)
  133. buf[align + i] = 1;
  134. }
  135. buf[align + len] = 0;
  136. if (pos < len)
  137. {
  138. buf[align + pos] = seek_char;
  139. result = buf + align + pos;
  140. }
  141. else if (seek_char == 0)
  142. result = buf + align + len;
  143. else
  144. result = NULLRET (buf + align + len);
  145. FOR_EACH_IMPL (impl, 0)
  146. {
  147. if (do_one_test (impl, buf + align, seek_char, result) != 0)
  148. {
  149. error (0, 0,
  150. "\tAlign=%zu, Pos=%zu, Len=%zu, seek=%d, max_char=%d, "
  151. "Buf=%p, Res=%p",
  152. align, pos, len, seek_char, max_char, buf, result);
  153. }
  154. }
  155. }
  156. static void
  157. do_random_tests (void)
  158. {
  159. size_t i, j, n, align, pos, len;
  160. int seek_char;
  161. CHAR *result;
  162. UCHAR *p = (UCHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
  163. for (n = 0; n < ITERATIONS; n++)
  164. {
  165. /* For wcschr: align here means align not in bytes, but in wchar_ts,
  166. in bytes it will equal to align * (sizeof (wchar_t)). */
  167. align = random () & 15;
  168. pos = random () & 511;
  169. seek_char = random () & 255;
  170. if (pos + align >= 511)
  171. pos = 510 - align - (random () & 7);
  172. /* len for wcschr here isn't in bytes but it's number of wchar_t
  173. symbols. */
  174. len = random () & 511;
  175. if ((pos == len && seek_char)
  176. || (pos > len && (random () & 1)))
  177. len = pos + 1 + (random () & 7);
  178. if (len + align >= 512)
  179. len = 511 - align - (random () & 7);
  180. if (pos == len && seek_char)
  181. len = pos + 1;
  182. j = (pos > len ? pos : len) + align + 64;
  183. if (j > 512)
  184. j = 512;
  185. for (i = 0; i < j; i++)
  186. {
  187. if (i == pos + align)
  188. p[i] = seek_char;
  189. else if (i == len + align)
  190. p[i] = 0;
  191. else
  192. {
  193. p[i] = random () & 255;
  194. if (i < pos + align && p[i] == seek_char)
  195. p[i] = seek_char + 13;
  196. if (i < len + align && !p[i])
  197. {
  198. p[i] = seek_char - 13;
  199. if (!p[i])
  200. p[i] = 140;
  201. }
  202. }
  203. }
  204. if (pos <= len)
  205. result = (CHAR *) (p + pos + align);
  206. else if (seek_char == 0)
  207. result = (CHAR *) (p + len + align);
  208. else
  209. result = NULLRET ((CHAR *) (p + len + align));
  210. FOR_EACH_IMPL (impl, 1)
  211. if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
  212. {
  213. error (0, 0, "Iteration %zd - wrong result in function \
  214. %s (align in bytes: %zd, seek_char: %d, len: %zd, pos: %zd) %p != %p, p %p",
  215. n, impl->name, align * sizeof (CHAR), seek_char, len, pos,
  216. CALL (impl, (CHAR *) (p + align), seek_char), result, p);
  217. ret = 1;
  218. }
  219. }
  220. }
  221. static void
  222. check1 (void)
  223. {
  224. CHAR s[] __attribute__((aligned(16))) = L ("\xff");
  225. CHAR c = L ('\xfe');
  226. #ifndef USE_FOR_STRCHRNUL
  227. CHAR *exp_result = NULL;
  228. #else
  229. CHAR *exp_result = s + STRLEN (s);
  230. #endif
  231. FOR_EACH_IMPL (impl, 0)
  232. check_result (impl, s, c, exp_result);
  233. }
  234. static void
  235. check2 (void)
  236. {
  237. CHAR *s = (CHAR *) (buf1 + getpagesize () - 4 * sizeof (CHAR));
  238. CHAR *s_begin = (CHAR *) (buf1 + getpagesize () - 64);
  239. #ifndef USE_FOR_STRCHRNUL
  240. CHAR *exp_result = NULL;
  241. #else
  242. CHAR *exp_result = s + 1;
  243. #endif
  244. CHAR val = 0x12;
  245. for (; s_begin != s; ++s_begin)
  246. *s_begin = val;
  247. s[0] = val + 1;
  248. s[1] = 0;
  249. s[2] = val + 1;
  250. s[3] = val + 1;
  251. {
  252. FOR_EACH_IMPL (impl, 0)
  253. check_result (impl, s, val, exp_result);
  254. }
  255. s[3] = val;
  256. {
  257. FOR_EACH_IMPL (impl, 0)
  258. check_result (impl, s, val, exp_result);
  259. }
  260. exp_result = s;
  261. s[0] = val;
  262. {
  263. FOR_EACH_IMPL (impl, 0)
  264. check_result (impl, s, val, exp_result);
  265. }
  266. s[3] = val + 1;
  267. {
  268. FOR_EACH_IMPL (impl, 0)
  269. check_result (impl, s, val, exp_result);
  270. }
  271. s[0] = val + 1;
  272. s[1] = val + 1;
  273. s[2] = val + 1;
  274. s[3] = val + 1;
  275. s[4] = val;
  276. exp_result = s + 4;
  277. {
  278. FOR_EACH_IMPL (impl, 0)
  279. check_result (impl, s, val, exp_result);
  280. }
  281. s[4] = 0;
  282. #ifndef USE_FOR_STRCHRNUL
  283. exp_result = NULL;
  284. #else
  285. exp_result = s + 4;
  286. #endif
  287. {
  288. FOR_EACH_IMPL (impl, 0)
  289. check_result (impl, s, val, exp_result);
  290. }
  291. }
  292. int
  293. test_main (void)
  294. {
  295. size_t i;
  296. test_init ();
  297. check1 ();
  298. check2 ();
  299. printf ("%20s", "");
  300. FOR_EACH_IMPL (impl, 0)
  301. printf ("\t%s", impl->name);
  302. putchar ('\n');
  303. for (i = 1; i < 8; ++i)
  304. {
  305. do_test (0, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
  306. do_test (i, 16 << i, 2048, SMALL_CHAR, MIDDLE_CHAR);
  307. }
  308. for (i = 1; i < 8; ++i)
  309. {
  310. do_test (0, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
  311. do_test (i, 16 << i, 4096, SMALL_CHAR, MIDDLE_CHAR);
  312. }
  313. for (i = 1; i < 8; ++i)
  314. {
  315. do_test (i, 64, 256, SMALL_CHAR, MIDDLE_CHAR);
  316. do_test (i, 64, 256, SMALL_CHAR, BIG_CHAR);
  317. }
  318. for (i = 0; i < 8; ++i)
  319. {
  320. do_test (16 * i, 256, 512, SMALL_CHAR, MIDDLE_CHAR);
  321. do_test (16 * i, 256, 512, SMALL_CHAR, BIG_CHAR);
  322. }
  323. for (i = 0; i < 32; ++i)
  324. {
  325. do_test (0, i, i + 1, SMALL_CHAR, MIDDLE_CHAR);
  326. do_test (0, i, i + 1, SMALL_CHAR, BIG_CHAR);
  327. }
  328. for (i = 1; i < 8; ++i)
  329. {
  330. do_test (0, 16 << i, 2048, 0, MIDDLE_CHAR);
  331. do_test (i, 16 << i, 2048, 0, MIDDLE_CHAR);
  332. }
  333. for (i = 1; i < 8; ++i)
  334. {
  335. do_test (0, 16 << i, 4096, 0, MIDDLE_CHAR);
  336. do_test (i, 16 << i, 4096, 0, MIDDLE_CHAR);
  337. }
  338. for (i = 1; i < 8; ++i)
  339. {
  340. do_test (i, 64, 256, 0, MIDDLE_CHAR);
  341. do_test (i, 64, 256, 0, BIG_CHAR);
  342. }
  343. for (i = 0; i < 8; ++i)
  344. {
  345. do_test (16 * i, 256, 512, 0, MIDDLE_CHAR);
  346. do_test (16 * i, 256, 512, 0, BIG_CHAR);
  347. }
  348. for (i = 0; i < 32; ++i)
  349. {
  350. do_test (0, i, i + 1, 0, MIDDLE_CHAR);
  351. do_test (0, i, i + 1, 0, BIG_CHAR);
  352. }
  353. do_random_tests ();
  354. return ret;
  355. }
  356. #include <support/test-driver.c>