test-strpbrk.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Test and measure strpbrk 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. #ifndef WIDE
  16. # define CHAR char
  17. # define UCHAR unsigned char
  18. # define STRLEN strlen
  19. # define STRCHR strchr
  20. # define BIG_CHAR CHAR_MAX
  21. # define SMALL_CHAR 127
  22. #else
  23. # include <wchar.h>
  24. # define CHAR wchar_t
  25. # define UCHAR wchar_t
  26. # define STRLEN wcslen
  27. # define STRCHR wcschr
  28. # define BIG_CHAR WCHAR_MAX
  29. # define SMALL_CHAR 1273
  30. #endif /* WIDE */
  31. #ifndef STRPBRK_RESULT
  32. # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
  33. # define RES_TYPE CHAR *
  34. # define TEST_MAIN
  35. # ifndef WIDE
  36. # define TEST_NAME "strpbrk"
  37. # else
  38. # define TEST_NAME "wcspbrk"
  39. # endif /* WIDE */
  40. # include "test-string.h"
  41. # ifndef WIDE
  42. # define STRPBRK strpbrk
  43. # define SIMPLE_STRPBRK simple_strpbrk
  44. # else
  45. # include <wchar.h>
  46. # define STRPBRK wcspbrk
  47. # define SIMPLE_STRPBRK simple_wcspbrk
  48. # endif /* WIDE */
  49. typedef CHAR *(*proto_t) (const CHAR *, const CHAR *);
  50. IMPL (STRPBRK, 1)
  51. /* Naive implementation to verify results. */
  52. CHAR *
  53. SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej)
  54. {
  55. const CHAR *r;
  56. CHAR c;
  57. while ((c = *s++) != '\0')
  58. for (r = rej; *r != '\0'; ++r)
  59. if (*r == c)
  60. return (CHAR *) s - 1;
  61. return NULL;
  62. }
  63. #endif /* !STRPBRK_RESULT */
  64. static void
  65. do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res)
  66. {
  67. RES_TYPE res = CALL (impl, s, rej);
  68. if (res != exp_res)
  69. {
  70. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  71. (void *) res, (void *) exp_res);
  72. ret = 1;
  73. return;
  74. }
  75. }
  76. static void
  77. do_test (size_t align, size_t pos, size_t len)
  78. {
  79. size_t i;
  80. int c;
  81. RES_TYPE result;
  82. CHAR *rej, *s;
  83. align &= 7;
  84. if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240)
  85. return;
  86. rej = (CHAR *) (buf2) + (random () & 255);
  87. s = (CHAR *) (buf1) + align;
  88. for (i = 0; i < len; ++i)
  89. {
  90. rej[i] = random () & BIG_CHAR;
  91. if (!rej[i])
  92. rej[i] = random () & BIG_CHAR;
  93. if (!rej[i])
  94. rej[i] = 1 + (random () & SMALL_CHAR);
  95. }
  96. rej[len] = '\0';
  97. for (c = 1; c <= BIG_CHAR; ++c)
  98. if (STRCHR (rej, c) == NULL)
  99. break;
  100. for (i = 0; i < pos; ++i)
  101. {
  102. s[i] = random () & BIG_CHAR;
  103. if (STRCHR (rej, s[i]))
  104. {
  105. s[i] = random () & BIG_CHAR;
  106. if (STRCHR (rej, s[i]))
  107. s[i] = c;
  108. }
  109. }
  110. s[pos] = rej[random () % (len + 1)];
  111. if (s[pos])
  112. {
  113. for (i = pos + 1; i < pos + 10; ++i)
  114. s[i] = random () & BIG_CHAR;
  115. s[i] = '\0';
  116. }
  117. result = STRPBRK_RESULT (s, pos);
  118. FOR_EACH_IMPL (impl, 0)
  119. do_one_test (impl, s, rej, result);
  120. }
  121. static void
  122. do_random_tests (void)
  123. {
  124. size_t i, j, n, align, pos, len, rlen;
  125. RES_TYPE result;
  126. int c;
  127. UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
  128. UCHAR *rej;
  129. for (n = 0; n < ITERATIONS; n++)
  130. {
  131. align = random () & 15;
  132. pos = random () & 511;
  133. if (pos + align >= 511)
  134. pos = 510 - align - (random () & 7);
  135. len = random () & 511;
  136. if (pos >= len && (random () & 1))
  137. len = pos + 1 + (random () & 7);
  138. if (len + align >= 512)
  139. len = 511 - align - (random () & 7);
  140. if (random () & 1)
  141. rlen = random () & 63;
  142. else
  143. rlen = random () & 15;
  144. rej = (UCHAR *) (buf2 + page_size) - rlen - 1 - (random () & 7);
  145. for (i = 0; i < rlen; ++i)
  146. {
  147. rej[i] = random () & BIG_CHAR;
  148. if (!rej[i])
  149. rej[i] = random () & BIG_CHAR;
  150. if (!rej[i])
  151. rej[i] = 1 + (random () & SMALL_CHAR);
  152. }
  153. rej[i] = '\0';
  154. for (c = 1; c <= BIG_CHAR; ++c)
  155. if (STRCHR ((CHAR *) rej, c) == NULL)
  156. break;
  157. j = (pos > len ? pos : len) + align + 64;
  158. if (j > 512)
  159. j = 512;
  160. for (i = 0; i < j; i++)
  161. {
  162. if (i == len + align)
  163. p[i] = '\0';
  164. else if (i == pos + align)
  165. p[i] = rej[random () % (rlen + 1)];
  166. else if (i < align || i > pos + align)
  167. p[i] = random () & BIG_CHAR;
  168. else
  169. {
  170. p[i] = random () & BIG_CHAR;
  171. if (STRCHR ((CHAR *) rej, p[i]))
  172. {
  173. p[i] = random () & BIG_CHAR;
  174. if (STRCHR ((CHAR *) rej, p[i]))
  175. p[i] = c;
  176. }
  177. }
  178. }
  179. result = STRPBRK_RESULT ((CHAR *) (p + align), pos < len ? pos : len);
  180. FOR_EACH_IMPL (impl, 1)
  181. if (CALL (impl, (CHAR *) (p + align), (CHAR *) rej) != result)
  182. {
  183. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p",
  184. n, impl->name, align, rej, rlen, pos, len,
  185. (void *) CALL (impl, (CHAR *) (p + align), (CHAR *) rej),
  186. (void *) result);
  187. ret = 1;
  188. }
  189. }
  190. }
  191. int
  192. test_main (void)
  193. {
  194. size_t i;
  195. test_init ();
  196. printf ("%32s", "");
  197. FOR_EACH_IMPL (impl, 0)
  198. printf ("\t%s", impl->name);
  199. putchar ('\n');
  200. for (i = 0; i < 32; ++i)
  201. {
  202. do_test (0, 512, i);
  203. do_test (i, 512, i);
  204. }
  205. for (i = 1; i < 8; ++i)
  206. {
  207. do_test (0, 16 << i, 4);
  208. do_test (i, 16 << i, 4);
  209. }
  210. for (i = 1; i < 8; ++i)
  211. do_test (i, 64, 10);
  212. for (i = 0; i < 64; ++i)
  213. do_test (0, i, 6);
  214. do_random_tests ();
  215. return ret;
  216. }
  217. #include <support/test-driver.c>