Back to index

glibc  2.9
testcopy.c
Go to the documentation of this file.
00001 /* Copyright (C) 1990, 1991, 1992, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Torbjorn Granlund (tege@sics.se).
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <malloc.h>
00024 
00025 int
00026 main (void)
00027 {
00028   char *mem, *memp;
00029   char *rand_mem;
00030   char *lo_around, *hi_around;
00031   int size, max_size;
00032   int src_off, dst_off;
00033   int i;
00034   int space_around = 10;
00035 
00036   max_size = 256;
00037 
00038   mem = malloc (max_size + 2 * max_size + 2 * space_around);
00039   rand_mem = malloc (max_size);
00040   lo_around = malloc (space_around);
00041   hi_around = malloc (space_around);
00042   memp = mem + space_around;
00043 
00044   /* Fill RAND_MEM with random bytes, each non-zero.  */
00045   for (i = 0; i < max_size; i++)
00046     {
00047       int x;
00048       do
00049        x = random ();
00050       while (x == 0);
00051       rand_mem[i] = x;
00052     }
00053 
00054   for (size = 0; size < max_size; size++)
00055     {
00056       printf("phase %d\n", size);
00057       for (src_off = 0; src_off <= 16; src_off++)
00058        {
00059          for (dst_off = 0; dst_off <= 16; dst_off++)
00060            {
00061              /* Put zero around the intended destination, to check
00062                that it's not clobbered.  */
00063              for (i = 1; i < space_around; i++)
00064               {
00065                 memp[dst_off - i] = 0;
00066                 memp[dst_off + size - 1 + i] = 0;
00067               }
00068 
00069              /* Fill the source area with known contents.  */
00070              for (i = 0; i < size; i++)
00071               memp[src_off + i] = rand_mem[i];
00072 
00073              /* Remember the contents around the destination area.
00074                (It might not be what we wrote some lines above, since
00075                the src area and the dst area overlap.)  */
00076              for (i = 1; i < space_around; i++)
00077               {
00078                 lo_around[i] = memp[dst_off - i];
00079                 hi_around[i] = memp[dst_off + size - 1 + i];
00080               }
00081 
00082              memmove (memp + dst_off, memp + src_off, size);
00083 
00084              /* Check that the destination area has the same
00085                contents we wrote to the source area.  */
00086              for (i = 0; i < size; i++)
00087               {
00088                 if (memp[dst_off + i] != rand_mem[i])
00089                   abort ();
00090               }
00091 
00092              /* Check that the area around the destination is not
00093                clobbered.  */
00094              for (i = 1; i < space_around; i++)
00095               {
00096                 if (memp[dst_off - i] != lo_around[i])
00097                   abort ();
00098                 if (memp[dst_off + size - 1 + i] != hi_around[i])
00099                   abort ();
00100               }
00101            }
00102        }
00103     }
00104 
00105   puts ("Test succeeded.");
00106 
00107   return 0;
00108 }