Update to 2.1.x development version
[glibc.git] / string / testcopy.c
blobb1c491e62c2d4c6b8418f4986d3f3c674fa89200
1 /* Copyright (C) 1990, 1991, 1992, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Torbjorn Granlund (tege@sics.se).
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <malloc.h>
25 int
26 main (argc, argv)
27 int argc;
28 char **argv;
30 char *mem, *memp;
31 char *rand_mem;
32 char *lo_around, *hi_around;
33 int size, max_size;
34 int src_off, dst_off;
35 int i;
36 int space_around = 10;
38 max_size = 256;
40 mem = malloc (max_size + 2 * max_size + 2 * space_around);
41 rand_mem = malloc (max_size);
42 lo_around = malloc (space_around);
43 hi_around = malloc (space_around);
44 memp = mem + space_around;
46 /* Fill RAND_MEM with random bytes, each non-zero. */
47 for (i = 0; i < max_size; i++)
49 int x;
51 x = random ();
52 while (x == 0);
53 rand_mem[i] = x;
56 for (size = 0; size < max_size; size++)
58 printf("phase %d\n", size);
59 for (src_off = 0; src_off <= 16; src_off++)
61 for (dst_off = 0; dst_off <= 16; dst_off++)
63 /* Put zero around the intended destination, to check
64 that it's not clobbered. */
65 for (i = 1; i < space_around; i++)
67 memp[dst_off - i] = 0;
68 memp[dst_off + size - 1 + i] = 0;
71 /* Fill the source area with known contents. */
72 for (i = 0; i < size; i++)
73 memp[src_off + i] = rand_mem[i];
75 /* Remember the contents around the destination area.
76 (It might not be what we wrote some lines above, since
77 the src area and the dst area overlap.) */
78 for (i = 1; i < space_around; i++)
80 lo_around[i] = memp[dst_off - i];
81 hi_around[i] = memp[dst_off + size - 1 + i];
84 memmove (memp + dst_off, memp + src_off, size);
86 /* Check that the destination area has the same
87 contents we wrote to the source area. */
88 for (i = 0; i < size; i++)
90 if (memp[dst_off + i] != rand_mem[i])
91 abort ();
94 /* Check that the area around the destination is not
95 clobbered. */
96 for (i = 1; i < space_around; i++)
98 if (memp[dst_off - i] != lo_around[i])
99 abort ();
100 if (memp[dst_off + size - 1 + i] != hi_around[i])
101 abort ();
107 puts ("Test succeeded.");
109 return 0;