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. */
23 pivot = (const char *)base + (nmemb * size);
24 rc = compar( key, pivot );
27 base = (const char *)pivot + size;
28 /* applying correction */
29 nmemb -= ( 1 - corr );
42 #include <_PDCLIB_test.h>
44 static int compare( const void * left, const void * right )
46 return *( (unsigned char *)left ) - *( (unsigned char *)right );
51 TESTCASE( bsearch( "e", abcde, 4, 1, compare ) == NULL );
52 TESTCASE( bsearch( "e", abcde, 5, 1, compare ) == &abcde[4] );
53 TESTCASE( bsearch( "a", abcde + 1, 4, 1, compare ) == NULL );
54 TESTCASE( bsearch( "0", abcde, 1, 1, compare ) == NULL );
55 TESTCASE( bsearch( "a", abcde, 1, 1, compare ) == &abcde[0] );
56 TESTCASE( bsearch( "a", abcde, 0, 1, compare ) == NULL );
57 TESTCASE( bsearch( "e", abcde, 3, 2, compare ) == &abcde[4] );
58 TESTCASE( bsearch( "b", abcde, 3, 2, compare ) == NULL );