test-strlen.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Test and measure STRLEN 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 "strlen"
  18. #else
  19. # define TEST_NAME "wcslen"
  20. #endif
  21. #include "test-string.h"
  22. #ifndef WIDE
  23. # define STRLEN strlen
  24. # define CHAR char
  25. # define MAX_CHAR CHAR_MAX
  26. #else
  27. # include <wchar.h>
  28. # define STRLEN wcslen
  29. # define CHAR wchar_t
  30. # define MAX_CHAR WCHAR_MAX
  31. #endif
  32. typedef size_t (*proto_t) (const CHAR *);
  33. IMPL (STRLEN, 1)
  34. /* Also check the generic implementation. */
  35. #undef STRLEN
  36. #undef static_weak_alias
  37. #define static_weak_alias(a, b)
  38. #undef libc_hidden_builtin_def
  39. #define libc_hidden_builtin_def(a)
  40. #ifndef WIDE
  41. # define STRLEN __strlen_default
  42. # include "string/strlen.c"
  43. IMPL (__strlen_default, 1)
  44. #else
  45. # define WCSLEN __wcslen_default
  46. # include "wcsmbs/wcslen.c"
  47. IMPL (__wcslen_default, 1)
  48. #endif
  49. static void
  50. do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
  51. {
  52. size_t len = CALL (impl, s);
  53. if (len != exp_len)
  54. {
  55. error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
  56. len, exp_len);
  57. ret = 1;
  58. return;
  59. }
  60. }
  61. static void
  62. do_test (size_t align, size_t len)
  63. {
  64. size_t i;
  65. align &= (getpagesize () / sizeof (CHAR)) - 1;
  66. if (align + sizeof (CHAR) * len >= page_size)
  67. return;
  68. CHAR *buf = (CHAR *) (buf1);
  69. for (i = 0; i < len; ++i)
  70. buf[align + i] = 1 + 11111 * i % MAX_CHAR;
  71. buf[align + len] = 0;
  72. FOR_EACH_IMPL (impl, 0)
  73. do_one_test (impl, (CHAR *) (buf + align), len);
  74. }
  75. static void
  76. do_random_tests (void)
  77. {
  78. size_t i, j, n, align, len;
  79. CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof (CHAR));
  80. for (n = 0; n < ITERATIONS; n++)
  81. {
  82. align = random () & 15;
  83. len = random () & 511;
  84. if (len + align > 510)
  85. len = 511 - align - (random () & 7);
  86. j = len + align + 64;
  87. if (j > 512)
  88. j = 512;
  89. for (i = 0; i < j; i++)
  90. {
  91. if (i == len + align)
  92. p[i] = 0;
  93. else
  94. {
  95. p[i] = random () & 255;
  96. if (i >= align && i < len + align && !p[i])
  97. p[i] = (random () & 127) + 1;
  98. }
  99. }
  100. FOR_EACH_IMPL (impl, 1)
  101. if (CALL (impl, (CHAR *) (p + align)) != len)
  102. {
  103. error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p",
  104. n, impl->name, align, CALL (impl, (CHAR *) (p + align)),
  105. len, p);
  106. ret = 1;
  107. }
  108. }
  109. }
  110. int
  111. test_main (void)
  112. {
  113. size_t i;
  114. test_init ();
  115. printf ("%20s", "");
  116. FOR_EACH_IMPL (impl, 0)
  117. printf ("\t%s", impl->name);
  118. putchar ('\n');
  119. /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
  120. for (i = 1; i < 8; ++i)
  121. {
  122. do_test (sizeof (CHAR) * i, i);
  123. do_test (0, i);
  124. }
  125. for (i = 2; i <= 12; ++i)
  126. {
  127. do_test (0, 1 << i);
  128. do_test (sizeof (CHAR) * 7, 1 << i);
  129. do_test (sizeof (CHAR) * i, 1 << i);
  130. do_test (sizeof (CHAR) * i, (size_t)((1 << i) / 1.5));
  131. }
  132. /* Test strings near page boundary */
  133. size_t maxlength = 64 / sizeof (CHAR) - 1;
  134. size_t pagesize = getpagesize () / sizeof (CHAR);
  135. for (i = maxlength ; i > 1; --i)
  136. {
  137. /* String stays on the same page. */
  138. do_test (pagesize - i, i - 1);
  139. /* String crosses page boundary. */
  140. do_test (pagesize - i, maxlength);
  141. }
  142. do_random_tests ();
  143. return ret;
  144. }
  145. #include <support/test-driver.c>