tst-bsearch.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Copyright (C) 2000-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
  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. <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <tst-stack-align.h>
  18. struct item
  19. {
  20. int val;
  21. const char *str;
  22. } arr[] =
  23. {
  24. { 0, "zero" },
  25. { 1, "one" },
  26. { 2, "two" },
  27. { 3, "three" },
  28. { 4, "four" },
  29. { 5, "five" },
  30. { 6, "six" },
  31. { 7, "seven" },
  32. { 8, "eight" },
  33. { 9, "nine" },
  34. { 10, "ten" }
  35. };
  36. #define narr (sizeof (arr) / sizeof (arr[0]))
  37. static int align_check;
  38. static int
  39. comp (const void *p1, const void *p2)
  40. {
  41. struct item *e1 = (struct item *) p1;
  42. struct item *e2 = (struct item *) p2;
  43. if (!align_check)
  44. align_check = TEST_STACK_ALIGN () ? -1 : 1;
  45. return e1->val - e2->val;
  46. }
  47. static int
  48. do_test (void)
  49. {
  50. size_t cnt;
  51. int result = 0;
  52. struct item key;
  53. struct item *res;
  54. for (cnt = 0; cnt < narr; ++cnt)
  55. {
  56. key.val = arr[cnt].val;
  57. res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
  58. if (res == NULL)
  59. {
  60. printf ("entry %zd not found\n", cnt);
  61. result = 1;
  62. }
  63. else if (res != &arr[cnt])
  64. {
  65. puts ("wrong entry returned");
  66. result = 1;
  67. }
  68. }
  69. /* And some special tests that shouldn't find any entry. */
  70. key.val = -1;
  71. res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
  72. if (res != NULL)
  73. {
  74. puts ("found an entry that's not there");
  75. result = 1;
  76. }
  77. key.val = 11;
  78. res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
  79. if (res != NULL)
  80. {
  81. puts ("found an entry that's not there");
  82. result = 1;
  83. }
  84. key.val = 11;
  85. res = (struct item *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
  86. if (res != NULL)
  87. {
  88. puts ("found an entry that's not there");
  89. result = 1;
  90. }
  91. /* Now the array contains only one element - no entry should be found. */
  92. for (cnt = 0; cnt < narr; ++cnt)
  93. {
  94. key.val = arr[cnt].val;
  95. res = (struct item *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
  96. if (cnt == 5)
  97. {
  98. if (res == NULL)
  99. {
  100. printf ("entry %zd not found\n", cnt);
  101. result = 1;
  102. }
  103. else if (res != &arr[cnt])
  104. {
  105. puts ("wrong entry returned");
  106. result = 1;
  107. }
  108. }
  109. else if (res != NULL)
  110. {
  111. puts ("found an entry that's not there");
  112. result = 1;
  113. }
  114. }
  115. if (align_check == 0)
  116. {
  117. puts ("alignment not checked");
  118. result = 1;
  119. }
  120. else if (align_check == -1)
  121. {
  122. puts ("stack not sufficiently aligned");
  123. result = 1;
  124. }
  125. if (result == 0)
  126. puts ("all OK");
  127. return result;
  128. }
  129. #define TEST_FUNCTION do_test ()
  130. #include "../test-skeleton.c"