test-strcasecmp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Test and measure strcasecmp 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 "strcasecmp"
  20. #include "test-string.h"
  21. typedef int (*proto_t) (const char *, const char *);
  22. static int simple_strcasecmp (const char *, const char *);
  23. static int stupid_strcasecmp (const char *, const char *);
  24. IMPL (stupid_strcasecmp, 0)
  25. IMPL (simple_strcasecmp, 0)
  26. IMPL (strcasecmp, 1)
  27. static int
  28. simple_strcasecmp (const char *s1, const char *s2)
  29. {
  30. int ret;
  31. while ((ret = ((unsigned char) tolower (*s1)
  32. - (unsigned char) tolower (*s2))) == 0
  33. && *s1++)
  34. ++s2;
  35. return ret;
  36. }
  37. static int
  38. stupid_strcasecmp (const char *s1, const char *s2)
  39. {
  40. size_t ns1 = strlen (s1) + 1, ns2 = strlen (s2) + 1;
  41. size_t n = ns1 < ns2 ? ns1 : ns2;
  42. int ret = 0;
  43. while (n--)
  44. {
  45. if ((ret = ((unsigned char) tolower (*s1)
  46. - (unsigned char) tolower (*s2))) != 0)
  47. break;
  48. ++s1;
  49. ++s2;
  50. }
  51. return ret;
  52. }
  53. static void
  54. do_one_test (impl_t *impl, const char *s1, const char *s2, int exp_result)
  55. {
  56. int result = CALL (impl, s1, s2);
  57. if ((exp_result == 0 && result != 0)
  58. || (exp_result < 0 && result >= 0)
  59. || (exp_result > 0 && result <= 0))
  60. {
  61. error (0, 0, "Wrong result in function %s %d %d", impl->name,
  62. result, exp_result);
  63. ret = 1;
  64. return;
  65. }
  66. }
  67. static void
  68. do_test (size_t align1, size_t align2, size_t len, int max_char,
  69. int exp_result)
  70. {
  71. size_t i;
  72. char *s1, *s2;
  73. if (len == 0)
  74. return;
  75. align1 &= 7;
  76. if (align1 + len + 1 >= page_size)
  77. return;
  78. align2 &= 7;
  79. if (align2 + len + 1 >= page_size)
  80. return;
  81. s1 = (char *) (buf1 + align1);
  82. s2 = (char *) (buf2 + align2);
  83. for (i = 0; i < len; i++)
  84. {
  85. s1[i] = toupper (1 + 23 * i % max_char);
  86. s2[i] = tolower (s1[i]);
  87. }
  88. s1[len] = s2[len] = 0;
  89. s1[len + 1] = 23;
  90. s2[len + 1] = 24 + exp_result;
  91. if ((s2[len - 1] == 'z' && exp_result == -1)
  92. || (s2[len - 1] == 'a' && exp_result == 1))
  93. s1[len - 1] += exp_result;
  94. else
  95. s2[len - 1] -= exp_result;
  96. FOR_EACH_IMPL (impl, 0)
  97. do_one_test (impl, s1, s2, exp_result);
  98. }
  99. static void
  100. do_random_tests (void)
  101. {
  102. size_t i, j, n, align1, align2, pos, len1, len2;
  103. int result;
  104. long r;
  105. unsigned char *p1 = buf1 + page_size - 512;
  106. unsigned char *p2 = buf2 + page_size - 512;
  107. for (n = 0; n < ITERATIONS; n++)
  108. {
  109. align1 = random () & 31;
  110. if (random () & 1)
  111. align2 = random () & 31;
  112. else
  113. align2 = align1 + (random () & 24);
  114. pos = random () & 511;
  115. j = align1 > align2 ? align1 : align2;
  116. if (pos + j >= 511)
  117. pos = 510 - j - (random () & 7);
  118. len1 = random () & 511;
  119. if (pos >= len1 && (random () & 1))
  120. len1 = pos + (random () & 7);
  121. if (len1 + j >= 512)
  122. len1 = 511 - j - (random () & 7);
  123. if (pos >= len1)
  124. len2 = len1;
  125. else
  126. len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
  127. j = (pos > len2 ? pos : len2) + align1 + 64;
  128. if (j > 512)
  129. j = 512;
  130. for (i = 0; i < j; ++i)
  131. {
  132. p1[i] = tolower (random () & 255);
  133. if (i < len1 + align1 && !p1[i])
  134. {
  135. p1[i] = tolower (random () & 255);
  136. if (!p1[i])
  137. p1[i] = tolower (1 + (random () & 127));
  138. }
  139. }
  140. for (i = 0; i < j; ++i)
  141. {
  142. p2[i] = toupper (random () & 255);
  143. if (i < len2 + align2 && !p2[i])
  144. {
  145. p2[i] = toupper (random () & 255);
  146. if (!p2[i])
  147. toupper (p2[i] = 1 + (random () & 127));
  148. }
  149. }
  150. result = 0;
  151. memcpy (p2 + align2, p1 + align1, pos);
  152. if (pos < len1)
  153. {
  154. if (tolower (p2[align2 + pos]) == p1[align1 + pos])
  155. {
  156. p2[align2 + pos] = toupper (random () & 255);
  157. if (tolower (p2[align2 + pos]) == p1[align1 + pos])
  158. p2[align2 + pos] = toupper (p1[align1 + pos]
  159. + 3 + (random () & 127));
  160. }
  161. if (p1[align1 + pos] < tolower (p2[align2 + pos]))
  162. result = -1;
  163. else
  164. result = 1;
  165. }
  166. p1[len1 + align1] = 0;
  167. p2[len2 + align2] = 0;
  168. FOR_EACH_IMPL (impl, 1)
  169. {
  170. r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2));
  171. /* Test whether on 64-bit architectures where ABI requires
  172. callee to promote has the promotion been done. */
  173. asm ("" : "=g" (r) : "0" (r));
  174. if ((r == 0 && result)
  175. || (r < 0 && result >= 0)
  176. || (r > 0 && result <= 0))
  177. {
  178. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
  179. n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
  180. ret = 1;
  181. }
  182. }
  183. }
  184. }
  185. static void
  186. test_locale (const char *locale)
  187. {
  188. size_t i;
  189. if (setlocale (LC_CTYPE, locale) == NULL)
  190. {
  191. error (0, 0, "cannot set locale \"%s\"", locale);
  192. ret = 1;
  193. }
  194. printf ("%-23s", locale);
  195. FOR_EACH_IMPL (impl, 0)
  196. printf ("\t%s", impl->name);
  197. putchar ('\n');
  198. for (i = 1; i < 16; ++i)
  199. {
  200. do_test (i, i, i, 127, 0);
  201. do_test (i, i, i, 127, 1);
  202. do_test (i, i, i, 127, -1);
  203. }
  204. for (i = 1; i < 10; ++i)
  205. {
  206. do_test (0, 0, 2 << i, 127, 0);
  207. do_test (0, 0, 2 << i, 254, 0);
  208. do_test (0, 0, 2 << i, 127, 1);
  209. do_test (0, 0, 2 << i, 254, 1);
  210. do_test (0, 0, 2 << i, 127, -1);
  211. do_test (0, 0, 2 << i, 254, -1);
  212. }
  213. for (i = 1; i < 8; ++i)
  214. {
  215. do_test (i, 2 * i, 8 << i, 127, 0);
  216. do_test (2 * i, i, 8 << i, 254, 0);
  217. do_test (i, 2 * i, 8 << i, 127, 1);
  218. do_test (2 * i, i, 8 << i, 254, 1);
  219. do_test (i, 2 * i, 8 << i, 127, -1);
  220. do_test (2 * i, i, 8 << i, 254, -1);
  221. }
  222. do_random_tests ();
  223. }
  224. int
  225. test_main (void)
  226. {
  227. test_init ();
  228. test_locale ("C");
  229. test_locale ("en_US.ISO-8859-1");
  230. test_locale ("en_US.UTF-8");
  231. test_locale ("tr_TR.ISO-8859-9");
  232. test_locale ("tr_TR.UTF-8");
  233. return ret;
  234. }
  235. #include <support/test-driver.c>