2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.c-torture / execute / strcmp-1.c
blob79999af3cbe8866f85b0efba48ae4227cdf89594
1 /* Copyright (C) 2002 Free Software Foundation.
3 Test strcmp with various combinations of pointer alignments and lengths to
4 make sure any optimizations in the library are correct.
6 Written by Michael Meissner, March 9, 2002. */
8 #include <string.h>
9 #include <stddef.h>
11 #ifndef MAX_OFFSET
12 #define MAX_OFFSET (sizeof (long long))
13 #endif
15 #ifndef MAX_TEST
16 #define MAX_TEST (8 * sizeof (long long))
17 #endif
19 #ifndef MAX_EXTRA
20 #define MAX_EXTRA (sizeof (long long))
21 #endif
23 #define MAX_LENGTH (MAX_OFFSET + MAX_TEST + MAX_EXTRA + 2)
25 static union {
26 unsigned char buf[MAX_LENGTH];
27 long long align_int;
28 long double align_fp;
29 } u1, u2;
31 void
32 test (const unsigned char *s1, const unsigned char *s2, int expected)
34 int value = strcmp ((char *) s1, (char *) s2);
36 if (expected < 0 && value >= 0)
37 abort ();
38 else if (expected == 0 && value != 0)
39 abort ();
40 else if (expected > 0 && value <= 0)
41 abort ();
44 main ()
46 size_t off1, off2, len, i;
47 unsigned char *buf1, *buf2;
48 unsigned char *mod1, *mod2;
49 unsigned char *p1, *p2;
51 for (off1 = 0; off1 < MAX_OFFSET; off1++)
52 for (off2 = 0; off2 < MAX_OFFSET; off2++)
53 for (len = 0; len < MAX_TEST; len++)
55 p1 = u1.buf;
56 for (i = 0; i < off1; i++)
57 *p1++ = '\0';
59 buf1 = p1;
60 for (i = 0; i < len; i++)
61 *p1++ = 'a';
63 mod1 = p1;
64 for (i = 0; i < MAX_EXTRA+2; i++)
65 *p1++ = 'x';
67 p2 = u2.buf;
68 for (i = 0; i < off2; i++)
69 *p2++ = '\0';
71 buf2 = p2;
72 for (i = 0; i < len; i++)
73 *p2++ = 'a';
75 mod2 = p2;
76 for (i = 0; i < MAX_EXTRA+2; i++)
77 *p2++ = 'x';
79 mod1[0] = '\0';
80 mod2[0] = '\0';
81 test (buf1, buf2, 0);
83 mod1[0] = 'a';
84 mod1[1] = '\0';
85 mod2[0] = '\0';
86 test (buf1, buf2, +1);
88 mod1[0] = '\0';
89 mod2[0] = 'a';
90 mod2[1] = '\0';
91 test (buf1, buf2, -1);
93 mod1[0] = 'b';
94 mod1[1] = '\0';
95 mod2[0] = 'c';
96 mod2[1] = '\0';
97 test (buf1, buf2, -1);
99 mod1[0] = 'c';
100 mod1[1] = '\0';
101 mod2[0] = 'b';
102 mod2[1] = '\0';
103 test (buf1, buf2, +1);
105 mod1[0] = 'b';
106 mod1[1] = '\0';
107 mod2[0] = (unsigned char)'\251';
108 mod2[1] = '\0';
109 test (buf1, buf2, -1);
111 mod1[0] = (unsigned char)'\251';
112 mod1[1] = '\0';
113 mod2[0] = 'b';
114 mod2[1] = '\0';
115 test (buf1, buf2, +1);
117 mod1[0] = (unsigned char)'\251';
118 mod1[1] = '\0';
119 mod2[0] = (unsigned char)'\252';
120 mod2[1] = '\0';
121 test (buf1, buf2, -1);
123 mod1[0] = (unsigned char)'\252';
124 mod1[1] = '\0';
125 mod2[0] = (unsigned char)'\251';
126 mod2[1] = '\0';
127 test (buf1, buf2, +1);
130 exit (0);