test-strlen.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Test and measure 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. Added wcslen support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #define TEST_MAIN
  18. #ifndef WIDE
  19. # define TEST_NAME "strlen"
  20. #else
  21. # define TEST_NAME "wcslen"
  22. #endif
  23. #include "test-string.h"
  24. #ifndef WIDE
  25. # define STRLEN strlen
  26. # define CHAR char
  27. # define MAX_CHAR CHAR_MAX
  28. #else
  29. # include <wchar.h>
  30. # define STRLEN wcslen
  31. # define CHAR wchar_t
  32. # define MAX_CHAR WCHAR_MAX
  33. #endif
  34. typedef size_t (*proto_t) (const CHAR *);
  35. size_t
  36. simple_STRLEN (const CHAR *s)
  37. {
  38. const CHAR *p;
  39. for (p = s; *p; ++p);
  40. return p - s;
  41. }
  42. #ifndef WIDE
  43. size_t
  44. builtin_strlen (const CHAR *p)
  45. {
  46. return __builtin_strlen (p);
  47. }
  48. IMPL (builtin_strlen, 0)
  49. #endif
  50. IMPL (simple_STRLEN, 0)
  51. IMPL (STRLEN, 1)
  52. static void
  53. do_one_test (impl_t *impl, const CHAR *s, size_t exp_len)
  54. {
  55. size_t len = CALL (impl, s);
  56. if (len != exp_len)
  57. {
  58. error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
  59. len, exp_len);
  60. ret = 1;
  61. return;
  62. }
  63. }
  64. static void
  65. do_test (size_t align, size_t len)
  66. {
  67. size_t i;
  68. align &= 63;
  69. if (align + sizeof(CHAR) * len >= page_size)
  70. return;
  71. CHAR *buf = (CHAR *) (buf1);
  72. for (i = 0; i < len; ++i)
  73. buf[align + i] = 1 + 11111 * i % MAX_CHAR;
  74. buf[align + len] = 0;
  75. FOR_EACH_IMPL (impl, 0)
  76. do_one_test (impl, (CHAR *) (buf + align), len);
  77. }
  78. static void
  79. do_random_tests (void)
  80. {
  81. size_t i, j, n, align, len;
  82. CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof(CHAR));
  83. for (n = 0; n < ITERATIONS; n++)
  84. {
  85. align = random () & 15;
  86. len = random () & 511;
  87. if (len + align > 510)
  88. len = 511 - align - (random () & 7);
  89. j = len + align + 64;
  90. if (j > 512)
  91. j = 512;
  92. for (i = 0; i < j; i++)
  93. {
  94. if (i == len + align)
  95. p[i] = 0;
  96. else
  97. {
  98. p[i] = random () & 255;
  99. if (i >= align && i < len + align && !p[i])
  100. p[i] = (random () & 127) + 1;
  101. }
  102. }
  103. FOR_EACH_IMPL (impl, 1)
  104. if (CALL (impl, (CHAR *) (p + align)) != len)
  105. {
  106. error (0, 0, "Iteration %zd - wrong result in function %s (%zd) %zd != %zd, p %p",
  107. n, impl->name, align, CALL (impl, (CHAR *) (p + align)),
  108. len, p);
  109. ret = 1;
  110. }
  111. }
  112. }
  113. int
  114. test_main (void)
  115. {
  116. size_t i;
  117. test_init ();
  118. printf ("%20s", "");
  119. FOR_EACH_IMPL (impl, 0)
  120. printf ("\t%s", impl->name);
  121. putchar ('\n');
  122. /* Checking with only 4 * N alignments for wcslen, other alignments are wrong for wchar_t type arrays*/
  123. for (i = 1; i < 8; ++i)
  124. {
  125. do_test (sizeof(CHAR) * i, i);
  126. do_test (0, i);
  127. }
  128. for (i = 2; i <= 12; ++i)
  129. {
  130. do_test (0, 1 << i);
  131. do_test (sizeof(CHAR) * 7, 1 << i);
  132. do_test (sizeof(CHAR) * i, 1 << i);
  133. do_test (sizeof(CHAR) * i, (size_t)((1 << i) / 1.5));
  134. }
  135. do_random_tests ();
  136. return ret;
  137. }
  138. #include <support/test-driver.c>