test-strcpy.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* Test and measure strcpy 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. #ifdef WIDE
  16. # include <wchar.h>
  17. # define CHAR wchar_t
  18. # define UCHAR wchar_t
  19. # define sfmt "ls"
  20. # define BIG_CHAR WCHAR_MAX
  21. # define SMALL_CHAR 1273
  22. # define STRCMP wcscmp
  23. # define MEMCMP wmemcmp
  24. # define MEMSET wmemset
  25. #else
  26. # define CHAR char
  27. # define UCHAR unsigned char
  28. # define sfmt "s"
  29. # define BIG_CHAR CHAR_MAX
  30. # define SMALL_CHAR 127
  31. # define STRCMP strcmp
  32. # define MEMCMP memcmp
  33. # define MEMSET memset
  34. #endif
  35. #ifndef STRCPY_RESULT
  36. # define STRCPY_RESULT(dst, len) dst
  37. # define TEST_MAIN
  38. # ifndef WIDE
  39. # define TEST_NAME "strcpy"
  40. # else
  41. # define TEST_NAME "wcscpy"
  42. # endif
  43. # include "test-string.h"
  44. # ifndef WIDE
  45. # define STRCPY strcpy
  46. # else
  47. # define STRCPY wcscpy
  48. # endif
  49. IMPL (STRCPY, 1)
  50. /* Also check the generic implementation. */
  51. #undef STRCPY
  52. #undef libc_hidden_builtin_def
  53. #define libc_hidden_builtin_def(a)
  54. #undef libc_hidden_def
  55. #define libc_hidden_def(a)
  56. #undef weak_alias
  57. #define weak_alias(a,b)
  58. #undef attribute_hidden
  59. #define attribute_hidden
  60. # ifndef WIDE
  61. # define STPCPY __stpcpy_default
  62. # include "string/stpcpy.c"
  63. # define STRCPY __strcpy_default
  64. # define __stpcpy __stpcpy_default
  65. # include "string/strcpy.c"
  66. IMPL (__strcpy_default, 1)
  67. # else
  68. # define __wcslen wcslen
  69. # define __wmemcpy wmemcpy
  70. # define WCSCPY __wcscpy_default
  71. # include "wcsmbs/wcscpy.c"
  72. IMPL (__wcscpy_default, 1)
  73. # endif
  74. #endif
  75. typedef CHAR *(*proto_t) (CHAR *, const CHAR *);
  76. static void
  77. do_one_test (impl_t *impl, CHAR *dst, const CHAR *src,
  78. size_t len __attribute__((unused)))
  79. {
  80. if (CALL (impl, dst, src) != STRCPY_RESULT (dst, len))
  81. {
  82. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  83. CALL (impl, dst, src), STRCPY_RESULT (dst, len));
  84. ret = 1;
  85. return;
  86. }
  87. if (STRCMP (dst, src) != 0)
  88. {
  89. error (0, 0,
  90. "Wrong result in function %s dst \"%" sfmt "\" src \"%" sfmt "\"",
  91. impl->name, dst, src);
  92. ret = 1;
  93. return;
  94. }
  95. }
  96. static void
  97. do_test (size_t align1, size_t align2, size_t len, int max_char)
  98. {
  99. size_t i;
  100. CHAR *s1, *s2;
  101. /* For wcscpy: align1 and align2 here mean alignment not in bytes,
  102. but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t))
  103. len for wcschr here isn't in bytes but it's number of wchar_t symbols. */
  104. align1 &= 7;
  105. if ((align1 + len) * sizeof (CHAR) >= page_size)
  106. return;
  107. align2 &= 7;
  108. if ((align2 + len) * sizeof (CHAR) >= page_size)
  109. return;
  110. s1 = (CHAR *) (buf1) + align1;
  111. s2 = (CHAR *) (buf2) + align2;
  112. for (i = 0; i < len; i++)
  113. s1[i] = 32 + 23 * i % (max_char - 32);
  114. s1[len] = 0;
  115. FOR_EACH_IMPL (impl, 0)
  116. do_one_test (impl, s2, s1, len);
  117. }
  118. static void
  119. do_random_tests (void)
  120. {
  121. size_t i, j, n, align1, align2, len;
  122. UCHAR *p1 = (UCHAR *) (buf1 + page_size) - 512;
  123. UCHAR *p2 = (UCHAR *) (buf2 + page_size) - 512;
  124. UCHAR *res;
  125. for (n = 0; n < ITERATIONS; n++)
  126. {
  127. /* For wcsrchr: align1 and align2 here mean align not in bytes,
  128. but in wchar_ts, in bytes it will equal to align * (sizeof
  129. (wchar_t)). For strrchr we need to check all alignments from
  130. 0 to 63 since some assembly implementations have separate
  131. prolog for alignments more 48. */
  132. align1 = random () & (63 / sizeof (CHAR));
  133. if (random () & 1)
  134. align2 = random () & (63 / sizeof (CHAR));
  135. else
  136. align2 = align1 + (random () & 24);
  137. len = random () & 511;
  138. j = align1;
  139. if (align2 > j)
  140. j = align2;
  141. if (len + j >= 511)
  142. len = 510 - j - (random () & 7);
  143. j = len + align1 + 64;
  144. if (j > 512)
  145. j = 512;
  146. for (i = 0; i < j; i++)
  147. {
  148. if (i == len + align1)
  149. p1[i] = 0;
  150. else
  151. {
  152. p1[i] = random () & BIG_CHAR;
  153. if (i >= align1 && i < len + align1 && !p1[i])
  154. p1[i] = (random () & SMALL_CHAR) + 3;
  155. }
  156. }
  157. FOR_EACH_IMPL (impl, 1)
  158. {
  159. MEMSET (p2 - 64, '\1', 512 + 64);
  160. res = (UCHAR *) CALL (impl, (CHAR *) (p2 + align2), (CHAR *) (p1 + align1));
  161. if (res != STRCPY_RESULT (p2 + align2, len))
  162. {
  163. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p",
  164. n, impl->name, align1, align2, len, res,
  165. STRCPY_RESULT (p2 + align2, len));
  166. ret = 1;
  167. }
  168. for (j = 0; j < align2 + 64; ++j)
  169. {
  170. if (p2[j - 64] != '\1')
  171. {
  172. error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
  173. n, impl->name, align1, align2, len);
  174. ret = 1;
  175. break;
  176. }
  177. }
  178. for (j = align2 + len + 1; j < 512; ++j)
  179. {
  180. if (p2[j] != '\1')
  181. {
  182. error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
  183. n, impl->name, align1, align2, len);
  184. ret = 1;
  185. break;
  186. }
  187. }
  188. if (MEMCMP (p1 + align1, p2 + align2, len + 1))
  189. {
  190. error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
  191. n, impl->name, align1, align2, len);
  192. ret = 1;
  193. }
  194. }
  195. }
  196. }
  197. int
  198. test_main (void)
  199. {
  200. size_t i;
  201. test_init ();
  202. printf ("%23s", "");
  203. FOR_EACH_IMPL (impl, 0)
  204. printf ("\t%s", impl->name);
  205. putchar ('\n');
  206. for (i = 0; i < 16; ++i)
  207. {
  208. do_test (0, 0, i, SMALL_CHAR);
  209. do_test (0, 0, i, BIG_CHAR);
  210. do_test (0, i, i, SMALL_CHAR);
  211. do_test (i, 0, i, BIG_CHAR);
  212. }
  213. for (i = 1; i < 8; ++i)
  214. {
  215. do_test (0, 0, 8 << i, SMALL_CHAR);
  216. do_test (8 - i, 2 * i, 8 << i, SMALL_CHAR);
  217. }
  218. for (i = 1; i < 8; ++i)
  219. {
  220. do_test (i, 2 * i, 8 << i, SMALL_CHAR);
  221. do_test (2 * i, i, 8 << i, BIG_CHAR);
  222. do_test (i, i, 8 << i, SMALL_CHAR);
  223. do_test (i, i, 8 << i, BIG_CHAR);
  224. }
  225. do_random_tests ();
  226. return ret;
  227. }
  228. #include <support/test-driver.c>