Blame string/testcopy.c

Packit 6c4009
/* Copyright (C) 1990-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Torbjorn Granlund (tege@sics.se).
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 <stdlib.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/support.h>
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char *mem, *memp;
Packit 6c4009
  char *rand_mem;
Packit 6c4009
  char *lo_around, *hi_around;
Packit 6c4009
  int size, max_size;
Packit 6c4009
  int src_off, dst_off;
Packit 6c4009
  int i;
Packit 6c4009
  int space_around = 10;
Packit 6c4009
Packit 6c4009
  max_size = 256;
Packit 6c4009
Packit 6c4009
  mem = xmalloc (max_size + 2 * max_size + 2 * space_around);
Packit 6c4009
  rand_mem = xmalloc (max_size);
Packit 6c4009
  lo_around = xmalloc (space_around);
Packit 6c4009
  hi_around = xmalloc (space_around);
Packit 6c4009
  memp = mem + space_around;
Packit 6c4009
Packit 6c4009
  /* Fill RAND_MEM with random bytes, each non-zero.  */
Packit 6c4009
  for (i = 0; i < max_size; i++)
Packit 6c4009
    {
Packit 6c4009
      int x;
Packit 6c4009
      do
Packit 6c4009
	x = random ();
Packit 6c4009
      while (x == 0);
Packit 6c4009
      rand_mem[i] = x;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (size = 0; size < max_size; size++)
Packit 6c4009
    {
Packit 6c4009
      printf("phase %d\n", size);
Packit 6c4009
      for (src_off = 0; src_off <= 16; src_off++)
Packit 6c4009
	{
Packit 6c4009
	  for (dst_off = 0; dst_off <= 16; dst_off++)
Packit 6c4009
	    {
Packit 6c4009
	      /* Put zero around the intended destination, to check
Packit 6c4009
		 that it's not clobbered.  */
Packit 6c4009
	      for (i = 1; i < space_around; i++)
Packit 6c4009
		{
Packit 6c4009
		  memp[dst_off - i] = 0;
Packit 6c4009
		  memp[dst_off + size - 1 + i] = 0;
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      /* Fill the source area with known contents.  */
Packit 6c4009
	      for (i = 0; i < size; i++)
Packit 6c4009
		memp[src_off + i] = rand_mem[i];
Packit 6c4009
Packit 6c4009
	      /* Remember the contents around the destination area.
Packit 6c4009
		 (It might not be what we wrote some lines above, since
Packit 6c4009
		 the src area and the dst area overlap.)  */
Packit 6c4009
	      for (i = 1; i < space_around; i++)
Packit 6c4009
		{
Packit 6c4009
		  lo_around[i] = memp[dst_off - i];
Packit 6c4009
		  hi_around[i] = memp[dst_off + size - 1 + i];
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      memmove (memp + dst_off, memp + src_off, size);
Packit 6c4009
Packit 6c4009
	      /* Check that the destination area has the same
Packit 6c4009
		 contents we wrote to the source area.  */
Packit 6c4009
	      for (i = 0; i < size; i++)
Packit 6c4009
		{
Packit 6c4009
		  if (memp[dst_off + i] != rand_mem[i])
Packit 6c4009
		    abort ();
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      /* Check that the area around the destination is not
Packit 6c4009
		 clobbered.  */
Packit 6c4009
	      for (i = 1; i < space_around; i++)
Packit 6c4009
		{
Packit 6c4009
		  if (memp[dst_off - i] != lo_around[i])
Packit 6c4009
		    abort ();
Packit 6c4009
		  if (memp[dst_off + size - 1 + i] != hi_around[i])
Packit 6c4009
		    abort ();
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  puts ("Test succeeded.");
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>