1 /* Copyright (C) 2000, 2003  Free Software Foundation.
2 
3    Ensure all expected transformations of builtin strcat occur and
4    perform correctly.
5 
6    Written by Kaveh R. Ghazi, 11/27/2000.  */
7 
8 extern int inside_main;
9 extern void abort (void);
10 typedef __SIZE_TYPE__ size_t;
11 extern char *strcat (char *, const char *);
12 extern char *strcpy (char *, const char *);
13 extern void *memset (void *, int, size_t);
14 extern int memcmp (const void *, const void *, size_t);
15 #define RESET_DST_WITH(FILLER) \
16   do { memset (dst, 'X', sizeof (dst)); strcpy (dst, (FILLER)); } while (0)
17 
main_test(void)18 void main_test (void)
19 {
20   const char *const s1 = "hello world";
21   const char *const s2 = "";
22   char dst[64], *d2;
23 
24   RESET_DST_WITH (s1);
25   if (strcat (dst, "") != dst || memcmp (dst, "hello world\0XXX", 15))
26     abort();
27   RESET_DST_WITH (s1);
28   if (strcat (dst, s2) != dst || memcmp (dst, "hello world\0XXX", 15))
29     abort();
30   RESET_DST_WITH (s1); d2 = dst;
31   if (strcat (++d2, s2) != dst+1 || d2 != dst+1
32       || memcmp (dst, "hello world\0XXX", 15))
33     abort();
34   RESET_DST_WITH (s1); d2 = dst;
35   if (strcat (++d2+5, s2) != dst+6 || d2 != dst+1
36       || memcmp (dst, "hello world\0XXX", 15))
37     abort();
38   RESET_DST_WITH (s1); d2 = dst;
39   if (strcat (++d2+5, s1+11) != dst+6 || d2 != dst+1
40       || memcmp (dst, "hello world\0XXX", 15))
41     abort();
42 
43 #ifndef __OPTIMIZE_SIZE__
44 # if !defined __i386__ && !defined __x86_64__
45   /* The functions below might not be optimized into direct stores on all
46      arches.  It depends on how many instructions would be generated and
47      what limits the architecture chooses in STORE_BY_PIECES_P.  */
48   inside_main = 0;
49 # endif
50 
51   RESET_DST_WITH (s1);
52   if (strcat (dst, " 1111") != dst
53       || memcmp (dst, "hello world 1111\0XXX", 20))
54     abort();
55 
56   RESET_DST_WITH (s1);
57   if (strcat (dst+5, " 2222") != dst+5
58       || memcmp (dst, "hello world 2222\0XXX", 20))
59     abort();
60 
61   RESET_DST_WITH (s1); d2 = dst;
62   if (strcat (++d2+5, " 3333") != dst+6 || d2 != dst+1
63       || memcmp (dst, "hello world 3333\0XXX", 20))
64     abort();
65 
66   RESET_DST_WITH (s1);
67   strcat (strcat (strcat (strcat (strcat (strcat (dst, ": this "), ""),
68 				  "is "), "a "), "test"), ".");
69   if (memcmp (dst, "hello world: this is a test.\0X", 30))
70     abort();
71 
72   /* Set inside_main again.  */
73   inside_main = 1;
74 #endif
75 
76   /* Test at least one instance of the __builtin_ style.  We do this
77      to ensure that it works and that the prototype is correct.  */
78   RESET_DST_WITH (s1);
79   if (__builtin_strcat (dst, "") != dst || memcmp (dst, "hello world\0XXX", 15))
80     abort();
81 }
82