OSDN Git Service

PR c++/14971
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / builtins / strncat.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 void
16 main_test (void)
17 {
18   const char *const s1 = "hello world";
19   const char *const s2 = "";
20   char dst[64], *d2;
21   
22   strcpy (dst, s1);
23   if (strncat (dst, "", 100) != dst || strcmp (dst, s1))
24     abort();
25   strcpy (dst, s1);
26   if (strncat (dst, s2, 100) != dst || strcmp (dst, s1))
27     abort();
28   strcpy (dst, s1); d2 = dst;
29   if (strncat (++d2, s2, 100) != dst+1 || d2 != dst+1 || strcmp (dst, s1))
30     abort();
31   strcpy (dst, s1); d2 = dst;
32   if (strncat (++d2+5, s2, 100) != dst+6 || d2 != dst+1 || strcmp (dst, s1))
33     abort();
34   strcpy (dst, s1); d2 = dst;
35   if (strncat (++d2+5, s1+11, 100) != dst+6 || d2 != dst+1 || strcmp (dst, s1))
36     abort();
37   strcpy (dst, s1); d2 = dst;
38   if (strncat (++d2+5, s1, 0) != dst+6 || d2 != dst+1 || strcmp (dst, s1))
39     abort();
40   strcpy (dst, s1); d2 = dst;
41   if (strncat (++d2+5, "", ++x) != dst+6 || d2 != dst+1 || x != 124
42       || strcmp (dst, s1))
43     abort();
44
45   strcpy (dst, s1);
46   if (strncat (dst, "foo", 3) != dst || strcmp (dst, "hello worldfoo"))
47     abort();
48   strcpy (dst, s1);
49   if (strncat (dst, "foo", 100) != dst || strcmp (dst, "hello worldfoo"))
50     abort();
51   strcpy (dst, s1);
52   if (strncat (dst, s1, 100) != dst || strcmp (dst, "hello worldhello world"))
53     abort();
54   strcpy (dst, s1); d2 = dst;
55   if (strncat (++d2, s1, 100) != dst+1 || d2 != dst+1
56       || strcmp (dst, "hello worldhello world"))
57     abort();
58   strcpy (dst, s1); d2 = dst;
59   if (strncat (++d2+5, s1, 100) != dst+6 || d2 != dst+1
60       || strcmp (dst, "hello worldhello world"))
61     abort();
62   strcpy (dst, s1); d2 = dst;
63   if (strncat (++d2+5, s1+5, 100) != dst+6 || d2 != dst+1
64       || strcmp (dst, "hello world world"))
65     abort();
66
67   /* Test at least one instance of the __builtin_ style.  We do this
68      to ensure that it works and that the prototype is correct.  */
69   strcpy (dst, s1);
70   if (__builtin_strncat (dst, "", 100) != dst || strcmp (dst, s1))
71     abort();
72 }