test-strrchr.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Test and measure STRCHR 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. Added wcsrrchr support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>,
  6. 2011.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, see
  17. <http://www.gnu.org/licenses/>. */
  18. #define TEST_MAIN
  19. #ifdef WIDE
  20. # define TEST_NAME "wcsrchr"
  21. #else
  22. # define TEST_NAME "strrchr"
  23. #endif
  24. #include "test-string.h"
  25. #ifdef WIDE
  26. # include <wchar.h>
  27. # define SIMPLE_STRRCHR simple_wcsrchr
  28. # define STRRCHR wcsrchr
  29. # define CHAR wchar_t
  30. # define UCHAR wchar_t
  31. # define BIG_CHAR WCHAR_MAX
  32. # define SMALL_CHAR 1273
  33. #else
  34. # define SIMPLE_STRRCHR simple_strrchr
  35. # define STRRCHR strrchr
  36. # define CHAR char
  37. # define UCHAR unsigned char
  38. # define BIG_CHAR CHAR_MAX
  39. # define SMALL_CHAR 127
  40. #endif
  41. typedef CHAR *(*proto_t) (const CHAR *, int);
  42. CHAR *SIMPLE_STRRCHR (const CHAR *, int);
  43. IMPL (SIMPLE_STRRCHR, 0)
  44. IMPL (STRRCHR, 1)
  45. CHAR *
  46. SIMPLE_STRRCHR (const CHAR *s, int c)
  47. {
  48. const CHAR *ret = NULL;
  49. for (; *s != '\0'; ++s)
  50. if (*s == (CHAR) c)
  51. ret = s;
  52. return (CHAR *) (c == '\0' ? s : ret);
  53. }
  54. static void
  55. do_one_test (impl_t *impl, const CHAR *s, int c, CHAR *exp_res)
  56. {
  57. CHAR *res = CALL (impl, s, c);
  58. if (res != exp_res)
  59. {
  60. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  61. res, exp_res);
  62. ret = 1;
  63. return;
  64. }
  65. }
  66. static void
  67. do_test (size_t align, size_t pos, size_t len, int seek_char, int max_char)
  68. /* For wcsrchr: align here means align not in bytes,
  69. but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
  70. len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
  71. {
  72. size_t i;
  73. CHAR *result;
  74. CHAR *buf = (CHAR *) buf1;
  75. align &= 7;
  76. if ( (align + len) * sizeof(CHAR) >= page_size)
  77. return;
  78. for (i = 0; i < len; ++i)
  79. {
  80. buf[align + i] = (random () * random ()) & max_char;
  81. if (!buf[align + i])
  82. buf[align + i] = (random () * random ()) & max_char;
  83. if (!buf[align + i])
  84. buf[align + i] = 1;
  85. if ((i > pos || pos >= len) && buf[align + i] == seek_char)
  86. buf[align + i] = seek_char + 10 + (random () & 15);
  87. }
  88. buf[align + len] = 0;
  89. if (pos < len)
  90. {
  91. buf[align + pos] = seek_char;
  92. result = (CHAR *) (buf + align + pos);
  93. }
  94. else if (seek_char == 0)
  95. result = (CHAR *) (buf + align + len);
  96. else
  97. result = NULL;
  98. FOR_EACH_IMPL (impl, 0)
  99. do_one_test (impl, (CHAR *) (buf + align), seek_char, result);
  100. }
  101. static void
  102. do_random_tests (void)
  103. {
  104. size_t i, j, n, align, pos, len;
  105. int seek_char;
  106. CHAR *result;
  107. UCHAR *p = (UCHAR *) (buf1 + page_size) - 512;
  108. for (n = 0; n < ITERATIONS; n++)
  109. {
  110. align = random () & (63 / sizeof(CHAR));
  111. /* For wcsrchr: align here means align not in bytes, but in wchar_ts,
  112. in bytes it will equal to align * (sizeof (wchar_t)).
  113. For strrchr we need to check all alignments from 0 to 63 since
  114. some assembly implementations have separate prolog for alignments
  115. more 48. */
  116. pos = random () & 511;
  117. if (pos + align >= 511)
  118. pos = 510 - align - (random () & 7);
  119. len = random () & 511;
  120. /* len for wcschr here isn't in bytes but it's number of wchar_t
  121. symbols. */
  122. if (pos >= len)
  123. len = pos + (random () & 7);
  124. if (len + align >= 512)
  125. len = 511 - align - (random () & 7);
  126. seek_char = random () & 255;
  127. if (seek_char && pos == len)
  128. {
  129. if (pos)
  130. --pos;
  131. else
  132. ++len;
  133. }
  134. j = len + align + 64;
  135. if (j > 512)
  136. j = 512;
  137. for (i = 0; i < j; i++)
  138. {
  139. if (i == pos + align)
  140. p[i] = seek_char;
  141. else if (i == len + align)
  142. p[i] = 0;
  143. else
  144. {
  145. p[i] = random () & 255;
  146. if (((i > pos + align && i < len + align) || pos > len)
  147. && p[i] == seek_char)
  148. p[i] = seek_char + 13;
  149. if (i < len + align && !p[i])
  150. {
  151. p[i] = seek_char - 13;
  152. if (!p[i])
  153. p[i] = 140;
  154. }
  155. }
  156. }
  157. if (pos <= len)
  158. result = (CHAR *) (p + pos + align);
  159. else if (seek_char == 0)
  160. result = (CHAR *) (p + len + align);
  161. else
  162. result = NULL;
  163. FOR_EACH_IMPL (impl, 1)
  164. if (CALL (impl, (CHAR *) (p + align), seek_char) != result)
  165. {
  166. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
  167. n, impl->name, align, seek_char, len, pos,
  168. CALL (impl, (CHAR *) (p + align), seek_char), result, p);
  169. ret = 1;
  170. }
  171. }
  172. }
  173. int
  174. test_main (void)
  175. {
  176. size_t i;
  177. test_init ();
  178. printf ("%20s", "");
  179. FOR_EACH_IMPL (impl, 0)
  180. printf ("\t%s", impl->name);
  181. putchar ('\n');
  182. for (i = 1; i < 8; ++i)
  183. {
  184. do_test (0, 16 << i, 2048, 23, SMALL_CHAR);
  185. do_test (i, 16 << i, 2048, 23, SMALL_CHAR);
  186. }
  187. for (i = 1; i < 8; ++i)
  188. {
  189. do_test (i, 64, 256, 23, SMALL_CHAR);
  190. do_test (i, 64, 256, 23, BIG_CHAR);
  191. }
  192. for (i = 0; i < 32; ++i)
  193. {
  194. do_test (0, i, i + 1, 23, SMALL_CHAR);
  195. do_test (0, i, i + 1, 23, BIG_CHAR);
  196. }
  197. for (i = 1; i < 8; ++i)
  198. {
  199. do_test (0, 16 << i, 2048, 0, SMALL_CHAR);
  200. do_test (i, 16 << i, 2048, 0, SMALL_CHAR);
  201. }
  202. for (i = 1; i < 8; ++i)
  203. {
  204. do_test (i, 64, 256, 0, SMALL_CHAR);
  205. do_test (i, 64, 256, 0, BIG_CHAR);
  206. }
  207. for (i = 0; i < 32; ++i)
  208. {
  209. do_test (0, i, i + 1, 0, SMALL_CHAR);
  210. do_test (0, i, i + 1, 0, BIG_CHAR);
  211. }
  212. do_random_tests ();
  213. return ret;
  214. }
  215. #include <support/test-driver.c>