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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [testsuite/] [newlib.string/] [memmove1.c] - Blame information for rev 300

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/* A minor test-program for memmove.
2
   Copyright (C) 2005 Axis Communications.
3
   All rights reserved.
4
 
5
   Redistribution and use in source and binary forms, with or without
6
   modification, are permitted provided that the following conditions
7
   are met:
8
 
9
   1. Redistributions of source code must retain the above copyright
10
      notice, this list of conditions and the following disclaimer.
11
 
12
   2. Neither the name of Axis Communications nor the names of its
13
      contributors may be used to endorse or promote products derived
14
      from this software without specific prior written permission.
15
 
16
   THIS SOFTWARE IS PROVIDED BY AXIS COMMUNICATIONS AND ITS CONTRIBUTORS
17
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AXIS
20
   COMMUNICATIONS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21
   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23
   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25
   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26
   IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
   POSSIBILITY OF SUCH DAMAGE.  */
28
 
29
/* Test moves of 0..MAX bytes; overlapping-src-higher,
30
   overlapping-src-lower and non-overlapping.  The overlap varies with
31
   1..N where N is the size moved.  This means an order of MAX**2
32
   iterations.  The size of an octet may seem appropriate for MAX and
33
   makes an upper limit for simple testing.  For the CRIS simulator,
34
   making this 256 added 90s to the test-run (2GHz P4) while 64 (4s) was
35
   enough to spot the bugs that had crept in, hence the number chosen.  */
36
#define MAX 64
37
 
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
 
42
#define TOO_MANY_ERRORS 11
43
int errors = 0;
44
 
45
#define DEBUGP                                  \
46
 if (errors == TOO_MANY_ERRORS)                 \
47
   printf ("Further errors omitted\n");         \
48
 else if (errors < TOO_MANY_ERRORS)             \
49
   printf
50
 
51
/* A safe target-independent memmove.  */
52
 
53
void
54
mymemmove (unsigned char *dest, unsigned char *src, size_t n)
55
{
56
  size_t i;
57
 
58
  if ((src <= dest && src + n <= dest)
59
      || src >= dest)
60
    while (n-- > 0)
61
      *dest++ = *src++;
62
  else
63
    {
64
      dest += n;
65
      src += n;
66
      while (n-- > 0)
67
        *--dest = *--src;
68
    }
69
}
70
 
71
/* It's either the noinline attribute or forcing the test framework to
72
   pass -fno-builtin-memmove.  */
73
void
74
xmemmove (unsigned char *dest, unsigned char *src, size_t n)
75
     __attribute__ ((__noinline__));
76
 
77
void
78
xmemmove (unsigned char *dest, unsigned char *src, size_t n)
79
{
80
  void *retp;
81
  retp = memmove (dest, src, n);
82
 
83
  if (retp != dest)
84
    {
85
      errors++;
86
      DEBUGP ("memmove of n bytes returned %p instead of dest=%p\n",
87
              retp, dest);
88
    }
89
}
90
 
91
 
92
/* Fill the array with something we can associate with a position, but
93
   not exactly the same as the position index.  */
94
 
95
void
96
fill (unsigned char dest[MAX*3])
97
{
98
  size_t i;
99
  for (i = 0; i < MAX*3; i++)
100
    dest[i] = (10 + i) % MAX;
101
}
102
 
103
int
104
main (void)
105
{
106
  size_t i;
107
  int errors = 0;
108
 
109
  /* Leave some room before and after the area tested, so we can detect
110
     overwrites of up to N bytes, N being the amount tested.  If you
111
     want to test using valgrind, make these malloced instead.  */
112
  unsigned char from_test[MAX*3];
113
  unsigned char to_test[MAX*3];
114
  unsigned char from_known[MAX*3];
115
  unsigned char to_known[MAX*3];
116
 
117
  /* Non-overlap.  */
118
  for (i = 0; i < MAX; i++)
119
    {
120
      /* Do the memmove first before setting the known array, so we know
121
         it didn't change any of the known array.  */
122
      fill (from_test);
123
      fill (to_test);
124
      xmemmove (to_test + MAX, 1 + from_test + MAX, i);
125
 
126
      fill (from_known);
127
      fill (to_known);
128
      mymemmove (to_known + MAX, 1 + from_known + MAX, i);
129
 
130
      if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
131
        {
132
          errors++;
133
          DEBUGP ("memmove failed non-overlap test for %d bytes\n", i);
134
        }
135
    }
136
 
137
  /* Overlap-from-before.  */
138
  for (i = 0; i < MAX; i++)
139
    {
140
      size_t j;
141
      for (j = 0; j < i; j++)
142
        {
143
          fill (to_test);
144
          xmemmove (to_test + MAX * 2 - i, to_test + MAX * 2 - i - j, i);
145
 
146
          fill (to_known);
147
          mymemmove (to_known + MAX * 2 - i, to_known + MAX * 2 - i - j, i);
148
 
149
          if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
150
            {
151
              errors++;
152
              DEBUGP ("memmove failed for %d bytes,"
153
                      " with src %d bytes before dest\n",
154
                      i, j);
155
            }
156
        }
157
    }
158
 
159
  /* Overlap-from-after.  */
160
  for (i = 0; i < MAX; i++)
161
    {
162
      size_t j;
163
      for (j = 0; j < i; j++)
164
        {
165
          fill (to_test);
166
          xmemmove (to_test + MAX, to_test + MAX + j, i);
167
 
168
          fill (to_known);
169
          mymemmove (to_known + MAX, to_known + MAX + j, i);
170
 
171
          if (memcmp (to_known, to_test, sizeof (to_known)) != 0)
172
            {
173
              errors++;
174
              DEBUGP ("memmove failed when moving %d bytes,"
175
                      " with src %d bytes after dest\n",
176
                      i, j);
177
            }
178
        }
179
    }
180
 
181
  if (errors != 0)
182
    abort ();
183
  exit (0);
184
}

powered by: WebSVN 2.1.0

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