OSDN Git Service

* gcc.c-torture/execute/string-opt-18.c: Clean up. Fix copyright date.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / string-opt-18.c
1 /* Copyright (C) 2003  Free Software Foundation.
2
3    Ensure builtin mempcpy and stpcpy perform correctly.
4
5    Written by Kaveh Ghazi, 4/11/2003.  */
6
7 extern void abort (void);
8 extern char *strcpy (char *, const char *);
9 extern char *stpcpy (char *, const char *);
10 typedef __SIZE_TYPE__ size_t;
11 extern size_t strlen(const char *);
12 extern void *memcpy (void *, const void *, size_t);
13 extern void *mempcpy (void *, const void *, size_t);
14 extern int memcmp (const void *, const void *, size_t);
15
16 const char s1[] = "123";
17 char p[32] = "";
18
19 int main()
20 {
21   int i;
22   const char *s;
23
24   if (stpcpy (p, "abcde") != p + 5 || memcmp (p, "abcde", 6))
25     abort ();
26   if (stpcpy (p + 16, "vwxyz" + 1) != p + 16 + 4 || memcmp (p + 16, "wxyz", 5))
27     abort ();
28   if (stpcpy (p + 1, "") != p + 1 + 0 || memcmp (p, "a\0cde", 6))
29     abort ();  
30   if (stpcpy (p + 3, "fghij") != p + 3 + 5 || memcmp (p, "a\0cfghij", 9))
31     abort ();
32   if (mempcpy (p, "ABCDE", 6) != p + 6 || memcmp (p, "ABCDE", 6))
33     abort ();
34   if (mempcpy (p + 16, "VWX" + 1, 2) != p + 16 + 2 || memcmp (p + 16, "WXyz", 5))
35     abort ();
36   if (mempcpy (p + 1, "", 1) != p + 1 + 1 || memcmp (p, "A\0CDE", 6))
37     abort ();  
38   if (mempcpy (p + 3, "FGHI", 4) != p + 3 + 4 || memcmp (p, "A\0CFGHIj", 9))
39     abort ();
40
41   i = 8;
42   memcpy (p + 20, "qrstu", 6);
43   if (stpcpy ((i++, p + 20 + 1), "23") != (p + 20 + 1 + 2) || i != 9 || memcmp (p + 20, "q23\0u", 6))
44     abort ();
45
46   s = s1; i = 3;
47   memcpy (p + 25, "QRSTU", 6);
48   if (mempcpy (p + 25 + 1, s++, i++) != (p + 25 + 1 + 3) || i != 4 || s != s1 + 1 || memcmp (p + 25, "Q123U", 6))
49     abort ();
50
51   if (stpcpy (stpcpy (p, "ABCD"), "EFG") != p + 7 || memcmp (p, "ABCDEFG", 8))
52     abort();
53   if (mempcpy (mempcpy (p, "abcdEFG", 4), "efg", 4) != p + 8 || memcmp (p, "abcdefg", 8))
54     abort();
55   
56   /* Test at least one instance of the __builtin_ style.  We do this
57      to ensure that it works and that the prototype is correct.  */
58   if (__builtin_stpcpy (p, "abcde") != p + 5 || memcmp (p, "abcde", 6))
59     abort ();
60   if (__builtin_mempcpy (p, "ABCDE", 6) != p + 6 || memcmp (p, "ABCDE", 6))
61     abort ();
62
63   return 0;
64 }
65
66 /* When optimizing, all the above cases should be transformed into
67    something else.  So any remaining calls to the original function
68    should abort.  When not optimizing, we provide fallback funcs for
69    platforms that don't have mempcpy or stpcpy in libc.*/
70 __attribute__ ((noinline))
71 static char *
72 stpcpy (char *d, const char *s)
73 {
74 #ifdef __OPTIMIZE__
75   abort ();
76 #else
77   return strcpy (d, s) + strlen (s);
78 #endif
79 }
80
81 __attribute__ ((noinline))
82 static void *
83 mempcpy (void *dst, const void *src, size_t sz)
84 {
85 #ifdef __OPTIMIZE__
86   abort ();
87 #else
88   return (char *) memcpy (dst, src, sz) + sz;
89 #endif
90 }