Blame stdlib/tst-bsearch.c

Packit 6c4009
/* Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <tst-stack-align.h>
Packit 6c4009
Packit 6c4009
struct item
Packit 6c4009
{
Packit 6c4009
  int val;
Packit 6c4009
  const char *str;
Packit 6c4009
} arr[] =
Packit 6c4009
{
Packit 6c4009
  { 0, "zero" },
Packit 6c4009
  { 1, "one" },
Packit 6c4009
  { 2, "two" },
Packit 6c4009
  { 3, "three" },
Packit 6c4009
  { 4, "four" },
Packit 6c4009
  { 5, "five" },
Packit 6c4009
  { 6, "six" },
Packit 6c4009
  { 7, "seven" },
Packit 6c4009
  { 8, "eight" },
Packit 6c4009
  { 9, "nine" },
Packit 6c4009
  { 10, "ten" }
Packit 6c4009
};
Packit 6c4009
#define narr (sizeof (arr) / sizeof (arr[0]))
Packit 6c4009
Packit 6c4009
static int align_check;
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
comp (const void *p1, const void *p2)
Packit 6c4009
{
Packit 6c4009
  struct item *e1 = (struct item *) p1;
Packit 6c4009
  struct item *e2 = (struct item *) p2;
Packit 6c4009
Packit 6c4009
  if (!align_check)
Packit 6c4009
    align_check = TEST_STACK_ALIGN () ? -1 : 1;
Packit 6c4009
Packit 6c4009
  return e1->val - e2->val;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int result = 0;
Packit 6c4009
  struct item key;
Packit 6c4009
  struct item *res;
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < narr; ++cnt)
Packit 6c4009
    {
Packit 6c4009
Packit 6c4009
      key.val = arr[cnt].val;
Packit 6c4009
Packit 6c4009
      res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
Packit 6c4009
      if (res == NULL)
Packit 6c4009
	{
Packit 6c4009
	  printf ("entry %zd not found\n", cnt);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else if (res != &arr[cnt])
Packit 6c4009
	{
Packit 6c4009
	  puts ("wrong entry returned");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* And some special tests that shouldn't find any entry.  */
Packit 6c4009
  key.val = -1;
Packit 6c4009
  res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
Packit 6c4009
  if (res != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("found an entry that's not there");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  key.val = 11;
Packit 6c4009
  res = (struct item *) bsearch (&key, arr, narr, sizeof (arr[0]), comp);
Packit 6c4009
  if (res != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("found an entry that's not there");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  key.val = 11;
Packit 6c4009
  res = (struct item *) bsearch (&key, arr, 0, sizeof (arr[0]), comp);
Packit 6c4009
  if (res != NULL)
Packit 6c4009
    {
Packit 6c4009
      puts ("found an entry that's not there");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now the array contains only one element - no entry should be found.  */
Packit 6c4009
  for (cnt = 0; cnt < narr; ++cnt)
Packit 6c4009
    {
Packit 6c4009
      key.val = arr[cnt].val;
Packit 6c4009
Packit 6c4009
      res = (struct item *) bsearch (&key, &arr[5], 1, sizeof (arr[0]), comp);
Packit 6c4009
      if (cnt == 5)
Packit 6c4009
	{
Packit 6c4009
	  if (res == NULL)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("entry %zd not found\n", cnt);
Packit 6c4009
	      result = 1;
Packit 6c4009
	    }
Packit 6c4009
	  else if (res != &arr[cnt])
Packit 6c4009
	    {
Packit 6c4009
	      puts ("wrong entry returned");
Packit 6c4009
	      result = 1;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else if (res != NULL)
Packit 6c4009
	{
Packit 6c4009
	  puts ("found an entry that's not there");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (align_check == 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("alignment not checked");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (align_check == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("stack not sufficiently aligned");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (result == 0)
Packit 6c4009
    puts ("all OK");
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"