2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / builtins / string-1.c
blob8a60e7e80ae7ebed8a6854a9198b85af7b253dd3
1 /* Copyright (C) 2000, 2003 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 void
18 main_test (void)
20 const char *const foo = "hello world";
22 if (strlen (foo) != 11)
23 abort ();
24 if (strlen (foo + 4) != 7)
25 abort ();
26 if (strlen (foo + (x++ & 7)) != 5)
27 abort ();
28 if (x != 7)
29 abort ();
30 if (strcmp (foo, "hello") <= 0)
31 abort ();
32 if (strcmp (foo + 2, "llo") <= 0)
33 abort ();
34 if (strcmp (foo, foo) != 0)
35 abort ();
36 if (strcmp (foo, "hello world ") >= 0)
37 abort ();
38 if (strcmp (foo + 10, "dx") >= 0)
39 abort ();
40 if (strcmp (10 + foo, "dx") >= 0)
41 abort ();
42 if (strcmp (bar, "") <= 0)
43 abort ();
44 if (strcmp ("", bar) >= 0)
45 abort ();
46 if (strcmp (bar+8, "") != 0)
47 abort ();
48 if (strcmp ("", bar+8) != 0)
49 abort ();
50 if (strcmp (bar+(--x), "") <= 0 || x != 6)
51 abort ();
52 if (strcmp ("", bar+(++x)) >= 0 || x != 7)
53 abort ();
54 if (strrchr (foo, 'x'))
55 abort ();
56 if (strrchr (foo, 'o') != foo + 7)
57 abort ();
58 if (strrchr (foo, 'e') != foo + 1)
59 abort ();
60 if (strrchr (foo + 3, 'e'))
61 abort ();
62 if (strrchr (foo, '\0') != foo + 11)
63 abort ();
64 if (strrchr (bar, '\0') != bar + 8)
65 abort ();
66 if (strrchr (bar + 4, '\0') != bar + 8)
67 abort ();
68 if (strrchr (bar + (x++ & 3), '\0') != bar + 8)
69 abort ();
70 if (x != 8)
71 abort ();
72 /* Test only one instance of rindex since the code path is the same
73 as that of strrchr. */
74 if (rindex ("hello", 'z') != 0)
75 abort ();
77 /* Test at least one instance of the __builtin_ style. We do this
78 to ensure that it works and that the prototype is correct. */
79 if (__builtin_rindex (foo, 'o') != foo + 7)
80 abort ();
81 if (__builtin_strrchr (foo, 'o') != foo + 7)
82 abort ();
83 if (__builtin_strlen (foo) != 11)
84 abort ();
85 if (__builtin_strcmp (foo, "hello") <= 0)
86 abort ();