OSDN Git Service

* gcc.c-torture/execute/20040208-2.c: Move ...
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / string-opt-7.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 int main ()
18 {
19   const char *const src = "hello world";
20   const char *src2;
21   char dst[64], *dst2;
22   
23   memset (dst, 0, sizeof (dst));
24   if (strncpy (dst, src, 4) != dst || strncmp (dst, src, 4))
25     abort();
26
27   memset (dst, 0, sizeof (dst));
28   if (strncpy (dst+16, src, 4) != dst+16 || strncmp (dst+16, src, 4))
29     abort();
30
31   memset (dst, 0, sizeof (dst));
32   if (strncpy (dst+32, src+5, 4) != dst+32 || strncmp (dst+32, src+5, 4))
33     abort();
34
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();
40
41   memset (dst, 0, sizeof (dst));
42   if (strncpy (dst, src, 0) != dst || strcmp (dst, ""))
43     abort();
44   
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();
50
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();
56
57   memset (dst, 0, sizeof (dst));
58   if (strncpy (dst, src, 12) != dst || strcmp (dst, src))
59     abort();
60
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();
66
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 ();
72
73   return 0;
74 }
75
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)
83 {
84   abort();
85 }
86 #endif