Properly fix memory leak in _nss_dns_gethostbyname4_r with big DNS answer
[glibc.git] / string / test-strcasecmp.c
blob11bc65b0c62100aedcc480d5f3fe584cfce60904
1 /* Test and measure strcasecmp functions.
2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Jakub Jelinek <jakub@redhat.com>, 1999.
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.
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.
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/>. */
20 #include <locale.h>
21 #include <ctype.h>
22 #define TEST_MAIN
23 #define TEST_NAME "strcasecmp"
24 #include "test-string.h"
26 typedef int (*proto_t) (const char *, const char *);
27 static int simple_strcasecmp (const char *, const char *);
28 static int stupid_strcasecmp (const char *, const char *);
30 IMPL (stupid_strcasecmp, 0)
31 IMPL (simple_strcasecmp, 0)
32 IMPL (strcasecmp, 1)
34 static int
35 simple_strcasecmp (const char *s1, const char *s2)
37 int ret;
39 while ((ret = ((unsigned char) tolower (*s1)
40 - (unsigned char) tolower (*s2))) == 0
41 && *s1++)
42 ++s2;
43 return ret;
46 static int
47 stupid_strcasecmp (const char *s1, const char *s2)
49 size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
50 size_t n = ns1 < ns2 ? ns1 : ns2;
51 int ret = 0;
53 while (n--)
55 if ((ret = ((unsigned char) tolower (*s1)
56 - (unsigned char) tolower (*s2))) != 0)
57 break;
58 ++s1;
59 ++s2;
61 return ret;
64 static void
65 do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
67 int result = CALL (impl, s1, s2);
68 if ((exp_result == 0 && result != 0)
69 || (exp_result < 0 && result >= 0)
70 || (exp_result > 0 && result <= 0))
72 error (0, 0, "Wrong result in function %s %d %d", impl->name,
73 result, exp_result);
74 ret = 1;
75 return;
79 static void
80 do_test (size_t align1, size_t align2, size_t len, int max_char,
81 int exp_result)
83 size_t i;
84 char *s1, *s2;
86 if (len == 0)
87 return;
89 align1 &= 7;
90 if (align1 + len + 1 >= page_size)
91 return;
93 align2 &= 7;
94 if (align2 + len + 1 >= page_size)
95 return;
97 s1 = (char *) (buf1 + align1);
98 s2 = (char *) (buf2 + align2);
100 for (i = 0; i < len; i++)
102 s1[i] = toupper (1 + 23 * i % max_char);
103 s2[i] = tolower (s1[i]);
106 s1[len] = s2[len] = 0;
107 s1[len + 1] = 23;
108 s2[len + 1] = 24 + exp_result;
109 if ((s2[len - 1] == 'z' && exp_result == -1)
110 || (s2[len - 1] == 'a' && exp_result == 1))
111 s1[len - 1] += exp_result;
112 else
113 s2[len - 1] -= exp_result;
115 FOR_EACH_IMPL (impl, 0)
116 do_one_test (impl, s1, s2, exp_result);
119 static void
120 do_random_tests (void)
122 size_t i, j, n, align1, align2, pos, len1, len2;
123 int result;
124 long r;
125 unsigned char *p1 = buf1 + page_size - 512;
126 unsigned char *p2 = buf2 + page_size - 512;
128 for (n = 0; n < ITERATIONS; n++)
130 align1 = random () & 31;
131 if (random () & 1)
132 align2 = random () & 31;
133 else
134 align2 = align1 + (random () & 24);
135 pos = random () & 511;
136 j = align1 > align2 ? align1 : align2;
137 if (pos + j >= 511)
138 pos = 510 - j - (random () & 7);
139 len1 = random () & 511;
140 if (pos >= len1 && (random () & 1))
141 len1 = pos + (random () & 7);
142 if (len1 + j >= 512)
143 len1 = 511 - j - (random () & 7);
144 if (pos >= len1)
145 len2 = len1;
146 else
147 len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
148 j = (pos > len2 ? pos : len2) + align1 + 64;
149 if (j > 512)
150 j = 512;
151 for (i = 0; i < j; ++i)
153 p1[i] = tolower (random () & 255);
154 if (i < len1 + align1 && !p1[i])
156 p1[i] = tolower (random () & 255);
157 if (!p1[i])
158 p1[i] = tolower (1 + (random () & 127));
161 for (i = 0; i < j; ++i)
163 p2[i] = toupper (random () & 255);
164 if (i < len2 + align2 && !p2[i])
166 p2[i] = toupper (random () & 255);
167 if (!p2[i])
168 toupper (p2[i] = 1 + (random () & 127));
172 result = 0;
173 memcpy (p2 + align2, p1 + align1, pos);
174 if (pos < len1)
176 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
178 p2[align2 + pos] = toupper (random () & 255);
179 if (tolower (p2[align2 + pos]) == p1[align1 + pos])
180 p2[align2 + pos] = toupper (p1[align1 + pos]
181 + 3 + (random () & 127));
184 if (p1[align1 + pos] < tolower (p2[align2 + pos]))
185 result = -1;
186 else
187 result = 1;
189 p1[len1 + align1] = 0;
190 p2[len2 + align2] = 0;
192 FOR_EACH_IMPL (impl, 1)
194 r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2));
195 /* Test whether on 64-bit architectures where ABI requires
196 callee to promote has the promotion been done. */
197 asm ("" : "=g" (r) : "0" (r));
198 if ((r == 0 && result)
199 || (r < 0 && result >= 0)
200 || (r > 0 && result <= 0))
202 error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
203 n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
204 ret = 1;
210 static void
211 test_locale (const char *locale)
213 size_t i;
215 if (setlocale (LC_CTYPE, locale) == NULL)
217 error (0, 0, "cannot set locale \"%s\"", locale);
218 ret = 1;
221 printf ("%-23s", locale);
222 FOR_EACH_IMPL (impl, 0)
223 printf ("\t%s", impl->name);
224 putchar ('\n');
226 for (i = 1; i < 16; ++i)
228 do_test (i, i, i, 127, 0);
229 do_test (i, i, i, 127, 1);
230 do_test (i, i, i, 127, -1);
233 for (i = 1; i < 10; ++i)
235 do_test (0, 0, 2 << i, 127, 0);
236 do_test (0, 0, 2 << i, 254, 0);
237 do_test (0, 0, 2 << i, 127, 1);
238 do_test (0, 0, 2 << i, 254, 1);
239 do_test (0, 0, 2 << i, 127, -1);
240 do_test (0, 0, 2 << i, 254, -1);
243 for (i = 1; i < 8; ++i)
245 do_test (i, 2 * i, 8 << i, 127, 0);
246 do_test (2 * i, i, 8 << i, 254, 0);
247 do_test (i, 2 * i, 8 << i, 127, 1);
248 do_test (2 * i, i, 8 << i, 254, 1);
249 do_test (i, 2 * i, 8 << i, 127, -1);
250 do_test (2 * i, i, 8 << i, 254, -1);
253 do_random_tests ();
257 test_main (void)
259 test_init ();
261 test_locale ("C");
262 test_locale ("en_US.ISO-8859-1");
263 test_locale ("en_US.UTF-8");
264 test_locale ("tr_TR.ISO-8859-9");
265 test_locale ("tr_TR.UTF-8");
267 return ret;
270 #include "../test-skeleton.c"