brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.4 KiB · d8b1638 Raw
56 lines · cpp
1// Check interceptor.2// RUN: %clangxx -O0 %s -o %t && %run %t 2>&1 | FileCheck %s3 4// Inlined bsearch works even without interceptors.5// RUN: %clangxx -O2 %s -o %t && %run %t 2>&1 | FileCheck %s6 7#include <stdio.h>8#include <stdlib.h>9 10static int arr1[] = {8, 7, 6, 5, 4, 3, 2, 1, 0};11static int arr2[] = {10, 1, 1, 3, 4, 6, 7, 7};12 13#define array_size(x) (sizeof(x) / sizeof(x[0]))14 15static int cmp_ints(const void *a, const void *b) {16  return *(const int *)b - *(const int *)a;17}18 19static 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;29}30 31int 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    else39      printf(" %d", *(const int *)r);40  }41  printf("\n");42  // CHECK: 0 1 2 3 4 5 6 7 8 null43 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    else51      printf(" %d", *(const int *)r);52  }53  printf("\n");54  // CHECK: null 1 null 3 4 null 6 7 null 1055}56