* gcc.c-torture/execute/stdio-opt-1.c: Test __builtin_ style too.
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / string-opt-3.c
blob61718f2900f6ffa5e73bd4c4aba45c016528ecc6
1 /* Copyright (C) 2000 Free Software Foundation.
3 Ensure all expected transformations of builtin strlen, strcmp,
4 strrchr and rindex occur and perform correctly.
6 Written by Jakub Jelinek, 11/7/2000. */
8 extern void abort (void);
9 extern __SIZE_TYPE__ strlen (const char *);
10 extern int strcmp (const char *, const char *);
11 extern char *strrchr (const char *, int);
12 extern char *rindex (const char *, int);
14 int x = 6;
15 char *bar = "hi world";
17 int main()
19 const char *const foo = "hello world";
21 if (strlen (foo) != 11)
22 abort ();
23 if (strlen (foo + 4) != 7)
24 abort ();
25 if (strlen (foo + (x++ & 7)) != 5)
26 abort ();
27 if (x != 7)
28 abort ();
29 if (strcmp (foo, "hello") <= 0)
30 abort ();
31 if (strcmp (foo + 2, "llo") <= 0)
32 abort ();
33 if (strcmp (foo, foo) != 0)
34 abort ();
35 if (strcmp (foo, "hello world ") >= 0)
36 abort ();
37 if (strcmp (foo + 10, "dx") >= 0)
38 abort ();
39 if (strcmp (10 + foo, "dx") >= 0)
40 abort ();
41 if (strcmp (bar, "") <= 0)
42 abort ();
43 if (strcmp ("", bar) >= 0)
44 abort ();
45 if (strcmp (bar+8, "") != 0)
46 abort ();
47 if (strcmp ("", bar+8) != 0)
48 abort ();
49 if (strcmp (bar+(--x), "") <= 0 || x != 6)
50 abort ();
51 if (strcmp ("", bar+(++x)) >= 0 || x != 7)
52 abort ();
53 if (strrchr (foo, 'x'))
54 abort ();
55 if (strrchr (foo, 'o') != foo + 7)
56 abort ();
57 if (strrchr (foo, 'e') != foo + 1)
58 abort ();
59 if (strrchr (foo + 3, 'e'))
60 abort ();
61 if (strrchr (foo, '\0') != foo + 11)
62 abort ();
63 if (strrchr (bar, '\0') != bar + 8)
64 abort ();
65 if (strrchr (bar + 4, '\0') != bar + 8)
66 abort ();
67 if (strrchr (bar + (x++ & 3), '\0') != bar + 8)
68 abort ();
69 if (x != 8)
70 abort ();
71 /* Test only one instance of rindex since the code path is the same
72 as that of strrchr. */
73 if (rindex ("hello", 'z') != 0)
74 abort ();
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 if (__builtin_rindex (foo, 'o') != foo + 7)
79 abort ();
80 if (__builtin_strrchr (foo, 'o') != foo + 7)
81 abort ();
82 if (__builtin_strlen (foo) != 11)
83 abort ();
84 if (__builtin_strcmp (foo, "hello") <= 0)
85 abort ();
87 return 0;
90 static char *
91 rindex (const char *s, int c)
93 /* For systems which don't have rindex, we ensure no link failures
94 occur by always providing a backup definition. During
95 optimization this function aborts to catch errors. */
96 #ifdef __OPTIMIZE__
97 abort ();
98 #else
99 return strrchr(s, c);
100 #endif
103 #ifdef __OPTIMIZE__
104 /* When optimizing, all the above cases should be transformed into
105 something else. So any remaining calls to the original function
106 should abort. */
107 static __SIZE_TYPE__
108 strlen (const char *s)
110 abort ();
113 static int
114 strcmp (const char *s1, const char *s2)
116 abort ();
119 static char *
120 strrchr (const char *s, int c)
122 abort ();
124 #endif