2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / string-opt-7.c
blob5b915d7a7d33f8437aff926e1cc8f6f335338ed7
1 /* Copyright (C) 2000 Free Software Foundation.
3 Ensure all expected transformations of builtin strncpy occur and
4 perform correctly.
6 Written by Kaveh R. Ghazi, 11/25/2000. */
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);
15 int i;
17 int main ()
19 const char *const src = "hello world";
20 const char *src2;
21 char dst[64], *dst2;
23 memset (dst, 0, sizeof (dst));
24 if (strncpy (dst, src, 4) != dst || strncmp (dst, src, 4))
25 abort();
27 memset (dst, 0, sizeof (dst));
28 if (strncpy (dst+16, src, 4) != dst+16 || strncmp (dst+16, src, 4))
29 abort();
31 memset (dst, 0, sizeof (dst));
32 if (strncpy (dst+32, src+5, 4) != dst+32 || strncmp (dst+32, src+5, 4))
33 abort();
35 memset (dst, 0, sizeof (dst));
36 dst2 = dst;
37 if (strncpy (++dst2, src+5, 4) != dst+1 || strncmp (dst2, src+5, 4)
38 || dst2 != dst+1)
39 abort();
41 memset (dst, 0, sizeof (dst));
42 if (strncpy (dst, src, 0) != dst || strcmp (dst, ""))
43 abort();
45 memset (dst, 0, sizeof (dst));
46 dst2 = dst; src2 = src;
47 if (strncpy (++dst2, ++src2, 0) != dst+1 || strcmp (dst2, "")
48 || dst2 != dst+1 || src2 != src+1)
49 abort();
51 memset (dst, 0, sizeof (dst));
52 dst2 = dst; src2 = src;
53 if (strncpy (++dst2+5, ++src2+5, 0) != dst+6 || strcmp (dst2+5, "")
54 || dst2 != dst+1 || src2 != src+1)
55 abort();
57 memset (dst, 0, sizeof (dst));
58 if (strncpy (dst, src, 12) != dst || strcmp (dst, src))
59 abort();
61 /* Test at least one instance of the __builtin_ style. We do this
62 to ensure that it works and that the prototype is correct. */
63 memset (dst, 0, sizeof (dst));
64 if (__builtin_strncpy (dst, src, 4) != dst || strncmp (dst, src, 4))
65 abort();
67 memset (dst, 0, sizeof (dst));
68 if (strncpy (dst, i++ ? "xfoo" + 1 : "bar", 4) != dst
69 || strcmp (dst, "bar")
70 || i != 1)
71 abort ();
73 return 0;
76 #ifdef __OPTIMIZE__
77 /* When optimizing, all the above cases should be transformed into
78 something else. So any remaining calls to the original function
79 should abort. */
80 __attribute__ ((noinline))
81 static char *
82 strncpy(char *s1, const char *s2, size_t n)
84 abort();
86 #endif