source: trunk/libs/newlib/src/newlib/testsuite/newlib.string/strcmp-1.c @ 567

Last change on this file since 567 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 6.9 KB
Line 
1/*
2 * Copyright (c) 2011 ARM Ltd
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 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the company may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <stdarg.h>
33
34/* The macro LONG_TEST controls whether a short or a more comprehensive test
35   of strcmp should be performed.  */
36#ifdef LONG_TEST
37#ifndef BUFF_SIZE
38#define BUFF_SIZE 1024
39#endif
40
41#ifndef MAX_BLOCK_SIZE
42#define MAX_BLOCK_SIZE 128
43#endif
44
45#ifndef MAX_OFFSET
46#define MAX_OFFSET 3
47#endif
48
49#ifndef MAX_DIFF
50#define MAX_DIFF 8
51#endif
52
53#ifndef MAX_LEN
54#define MAX_LEN 8
55#endif
56
57#ifndef MAX_ZEROS
58#define MAX_ZEROS 8
59#endif
60#else /* not defined LONG_TEST */
61#ifndef BUFF_SIZE
62#define BUFF_SIZE 1024
63#endif
64
65#ifndef MAX_BLOCK_SIZE
66#define MAX_BLOCK_SIZE 64
67#endif
68
69#ifndef MAX_OFFSET
70#define MAX_OFFSET 3
71#endif
72
73#ifndef MAX_DIFF
74#define MAX_DIFF 4
75#endif
76
77#ifndef MAX_LEN
78#define MAX_LEN 4
79#endif
80
81#ifndef MAX_ZEROS
82#define MAX_ZEROS 4
83#endif
84#endif /* not defined LONG_TEST */
85
86#if (MAX_OFFSET >= 26)
87#error "MAX_OFFSET >= 26"
88#endif
89#if (MAX_OFFSET + MAX_BLOCK_SIZE + MAX_DIFF + MAX_LEN + MAX_ZEROS >= BUFF_SIZE)
90#error "Buffer overrun: MAX_OFFSET + MAX_BLOCK_SIZE + MAX_DIFF + MAX_LEN + MAX_ZEROS >= BUFF_SIZE."
91#endif
92
93
94#define TOO_MANY_ERRORS 11
95int errors = 0;
96
97const char *testname = "strcmp";
98
99void
100print_error (char const* msg, ...)
101{
102  errors++;
103  if (errors == TOO_MANY_ERRORS)
104    {
105      fprintf (stderr, "Too many errors.\n");
106    }
107  else if (errors < TOO_MANY_ERRORS)
108    {
109      va_list ap;
110      va_start (ap, msg);
111      vfprintf (stderr, msg, ap);
112      va_end (ap);
113    }
114  else
115    {
116      /* Further errors omitted.  */
117    }
118}
119
120void
121printbuf (char *buf, char *name)
122{
123  int i;
124  printf ("\n %s=", name);
125  for (i = 0; i < BUFF_SIZE; i++)
126    if (buf[i] != 0)
127      printf ("(%d,%c)", i, buf[i]);
128    else
129      printf ("(%d,%s)", i, "\\0");
130  printf ("\n");
131}
132
133int
134main (void)
135{
136  /* Allocate buffers to read and write from.  */
137  char src[BUFF_SIZE], dest[BUFF_SIZE];
138
139  /* Fill the source buffer with non-null values, reproducable random data.  */
140  srand (1539);
141  int i, j, zeros;
142  unsigned sa;
143  unsigned da;
144  unsigned n, m, len;
145  char *p;
146  int ret;
147
148  /* Make calls to strcmp with block sizes ranging between 1 and
149     MAX_BLOCK_SIZE bytes, aligned and misaligned source and destination.  */
150  for (sa = 0; sa <= MAX_OFFSET; sa++)
151    for (da = 0; da <= MAX_OFFSET; da++)
152      for (n = 1; n <= MAX_BLOCK_SIZE; n++)
153        {
154        for (m = 1;  m < n + MAX_DIFF; m++)
155          for (len = 0; len < MAX_LEN; len++)
156            for  (zeros = 1; zeros < MAX_ZEROS; zeros++)
157            {
158              if (n - m > MAX_DIFF)
159                continue;
160              /* Make a copy of the source.  */
161              for (i = 0; i < BUFF_SIZE; i++)
162                {
163                  src[i] = 'A' + (i % 26);
164                  dest[i] = src[i];
165                }
166              memcpy (dest + da, src + sa, n);
167
168              /* Make src 0-terminated.  */
169              p = src + sa + n - 1;
170              for (i = 0; i < zeros; i++)
171                {
172                  *p++ = '\0';
173                }
174
175              /* Modify dest.  */
176              p = dest + da + m - 1;
177              for (j = 0; j < len; j++)
178                *p++ = 'x';
179              /* Make dest 0-terminated.  */
180              *p = '\0';
181
182              ret = strcmp (src + sa, dest + da);
183
184              /* Check return value.  */
185              if (n == m)
186                {
187                  if (len == 0)
188                    {
189                      if (ret != 0)
190                        {
191                        print_error ("\nFailed: after %s of %u bytes "
192                                     "with src_align %u and dst_align %u, "
193                                     "dest after %d bytes is modified for %d bytes, "
194                                     "return value is %d, expected 0.\n",
195                                     testname, n, sa, da, m, len, ret);
196                        }
197                    }
198                  else
199                    {
200                      if (ret >= 0)
201                        print_error ("\nFailed: after %s of %u bytes "
202                                     "with src_align %u and dst_align %u, "
203                                     "dest after %d bytes is modified for %d bytes, "
204                                     "return value is %d, expected negative.\n",
205                                     testname, n, sa, da, m, len, ret);
206                    }
207                }
208              else if (m > n)
209                {
210                  if (ret >= 0)
211                    {
212                      print_error ("\nFailed: after %s of %u bytes "
213                                   "with src_align %u and dst_align %u, "
214                                   "dest after %d bytes is modified for %d bytes, "
215                                   "return value is %d, expected negative.\n",
216                                   testname, n, sa, da, m, len, ret);
217                    }
218                }
219              else  /* m < n */
220                {
221                  if (len == 0)
222                    {
223                      if (ret <= 0)
224                        print_error ("\nFailed: after %s of %u bytes "
225                                     "with src_align %u and dst_align %u, "
226                                     "dest after %d bytes is modified for %d bytes, "
227                                     "return value is %d, expected positive.\n",
228                                     testname, n, sa, da, m, len, ret);
229                    }
230                  else
231                    {
232                      if (ret >= 0)
233                        print_error ("\nFailed: after %s of %u bytes "
234                                     "with src_align %u and dst_align %u, "
235                                     "dest after %d bytes is modified for %d bytes, "
236                                     "return value is %d, expected negative.\n",
237                                     testname, n, sa, da, m, len, ret);
238                    }
239                }
240            }
241        }
242
243  /* Check some corner cases.  */
244  src[1] = 'A';
245  dest[1] = 'A';
246  src[2] = 'B';
247  dest[2] = 'B';
248  src[3] = 'C';
249  dest[3] = 'C';
250  src[4] = '\0';
251  dest[4] = '\0';
252
253  src[0] = 0xc1;
254  dest[0] = 0x41;
255  ret = strcmp (src, dest);
256  if (ret <= 0)
257    print_error ("\nFailed: expected positive, return %d\n", ret);
258
259  src[0] = 0x01;
260  dest[0] = 0x82;
261  ret = strcmp (src, dest);
262  if (ret >= 0)
263    print_error ("\nFailed: expected negative, return %d\n", ret);
264
265  dest[0] = src[0] = 'D';
266  src[3] = 0xc1;
267  dest[3] = 0x41;
268  ret = strcmp (src, dest);
269  if (ret <= 0)
270    print_error ("\nFailed: expected positive, return %d\n", ret);
271
272  src[3] = 0x01;
273  dest[3] = 0x82;
274  ret = strcmp (src, dest);
275  if (ret >= 0)
276    print_error ("\nFailed: expected negative, return %d\n", ret);
277
278  printf ("\n");
279  if (errors != 0)
280    {
281      printf ("ERROR. FAILED.\n");
282      abort ();
283    }
284  exit (0);
285}
Note: See TracBrowser for help on using the repository browser.