test-strnlen.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Test strlen 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 "strnlen"
  19. #else
  20. # define TEST_NAME "wcsnlen"
  21. #endif /* !WIDE */
  22. #include "test-string.h"
  23. #ifndef WIDE
  24. # define STRNLEN strnlen
  25. # define CHAR char
  26. # define BIG_CHAR CHAR_MAX
  27. # define MIDDLE_CHAR 127
  28. # define SIMPLE_STRNLEN simple_strnlen
  29. #else
  30. # include <wchar.h>
  31. # define STRNLEN wcsnlen
  32. # define CHAR wchar_t
  33. # define BIG_CHAR WCHAR_MAX
  34. # define MIDDLE_CHAR 1121
  35. # define SIMPLE_STRNLEN simple_wcsnlen
  36. #endif /* !WIDE */
  37. typedef size_t (*proto_t) (const CHAR *, size_t);
  38. size_t SIMPLE_STRNLEN (const CHAR *, size_t);
  39. IMPL (SIMPLE_STRNLEN, 0)
  40. IMPL (STRNLEN, 1)
  41. size_t
  42. SIMPLE_STRNLEN (const CHAR *s, size_t maxlen)
  43. {
  44. size_t i;
  45. for (i = 0; i < maxlen && s[i]; ++i);
  46. return i;
  47. }
  48. static void
  49. do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len)
  50. {
  51. size_t len = CALL (impl, s, maxlen);
  52. if (len != exp_len)
  53. {
  54. error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
  55. len, exp_len);
  56. ret = 1;
  57. return;
  58. }
  59. }
  60. static void
  61. do_test (size_t align, size_t len, size_t maxlen, int max_char)
  62. {
  63. size_t i;
  64. align &= 63;
  65. if ((align + len) * sizeof (CHAR) >= page_size)
  66. return;
  67. CHAR *buf = (CHAR *) (buf1);
  68. for (i = 0; i < len; ++i)
  69. buf[align + i] = 1 + 11111 * i % max_char;
  70. buf[align + len] = 0;
  71. FOR_EACH_IMPL (impl, 0)
  72. do_one_test (impl, (CHAR *) (buf + align), maxlen, MIN (len, maxlen));
  73. }
  74. static void
  75. do_random_tests (void)
  76. {
  77. size_t i, j, n, align, len;
  78. CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
  79. for (n = 0; n < ITERATIONS; n++)
  80. {
  81. align = random () & 15;
  82. len = random () & 511;
  83. if (len + align > 510)
  84. len = 511 - align - (random () & 7);
  85. j = len + align + 64;
  86. if (j > 512)
  87. j = 512;
  88. for (i = 0; i < j; i++)
  89. {
  90. if (i == len + align)
  91. p[i] = 0;
  92. else
  93. {
  94. p[i] = random () & 255;
  95. if (i >= align && i < len + align && !p[i])
  96. p[i] = (random () & 127) + 1;
  97. }
  98. }
  99. FOR_EACH_IMPL (impl, 1)
  100. {
  101. if (len > 0
  102. && CALL (impl, (CHAR *) (p + align), len - 1) != len - 1)
  103. {
  104. error (0, 0, "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p",
  105. n, impl->name, align,
  106. CALL (impl, (CHAR *) (p + align), len - 1), len - 1, p);
  107. ret = 1;
  108. }
  109. if (CALL (impl, (CHAR *) (p + align), len) != len)
  110. {
  111. error (0, 0, "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p",
  112. n, impl->name, align,
  113. CALL (impl, (CHAR *) (p + align), len), len, p);
  114. ret = 1;
  115. }
  116. if (CALL (impl, (CHAR *) (p + align), len + 1) != len)
  117. {
  118. error (0, 0, "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p",
  119. n, impl->name, align,
  120. CALL (impl, (CHAR *) (p + align), len + 1), len, p);
  121. ret = 1;
  122. }
  123. }
  124. }
  125. }
  126. /* Tests meant to unveil fail on implementation that does not access bytes
  127. around the page boundary accordingly. */
  128. static void
  129. do_page_tests (void)
  130. {
  131. size_t i, exp_len, start_offset, offset;
  132. /* Calculate the null character offset. */
  133. size_t last_offset = (page_size / sizeof (CHAR)) - 1;
  134. CHAR *s = (CHAR *) buf2;
  135. memset (s, 65, (last_offset - 1));
  136. s[last_offset] = 0;
  137. /* Place short strings ending at page boundary. */
  138. offset = last_offset;
  139. for (i = 0; i < 128; i++)
  140. {
  141. /* Decrease offset to stress several sizes and alignments. */
  142. offset--;
  143. exp_len = last_offset - offset;
  144. FOR_EACH_IMPL (impl, 0)
  145. {
  146. /* Varies maxlen value to cover the cases where it is:
  147. - larger than length;
  148. - slightly greater than length;
  149. - equal to length;
  150. - slightly less than length. */
  151. do_one_test (impl, (CHAR *) (s + offset), page_size, exp_len);
  152. do_one_test (impl, (CHAR *) (s + offset), exp_len + 1, exp_len);
  153. do_one_test (impl, (CHAR *) (s + offset), exp_len, exp_len);
  154. if (exp_len > 0)
  155. do_one_test (impl, (CHAR *) (s + offset), exp_len - 1, exp_len - 1);
  156. }
  157. }
  158. /* Place long strings ending at page boundary. */
  159. start_offset = (last_offset + 1) / 2;
  160. for (i = 0; i < 64; ++i)
  161. {
  162. /* Increase offset to stress several alignments. */
  163. offset = start_offset + i;
  164. if (offset >= (last_offset + 1))
  165. break;
  166. exp_len = last_offset - offset;
  167. FOR_EACH_IMPL (impl, 0)
  168. {
  169. /* Checks only for maxlen much larger than length because smaller
  170. values are already covered in do_random_tests function. */
  171. do_one_test (impl, (CHAR *) (s + offset), page_size, exp_len);
  172. }
  173. }
  174. }
  175. int
  176. test_main (void)
  177. {
  178. size_t i;
  179. test_init ();
  180. printf ("%20s", "");
  181. FOR_EACH_IMPL (impl, 0)
  182. printf ("\t%s", impl->name);
  183. putchar ('\n');
  184. for (i = 1; i < 8; ++i)
  185. {
  186. do_test (0, i, i - 1, MIDDLE_CHAR);
  187. do_test (0, i, i, MIDDLE_CHAR);
  188. do_test (0, i, i + 1, MIDDLE_CHAR);
  189. }
  190. for (i = 1; i < 8; ++i)
  191. {
  192. do_test (i, i, i - 1, MIDDLE_CHAR);
  193. do_test (i, i, i, MIDDLE_CHAR);
  194. do_test (i, i, i + 1, MIDDLE_CHAR);
  195. }
  196. for (i = 2; i <= 10; ++i)
  197. {
  198. do_test (0, 1 << i, 5000, MIDDLE_CHAR);
  199. do_test (1, 1 << i, 5000, MIDDLE_CHAR);
  200. }
  201. for (i = 1; i < 8; ++i)
  202. do_test (0, i, 5000, BIG_CHAR);
  203. for (i = 1; i < 8; ++i)
  204. do_test (i, i, 5000, BIG_CHAR);
  205. for (i = 2; i <= 10; ++i)
  206. {
  207. do_test (0, 1 << i, 5000, BIG_CHAR);
  208. do_test (1, 1 << i, 5000, BIG_CHAR);
  209. }
  210. do_random_tests ();
  211. do_page_tests ();
  212. return ret;
  213. }
  214. #include <support/test-driver.c>