1
0

bsrchtst.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*++
  2. Copyright (c) 2013 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. bsrchtst.c
  9. Abstract:
  10. This module tests the binary search function in the C library.
  11. Author:
  12. Evan Green 20-Aug-2013
  13. Environment:
  14. Test
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. //
  20. // Define this so it doesn't get defined to an import.
  21. //
  22. #define LIBC_API
  23. #include <minoca/lib/types.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. //
  27. // ---------------------------------------------------------------- Definitions
  28. //
  29. #define TEST_BINARY_SEARCH_ARRAY_COUNT 1000
  30. //
  31. // ------------------------------------------------------ Data Type Definitions
  32. //
  33. //
  34. // ----------------------------------------------- Internal Function Prototypes
  35. //
  36. ULONG
  37. TestBinarySearchCase (
  38. ULONG ArraySize,
  39. LONG DesiredIndex
  40. );
  41. int
  42. TestBinarySearchCompare (
  43. const void *LeftPointer,
  44. const void *RightPointer
  45. );
  46. //
  47. // -------------------------------------------------------------------- Globals
  48. //
  49. LONG TestBinarySearchArray[TEST_BINARY_SEARCH_ARRAY_COUNT];
  50. //
  51. // ------------------------------------------------------------------ Functions
  52. //
  53. ULONG
  54. TestBinarySearch (
  55. VOID
  56. )
  57. /*++
  58. Routine Description:
  59. This routine implements the entry point for the binary search test.
  60. Arguments:
  61. None.
  62. Return Value:
  63. Returns the count of test failures.
  64. --*/
  65. {
  66. ULONG Failures;
  67. LONG Index;
  68. ULONG Size;
  69. //
  70. // Initialize the sorted array.
  71. //
  72. for (Index = 0; Index < TEST_BINARY_SEARCH_ARRAY_COUNT; Index += 1) {
  73. TestBinarySearchArray[Index] = Index;
  74. }
  75. //
  76. // Perform the tests. Start with every possibility between 0 and 10.
  77. //
  78. Failures = 0;
  79. for (Size = 0; Size < 10; Size += 1) {
  80. for (Index = 0; Index <= Size; Index += 1) {
  81. Failures += TestBinarySearchCase(Size, Index);
  82. }
  83. }
  84. //
  85. // Test some slightly bigger ones.
  86. //
  87. Failures += TestBinarySearchCase(50, 25);
  88. Failures += TestBinarySearchCase(50, 48);
  89. Failures += TestBinarySearchCase(50, 49);
  90. Failures += TestBinarySearchCase(50, 0);
  91. Failures += TestBinarySearchCase(50, 1);
  92. Failures += TestBinarySearchCase(50, 3);
  93. Failures += TestBinarySearchCase(50, 12);
  94. Failures += TestBinarySearchCase(50, -1);
  95. Failures += TestBinarySearchCase(50, 51);
  96. Failures += TestBinarySearchCase(500, 25);
  97. Failures += TestBinarySearchCase(500, 250);
  98. Failures += TestBinarySearchCase(500, 1);
  99. Failures += TestBinarySearchCase(500, 2);
  100. Failures += TestBinarySearchCase(500, -1);
  101. Failures += TestBinarySearchCase(500, 60);
  102. Failures += TestBinarySearchCase(500, 61);
  103. Failures += TestBinarySearchCase(500, 497);
  104. Failures += TestBinarySearchCase(500, 498);
  105. Failures += TestBinarySearchCase(500, 499);
  106. Failures += TestBinarySearchCase(501, 499);
  107. //
  108. // Try the big ones.
  109. //
  110. for (Index = -1; Index <= TEST_BINARY_SEARCH_ARRAY_COUNT; Index += 1) {
  111. Failures += TestBinarySearchCase(TEST_BINARY_SEARCH_ARRAY_COUNT, Index);
  112. }
  113. return Failures;
  114. }
  115. //
  116. // --------------------------------------------------------- Internal Functions
  117. //
  118. ULONG
  119. TestBinarySearchCase (
  120. ULONG ArraySize,
  121. LONG DesiredIndex
  122. )
  123. /*++
  124. Routine Description:
  125. This routine implements a binary search test.
  126. Arguments:
  127. ArraySize - Supplies the supposed size of the array, up to
  128. TEST_BINARY_SEARCH_ARRAY_COUNT.
  129. DesiredIndex - Supplies the desired index to search for. If this is greater
  130. than or equal to the array size, then the test fails if the element is
  131. found. Otherwise the test fails if the element is not found.
  132. Return Value:
  133. 0 if the test passed.
  134. 1 if the test failed.
  135. --*/
  136. {
  137. PLONG FoundValue;
  138. FoundValue = bsearch(&DesiredIndex,
  139. TestBinarySearchArray,
  140. ArraySize,
  141. sizeof(LONG),
  142. TestBinarySearchCompare);
  143. if (DesiredIndex < ArraySize) {
  144. if (FoundValue == NULL) {
  145. printf("bsearch: Failed to find element %d in array of size %d.\n",
  146. DesiredIndex,
  147. ArraySize);
  148. return 1;
  149. }
  150. if (*FoundValue != DesiredIndex) {
  151. printf("bsearch: Found wrong value %d. Should have found %d. "
  152. "Array size was %d.\n",
  153. *FoundValue,
  154. DesiredIndex,
  155. ArraySize);
  156. return 1;
  157. }
  158. } else {
  159. if (FoundValue != NULL) {
  160. printf("bsearch: Found value %d (desired %d) in array of "
  161. "size %d that should not have had that element.\n",
  162. *FoundValue,
  163. DesiredIndex,
  164. ArraySize);
  165. return 1;
  166. }
  167. }
  168. return 0;
  169. }
  170. int
  171. TestBinarySearchCompare (
  172. const void *LeftPointer,
  173. const void *RightPointer
  174. )
  175. /*++
  176. Routine Description:
  177. This routine compares two elements in a binary search test.
  178. Arguments:
  179. LeftPointer - Supplies a pointer to the left element of the comparison.
  180. RightPointer - Supplies a pointer to the right element of the comparison.
  181. Return Value:
  182. -1 if Left < Right.
  183. 0 if Left == Right.
  184. 1 if Left > Right.
  185. --*/
  186. {
  187. LONG Left;
  188. LONG Right;
  189. Left = *((PLONG)LeftPointer);
  190. Right = *((PLONG)RightPointer);
  191. if (Left < Right) {
  192. return -1;
  193. }
  194. if (Left > Right) {
  195. return 1;
  196. }
  197. return 0;
  198. }