Smack: add the execute lable to ldconfig
[platform/upstream/glibc.git] / string / test-strncmp.c
1 /* Test and measure strncmp functions.
2    Copyright (C) 1999-2015 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Written by Jakub Jelinek <jakub@redhat.com>, 1999.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #define TEST_MAIN
21 #define TEST_NAME "strncmp"
22 #include "test-string.h"
23
24 typedef int (*proto_t) (const char *, const char *, size_t);
25 int simple_strncmp (const char *, const char *, size_t);
26 int stupid_strncmp (const char *, const char *, size_t);
27
28 IMPL (stupid_strncmp, 0)
29 IMPL (simple_strncmp, 0)
30 IMPL (strncmp, 1)
31
32 int
33 simple_strncmp (const char *s1, const char *s2, size_t n)
34 {
35   int ret = 0;
36
37   while (n-- && (ret = *(unsigned char *) s1 - * (unsigned char *) s2++) == 0
38          && *s1++);
39   return ret;
40 }
41
42 int
43 stupid_strncmp (const char *s1, const char *s2, size_t n)
44 {
45   size_t ns1 = strnlen (s1, n) + 1, ns2 = strnlen (s2, n) + 1;
46   int ret = 0;
47
48   n = ns1 < n ? ns1 : n;
49   n = ns2 < n ? ns2 : n;
50   while (n-- && (ret = *(unsigned char *) s1++ - * (unsigned char *) s2++) == 0);
51   return ret;
52 }
53
54 static int
55 check_result (impl_t *impl, const char *s1, const char *s2, size_t n,
56              int exp_result)
57 {
58   int result = CALL (impl, s1, s2, n);
59   if ((exp_result == 0 && result != 0)
60       || (exp_result < 0 && result >= 0)
61       || (exp_result > 0 && result <= 0))
62     {
63       error (0, 0, "Wrong result in function %s %d %d", impl->name,
64              result, exp_result);
65       ret = 1;
66       return -1;
67     }
68
69   return 0;
70 }
71
72 static void
73 do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
74              int exp_result)
75 {
76   if (check_result (impl, s1, s2, n, exp_result) < 0)
77     return;
78 }
79
80 static void
81 do_test_limit (size_t align1, size_t align2, size_t len, size_t n, int max_char,
82          int exp_result)
83 {
84   size_t i, align_n;
85   char *s1, *s2;
86
87   if (n == 0)
88     {
89       s1 = (char*)(buf1 + page_size);
90       s2 = (char*)(buf2 + page_size);
91
92       FOR_EACH_IMPL (impl, 0)
93         do_one_test (impl, s1, s2, n, 0);
94
95       return;
96     }
97
98   align1 &= 15;
99   align2 &= 15;
100   align_n = (page_size - n) & 15;
101
102   s1 = (char*)(buf1 + page_size - n);
103   s2 = (char*)(buf2 + page_size - n);
104
105   if (align1 < align_n)
106     s1 -= (align_n - align1);
107
108   if (align2 < align_n)
109     s2 -= (align_n - align2);
110
111   for (i = 0; i < n; i++)
112     s1[i] = s2[i] = 1 + 23 * i % max_char;
113
114   if (len < n)
115     {
116       s1[len] = 0;
117       s2[len] = 0;
118       if (exp_result < 0)
119         s2[len] = 32;
120       else if (exp_result > 0)
121         s1[len] = 64;
122     }
123
124   FOR_EACH_IMPL (impl, 0)
125     do_one_test (impl, s1, s2, n, exp_result);
126 }
127
128 static void
129 do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char,
130          int exp_result)
131 {
132   size_t i;
133   char *s1, *s2;
134
135   if (n == 0)
136     return;
137
138   align1 &= 7;
139   if (align1 + n + 1 >= page_size)
140     return;
141
142   align2 &= 7;
143   if (align2 + n + 1 >= page_size)
144     return;
145
146   s1 = (char*)(buf1 + align1);
147   s2 = (char*)(buf2 + align2);
148
149   for (i = 0; i < n; i++)
150     s1[i] = s2[i] = 1 + 23 * i % max_char;
151
152   s1[n] = 24 + exp_result;
153   s2[n] = 23;
154   s1[len] = 0;
155   s2[len] = 0;
156   if (exp_result < 0)
157     s2[len] = 32;
158   else if (exp_result > 0)
159     s1[len] = 64;
160   if (len >= n)
161     s2[n - 1] -= exp_result;
162
163   FOR_EACH_IMPL (impl, 0)
164     do_one_test (impl, (char*)s1, (char*)s2, n, exp_result);
165 }
166
167 static void
168 do_page_test (size_t offset1, size_t offset2, char *s2)
169 {
170   char *s1;
171   int exp_result;
172
173   if (offset1 >= page_size || offset2 >= page_size)
174     return;
175
176   s1 = (char *) (buf1 + offset1);
177   s2 += offset2;
178
179   exp_result= *s1;
180
181   FOR_EACH_IMPL (impl, 0)
182     {
183       check_result (impl, s1, s2, page_size, -exp_result);
184       check_result (impl, s2, s1, page_size, exp_result);
185     }
186 }
187
188 static void
189 do_random_tests (void)
190 {
191   size_t i, j, n, align1, align2, pos, len1, len2, size;
192   int result;
193   long r;
194   unsigned char *p1 = buf1 + page_size - 512;
195   unsigned char *p2 = buf2 + page_size - 512;
196
197   for (n = 0; n < ITERATIONS; n++)
198     {
199       align1 = random () & 31;
200       if (random () & 1)
201         align2 = random () & 31;
202       else
203         align2 = align1 + (random () & 24);
204       pos = random () & 511;
205       size = random () & 511;
206       j = align1 > align2 ? align1 : align2;
207       if (pos + j >= 511)
208         pos = 510 - j - (random () & 7);
209       len1 = random () & 511;
210       if (pos >= len1 && (random () & 1))
211         len1 = pos + (random () & 7);
212       if (len1 + j >= 512)
213         len1 = 511 - j - (random () & 7);
214       if (pos >= len1)
215         len2 = len1;
216       else
217         len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
218       j = (pos > len2 ? pos : len2) + align1 + 64;
219       if (j > 512)
220         j = 512;
221       for (i = 0; i < j; ++i)
222         {
223           p1[i] = random () & 255;
224           if (i < len1 + align1 && !p1[i])
225             {
226               p1[i] = random () & 255;
227               if (!p1[i])
228                 p1[i] = 1 + (random () & 127);
229             }
230         }
231       for (i = 0; i < j; ++i)
232         {
233           p2[i] = random () & 255;
234           if (i < len2 + align2 && !p2[i])
235             {
236               p2[i] = random () & 255;
237               if (!p2[i])
238                 p2[i] = 1 + (random () & 127);
239             }
240         }
241
242       result = 0;
243       memcpy (p2 + align2, p1 + align1, pos);
244       if (pos < len1)
245         {
246           if (p2[align2 + pos] == p1[align1 + pos])
247             {
248               p2[align2 + pos] = random () & 255;
249               if (p2[align2 + pos] == p1[align1 + pos])
250                 p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
251             }
252
253           if (pos < size)
254             {
255               if (p1[align1 + pos] < p2[align2 + pos])
256                 result = -1;
257               else
258                 result = 1;
259             }
260         }
261       p1[len1 + align1] = 0;
262       p2[len2 + align2] = 0;
263
264       FOR_EACH_IMPL (impl, 1)
265         {
266           r = CALL (impl, (char*)(p1 + align1), (char*)(p2 + align2), size);
267           /* Test whether on 64-bit architectures where ABI requires
268              callee to promote has the promotion been done.  */
269           asm ("" : "=g" (r) : "0" (r));
270           if ((r == 0 && result)
271               || (r < 0 && result >= 0)
272               || (r > 0 && result <= 0))
273             {
274               error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
275                      n, impl->name, align1, align2, len1, len2, pos, size, r, result, p1, p2);
276               ret = 1;
277             }
278         }
279     }
280 }
281
282 static void
283 check1 (void)
284 {
285   char *s1 = (char *)(buf1 + 0xb2c);
286   char *s2 = (char *)(buf1 + 0xfd8);
287   size_t i;
288   int exp_result;
289
290   strcpy(s1, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs");
291   strcpy(s2, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV");
292
293   for (i = 0; i < 80; i++)
294     {
295       exp_result = simple_strncmp (s1, s2, i);
296       FOR_EACH_IMPL (impl, 0)
297          check_result (impl, s1, s2, i, exp_result);
298     }
299 }
300
301 static void
302 check2 (void)
303 {
304   size_t i;
305   char *s1, *s2;
306
307   s1 = (char *) buf1;
308   for (i = 0; i < page_size - 1; i++)
309     s1[i] = 23;
310   s1[i] = 0;
311
312   s2 = strdup (s1);
313
314   for (i = 0; i < 64; ++i)
315     do_page_test (3990 + i, 2635, s2);
316
317   free (s2);
318 }
319
320 int
321 test_main (void)
322 {
323   size_t i;
324
325   test_init ();
326
327   check1 ();
328   check2 ();
329
330   printf ("%23s", "");
331   FOR_EACH_IMPL (impl, 0)
332     printf ("\t%s", impl->name);
333   putchar ('\n');
334
335   for (i =0; i < 16; ++i)
336     {
337       do_test (0, 0, 8, i, 127, 0);
338       do_test (0, 0, 8, i, 127, -1);
339       do_test (0, 0, 8, i, 127, 1);
340       do_test (i, i, 8, i, 127, 0);
341       do_test (i, i, 8, i, 127, 1);
342       do_test (i, i, 8, i, 127, -1);
343       do_test (i, 2 * i, 8, i, 127, 0);
344       do_test (2 * i, i, 8, i, 127, 1);
345       do_test (i, 3 * i, 8, i, 127, -1);
346       do_test (0, 0, 8, i, 255, 0);
347       do_test (0, 0, 8, i, 255, -1);
348       do_test (0, 0, 8, i, 255, 1);
349       do_test (i, i, 8, i, 255, 0);
350       do_test (i, i, 8, i, 255, 1);
351       do_test (i, i, 8, i, 255, -1);
352       do_test (i, 2 * i, 8, i, 255, 0);
353       do_test (2 * i, i, 8, i, 255, 1);
354       do_test (i, 3 * i, 8, i, 255, -1);
355     }
356
357   for (i = 1; i < 8; ++i)
358     {
359       do_test (0, 0, 8 << i, 16 << i, 127, 0);
360       do_test (0, 0, 8 << i, 16 << i, 127, 1);
361       do_test (0, 0, 8 << i, 16 << i, 127, -1);
362       do_test (0, 0, 8 << i, 16 << i, 255, 0);
363       do_test (0, 0, 8 << i, 16 << i, 255, 1);
364       do_test (0, 0, 8 << i, 16 << i, 255, -1);
365       do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 0);
366       do_test (8 - i, 2 * i, 8 << i, 16 << i, 127, 1);
367       do_test (2 * i, i, 8 << i, 16 << i, 255, 0);
368       do_test (2 * i, i, 8 << i, 16 << i, 255, 1);
369     }
370
371   do_test_limit (0, 0, 0, 0, 127, 0);
372   do_test_limit (4, 0, 21, 20, 127, 0);
373   do_test_limit (0, 4, 21, 20, 127, 0);
374   do_test_limit (8, 0, 25, 24, 127, 0);
375   do_test_limit (0, 8, 25, 24, 127, 0);
376
377   for (i = 0; i < 8; ++i)
378     {
379       do_test_limit (0, 0, 17 - i, 16 - i, 127, 0);
380       do_test_limit (0, 0, 17 - i, 16 - i, 255, 0);
381       do_test_limit (0, 0, 15 - i, 16 - i, 127, 0);
382       do_test_limit (0, 0, 15 - i, 16 - i, 127, 1);
383       do_test_limit (0, 0, 15 - i, 16 - i, 127, -1);
384       do_test_limit (0, 0, 15 - i, 16 - i, 255, 0);
385       do_test_limit (0, 0, 15 - i, 16 - i, 255, 1);
386       do_test_limit (0, 0, 15 - i, 16 - i, 255, -1);
387     }
388
389   do_random_tests ();
390   return ret;
391 }
392
393 #include "../test-skeleton.c"