1 /* Copyright (C) 2000  Free Software Foundation.
2 
3    Ensure builtin strlen, strcmp, strchr, strrchr and strncpy
4    perform correctly.
5 
6    Written by Jakub Jelinek, 11/7/2000.  */
7 
8 extern void abort (void);
9 extern __SIZE_TYPE__ strlen (const char *);
10 extern int strcmp (const char *, const char *);
11 extern char *strchr (const char *, int);
12 extern char *strrchr (const char *, int);
13 extern char *strncpy (char *, const char *, __SIZE_TYPE__);
14 extern void *memset (void *, int, __SIZE_TYPE__);
15 extern int memcmp (const void *, const void *, __SIZE_TYPE__);
16 
17 int x = 6;
18 int y = 1;
19 char *bar = "hi world";
20 char buf [64];
21 
main()22 int main()
23 {
24   const char *const foo = "hello world";
25   char dst [64];
26 
27   if (strlen (bar) != 8)
28     abort ();
29   if (strlen (bar + (++x & 2)) != 6)
30     abort ();
31   if (x != 7)
32     abort ();
33   if (strlen (foo + (x++, 6)) != 5)
34     abort ();
35   if (x != 8)
36     abort ();
37   if (strlen (foo + (++x & 1)) != 10)
38     abort ();
39   if (x != 9)
40     abort ();
41   if (strcmp (foo + (x -= 6), "lo world"))
42     abort ();
43   if (x != 3)
44     abort ();
45   if (strcmp (foo, bar) >= 0)
46     abort ();
47   if (strcmp (foo, bar + (x++ & 1)) >= 0)
48     abort ();
49   if (x != 4)
50     abort ();
51   if (strchr (foo + (x++ & 7), 'l') != foo + 9)
52     abort ();
53   if (x != 5)
54     abort ();
55   if (strchr (bar, 'o') != bar + 4)
56     abort ();
57   if (strchr (bar, '\0')  != bar + 8)
58     abort ();
59   if (strrchr (bar, 'x'))
60     abort ();
61   if (strrchr (bar, 'o') != bar + 4)
62     abort ();
63   if (strcmp (foo + (x++ & 1), "ello world" + (--y & 1)))
64     abort ();
65   if (x != 6 || y != 0)
66     abort ();
67   dst[5] = ' ';
68   dst[6] = '\0';
69   x = 5;
70   y = 1;
71   if (strncpy (dst + 1, foo + (x++ & 3), 4) != dst + 1
72       || x != 6
73       || strcmp (dst + 1, "ello "))
74     abort ();
75   memset (dst, ' ', sizeof dst);
76   if (strncpy (dst + (++x & 1), (y++ & 3) + "foo", 10) != dst + 1
77       || x != 7
78       || y != 2
79       || memcmp (dst, " oo\0\0\0\0\0\0\0\0 ", 12))
80     abort ();
81   memset (dst, ' ', sizeof dst);
82   if (strncpy (dst, "hello", 8) != dst || memcmp (dst, "hello\0\0\0 ", 9))
83     abort ();
84   x = '!';
85   memset (buf, ' ', sizeof buf);
86   if (memset (buf, x++, ++y) != buf
87       || x != '!' + 1
88       || y != 3
89       || memcmp (buf, "!!!", 3))
90     abort ();
91   if (memset (buf + y++, '-', 8) != buf + 3
92       || y != 4
93       || memcmp (buf, "!!!--------", 11))
94     abort ();
95   x = 10;
96   if (memset (buf + ++x, 0, y++) != buf + 11
97       || x != 11
98       || y != 5
99       || memcmp (buf + 8, "---\0\0\0", 7))
100     abort ();
101   if (memset (buf + (x += 4), 0, 6) != buf + 15
102       || x != 15
103       || memcmp (buf + 10, "-\0\0\0\0\0\0\0\0\0", 11))
104     abort ();
105 
106   return 0;
107 }
108