test-strspn.c 5.4 KB

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