test-memrchr.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Test and measure memrchr functions.
  2. Copyright (C) 2013-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. #define TEST_NAME "memrchr"
  18. #include "test-string.h"
  19. typedef char *(*proto_t) (const char *, int, size_t);
  20. char *simple_memrchr (const char *, int, size_t);
  21. IMPL (simple_memrchr, 0)
  22. IMPL (memrchr, 1)
  23. char *
  24. simple_memrchr (const char *s, int c, size_t n)
  25. {
  26. s = s + n;
  27. while (n--)
  28. if (*--s == (char) c)
  29. return (char *) s;
  30. return NULL;
  31. }
  32. static void
  33. do_one_test (impl_t *impl, const char *s, int c, size_t n, char *exp_res)
  34. {
  35. char *res = CALL (impl, s, c, n);
  36. if (res != exp_res)
  37. {
  38. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  39. res, exp_res);
  40. ret = 1;
  41. return;
  42. }
  43. }
  44. static void
  45. do_test (size_t align, size_t pos, size_t len, int seek_char)
  46. {
  47. size_t i;
  48. char *result;
  49. align &= 7;
  50. if (align + len >= page_size)
  51. return;
  52. for (i = 0; i < len; ++i)
  53. {
  54. buf1[align + i] = 1 + 23 * i % 127;
  55. if (buf1[align + i] == seek_char)
  56. buf1[align + i] = seek_char + 1;
  57. }
  58. buf1[align + len] = 0;
  59. if (pos < len)
  60. {
  61. buf1[align + pos] = seek_char;
  62. buf1[align + len] = -seek_char;
  63. result = (char *) (buf1 + align + pos);
  64. }
  65. else
  66. {
  67. result = NULL;
  68. buf1[align + len] = seek_char;
  69. }
  70. FOR_EACH_IMPL (impl, 0)
  71. do_one_test (impl, (char *) (buf1 + align), seek_char, len, result);
  72. }
  73. static void
  74. do_random_tests (void)
  75. {
  76. size_t i, j, n, align, pos, len;
  77. int seek_char;
  78. char *result;
  79. unsigned char *p = buf1 + page_size - 512;
  80. for (n = 0; n < ITERATIONS; n++)
  81. {
  82. align = random () & 15;
  83. pos = random () & 511;
  84. if (pos + align >= 512)
  85. pos = 511 - align - (random () & 7);
  86. len = random () & 511;
  87. if (pos >= len)
  88. len = pos + (random () & 7);
  89. if (len + align >= 512)
  90. len = 512 - align - (random () & 7);
  91. seek_char = random () & 255;
  92. j = len + align + 64;
  93. if (j > 512)
  94. j = 512;
  95. for (i = 0; i < j; i++)
  96. {
  97. if (i == pos + align)
  98. p[i] = seek_char;
  99. else
  100. {
  101. p[i] = random () & 255;
  102. if (p[i] == seek_char)
  103. p[i] = seek_char + 13;
  104. }
  105. }
  106. if (pos < len)
  107. result = (char *) (p + pos + align);
  108. else
  109. result = NULL;
  110. FOR_EACH_IMPL (impl, 1)
  111. if (CALL (impl, (char *) (p + align), seek_char, len) != result)
  112. {
  113. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
  114. n, impl->name, align, seek_char, len, pos,
  115. CALL (impl, (char *) (p + align), seek_char, len),
  116. result, p);
  117. ret = 1;
  118. }
  119. }
  120. }
  121. int
  122. test_main (void)
  123. {
  124. size_t i;
  125. test_init ();
  126. printf ("%20s", "");
  127. FOR_EACH_IMPL (impl, 0)
  128. printf ("\t%s", impl->name);
  129. putchar ('\n');
  130. for (i = 1; i < 8; ++i)
  131. {
  132. /* Test len == 0. */
  133. do_test (i, i, 0, 0);
  134. do_test (i, i, 0, 23);
  135. do_test (0, 16 << i, 2048, 23);
  136. do_test (i, 64, 256, 23);
  137. do_test (0, 16 << i, 2048, 0);
  138. do_test (i, 64, 256, 0);
  139. do_test (0, i, 256, 23);
  140. do_test (0, i, 256, 0);
  141. do_test (i, i, 256, 23);
  142. do_test (i, i, 256, 0);
  143. }
  144. for (i = 1; i < 32; ++i)
  145. {
  146. do_test (0, i, i + 1, 23);
  147. do_test (0, i, i + 1, 0);
  148. do_test (i, i, i + 1, 23);
  149. do_test (i, i, i + 1, 0);
  150. do_test (0, 1, i + 1, 23);
  151. do_test (0, 2, i + 1, 0);
  152. do_test (i, 1, i + 1, 23);
  153. do_test (i, 2, i + 1, 0);
  154. }
  155. do_random_tests ();
  156. return ret;
  157. }
  158. #include <support/test-driver.c>