OSDN Git Service

c0a815701e660b22d459f5021207cd721a81ef23
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / builtins / strncpy.c
1 /* Copyright (C) 2000  Free Software Foundation.
2
3    Ensure all expected transformations of builtin strncpy occur and
4    perform correctly.
5
6    Written by Kaveh R. Ghazi, 11/25/2000.  */
7
8 extern void abort (void);
9 typedef __SIZE_TYPE__ size_t;
10 extern char *strncpy (char *, const char *, size_t);
11 extern int strcmp (const char *, const char *);
12 extern int strncmp (const char *, const char *, size_t);
13 extern void *memset (void *, int, size_t);
14
15 int i;
16
17 void
18 main_test (void)
19 {
20   const char *const src = "hello world";
21   const char *src2;
22   char dst[64], *dst2;
23   
24   memset (dst, 0, sizeof (dst));
25   if (strncpy (dst, src, 4) != dst || strncmp (dst, src, 4))
26     abort();
27
28   memset (dst, 0, sizeof (dst));
29   if (strncpy (dst+16, src, 4) != dst+16 || strncmp (dst+16, src, 4))
30     abort();
31
32   memset (dst, 0, sizeof (dst));
33   if (strncpy (dst+32, src+5, 4) != dst+32 || strncmp (dst+32, src+5, 4))
34     abort();
35
36   memset (dst, 0, sizeof (dst));
37   dst2 = dst;
38   if (strncpy (++dst2, src+5, 4) != dst+1 || strncmp (dst2, src+5, 4)
39       || dst2 != dst+1)
40     abort();
41
42   memset (dst, 0, sizeof (dst));
43   if (strncpy (dst, src, 0) != dst || strcmp (dst, ""))
44     abort();
45   
46   memset (dst, 0, sizeof (dst));
47   dst2 = dst; src2 = src;
48   if (strncpy (++dst2, ++src2, 0) != dst+1 || strcmp (dst2, "")
49       || dst2 != dst+1 || src2 != src+1)
50     abort();
51
52   memset (dst, 0, sizeof (dst));
53   dst2 = dst; src2 = src;
54   if (strncpy (++dst2+5, ++src2+5, 0) != dst+6 || strcmp (dst2+5, "")
55       || dst2 != dst+1 || src2 != src+1)
56     abort();
57
58   memset (dst, 0, sizeof (dst));
59   if (strncpy (dst, src, 12) != dst || strcmp (dst, src))
60     abort();
61
62   /* Test at least one instance of the __builtin_ style.  We do this
63      to ensure that it works and that the prototype is correct.  */
64   memset (dst, 0, sizeof (dst));
65   if (__builtin_strncpy (dst, src, 4) != dst || strncmp (dst, src, 4))
66     abort();
67
68   memset (dst, 0, sizeof (dst));
69   if (strncpy (dst, i++ ? "xfoo" + 1 : "bar", 4) != dst
70       || strcmp (dst, "bar")
71       || i != 1)
72     abort ();
73
74   return 0;
75 }