OSDN Git Service

Fix PR 17408 and PR 17409.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / builtins / strlen.c
1 /* Copyright (C) 2000, 2001, 2003, 2004  Free Software Foundation.
2
3    Ensure all expected transformations of builtin strlen
4    occur and perform correctly.
5
6    Written by Jakub Jelinek, 11/7/2000.
7
8    Additional tests written by Roger Sayle, 11/02/2001:
9    Ensure all builtin strlen comparisons against zero are optimized
10    and perform correctly. The multiple calls to strcpy are to prevent
11    the potentially "pure" strlen calls from being removed by CSE. */
12
13 extern void abort (void);
14 extern __SIZE_TYPE__ strlen (const char *);
15 extern char *strcpy (char *, const char *);
16
17 int x = 6;
18
19 void
20 main_test(void)
21 {
22   const char *const foo = "hello world";
23   char str[8];
24   char *ptr;
25
26   if (strlen (foo) != 11)
27     abort ();
28   if (strlen (foo + 4) != 7)
29     abort ();
30   if (strlen (foo + (x++ & 7)) != 5)
31     abort ();
32   if (x != 7)
33     abort ();
34
35   ptr = str;
36   strcpy (ptr, "nts");
37   if (strlen (ptr) == 0)
38     abort ();
39
40   strcpy (ptr, "nts");
41   if (strlen (ptr) < 1)
42     abort ();
43
44   strcpy (ptr, "nts");
45   if (strlen (ptr) <= 0)
46     abort ();
47
48   strcpy (ptr, "nts");
49   if (strlen (ptr+3) != 0)
50     abort ();
51
52   strcpy (ptr, "nts");
53   if (strlen (ptr+3) > 0)
54     abort ();
55
56   strcpy (ptr, "nts");
57   if (strlen (str+3) >= 1)
58     abort ();
59
60   /* Test at least one instance of the __builtin_ style.  We do this
61      to ensure that it works and that the prototype is correct.  */
62   if (__builtin_strlen (foo) != 11)
63     abort ();
64 }