test-strrchr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Test and measure 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. #ifdef WIDE
  17. # define TEST_NAME "wcsrchr"
  18. #else
  19. # define TEST_NAME "strrchr"
  20. #endif
  21. #include "test-string.h"
  22. #ifdef WIDE
  23. # include <wchar.h>
  24. # define STRRCHR wcsrchr
  25. # define CHAR wchar_t
  26. # define UCHAR wchar_t
  27. # define BIG_CHAR WCHAR_MAX
  28. # define SMALL_CHAR 1273
  29. #else
  30. # define STRRCHR strrchr
  31. # define CHAR char
  32. # define UCHAR unsigned char
  33. # define BIG_CHAR CHAR_MAX
  34. # define SMALL_CHAR 127
  35. #endif
  36. typedef CHAR *(*proto_t) (const CHAR *, int);
  37. IMPL (STRRCHR, 1)
  38. /* Also check the generic implementation. */
  39. #undef STRRCHR
  40. #undef weak_alias
  41. #define weak_alias(a, b)
  42. #undef libc_hidden_builtin_def
  43. #define libc_hidden_builtin_def(a)
  44. #undef libc_hidden_def
  45. #define libc_hidden_def(a)
  46. #undef libc_hidden_weak
  47. #define libc_hidden_weak(a)
  48. #ifndef WIDE
  49. # define STRLEN __strlen_default
  50. # include "string/strlen.c"
  51. # define MEMRCHR __memrchr_default
  52. # include "string/memrchr.c"
  53. # define STRRCHR __strrchr_default
  54. # include "string/strrchr.c"
  55. # define STRRCHR_DEFAULT __strrchr_default
  56. #else
  57. # define WCSRCHR __wcsrchr_default
  58. # include "wcsmbs/wcsrchr.c"
  59. # define STRRCHR_DEFAULT __wcsrchr_default
  60. #endif
  61. IMPL (STRRCHR_DEFAULT, 1)
  62. static void
  63. do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
  64. {
  65. CHAR *res = CALL (impl, s, c);
  66. if (res != exp_res)
  67. {
  68. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  69. res, exp_res);
  70. ret = 1;
  71. return;
  72. }
  73. }
  74. static void
  75. do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
  76. /* For wcsrchr: align here means align not in bytes,
  77. but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
  78. len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
  79. {
  80. size_t i;
  81. CHAR *result;
  82. CHAR *buf = (CHAR *) buf1;
  83. align &= 7;
  84. if ( (align + len) * sizeof (CHAR) >= page_size)
  85. return;
  86. for (i = 0; i < len; ++i)
  87. {
  88. buf[align + i] = (random () * random ()) & max_char;
  89. if (!buf[align + i])
  90. buf[align + i] = (random () * random ()) & max_char;
  91. if (!buf[align + i])
  92. buf[align + i] = 1;
  93. if ((i > pos || pos >= len) && buf[align + i] == seek_char)
  94. buf[align + i] = seek_char + 10 + (random () & 15);
  95. }
  96. buf[align + len] = 0;
  97. if (pos < len)
  98. {
  99. buf[align + pos] = seek_char;
  100. result = (CHAR *) (buf + align + pos);
  101. }
  102. else if (seek_char == 0)
  103. result = (CHAR *) (buf + align + len);
  104. else
  105. result = NULL;
  106. FOR_EACH_IMPL (impl, 0)
  107. do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
  108. }
  109. static void
  110. do_random_tests (void)
  111. {
  112. size_t i, j, n, align, pos, len;
  113. int seek_char;
  114. CHAR *result;
  115. UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
  116. for (n = 0; n < ITERATIONS; n++)
  117. {
  118. align = random () & (63 / sizeof (CHAR));
  119. /* For wcsrchr: align here means align not in bytes, but in wchar_ts,
  120. in bytes it will equal to align * (sizeof (wchar_t)).
  121. For strrchr we need to check all alignments from 0 to 63 since
  122. some assembly implementations have separate prolog for alignments
  123. more 48. */
  124. pos = random () & 511;
  125. if (pos + align >= 511)
  126. pos = 510 - align - (random () & 7);
  127. len = random () & 511;
  128. /* len for wcschr here isn't in bytes but it's number of wchar_t
  129. symbols. */
  130. if (pos >= len)
  131. len = pos + (random () & 7);
  132. if (len + align >= 512)
  133. len = 511 - align - (random () & 7);
  134. seek_char = random () & 255;
  135. if (seek_char && pos == len)
  136. {
  137. if (pos)
  138. --pos;
  139. else
  140. ++len;
  141. }
  142. j = len + align + 64;
  143. if (j > 512)
  144. j = 512;
  145. for (i = 0; i < j; i++)
  146. {
  147. if (i == pos + align)
  148. p[i] = seek_char;
  149. else if (i == len + align)
  150. p[i] = 0;
  151. else
  152. {
  153. p[i] = random () & 255;
  154. if (((i > pos + align && i < len + align) || pos > len)
  155. && p[i] == seek_char)
  156. p[i] = seek_char + 13;
  157. if (i < len + align && !p[i])
  158. {
  159. p[i] = seek_char - 13;
  160. if (!p[i])
  161. p[i] = 140;
  162. }
  163. }
  164. }
  165. if (pos <= len)
  166. result = (CHAR *) (p + pos + align);
  167. else if (seek_char == 0)
  168. result = (CHAR *) (p + len + align);
  169. else
  170. result = NULL;
  171. FOR_EACH_IMPL (impl, 1)
  172. if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
  173. {
  174. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
  175. n, impl->name, align, seek_char, len, pos,
  176. CALL (impl, (CHAR *) (p + align), seek_char), result, p);
  177. ret = 1;
  178. }
  179. }
  180. }
  181. int
  182. test_main (void)
  183. {
  184. size_t i;
  185. test_init ();
  186. printf ("%20s", "");
  187. FOR_EACH_IMPL (impl, 0)
  188. printf ("\t%s", impl->name);
  189. putchar ('\n');
  190. for (i = 1; i < 8; ++i)
  191. {
  192. do_test (0, 16 << i, 2048, 23, SMALL_CHAR);
  193. do_test (i, 16 << i, 2048, 23, SMALL_CHAR);
  194. }
  195. for (i = 1; i < 8; ++i)
  196. {
  197. do_test (i, 64, 256, 23, SMALL_CHAR);
  198. do_test (i, 64, 256, 23, BIG_CHAR);
  199. }
  200. for (i = 0; i < 32; ++i)
  201. {
  202. do_test (0, i, i + 1, 23, SMALL_CHAR);
  203. do_test (0, i, i + 1, 23, BIG_CHAR);
  204. }
  205. for (i = 1; i < 8; ++i)
  206. {
  207. do_test (0, 16 << i, 2048, 0, SMALL_CHAR);
  208. do_test (i, 16 << i, 2048, 0, SMALL_CHAR);
  209. }
  210. for (i = 1; i < 8; ++i)
  211. {
  212. do_test (i, 64, 256, 0, SMALL_CHAR);
  213. do_test (i, 64, 256, 0, BIG_CHAR);
  214. }
  215. for (i = 0; i < 32; ++i)
  216. {
  217. do_test (0, i, i + 1, 0, SMALL_CHAR);
  218. do_test (0, i, i + 1, 0, BIG_CHAR);
  219. }
  220. do_random_tests ();
  221. return ret;
  222. }
  223. #include <support/test-driver.c>