test-strspn.c 4.9 KB

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