OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / test / string / testcopy.c
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).
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
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    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <malloc.h>
23
24 int
25 main (void)
26 {
27   char *mem, *memp;
28   char *rand_mem;
29   char *lo_around, *hi_around;
30   int size, max_size;
31   int src_off, dst_off;
32   int i;
33   int space_around = 10;
34
35   max_size = 256;
36
37   mem = malloc (max_size + 2 * max_size + 2 * space_around);
38   rand_mem = malloc (max_size);
39   lo_around = malloc (space_around);
40   hi_around = malloc (space_around);
41   memp = mem + space_around;
42
43   /* Fill RAND_MEM with random bytes, each non-zero.  */
44   for (i = 0; i < max_size; i++)
45     {
46       int x;
47       do
48         x = random ();
49       while (x == 0);
50       rand_mem[i] = x;
51     }
52
53   for (size = 0; size < max_size; size++)
54     {
55       printf("phase %d\n", size);
56       for (src_off = 0; src_off <= 16; src_off++)
57         {
58           for (dst_off = 0; dst_off <= 16; dst_off++)
59             {
60               /* Put zero around the intended destination, to check
61                  that it's not clobbered.  */
62               for (i = 1; i < space_around; i++)
63                 {
64                   memp[dst_off - i] = 0;
65                   memp[dst_off + size - 1 + i] = 0;
66                 }
67
68               /* Fill the source area with known contents.  */
69               for (i = 0; i < size; i++)
70                 memp[src_off + i] = rand_mem[i];
71
72               /* Remember the contents around the destination area.
73                  (It might not be what we wrote some lines above, since
74                  the src area and the dst area overlap.)  */
75               for (i = 1; i < space_around; i++)
76                 {
77                   lo_around[i] = memp[dst_off - i];
78                   hi_around[i] = memp[dst_off + size - 1 + i];
79                 }
80
81               memmove (memp + dst_off, memp + src_off, size);
82
83               /* Check that the destination area has the same
84                  contents we wrote to the source area.  */
85               for (i = 0; i < size; i++)
86                 {
87                   if (memp[dst_off + i] != rand_mem[i])
88                     abort ();
89                 }
90
91               /* Check that the area around the destination is not
92                  clobbered.  */
93               for (i = 1; i < space_around; i++)
94                 {
95                   if (memp[dst_off - i] != lo_around[i])
96                     abort ();
97                   if (memp[dst_off + size - 1 + i] != hi_around[i])
98                     abort ();
99                 }
100             }
101         }
102     }
103
104   puts ("Test succeeded.");
105
106   return 0;
107 }