xref: /llvm-project/compiler-rt/test/msan/tsearch.cpp (revision 02012a78b8e7d44341247059184350cbb4898e79)
1d21b3d34SFangrui Song // RUN: %clangxx_msan -O0 -g %s -o %t && %run %t
2d21b3d34SFangrui Song 
3d21b3d34SFangrui Song // tdestroy is a GNU extension
4*02012a78SPaul Robinson // UNSUPPORTED: target={{.*(netbsd|freebsd).*}}
5d21b3d34SFangrui Song 
6d21b3d34SFangrui Song #include <assert.h>
7d21b3d34SFangrui Song #include <search.h>
8d21b3d34SFangrui Song #include <stdlib.h>
9d21b3d34SFangrui Song 
compare(const void * pa,const void * pb)10d21b3d34SFangrui Song int compare(const void *pa, const void *pb) {
11d21b3d34SFangrui Song   int a = *(const int *)pa;
12d21b3d34SFangrui Song   int b = *(const int *)pb;
13d21b3d34SFangrui Song   if (a < b)
14d21b3d34SFangrui Song     return -1;
15d21b3d34SFangrui Song   else if (a > b)
16d21b3d34SFangrui Song     return 1;
17d21b3d34SFangrui Song   else
18d21b3d34SFangrui Song     return 0;
19d21b3d34SFangrui Song }
20d21b3d34SFangrui Song 
myfreenode(void * p)21d21b3d34SFangrui Song void myfreenode(void *p) {
22d21b3d34SFangrui Song   delete (int *)p;
23d21b3d34SFangrui Song }
24d21b3d34SFangrui Song 
main(void)25d21b3d34SFangrui Song int main(void) {
26d21b3d34SFangrui Song   void *root = NULL;
27d21b3d34SFangrui Song   for (int i = 0; i < 5; ++i) {
28d21b3d34SFangrui Song     int *p = new int(i);
29d21b3d34SFangrui Song     void *q = tsearch(p, &root, compare);
30d21b3d34SFangrui Song     if (q == NULL)
31d21b3d34SFangrui Song       exit(1);
32d21b3d34SFangrui Song     if (*(int **)q != p)
33d21b3d34SFangrui Song       delete p;
34d21b3d34SFangrui Song   }
35d21b3d34SFangrui Song 
36d21b3d34SFangrui Song   tdestroy(root, myfreenode);
37d21b3d34SFangrui Song 
38d21b3d34SFangrui Song   return 0;
39d21b3d34SFangrui Song }
40