xref: /freebsd/lib/libc/tests/stdlib/test-search.h (revision d59c7ea2701fe7b73b32eef49a7c712ef38de5a0)
1 /*
2  * Copyright (c) 2026 Faraz Vahedi <kfv@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #ifndef _TEST_SEARCH_H
8 #define _TEST_SEARCH_H
9 
10 #include <sys/param.h>
11 
12 #include <stddef.h>
13 
14 #include <atf-c.h>
15 
16 #define	SVEC_LEN	1024
17 
18 typedef void *search_int_t(const int *, const int *, size_t, void *);
19 
20 static int
searchhelp(const void * a,const void * b)21 searchhelp(const void *a, const void *b)
22 {
23 	const int *oa = a, *ob = b;
24 
25 	return ((*oa > *ob) - (*oa < *ob));
26 }
27 
28 /*
29  * Fill v[i] = i, then confirm every element is found and -1 and n are not.
30  */
31 static void
check_sorted_search(search_int_t * search,void * ctx,int * v,size_t n)32 check_sorted_search(search_int_t *search, void *ctx, int *v, size_t n)
33 {
34 	size_t i;
35 	int key;
36 
37 	for (i = 0; i < n; i++)
38 		v[i] = (int)i;
39 	for (i = 0; i < n; i++) {
40 		key = v[i];
41 		ATF_CHECK(search(&key, v, n, ctx) == &v[i]);
42 	}
43 	if (n != 0) {
44 		key = -1;
45 		ATF_CHECK(search(&key, v, n, ctx) == NULL);
46 		key = (int)n;
47 		ATF_CHECK(search(&key, v, n, ctx) == NULL);
48 	}
49 }
50 
51 #endif /* !_TEST_SEARCH_H */
52