xref: /aosp_15_r20/external/compiler-rt/test/msan/tsearch.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_msan -O0 -g %s -o %t && %run %t
2*7c3d14c8STreehugger Robot 
3*7c3d14c8STreehugger Robot #include <assert.h>
4*7c3d14c8STreehugger Robot #include <search.h>
5*7c3d14c8STreehugger Robot #include <stdlib.h>
6*7c3d14c8STreehugger Robot 
compare(const void * pa,const void * pb)7*7c3d14c8STreehugger Robot int compare(const void *pa, const void *pb) {
8*7c3d14c8STreehugger Robot   int a = *(const int *)pa;
9*7c3d14c8STreehugger Robot   int b = *(const int *)pb;
10*7c3d14c8STreehugger Robot   if (a < b)
11*7c3d14c8STreehugger Robot     return -1;
12*7c3d14c8STreehugger Robot   else if (a > b)
13*7c3d14c8STreehugger Robot     return 1;
14*7c3d14c8STreehugger Robot   else
15*7c3d14c8STreehugger Robot     return 0;
16*7c3d14c8STreehugger Robot }
17*7c3d14c8STreehugger Robot 
myfreenode(void * p)18*7c3d14c8STreehugger Robot void myfreenode(void *p) {
19*7c3d14c8STreehugger Robot   delete (int *)p;
20*7c3d14c8STreehugger Robot }
21*7c3d14c8STreehugger Robot 
main(void)22*7c3d14c8STreehugger Robot int main(void) {
23*7c3d14c8STreehugger Robot   void *root = NULL;
24*7c3d14c8STreehugger Robot   for (int i = 0; i < 5; ++i) {
25*7c3d14c8STreehugger Robot     int *p = new int(i);
26*7c3d14c8STreehugger Robot     void *q = tsearch(p, &root, compare);
27*7c3d14c8STreehugger Robot     if (q == NULL)
28*7c3d14c8STreehugger Robot       exit(1);
29*7c3d14c8STreehugger Robot     if (*(int **)q != p)
30*7c3d14c8STreehugger Robot       delete p;
31*7c3d14c8STreehugger Robot   }
32*7c3d14c8STreehugger Robot 
33*7c3d14c8STreehugger Robot   tdestroy(root, myfreenode);
34*7c3d14c8STreehugger Robot 
35*7c3d14c8STreehugger Robot   return 0;
36*7c3d14c8STreehugger Robot }
37