test-rawmemchr.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Test and measure memchr 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. #include <assert.h>
  17. #define TEST_MAIN
  18. #define TEST_NAME "rawmemchr"
  19. #include "test-string.h"
  20. typedef char *(*proto_t) (const char *, int);
  21. char *simple_rawmemchr (const char *, int);
  22. IMPL (simple_rawmemchr, 0)
  23. IMPL (rawmemchr, 1)
  24. char *
  25. simple_rawmemchr (const char *s, int c)
  26. {
  27. while (1)
  28. if (*s++ == (char) c)
  29. return (char *) s - 1;
  30. return NULL;
  31. }
  32. static void
  33. do_one_test (impl_t *impl, const char *s, int c, char *exp_res)
  34. {
  35. char *res = CALL (impl, s, c);
  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. assert (pos < len);
  60. buf1[align + pos] = seek_char;
  61. buf1[align + len] = -seek_char;
  62. result = (char *) (buf1 + align + pos);
  63. FOR_EACH_IMPL (impl, 0)
  64. do_one_test (impl, (char *) (buf1 + align), seek_char, result);
  65. }
  66. static void
  67. do_random_tests (void)
  68. {
  69. size_t i, j, n, align, pos, len;
  70. int seek_char;
  71. char *result;
  72. unsigned char *p = buf1 + page_size - 512;
  73. for (n = 0; n < ITERATIONS; n++)
  74. {
  75. align = random () & 15;
  76. pos = random () & 511;
  77. if (pos + align >= 512)
  78. pos = 511 - align - (random () & 7);
  79. len = random () & 511;
  80. if (len + align >= 512)
  81. len = 512 - align - (random () & 7);
  82. if (pos >= len)
  83. continue;
  84. seek_char = random () & 255;
  85. j = len + align + 64;
  86. if (j > 512)
  87. j = 512;
  88. for (i = 0; i < j; i++)
  89. {
  90. if (i == pos + align)
  91. p[i] = seek_char;
  92. else
  93. {
  94. p[i] = random () & 255;
  95. if (i < pos + align && p[i] == seek_char)
  96. p[i] = seek_char + 13;
  97. }
  98. }
  99. assert (pos < len);
  100. size_t r = random ();
  101. if ((r & 31) == 0)
  102. len = ~(uintptr_t) (p + align) - ((r >> 5) & 31);
  103. result = (char *) (p + pos + align);
  104. FOR_EACH_IMPL (impl, 1)
  105. if (CALL (impl, (char *) (p + align), seek_char) != result)
  106. {
  107. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %d, %zd, %zd) %p != %p, p %p",
  108. n, impl->name, align, seek_char, len, pos,
  109. CALL (impl, (char *) (p + align), seek_char),
  110. result, p);
  111. ret = 1;
  112. }
  113. }
  114. }
  115. int
  116. test_main (void)
  117. {
  118. size_t i;
  119. test_init ();
  120. printf ("%20s", "");
  121. FOR_EACH_IMPL (impl, 0)
  122. printf ("\t%s", impl->name);
  123. putchar ('\n');
  124. for (i = 1; i < 7; ++i)
  125. {
  126. do_test (0, 16 << i, 2048, 23);
  127. do_test (i, 64, 256, 23);
  128. do_test (0, 16 << i, 2048, 0);
  129. do_test (i, 64, 256, 0);
  130. }
  131. for (i = 1; i < 32; ++i)
  132. {
  133. do_test (0, i, i + 1, 23);
  134. do_test (0, i, i + 1, 0);
  135. }
  136. do_random_tests ();
  137. return ret;
  138. }
  139. #include <support/test-driver.c>