Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / sanitizer_common / TestCases / Posix / bsearch.cpp
blobd8b16388b4ac5179274cab36107c8798d6daf08b
1 // Check interceptor.
2 // RUN: %clangxx -O0 %s -o %t && %run %t 2>&1 | FileCheck %s
4 // Inlined bsearch works even without interceptors.
5 // RUN: %clangxx -O2 %s -o %t && %run %t 2>&1 | FileCheck %s
7 #include <stdio.h>
8 #include <stdlib.h>
10 static int arr1[] = {8, 7, 6, 5, 4, 3, 2, 1, 0};
11 static int arr2[] = {10, 1, 1, 3, 4, 6, 7, 7};
13 #define array_size(x) (sizeof(x) / sizeof(x[0]))
15 static int cmp_ints(const void *a, const void *b) {
16 return *(const int *)b - *(const int *)a;
19 static int cmp_pos(const void *a, const void *b) {
20 const int *ap =
21 (const int *)bsearch(a, arr1, array_size(arr1), sizeof(int), &cmp_ints);
22 if (!ap)
23 ap = arr1 + array_size(arr1);
24 const int *bp =
25 (const int *)bsearch(b, arr1, array_size(arr1), sizeof(int), &cmp_ints);
26 if (!bp)
27 bp = arr1 + array_size(arr1);
28 return bp - ap;
31 int main() {
32 // Simple bsearch.
33 for (int i = 0; i < 10; ++i) {
34 const void *r =
35 bsearch(&i, arr1, array_size(arr1), sizeof(arr1[0]), &cmp_ints);
36 if (!r)
37 printf(" null");
38 else
39 printf(" %d", *(const int *)r);
41 printf("\n");
42 // CHECK: 0 1 2 3 4 5 6 7 8 null
44 // Nested bsearch.
45 for (int i = 0; i < 10; ++i) {
46 const void *r =
47 bsearch(&i, arr2, array_size(arr2), sizeof(arr2[0]), &cmp_pos);
48 if (!r)
49 printf(" null");
50 else
51 printf(" %d", *(const int *)r);
53 printf("\n");
54 // CHECK: null 1 null 3 4 null 6 7 null 10