OSDN Git Service

PR c++/14971
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / string-opt-10.c
1 /* Copyright (C) 2000, 2003  Free Software Foundation.
2
3    Ensure all expected transformations of builtin strncat occur and
4    perform correctly.
5
6    Written by Kaveh R. Ghazi, 11/27/2000.  */
7
8 extern void abort (void);
9 typedef __SIZE_TYPE__ size_t;
10 extern char *strncat (char *, const char *, size_t);
11 extern char *strcpy (char *, const char *);
12 extern int strcmp (const char *, const char *);
13 int x = 123;
14
15 int main ()
16 {
17   const char *const s1 = "hello world";
18   const char *const s2 = "";
19   char dst[64], *d2;
20   
21   strcpy (dst, s1);
22   if (strncat (dst, "", 100) != dst || strcmp (dst, s1))
23     abort();
24   strcpy (dst, s1);
25   if (strncat (dst, s2, 100) != dst || strcmp (dst, s1))
26     abort();
27   strcpy (dst, s1); d2 = dst;
28   if (strncat (++d2, s2, 100) != dst+1 || d2 != dst+1 || strcmp (dst, s1))
29     abort();
30   strcpy (dst, s1); d2 = dst;
31   if (strncat (++d2+5, s2, 100) != dst+6 || d2 != dst+1 || strcmp (dst, s1))
32     abort();
33   strcpy (dst, s1); d2 = dst;
34   if (strncat (++d2+5, s1+11, 100) != dst+6 || d2 != dst+1 || strcmp (dst, s1))
35     abort();
36   strcpy (dst, s1); d2 = dst;
37   if (strncat (++d2+5, s1, 0) != dst+6 || d2 != dst+1 || strcmp (dst, s1))
38     abort();
39   strcpy (dst, s1); d2 = dst;
40   if (strncat (++d2+5, "", ++x) != dst+6 || d2 != dst+1 || x != 124
41       || strcmp (dst, s1))
42     abort();
43
44   strcpy (dst, s1);
45   if (strncat (dst, "foo", 3) != dst || strcmp (dst, "hello worldfoo"))
46     abort();
47   strcpy (dst, s1);
48   if (strncat (dst, "foo", 100) != dst || strcmp (dst, "hello worldfoo"))
49     abort();
50   strcpy (dst, s1);
51   if (strncat (dst, s1, 100) != dst || strcmp (dst, "hello worldhello world"))
52     abort();
53   strcpy (dst, s1); d2 = dst;
54   if (strncat (++d2, s1, 100) != dst+1 || d2 != dst+1
55       || strcmp (dst, "hello worldhello world"))
56     abort();
57   strcpy (dst, s1); d2 = dst;
58   if (strncat (++d2+5, s1, 100) != dst+6 || d2 != dst+1
59       || strcmp (dst, "hello worldhello world"))
60     abort();
61   strcpy (dst, s1); d2 = dst;
62   if (strncat (++d2+5, s1+5, 100) != dst+6 || d2 != dst+1
63       || strcmp (dst, "hello world world"))
64     abort();
65
66   /* Test at least one instance of the __builtin_ style.  We do this
67      to ensure that it works and that the prototype is correct.  */
68   strcpy (dst, s1);
69   if (__builtin_strncat (dst, "", 100) != dst || strcmp (dst, s1))
70     abort();
71
72   return 0;
73 }
74
75 #ifdef __OPTIMIZE__
76 /* When optimizing, all the above cases should be transformed into
77    something else.  So any remaining calls to the original function
78    should abort.  */
79 __attribute__ ((noinline))
80 static char *
81 strncat (char *s1, const char *s2, size_t n)
82 {
83   abort();
84 }
85 #endif