Blame string/test-strcasestr.c

Packit 6c4009
/* Test and measure strcasestr functions.
Packit 6c4009
   Copyright (C) 2010-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Written by Ulrich Drepper <drepper@redhat.com>, 2010.
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
#define TEST_MAIN
Packit 6c4009
#define TEST_NAME "strcasestr"
Packit 6c4009
#include "test-string.h"
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define STRCASESTR simple_strcasestr
Packit 6c4009
#define NO_ALIAS
Packit 6c4009
#define __strncasecmp strncasecmp
Packit 6c4009
#define __strnlen strnlen
Packit 6c4009
#include "strcasestr.c"
Packit 6c4009
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
stupid_strcasestr (const char *s1, const char *s2)
Packit 6c4009
{
Packit 6c4009
  ssize_t s1len = strlen (s1);
Packit 6c4009
  ssize_t s2len = strlen (s2);
Packit 6c4009
Packit 6c4009
  if (s2len > s1len)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  for (ssize_t i = 0; i <= s1len - s2len; ++i)
Packit 6c4009
    {
Packit 6c4009
      size_t j;
Packit 6c4009
      for (j = 0; j < s2len; ++j)
Packit 6c4009
	if (tolower (s1[i + j]) != tolower (s2[j]))
Packit 6c4009
	  break;
Packit 6c4009
      if (j == s2len)
Packit 6c4009
	return (char *) s1 + i;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
typedef char *(*proto_t) (const char *, const char *);
Packit 6c4009
Packit 6c4009
IMPL (stupid_strcasestr, 0)
Packit 6c4009
IMPL (simple_strcasestr, 0)
Packit 6c4009
IMPL (strcasestr, 1)
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
check_result (impl_t *impl, const char *s1, const char *s2,
Packit 6c4009
	      char *exp_result)
Packit 6c4009
{
Packit 6c4009
  char *result = CALL (impl, s1, s2);
Packit 6c4009
  if (result != exp_result)
Packit 6c4009
    {
Packit 6c4009
      error (0, 0, "Wrong result in function %s %s %s", impl->name,
Packit 6c4009
	     result, exp_result);
Packit 6c4009
      ret = 1;
Packit 6c4009
      return -1;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_one_test (impl_t *impl, const char *s1, const char *s2, char *exp_result)
Packit 6c4009
{
Packit 6c4009
  if (check_result (impl, s1, s2, exp_result) < 0)
Packit 6c4009
    return;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_test (size_t align1, size_t align2, size_t len1, size_t len2,
Packit 6c4009
	 int fail)
Packit 6c4009
{
Packit 6c4009
  char *s1 = (char *) (buf1 + align1);
Packit 6c4009
  char *s2 = (char *) (buf2 + align2);
Packit 6c4009
Packit 6c4009
  static const char d[] = "1234567890abcxyz";
Packit 6c4009
#define dl (sizeof (d) - 1)
Packit 6c4009
  char *ss2 = s2;
Packit 6c4009
  for (size_t l = len2; l > 0; l = l > dl ? l - dl : 0)
Packit 6c4009
    {
Packit 6c4009
      size_t t = l > dl ? dl : l;
Packit 6c4009
      ss2 = mempcpy (ss2, d, t);
Packit 6c4009
    }
Packit 6c4009
  s2[len2] = '\0';
Packit 6c4009
Packit 6c4009
  if (fail)
Packit 6c4009
    {
Packit 6c4009
      char *ss1 = s1;
Packit 6c4009
      for (size_t l = len1; l > 0; l = l > dl ? l - dl : 0)
Packit 6c4009
	{
Packit 6c4009
	  size_t t = l > dl ? dl : l;
Packit 6c4009
	  memcpy (ss1, d, t);
Packit 6c4009
	  ++ss1[len2 > 7 ? 7 : len2 - 1];
Packit 6c4009
	  ss1 += t;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      memset (s1, '0', len1);
Packit 6c4009
      for (size_t i = 0; i < len2; ++i)
Packit 6c4009
	s1[len1 - len2 + i] = toupper (s2[i]);
Packit 6c4009
    }
Packit 6c4009
  s1[len1] = '\0';
Packit 6c4009
Packit 6c4009
  FOR_EACH_IMPL (impl, 0)
Packit 6c4009
    do_one_test (impl, s1, s2, fail ? NULL : s1 + len1 - len2);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check1 (void)
Packit 6c4009
{
Packit 6c4009
  const char s1[] = "AOKB";
Packit 6c4009
  const char s2[] = "OK";
Packit 6c4009
  char *exp_result;
Packit 6c4009
Packit 6c4009
  exp_result = stupid_strcasestr (s1, s2);
Packit 6c4009
  FOR_EACH_IMPL (impl, 0)
Packit 6c4009
    check_result (impl, s1, s2, exp_result);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
test_main (void)
Packit 6c4009
{
Packit 6c4009
  test_init ();
Packit 6c4009
Packit 6c4009
  check1 ();
Packit 6c4009
Packit 6c4009
  printf ("%23s", "");
Packit 6c4009
  FOR_EACH_IMPL (impl, 0)
Packit 6c4009
    printf ("\t%s", impl->name);
Packit 6c4009
  putchar ('\n');
Packit 6c4009
Packit 6c4009
  for (size_t klen = 2; klen < 32; ++klen)
Packit 6c4009
    for (size_t hlen = 2 * klen; hlen < 16 * klen; hlen += klen)
Packit 6c4009
      {
Packit 6c4009
	do_test (0, 0, hlen, klen, 0);
Packit 6c4009
	do_test (0, 0, hlen, klen, 1);
Packit 6c4009
	do_test (0, 3, hlen, klen, 0);
Packit 6c4009
	do_test (0, 3, hlen, klen, 1);
Packit 6c4009
	do_test (0, 9, hlen, klen, 0);
Packit 6c4009
	do_test (0, 9, hlen, klen, 1);
Packit 6c4009
	do_test (0, 15, hlen, klen, 0);
Packit 6c4009
	do_test (0, 15, hlen, klen, 1);
Packit 6c4009
Packit 6c4009
	do_test (3, 0, hlen, klen, 0);
Packit 6c4009
	do_test (3, 0, hlen, klen, 1);
Packit 6c4009
	do_test (3, 3, hlen, klen, 0);
Packit 6c4009
	do_test (3, 3, hlen, klen, 1);
Packit 6c4009
	do_test (3, 9, hlen, klen, 0);
Packit 6c4009
	do_test (3, 9, hlen, klen, 1);
Packit 6c4009
	do_test (3, 15, hlen, klen, 0);
Packit 6c4009
	do_test (3, 15, hlen, klen, 1);
Packit 6c4009
Packit 6c4009
	do_test (9, 0, hlen, klen, 0);
Packit 6c4009
	do_test (9, 0, hlen, klen, 1);
Packit 6c4009
	do_test (9, 3, hlen, klen, 0);
Packit 6c4009
	do_test (9, 3, hlen, klen, 1);
Packit 6c4009
	do_test (9, 9, hlen, klen, 0);
Packit 6c4009
	do_test (9, 9, hlen, klen, 1);
Packit 6c4009
	do_test (9, 15, hlen, klen, 0);
Packit 6c4009
	do_test (9, 15, hlen, klen, 1);
Packit 6c4009
Packit 6c4009
	do_test (15, 0, hlen, klen, 0);
Packit 6c4009
	do_test (15, 0, hlen, klen, 1);
Packit 6c4009
	do_test (15, 3, hlen, klen, 0);
Packit 6c4009
	do_test (15, 3, hlen, klen, 1);
Packit 6c4009
	do_test (15, 9, hlen, klen, 0);
Packit 6c4009
	do_test (15, 9, hlen, klen, 1);
Packit 6c4009
	do_test (15, 15, hlen, klen, 0);
Packit 6c4009
	do_test (15, 15, hlen, klen, 1);
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
  do_test (0, 0, page_size - 1, 16, 0);
Packit 6c4009
  do_test (0, 0, page_size - 1, 16, 1);
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>