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