5 /* bsearch( const void *, const void *, size_t, size_t, int(*)( const void *, const void * ) )
7 This file is part of the Public Domain C Library (PDCLib).
8 Permission is granted to use, modify, and / or redistribute at will.
15 void * bsearch( const void * key, const void * base, size_t nmemb, size_t size, int (*compar)( const void *, const void * ) )
22 /* algorithm needs -1 correction if remaining elements are an even number. */
23 corr = ( nmemb % 2 ) - 1;
25 pivot = (const char *)base + (nmemb * size);
26 rc = compar( key, pivot );
29 base = (const char *)pivot + size;
43 #include <_PDCLIB_test.h>
45 int compare( const void * left, const void * right )
47 return *( (unsigned char *)left ) - *( (unsigned char *)right );
53 TESTCASE( bsearch( "e", abcde, 4, 1, compare ) == NULL );
54 TESTCASE( bsearch( "e", abcde, 5, 1, compare ) == &abcde[4] );
55 TESTCASE( bsearch( "a", abcde + 1, 4, 1, compare ) == NULL );
56 TESTCASE( bsearch( "0", abcde, 1, 1, compare ) == NULL );
57 TESTCASE( bsearch( "a", abcde, 1, 1, compare ) == &abcde[0] );
58 TESTCASE( bsearch( "a", abcde, 0, 1, compare ) == NULL );
59 TESTCASE( bsearch( "e", abcde, 3, 2, compare ) == &abcde[4] );
60 TESTCASE( bsearch( "b", abcde, 3, 2, compare ) == NULL );