OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [testsuite/] [newlib.string/] [memmove1.c] - Diff between revs 158 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 158 Rev 816
/* A minor test-program for memmove.
/* A minor test-program for memmove.
   Copyright (C) 2005 Axis Communications.
   Copyright (C) 2005 Axis Communications.
   All rights reserved.
   All rights reserved.
 
 
   Redistribution and use in source and binary forms, with or without
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   modification, are permitted provided that the following conditions
   are met:
   are met:
 
 
   1. Redistributions of source code must retain the above copyright
   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
      notice, this list of conditions and the following disclaimer.
 
 
   2. Neither the name of Axis Communications nor the names of its
   2. Neither the name of Axis Communications nor the names of its
      contributors may be used to endorse or promote products derived
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.
      from this software without specific prior written permission.
 
 
   THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
   THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
   COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
   IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   POSSIBILITY OF SUCH DAMAGE.  */
   POSSIBILITY OF SUCH DAMAGE.  */
 
 
/* Test moves of 0..MAX bytes; overlapping-src-higher,
/* Test moves of 0..MAX bytes; overlapping-src-higher,
   overlapping-src-lower and non-overlapping.  The overlap varies with
   overlapping-src-lower and non-overlapping.  The overlap varies with
   1..N where N is the size moved.  This means an order of MAX**2
   1..N where N is the size moved.  This means an order of MAX**2
   iterations.  The size of an octet may seem appropriate for MAX and
   iterations.  The size of an octet may seem appropriate for MAX and
   makes an upper limit for simple testing.  For the CRIS simulator,
   makes an upper limit for simple testing.  For the CRIS simulator,
   making this 256 added 90s to the test-run (2GHz P4) while 64 (4s) was
   making this 256 added 90s to the test-run (2GHz P4) while 64 (4s) was
   enough to spot the bugs that had crept in, hence the number chosen.  */
   enough to spot the bugs that had crept in, hence the number chosen.  */
#define MAX 64
#define MAX 64
 
 
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
 
 
#define TOO_MANY_ERRORS 11
#define TOO_MANY_ERRORS 11
int errors = 0;
int errors = 0;
 
 
#define DEBUGP                                  \
#define DEBUGP                                  \
 if (errors == TOO_MANY_ERRORS)                 \
 if (errors == TOO_MANY_ERRORS)                 \
   printf ("Further errors omitted\n");         \
   printf ("Further errors omitted\n");         \
 else if (errors < TOO_MANY_ERRORS)             \
 else if (errors < TOO_MANY_ERRORS)             \
   printf
   printf
 
 
/* A safe target-independent memmove.  */
/* A safe target-independent memmove.  */
 
 
void
void
mymemmove (unsigned char *dest, unsigned char *src, size_t n)
mymemmove (unsigned char *dest, unsigned char *src, size_t n)
{
{
  size_t i;
  size_t i;
 
 
  if ((src <= dest && src + n <= dest)
  if ((src <= dest && src + n <= dest)
      || src >= dest)
      || src >= dest)
    while (n-- > 0)
    while (n-- > 0)
      *dest++ = *src++;
      *dest++ = *src++;
  else
  else
    {
    {
      dest += n;
      dest += n;
      src += n;
      src += n;
      while (n-- > 0)
      while (n-- > 0)
        *--dest = *--src;
        *--dest = *--src;
    }
    }
}
}
 
 
/* It's either the noinline attribute or forcing the test framework to
/* It's either the noinline attribute or forcing the test framework to
   pass -fno-builtin-memmove.  */
   pass -fno-builtin-memmove.  */
void
void
xmemmove (unsigned char *dest, unsigned char *src, size_t n)
xmemmove (unsigned char *dest, unsigned char *src, size_t n)
     __attribute__ ((__noinline__));
     __attribute__ ((__noinline__));
 
 
void
void
xmemmove (unsigned char *dest, unsigned char *src, size_t n)
xmemmove (unsigned char *dest, unsigned char *src, size_t n)
{
{
  void *retp;
  void *retp;
  retp = memmove (dest, src, n);
  retp = memmove (dest, src, n);
 
 
  if (retp != dest)
  if (retp != dest)
    {
    {
      errors++;
      errors++;
      DEBUGP ("memmove of n bytes returned %p instead of dest=%p\n",
      DEBUGP ("memmove of n bytes returned %p instead of dest=%p\n",
              retp, dest);
              retp, dest);
    }
    }
}
}
 
 
 
 
/* Fill the array with something we can associate with a position, but
/* Fill the array with something we can associate with a position, but
   not exactly the same as the position index.  */
   not exactly the same as the position index.  */
 
 
void
void
fill (unsigned char dest[MAX*3])
fill (unsigned char dest[MAX*3])
{
{
  size_t i;
  size_t i;
  for (i = 0; i < MAX*3; i++)
  for (i = 0; i < MAX*3; i++)
    dest[i] = (10 + i) % MAX;
    dest[i] = (10 + i) % MAX;
}
}
 
 
int
int
main (void)
main (void)
{
{
  size_t i;
  size_t i;
  int errors = 0;
  int errors = 0;
 
 
  /* Leave some room before and after the area tested, so we can detect
  /* Leave some room before and after the area tested, so we can detect
     overwrites of up to N bytes, N being the amount tested.  If you
     overwrites of up to N bytes, N being the amount tested.  If you
     want to test using valgrind, make these malloced instead.  */
     want to test using valgrind, make these malloced instead.  */
  unsigned char from_test[MAX*3];
  unsigned char from_test[MAX*3];
  unsigned char to_test[MAX*3];
  unsigned char to_test[MAX*3];
  unsigned char from_known[MAX*3];
  unsigned char from_known[MAX*3];
  unsigned char to_known[MAX*3];
  unsigned char to_known[MAX*3];
 
 
  /* Non-overlap.  */
  /* Non-overlap.  */
  for (i = 0; i < MAX; i++)
  for (i = 0; i < MAX; i++)
    {
    {
      /* Do the memmove first before setting the known array, so we know
      /* Do the memmove first before setting the known array, so we know
         it didn't change any of the known array.  */
         it didn't change any of the known array.  */
      fill (from_test);
      fill (from_test);
      fill (to_test);
      fill (to_test);
      xmemmove (to_test + MAX, 1 + from_test + MAX, i);
      xmemmove (to_test + MAX, 1 + from_test + MAX, i);
 
 
      fill (from_known);
      fill (from_known);
      fill (to_known);
      fill (to_known);
      mymemmove (to_known + MAX, 1 + from_known + MAX, i);
      mymemmove (to_known + MAX, 1 + from_known + MAX, i);
 
 
      if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
      if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
        {
        {
          errors++;
          errors++;
          DEBUGP ("memmove failed non-overlap test for %d bytes\n", i);
          DEBUGP ("memmove failed non-overlap test for %d bytes\n", i);
        }
        }
    }
    }
 
 
  /* Overlap-from-before.  */
  /* Overlap-from-before.  */
  for (i = 0; i < MAX; i++)
  for (i = 0; i < MAX; i++)
    {
    {
      size_t j;
      size_t j;
      for (j = 0; j < i; j++)
      for (j = 0; j < i; j++)
        {
        {
          fill (to_test);
          fill (to_test);
          xmemmove (to_test + MAX * 2 - i, to_test + MAX * 2 - i - j, i);
          xmemmove (to_test + MAX * 2 - i, to_test + MAX * 2 - i - j, i);
 
 
          fill (to_known);
          fill (to_known);
          mymemmove (to_known + MAX * 2 - i, to_known + MAX * 2 - i - j, i);
          mymemmove (to_known + MAX * 2 - i, to_known + MAX * 2 - i - j, i);
 
 
          if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
          if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
            {
            {
              errors++;
              errors++;
              DEBUGP ("memmove failed for %d bytes,"
              DEBUGP ("memmove failed for %d bytes,"
                      " with src %d bytes before dest\n",
                      " with src %d bytes before dest\n",
                      i, j);
                      i, j);
            }
            }
        }
        }
    }
    }
 
 
  /* Overlap-from-after.  */
  /* Overlap-from-after.  */
  for (i = 0; i < MAX; i++)
  for (i = 0; i < MAX; i++)
    {
    {
      size_t j;
      size_t j;
      for (j = 0; j < i; j++)
      for (j = 0; j < i; j++)
        {
        {
          fill (to_test);
          fill (to_test);
          xmemmove (to_test + MAX, to_test + MAX + j, i);
          xmemmove (to_test + MAX, to_test + MAX + j, i);
 
 
          fill (to_known);
          fill (to_known);
          mymemmove (to_known + MAX, to_known + MAX + j, i);
          mymemmove (to_known + MAX, to_known + MAX + j, i);
 
 
          if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
          if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
            {
            {
              errors++;
              errors++;
              DEBUGP ("memmove failed when moving %d bytes,"
              DEBUGP ("memmove failed when moving %d bytes,"
                      " with src %d bytes after dest\n",
                      " with src %d bytes after dest\n",
                      i, j);
                      i, j);
            }
            }
        }
        }
    }
    }
 
 
  if (errors != 0)
  if (errors != 0)
    abort ();
    abort ();
  exit (0);
  exit (0);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.