test-strpbrk.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Test and measure strpbrk functions.
  2. Copyright (C) 1999-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Written by Jakub Jelinek <jakub@redhat.com>, 1999.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef WIDE
  17. # define CHAR char
  18. # define UCHAR unsigned char
  19. # define STRLEN strlen
  20. # define STRCHR strchr
  21. # define BIG_CHAR CHAR_MAX
  22. # define SMALL_CHAR 127
  23. #else
  24. # include <wchar.h>
  25. # define CHAR wchar_t
  26. # define UCHAR wchar_t
  27. # define STRLEN wcslen
  28. # define STRCHR wcschr
  29. # define BIG_CHAR WCHAR_MAX
  30. # define SMALL_CHAR 1273
  31. #endif /* WIDE */
  32. #ifndef STRPBRK_RESULT
  33. # define STRPBRK_RESULT(s, pos) ((s)[(pos)] ? (s) + (pos) : NULL)
  34. # define RES_TYPE CHAR *
  35. # define TEST_MAIN
  36. # ifndef WIDE
  37. # define TEST_NAME "strpbrk"
  38. # else
  39. # define TEST_NAME "wcspbrk"
  40. # endif /* WIDE */
  41. # include "test-string.h"
  42. # ifndef WIDE
  43. # define STRPBRK strpbrk
  44. # define SIMPLE_STRPBRK simple_strpbrk
  45. # define STUPID_STRPBRK stupid_strpbrk
  46. # else
  47. # include <wchar.h>
  48. # define STRPBRK wcspbrk
  49. # define SIMPLE_STRPBRK simple_wcspbrk
  50. # define STUPID_STRPBRK stupid_wcspbrk
  51. # endif /* WIDE */
  52. typedef CHAR *(*proto_t) (const CHAR *, const CHAR *);
  53. CHAR *SIMPLE_STRPBRK (const CHAR *, const CHAR *);
  54. CHAR *STUPID_STRPBRK (const CHAR *, const CHAR *);
  55. IMPL (STUPID_STRPBRK, 0)
  56. IMPL (SIMPLE_STRPBRK, 0)
  57. IMPL (STRPBRK, 1)
  58. CHAR *
  59. SIMPLE_STRPBRK (const CHAR *s, const CHAR *rej)
  60. {
  61. const CHAR *r;
  62. CHAR c;
  63. while ((c = *s++) != '\0')
  64. for (r = rej; *r != '\0'; ++r)
  65. if (*r == c)
  66. return (CHAR *) s - 1;
  67. return NULL;
  68. }
  69. CHAR *
  70. STUPID_STRPBRK (const CHAR *s, const CHAR *rej)
  71. {
  72. size_t ns = STRLEN (s), nrej = STRLEN (rej);
  73. size_t i, j;
  74. for (i = 0; i < ns; ++i)
  75. for (j = 0; j < nrej; ++j)
  76. if (s[i] == rej[j])
  77. return (CHAR *) s + i;
  78. return NULL;
  79. }
  80. #endif /* !STRPBRK_RESULT */
  81. static void
  82. do_one_test (impl_t *impl, const CHAR *s, const CHAR *rej, RES_TYPE exp_res)
  83. {
  84. RES_TYPE res = CALL (impl, s, rej);
  85. if (res != exp_res)
  86. {
  87. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  88. (void *) res, (void *) exp_res);
  89. ret = 1;
  90. return;
  91. }
  92. }
  93. static void
  94. do_test (size_t align, size_t pos, size_t len)
  95. {
  96. size_t i;
  97. int c;
  98. RES_TYPE result;
  99. CHAR *rej, *s;
  100. align &= 7;
  101. if ((align + pos + 10) * sizeof (CHAR) >= page_size || len > 240)
  102. return;
  103. rej = (CHAR *) (buf2) + (random () & 255);
  104. s = (CHAR *) (buf1) + align;
  105. for (i = 0; i < len; ++i)
  106. {
  107. rej[i] = random () & BIG_CHAR;
  108. if (!rej[i])
  109. rej[i] = random () & BIG_CHAR;
  110. if (!rej[i])
  111. rej[i] = 1 + (random () & SMALL_CHAR);
  112. }
  113. rej[len] = '\0';
  114. for (c = 1; c <= BIG_CHAR; ++c)
  115. if (STRCHR (rej, c) == NULL)
  116. break;
  117. for (i = 0; i < pos; ++i)
  118. {
  119. s[i] = random () & BIG_CHAR;
  120. if (STRCHR (rej, s[i]))
  121. {
  122. s[i] = random () & BIG_CHAR;
  123. if (STRCHR (rej, s[i]))
  124. s[i] = c;
  125. }
  126. }
  127. s[pos] = rej[random () % (len + 1)];
  128. if (s[pos])
  129. {
  130. for (i = pos + 1; i < pos + 10; ++i)
  131. s[i] = random () & BIG_CHAR;
  132. s[i] = '\0';
  133. }
  134. result = STRPBRK_RESULT (s, pos);
  135. FOR_EACH_IMPL (impl, 0)
  136. do_one_test (impl, s, rej, result);
  137. }
  138. static void
  139. do_random_tests (void)
  140. {
  141. size_t i, j, n, align, pos, len, rlen;
  142. RES_TYPE result;
  143. int c;
  144. UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
  145. UCHAR *rej;
  146. for (n = 0; n < ITERATIONS; n++)
  147. {
  148. align = random () & 15;
  149. pos = random () & 511;
  150. if (pos + align >= 511)
  151. pos = 510 - align - (random () & 7);
  152. len = random () & 511;
  153. if (pos >= len && (random () & 1))
  154. len = pos + 1 + (random () & 7);
  155. if (len + align >= 512)
  156. len = 511 - align - (random () & 7);
  157. if (random () & 1)
  158. rlen = random () & 63;
  159. else
  160. rlen = random () & 15;
  161. rej = (UCHAR *) (buf2 + page_size) - rlen - 1 - (random () & 7);
  162. for (i = 0; i < rlen; ++i)
  163. {
  164. rej[i] = random () & BIG_CHAR;
  165. if (!rej[i])
  166. rej[i] = random () & BIG_CHAR;
  167. if (!rej[i])
  168. rej[i] = 1 + (random () & SMALL_CHAR);
  169. }
  170. rej[i] = '\0';
  171. for (c = 1; c <= BIG_CHAR; ++c)
  172. if (STRCHR ((CHAR *) rej, c) == NULL)
  173. break;
  174. j = (pos > len ? pos : len) + align + 64;
  175. if (j > 512)
  176. j = 512;
  177. for (i = 0; i < j; i++)
  178. {
  179. if (i == len + align)
  180. p[i] = '\0';
  181. else if (i == pos + align)
  182. p[i] = rej[random () % (rlen + 1)];
  183. else if (i < align || i > pos + align)
  184. p[i] = random () & BIG_CHAR;
  185. else
  186. {
  187. p[i] = random () & BIG_CHAR;
  188. if (STRCHR ((CHAR *) rej, p[i]))
  189. {
  190. p[i] = random () & BIG_CHAR;
  191. if (STRCHR ((CHAR *) rej, p[i]))
  192. p[i] = c;
  193. }
  194. }
  195. }
  196. result = STRPBRK_RESULT ((CHAR *) (p + align), pos < len ? pos : len);
  197. FOR_EACH_IMPL (impl, 1)
  198. if (CALL (impl, (CHAR *) (p + align), (CHAR *) rej) != result)
  199. {
  200. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %p, %zd, %zd, %zd) %p != %p",
  201. n, impl->name, align, rej, rlen, pos, len,
  202. (void *) CALL (impl, (CHAR *) (p + align), (CHAR *) rej),
  203. (void *) result);
  204. ret = 1;
  205. }
  206. }
  207. }
  208. int
  209. test_main (void)
  210. {
  211. size_t i;
  212. test_init ();
  213. printf ("%32s", "");
  214. FOR_EACH_IMPL (impl, 0)
  215. printf ("\t%s", impl->name);
  216. putchar ('\n');
  217. for (i = 0; i < 32; ++i)
  218. {
  219. do_test (0, 512, i);
  220. do_test (i, 512, i);
  221. }
  222. for (i = 1; i < 8; ++i)
  223. {
  224. do_test (0, 16 << i, 4);
  225. do_test (i, 16 << i, 4);
  226. }
  227. for (i = 1; i < 8; ++i)
  228. do_test (i, 64, 10);
  229. for (i = 0; i < 64; ++i)
  230. do_test (0, i, 6);
  231. do_random_tests ();
  232. return ret;
  233. }
  234. #include <support/test-driver.c>