test-strncasecmp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* Test and measure strncasecmp functions.
  2. Copyright (C) 1999-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Written by Jakub Jelinek <jakub@redhat.com>, 1999.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <locale.h>
  17. #include <ctype.h>
  18. #define TEST_MAIN
  19. #define TEST_NAME "strncasecmp"
  20. #include "test-string.h"
  21. typedef int (*proto_t) (const char *, const char *, size_t);
  22. static int simple_strncasecmp (const char *, const char *, size_t);
  23. static int stupid_strncasecmp (const char *, const char *, size_t);
  24. IMPL (stupid_strncasecmp, 0)
  25. IMPL (simple_strncasecmp, 0)
  26. IMPL (strncasecmp, 1)
  27. static int
  28. simple_strncasecmp (const char *s1, const char *s2, size_t n)
  29. {
  30. int ret;
  31. if (n == 0)
  32. return 0;
  33. while ((ret = ((unsigned char) tolower (*s1)
  34. - (unsigned char) tolower (*s2))) == 0
  35. && *s1++)
  36. {
  37. if (--n == 0)
  38. return 0;
  39. ++s2;
  40. }
  41. return ret;
  42. }
  43. static int
  44. stupid_strncasecmp (const char *s1, const char *s2, size_t max)
  45. {
  46. size_t ns1 = strlen (s1) + 1;
  47. size_t ns2 = strlen (s2) + 1;
  48. size_t n = ns1 < ns2 ? ns1 : ns2;
  49. if (n > max)
  50. n = max;
  51. int ret = 0;
  52. while (n--)
  53. {
  54. if ((ret = ((unsigned char) tolower (*s1)
  55. - (unsigned char) tolower (*s2))) != 0)
  56. break;
  57. ++s1;
  58. ++s2;
  59. }
  60. return ret;
  61. }
  62. static int
  63. check_result (impl_t *impl, const char *s1, const char *s2, size_t n,
  64. int exp_result)
  65. {
  66. int result = CALL (impl, s1, s2, n);
  67. if ((exp_result == 0 && result != 0)
  68. || (exp_result < 0 && result >= 0)
  69. || (exp_result > 0 && result <= 0))
  70. {
  71. error (0, 0, "Wrong result in function %s %d %d", impl->name,
  72. result, exp_result);
  73. ret = 1;
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. static void
  79. do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
  80. int exp_result)
  81. {
  82. if (check_result (impl, s1, s2, n, exp_result) < 0)
  83. return;
  84. }
  85. static void
  86. do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
  87. int exp_result)
  88. {
  89. size_t i;
  90. char *s1, *s2;
  91. if (len == 0)
  92. return;
  93. align1 &= 7;
  94. if (align1 + len + 1 >= page_size)
  95. return;
  96. align2 &= 7;
  97. if (align2 + len + 1 >= page_size)
  98. return;
  99. s1 = (char *) (buf1 + align1);
  100. s2 = (char *) (buf2 + align2);
  101. for (i = 0; i < len; i++)
  102. {
  103. s1[i] = toupper (1 + 23 * i % max_char);
  104. s2[i] = tolower (s1[i]);
  105. }
  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;
  114. FOR_EACH_IMPL (impl, 0)
  115. do_one_test (impl, s1, s2, n, exp_result);
  116. }
  117. static void
  118. do_random_tests (void)
  119. {
  120. size_t i, j, n, align1, align2, pos, len1, len2;
  121. int result;
  122. long r;
  123. unsigned char *p1 = buf1 + page_size - 512;
  124. unsigned char *p2 = buf2 + page_size - 512;
  125. for (n = 0; n < ITERATIONS; n++)
  126. {
  127. align1 = random () & 31;
  128. if (random () & 1)
  129. align2 = random () & 31;
  130. else
  131. align2 = align1 + (random () & 24);
  132. pos = random () & 511;
  133. j = align1 > align2 ? align1 : align2;
  134. if (pos + j >= 511)
  135. pos = 510 - j - (random () & 7);
  136. len1 = random () & 511;
  137. if (pos >= len1 && (random () & 1))
  138. len1 = pos + (random () & 7);
  139. if (len1 + j >= 512)
  140. len1 = 511 - j - (random () & 7);
  141. if (pos >= len1)
  142. len2 = len1;
  143. else
  144. len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
  145. j = (pos > len2 ? pos : len2) + align1 + 64;
  146. if (j > 512)
  147. j = 512;
  148. for (i = 0; i < j; ++i)
  149. {
  150. p1[i] = tolower (random () & 255);
  151. if (i < len1 + align1 && !p1[i])
  152. {
  153. p1[i] = tolower (random () & 255);
  154. if (!p1[i])
  155. p1[i] = tolower (1 + (random () & 127));
  156. }
  157. }
  158. for (i = 0; i < j; ++i)
  159. {
  160. p2[i] = toupper (random () & 255);
  161. if (i < len2 + align2 && !p2[i])
  162. {
  163. p2[i] = toupper (random () & 255);
  164. if (!p2[i])
  165. toupper (p2[i] = 1 + (random () & 127));
  166. }
  167. }
  168. result = 0;
  169. memcpy (p2 + align2, p1 + align1, pos);
  170. if (pos < len1)
  171. {
  172. if (tolower (p2[align2 + pos]) == p1[align1 + pos])
  173. {
  174. p2[align2 + pos] = toupper (random () & 255);
  175. if (tolower (p2[align2 + pos]) == p1[align1 + pos])
  176. p2[align2 + pos] = toupper (p1[align1 + pos]
  177. + 3 + (random () & 127));
  178. }
  179. if (p1[align1 + pos] < tolower (p2[align2 + pos]))
  180. result = -1;
  181. else
  182. result = 1;
  183. }
  184. p1[len1 + align1] = 0;
  185. p2[len2 + align2] = 0;
  186. FOR_EACH_IMPL (impl, 1)
  187. {
  188. r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2),
  189. pos + 1 + (random () & 255));
  190. /* Test whether on 64-bit architectures where ABI requires
  191. callee to promote has the promotion been done. */
  192. asm ("" : "=g" (r) : "0" (r));
  193. if ((r == 0 && result)
  194. || (r < 0 && result >= 0)
  195. || (r > 0 && result <= 0))
  196. {
  197. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
  198. n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
  199. ret = 1;
  200. }
  201. }
  202. }
  203. }
  204. /* Regression test for BZ #12205 */
  205. static void
  206. bz12205 (void)
  207. {
  208. static char cp [4096+16] __attribute__ ((aligned(4096)));
  209. static char gotrel[4096] __attribute__ ((aligned(4096)));
  210. char *s1 = cp + 0xffa;
  211. char *s2 = gotrel + 0xcbe;
  212. int exp_result;
  213. size_t n = 6;
  214. strcpy (s1, "gottpoff");
  215. strcpy (s2, "GOTPLT");
  216. exp_result = simple_strncasecmp (s1, s2, n);
  217. FOR_EACH_IMPL (impl, 0)
  218. check_result (impl, s1, s2, n, exp_result);
  219. }
  220. /* Regression test for BZ #14195 */
  221. static void
  222. bz14195 (void)
  223. {
  224. const char *empty_string = "";
  225. FOR_EACH_IMPL (impl, 0)
  226. check_result (impl, empty_string, "", 5, 0);
  227. }
  228. static void
  229. test_locale (const char *locale)
  230. {
  231. size_t i;
  232. if (setlocale (LC_CTYPE, locale) == NULL)
  233. {
  234. error (0, 0, "cannot set locale \"%s\"", locale);
  235. ret = 1;
  236. }
  237. bz12205 ();
  238. bz14195 ();
  239. printf ("%23s", locale);
  240. FOR_EACH_IMPL (impl, 0)
  241. printf ("\t%s", impl->name);
  242. putchar ('\n');
  243. for (i = 1; i < 16; ++i)
  244. {
  245. do_test (i, i, i - 1, i, 127, 0);
  246. do_test (i, i, i, i, 127, 0);
  247. do_test (i, i, i, i, 127, 1);
  248. do_test (i, i, i, i, 127, -1);
  249. do_test (i, i, i + 1, i, 127, 0);
  250. do_test (i, i, i + 1, i, 127, 1);
  251. do_test (i, i, i + 1, i, 127, -1);
  252. }
  253. for (i = 1; i < 10; ++i)
  254. {
  255. do_test (0, 0, (2 << i) - 1, 2 << i, 127, 0);
  256. do_test (0, 0, 2 << i, 2 << i, 254, 0);
  257. do_test (0, 0, (2 << i) + 1, 2 << i, 127, 0);
  258. do_test (0, 0, (2 << i) + 1, 2 << i, 254, 0);
  259. do_test (0, 0, 2 << i, 2 << i, 127, 1);
  260. do_test (0, 0, (2 << i) + 10, 2 << i, 127, 1);
  261. do_test (0, 0, 2 << i, 2 << i, 254, 1);
  262. do_test (0, 0, (2 << i) + 10, 2 << i, 254, 1);
  263. do_test (0, 0, 2 << i, 2 << i, 127, -1);
  264. do_test (0, 0, (2 << i) + 10, 2 << i, 127, -1);
  265. do_test (0, 0, 2 << i, 2 << i, 254, -1);
  266. do_test (0, 0, (2 << i) + 10, 2 << i, 254, -1);
  267. }
  268. for (i = 1; i < 8; ++i)
  269. {
  270. do_test (i, 2 * i, (8 << i) - 1, 8 << i, 127, 0);
  271. do_test (i, 2 * i, 8 << i, 8 << i, 127, 0);
  272. do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 0);
  273. do_test (2 * i, i, (8 << i) - 1, 8 << i, 254, 0);
  274. do_test (2 * i, i, 8 << i, 8 << i, 254, 0);
  275. do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 0);
  276. do_test (i, 2 * i, 8 << i, 8 << i, 127, 1);
  277. do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 1);
  278. do_test (2 * i, i, 8 << i, 8 << i, 254, 1);
  279. do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 1);
  280. do_test (i, 2 * i, 8 << i, 8 << i, 127, -1);
  281. do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, -1);
  282. do_test (2 * i, i, 8 << i, 8 << i, 254, -1);
  283. do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, -1);
  284. }
  285. do_random_tests ();
  286. }
  287. int
  288. test_main (void)
  289. {
  290. test_init ();
  291. test_locale ("C");
  292. test_locale ("en_US.ISO-8859-1");
  293. test_locale ("en_US.UTF-8");
  294. test_locale ("tr_TR.ISO-8859-9");
  295. test_locale ("tr_TR.UTF-8");
  296. return ret;
  297. }
  298. #include <support/test-driver.c>