Back to index

glibc  2.9
Classes | Defines | Functions | Variables
tst-bsearch.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <tst-stack-align.h>

Go to the source code of this file.

Classes

struct  entry

Defines

#define narr   (sizeof (arr) / sizeof (arr[0]))

Functions

static int comp (const void *p1, const void *p2)
int main (void)

Variables

struct entry arr []
static int align_check

Class Documentation

struct entry

Definition at line 105 of file memusage.c.

Class Members
void * data
uint64_t heap
char * key
uint64_t stack
const char * str
uint32_t time_high
uint32_t time_low
int val

Define Documentation

#define narr   (sizeof (arr) / sizeof (arr[0]))

Definition at line 42 of file tst-bsearch.c.


Function Documentation

static int comp ( const void *  p1,
const void *  p2 
) [static]

Definition at line 47 of file tst-bsearch.c.

{
  struct entry *e1 = (struct entry *) p1;
  struct entry *e2 = (struct entry *) p2;

  if (!align_check)
    align_check = TEST_STACK_ALIGN () ? -1 : 1;

  return e1->val - e2->val;
}

Here is the caller graph for this function:

int main ( void  )

Definition at line 60 of file tst-bsearch.c.

{
  size_t cnt;
  int result = 0;
  struct entry key;
  struct entry *res;

  for (cnt = 0; cnt < narr; ++cnt)
    {

      key.val = arr[cnt].val;

      res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
      if (res == NULL)
       {
         printf ("entry %zd not found\n", cnt);
         result = 1;
       }
      else if (res != &arr[cnt])
       {
         puts ("wrong entry returned");
         result = 1;
       }
    }

  /* And some special tests that shouldn't find any entry.  */
  key.val = -1;
  res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
  if (res != NULL)
    {
      puts ("found an entry that's not there");
      result = 1;
    }

  key.val = 11;
  res = (struct entry *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
  if (res != NULL)
    {
      puts ("found an entry that's not there");
      result = 1;
    }

  key.val = 11;
  res = (struct entry *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
  if (res != NULL)
    {
      puts ("found an entry that's not there");
      result = 1;
    }

  /* Now the array contains only one element - no entry should be found.  */
  for (cnt = 0; cnt < narr; ++cnt)
    {
      key.val = arr[cnt].val;

      res = (struct entry *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
      if (cnt == 5)
       {
         if (res == NULL)
           {
             printf ("entry %zd not found\n", cnt);
             result = 1;
           }
         else if (res != &arr[cnt])
           {
             puts ("wrong entry returned");
             result = 1;
           }
       }
      else if (res != NULL)
       {
         puts ("found an entry that's not there");
         result = 1;
       }
    }

  if (align_check == 0)
    {
      puts ("alignment not checked");
      result = 1;
    }
  else if (align_check == -1)
    {
      puts ("stack not sufficiently aligned");
      result = 1;
    }

  if (result == 0)
    puts ("all OK");

  return result;
}

Here is the call graph for this function:


Variable Documentation

int align_check [static]

Definition at line 44 of file tst-bsearch.c.

struct entry arr[]