66static void makedata(
int nobj,
int **data,
int **sort,
int **indx,
int **rank )
70 *data = malloc(nobj*
sizeof(**data));
71 *sort = malloc(nobj*
sizeof(**data));
72 *indx = malloc(nobj*
sizeof(**indx));
73 *rank = malloc(nobj*
sizeof(**rank));
75 for ( i = 0; i < nobj; ++i )
76 (*sort)[i] = (*data)[i] = rand() % 100;
80static void freedata(
int *data,
int *sort,
int *indx,
int *rank)
89static int compar(
void *
p,
const void *
a,
const void *b )
91 int x = *((
const int *)
a);
92 int y = *((
const int *)b);
93 int ascend = *(
int *)
p;
112static int lt(
void *
p,
const void *
a,
const void *b )
114 return compar(
p,
a, b ) < 0;
118static int lte(
void *
p,
const void *
a,
const void *b )
120 return compar(
p,
a, b ) <= 0;
124static int check(
int *data,
int *sort,
int nobj,
int ascend )
128 for ( i = 1; i < nobj; ++i )
131 if ( sort[i] < sort[i-1] ) {
132 LALPrintError(
"ERROR: sort[i] < sort[i-1] at line %i\n", __LINE__ );
138 if ( sort[i] > sort[i-1] ) {
139 LALPrintError(
"ERROR: sort[i] > sort[i-1] at line %i\n", __LINE__ );
146 for ( i = 0; i < nobj; ++i )
148 for ( j = 0; j < nobj && sort[j] != data[i]; ++j );
154 for ( i = 0; i < nobj; ++i )
156 for ( j = 0; j < nobj && data[j] != sort[i]; ++j );
166int main(
int argc,
char **argv)
175 for ( testnum = 0; testnum < 200; testnum++ )
177 int nobj = rand() % 24;
178 int ascend = rand() & 1;
185 makedata( nobj, &data, &sort, &indx, &rank );
187 if (
XLALHeapIndex( indx, data, nobj,
sizeof(*data), &ascend, compar ) < 0 ) {
188 LALPrintError(
"ERROR: XLALHeapIndex( indx, data, nobj, sizeof(*data), &ascend, compar ) < 0 at line %i\n", __LINE__ );
191 if (
XLALHeapRank( rank, data, nobj,
sizeof(*data), &ascend, compar ) < 0 ) {
192 LALPrintError(
"ERROR: XLALHeapRank( rank, data, nobj, sizeof(*data), &ascend, compar ) < 0 at line %i\n", __LINE__ );
195 if (
XLALHeapSort( sort, nobj,
sizeof(*data), &ascend, compar ) < 0 ) {
196 LALPrintError(
"ERROR: XLALHeapSort( sort, nobj, sizeof(*data), &ascend, compar ) < 0 at line %i\n", __LINE__ );
200 for ( i = 0; i < nobj; ++i )
201 if ( sort[i] != data[indx[i]] ) {
202 LALPrintError(
"ERROR: sort[i] != data[indx[i]] at line %i\n", __LINE__ );
206 check( data, sort, nobj, ascend );
208 freedata( data, sort, indx, rank );
210 fprintf(stderr,
"Passed XLALHeapSort\n");
211 fprintf(stderr,
"Passed XLALHeapRank\n");
212 fprintf(stderr,
"Passed XLALHeapIndex\n");
214 for ( testnum = 0; testnum < 200; testnum++ )
216 int nobj = rand() % 24;
217 int ascend = rand() & 1;
223 makedata( nobj, &data, &sort, &indx, &rank );
226 LALPrintError(
"ERROR: XLALInsertionSort( sort, nobj, sizeof(*data), &ascend, compar ) < 0 at line %i\n", __LINE__ );
230 check( data, sort, nobj, ascend );
232 freedata( data, sort, indx, rank );
234 fprintf(stderr,
"Passed XLALInsertionSort\n");
236 for ( testnum = 0; testnum < 200; testnum++ )
238 int nobj = rand() % 24;
239 int ascend = rand() & 1;
245 makedata( nobj, &data, &sort, &indx, &rank );
247 if (
XLALMergeSort( sort, nobj,
sizeof(*data), &ascend, compar ) < 0 ) {
248 LALPrintError(
"ERROR: XLALMergeSort( sort, nobj, sizeof(*data), &ascend, compar ) < 0 at line %i\n", __LINE__ );
252 check( data, sort, nobj, ascend );
254 freedata( data, sort, indx, rank );
256 fprintf(stderr,
"Passed XLALMergeSort\n");
258 for ( testnum = 0; testnum < 200; testnum++ )
260 int nobj = rand() % 24 + 1;
261 int ascend = rand() & 1;
267 makedata( nobj, &data, &sort, &indx, &rank );
269 if (
XLALMergeSort( sort, nobj,
sizeof(*data), &ascend, compar ) < 0 ) {
270 LALPrintError(
"ERROR: XLALMergeSort( sort, nobj, sizeof(*data), &ascend, compar ) < 0 at line %i\n", __LINE__ );
274 if ( !
XLALIsSorted( sort, nobj,
sizeof(*data), &ascend, compar ) ) {
275 LALPrintError(
"ERROR: !XLALIsSorted( sort, nobj, sizeof(*data), &ascend, compar ) at line %i\n", __LINE__ );
279 freedata( data, sort, indx, rank );
281 fprintf(stderr,
"Passed XLALIsSorted\n");
283 for ( testnum = 0; testnum < 200; testnum++ )
285 int nobj = rand() % 24;
286 int ascend = rand() & 1;
294 makedata( nobj, &data, &sort, &indx, &rank );
296 if (
XLALMergeSort( sort, nobj,
sizeof(*data), &ascend, compar ) < 0 ) {
297 LALPrintError(
"ERROR: XLALMergeSort( sort, nobj, sizeof(*data), &ascend, compar ) < 0 at line %i\n", __LINE__ );
303 if ( i < 0 || i > nobj ) {
304 LALPrintError(
"ERROR: i=%d, nobj=%d, i < 0 || i > nobj at line %i\n", i, nobj, __LINE__ );
306 }
else if ( nobj == 0 ) {
311 }
else if ( i == 0 ) {
313 if ( !(
lte( &ascend, &v, &sort[i] ) ) ) {
314 LALPrintError(
"ERROR: !( lte( &ascend, &v, &sort[i] ) ) at line %i\n", __LINE__ );
317 }
else if ( i == nobj ) {
319 if ( !(
lt( &ascend, &sort[i-1], &v ) ) ) {
320 LALPrintError(
"ERROR: !( lt( &ascend, &sort[i-1], &v ) ) at line %i\n", __LINE__ );
324 }
else if ( !(
lt( &ascend, &sort[i-1], &v ) &&
lte( &ascend, &v , &sort[i] ) ) ) {
325 LALPrintError(
"ERROR: !( lt( &ascend, &sort[i-1], &v ) && lte( &ascend, &v , &sort[i] ) ) at line %i\n", __LINE__ );
331 if ( i < 0 || i > nobj ) {
332 LALPrintError(
"ERROR: i < 0 || i > nobj at line %i\n", __LINE__ );
335 else if ( nobj == 0 ) {
340 }
else if ( i == 0 ) {
342 if ( !(
lte( &ascend, &v, &sort[i] ) ) ) {
343 LALPrintError(
"ERROR: !( lte( &ascend, &v, &sort[i] ) ) at line %i\n", __LINE__ );
346 }
else if ( i == nobj ) {
348 if ( ! (
lte( &ascend, &sort[i-1], &v ) ) ) {
349 LALPrintError(
"ERROR: ! ( lte( &ascend, &sort[i-1], &v ) ) at line %i\n", __LINE__ );
353 }
else if ( !(
lte( &ascend, &sort[i-1], &v ) &&
lt( &ascend, &v, &sort[i] ) ) ) {
354 LALPrintError(
"ERROR: !( lte( &ascend, &sort[i-1], &v ) && lt( &ascend, &v, &sort[i] ) ) at line %i\n", __LINE__ );
360 int j = rand() % nobj;
371 }
else if ( i < 0 ) {
374 }
else if ( sort[i] != v ) {
375 LALPrintError(
"ERROR: sort[i] != v at line %i\n", __LINE__ );
379 freedata( data, sort, indx, rank );
381 fprintf(stderr,
"Passed XLALSearchSorted\n");
static _LAL_INLINE_ int lt(void *params, const void *a, const void *b, int(*compar)(void *, const void *, const void *))
static _LAL_INLINE_ int lte(void *params, const void *a, const void *b, int(*compar)(void *, const void *, const void *))
int main(int argc, char *argv[])
int LALPrintError(const char *fmt,...)
int XLALHeapSort(void *base, UINT4 nobj, UINT4 size, void *params, int(*compar)(void *, const void *, const void *))
int XLALMergeSort(void *base, size_t nobj, size_t size, void *params, int(*compar)(void *, const void *, const void *))
int XLALIsSorted(void *base, size_t nobj, size_t size, void *params, int(*compar)(void *, const void *, const void *))
int XLALHeapRank(INT4 *rank, void *base, UINT4 nobj, UINT4 size, void *params, int(*compar)(void *, const void *, const void *))
ssize_t XLALSearchSorted(const void *key, const void *base, size_t nobj, size_t size, void *params, int(*compar)(void *, const void *, const void *), int side)
int XLALInsertionSort(void *base, size_t nobj, size_t size, void *params, int(*compar)(void *, const void *, const void *))
int XLALHeapIndex(INT4 *indx, void *base, UINT4 nobj, UINT4 size, void *params, int(*compar)(void *, const void *, const void *))