OSDN Git Service

update copyrights
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.c-torture / execute / string-opt-8.c
1 /* Copyright (C) 2000, 2001  Free Software Foundation.
2
3    Ensure all expected transformations of builtin strncmp occur and
4    perform correctly.
5
6    Written by Kaveh R. Ghazi, 11/26/2000.  */
7
8 extern void abort (void);
9 typedef __SIZE_TYPE__ size_t;
10 extern int strncmp (const char *, const char *, size_t);
11
12 int main ()
13 {
14   const char *const s1 = "hello world";
15   const char *s2, *s3;
16   int n = 6, x;
17   
18   if (strncmp (s1, "hello world", 12) != 0)
19     abort();
20   if (strncmp ("hello world", s1, 12) != 0)
21     abort();
22   if (strncmp ("hello", "hello", 6) != 0)
23     abort();
24   if (strncmp ("hello", "hello", 2) != 0)
25     abort();
26   if (strncmp ("hello", "hello", 100) != 0)
27     abort();
28   if (strncmp (s1+10, "d", 100) != 0)
29     abort();
30   if (strncmp (10+s1, "d", 100) != 0)
31     abort();
32   if (strncmp ("d", s1+10, 1) != 0)
33     abort();
34   if (strncmp ("d", 10+s1, 1) != 0)
35     abort();
36   if (strncmp ("hello", "aaaaa", 100) <= 0)
37     abort();
38   if (strncmp ("aaaaa", "hello", 100) >= 0)
39     abort();
40   if (strncmp ("hello", "aaaaa", 1) <= 0)
41     abort();
42   if (strncmp ("aaaaa", "hello", 1) >= 0)
43     abort();
44
45   s2 = s1; s3 = s1+4;
46   if (strncmp (++s2, ++s3, 0) != 0 || s2 != s1+1 || s3 != s1+5)
47     abort();
48   s2 = s1;
49   if (strncmp (++s2, "", 1) <= 0 || s2 != s1+1)
50     abort();
51   if (strncmp ("", ++s2, 1) >= 0 || s2 != s1+2)
52     abort();
53   if (strncmp (++s2, "", 100) <= 0 || s2 != s1+3)
54     abort();
55   if (strncmp ("", ++s2, 100) >= 0 || s2 != s1+4)
56     abort();
57   if (strncmp (++s2+6, "", 100) != 0 || s2 != s1+5)
58     abort();
59   if (strncmp ("", ++s2+5, 100) != 0 || s2 != s1+6)
60     abort();
61   if (strncmp ("ozz", ++s2, 1) != 0 || s2 != s1+7)
62     abort();
63   if (strncmp (++s2, "rzz", 1) != 0 || s2 != s1+8)
64     abort();
65   s2 = s1; s3 = s1+4;
66   if (strncmp (++s2, ++s3+2, 1) >= 0 || s2 != s1+1 || s3 != s1+5)
67     abort();
68 #if defined(__i386__) || defined (__pj__) || defined (__i370__)
69   /* These tests work on platforms which support cmpstrsi.  */
70   s2 = s1;
71   if (strncmp (++s2, "ello", 3) != 0 || s2 != s1+1)
72     abort();
73   s2 = s1;
74   if (strncmp ("ello", ++s2, 3) != 0 || s2 != s1+1)
75     abort();
76   s2 = s1;
77   if (strncmp (++s2, "ello", 4) != 0 || s2 != s1+1)
78     abort();
79   s2 = s1;
80   if (strncmp ("ello", ++s2, 4) != 0 || s2 != s1+1)
81     abort();
82   s2 = s1;
83   if (strncmp (++s2, "ello", 5) <= 0 || s2 != s1+1)
84     abort();
85   s2 = s1;
86   if (strncmp ("ello", ++s2, 5) >= 0 || s2 != s1+1)
87     abort();
88   s2 = s1;
89   if (strncmp (++s2, "ello", 6) <= 0 || s2 != s1+1)
90     abort();
91   s2 = s1;
92   if (strncmp ("ello", ++s2, 6) >= 0 || s2 != s1+1)
93     abort();
94
95   s2 = s1;
96   if (strncmp (++s2, "zllo", 3) >= 0 || s2 != s1+1)
97     abort();
98   s2 = s1;
99   if (strncmp ("zllo", ++s2, 3) <= 0 || s2 != s1+1)
100     abort();
101   s2 = s1;
102   if (strncmp (++s2, "zllo", 4) >= 0 || s2 != s1+1)
103     abort();
104   s2 = s1;
105   if (strncmp ("zllo", ++s2, 4) <= 0 || s2 != s1+1)
106     abort();
107   s2 = s1;
108   if (strncmp (++s2, "zllo", 5) >= 0 || s2 != s1+1)
109     abort();
110   s2 = s1;
111   if (strncmp ("zllo", ++s2, 5) <= 0 || s2 != s1+1)
112     abort();
113   s2 = s1;
114   if (strncmp (++s2, "zllo", 6) >= 0 || s2 != s1+1)
115     abort();
116   s2 = s1;
117   if (strncmp ("zllo", ++s2, 6) <= 0 || s2 != s1+1)
118     abort();
119
120   s2 = s1;
121   if (strncmp (++s2, "allo", 3) <= 0 || s2 != s1+1)
122     abort();
123   s2 = s1;
124   if (strncmp ("allo", ++s2, 3) >= 0 || s2 != s1+1)
125     abort();
126   s2 = s1;
127   if (strncmp (++s2, "allo", 4) <= 0 || s2 != s1+1)
128     abort();
129   s2 = s1;
130   if (strncmp ("allo", ++s2, 4) >= 0 || s2 != s1+1)
131     abort();
132   s2 = s1;
133   if (strncmp (++s2, "allo", 5) <= 0 || s2 != s1+1)
134     abort();
135   s2 = s1;
136   if (strncmp ("allo", ++s2, 5) >= 0 || s2 != s1+1)
137     abort();
138   s2 = s1;
139   if (strncmp (++s2, "allo", 6) <= 0 || s2 != s1+1)
140     abort();
141   s2 = s1;
142   if (strncmp ("allo", ++s2, 6) >= 0 || s2 != s1+1)
143     abort();
144
145   s2 = s1; n = 2; x = 1;
146   if (strncmp (++s2, s1+(x&3), ++n) != 0 || s2 != s1+1 || n != 3)
147     abort();
148   s2 = s1; n = 2; x = 1;
149   if (strncmp (s1+(x&3), ++s2, ++n) != 0 || s2 != s1+1 || n != 3)
150     abort();
151   s2 = s1; n = 3; x = 1;
152   if (strncmp (++s2, s1+(x&3), ++n) != 0 || s2 != s1+1 || n != 4)
153     abort();
154   s2 = s1; n = 3; x = 1;
155   if (strncmp (s1+(x&3), ++s2, ++n) != 0 || s2 != s1+1 || n != 4)
156     abort();
157   s2 = s1; n = 4; x = 1;
158   if (strncmp (++s2, s1+(x&3), ++n) != 0 || s2 != s1+1 || n != 5)
159     abort();
160   s2 = s1; n = 4; x = 1;
161   if (strncmp (s1+(x&3), ++s2, ++n) != 0 || s2 != s1+1 || n != 5)
162     abort();
163   s2 = s1; n = 5; x = 1;
164   if (strncmp (++s2, s1+(x&3), ++n) != 0 || s2 != s1+1 || n != 6)
165     abort();
166   s2 = s1; n = 5; x = 1;
167   if (strncmp (s1+(x&3), ++s2, ++n) != 0 || s2 != s1+1 || n != 6)
168     abort();
169
170   s2 = s1; n = 2;
171   if (strncmp (++s2, "zllo", ++n) >= 0 || s2 != s1+1 || n != 3)
172     abort();
173   s2 = s1; n = 2; x = 1;
174   if (strncmp ("zllo", ++s2, ++n) <= 0 || s2 != s1+1 || n != 3)
175     abort();
176   s2 = s1; n = 3; x = 1;
177   if (strncmp (++s2, "zllo", ++n) >= 0 || s2 != s1+1 || n != 4)
178     abort();
179   s2 = s1; n = 3; x = 1;
180   if (strncmp ("zllo", ++s2, ++n) <= 0 || s2 != s1+1 || n != 4)
181     abort();
182   s2 = s1; n = 4; x = 1;
183   if (strncmp (++s2, "zllo", ++n) >= 0 || s2 != s1+1 || n != 5)
184     abort();
185   s2 = s1; n = 4; x = 1;
186   if (strncmp ("zllo", ++s2, ++n) <= 0 || s2 != s1+1 || n != 5)
187     abort();
188   s2 = s1; n = 5; x = 1;
189   if (strncmp (++s2, "zllo", ++n) >= 0 || s2 != s1+1 || n != 6)
190     abort();
191   s2 = s1; n = 5; x = 1;
192   if (strncmp ("zllo", ++s2, ++n) <= 0 || s2 != s1+1 || n != 6)
193     abort();
194
195   s2 = s1; n = 2;
196   if (strncmp (++s2, "allo", ++n) <= 0 || s2 != s1+1 || n != 3)
197     abort();
198   s2 = s1; n = 2; x = 1;
199   if (strncmp ("allo", ++s2, ++n) >= 0 || s2 != s1+1 || n != 3)
200     abort();
201   s2 = s1; n = 3; x = 1;
202   if (strncmp (++s2, "allo", ++n) <= 0 || s2 != s1+1 || n != 4)
203     abort();
204   s2 = s1; n = 3; x = 1;
205   if (strncmp ("allo", ++s2, ++n) >= 0 || s2 != s1+1 || n != 4)
206     abort();
207   s2 = s1; n = 4; x = 1;
208   if (strncmp (++s2, "allo", ++n) <= 0 || s2 != s1+1 || n != 5)
209     abort();
210   s2 = s1; n = 4; x = 1;
211   if (strncmp ("allo", ++s2, ++n) >= 0 || s2 != s1+1 || n != 5)
212     abort();
213   s2 = s1; n = 5; x = 1;
214   if (strncmp (++s2, "allo", ++n) <= 0 || s2 != s1+1 || n != 6)
215     abort();
216   s2 = s1; n = 5; x = 1;
217   if (strncmp ("allo", ++s2, ++n) >= 0 || s2 != s1+1 || n != 6)
218     abort();
219
220 #endif  
221   
222   /* Test at least one instance of the __builtin_ style.  We do this
223      to ensure that it works and that the prototype is correct.  */
224   if (__builtin_strncmp ("hello", "a", 100) <= 0)
225     abort();
226
227   return 0;
228 }
229
230 #ifdef __OPTIMIZE__
231 /* When optimizing, all the above cases should be transformed into
232    something else.  So any remaining calls to the original function
233    should abort.  */
234 static char *
235 strncmp(const char *s1, const char *s2, size_t n)
236 {
237   abort();
238 }
239 #endif