PageRenderTime 79ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/string/test-strncasecmp.c

https://bitbucket.org/Instigo/glibc
C | 358 lines | 282 code | 54 blank | 22 comment | 70 complexity | 34a05b01b3c8e320bac5a8bbe1b3730f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. /* Test and measure strncasecmp functions.
  2. Copyright (C) 1999-2012 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 <ctype.h>
  17. #define TEST_MAIN
  18. #include "test-string.h"
  19. typedef int (*proto_t) (const char *, const char *, size_t);
  20. static int simple_strncasecmp (const char *, const char *, size_t);
  21. static int stupid_strncasecmp (const char *, const char *, size_t);
  22. IMPL (stupid_strncasecmp, 0)
  23. IMPL (simple_strncasecmp, 0)
  24. IMPL (strncasecmp, 1)
  25. static int
  26. simple_strncasecmp (const char *s1, const char *s2, size_t n)
  27. {
  28. int ret;
  29. if (n == 0)
  30. return 0;
  31. while ((ret = ((unsigned char) tolower (*s1)
  32. - (unsigned char) tolower (*s2))) == 0
  33. && *s1++)
  34. {
  35. if (--n == 0)
  36. return 0;
  37. ++s2;
  38. }
  39. return ret;
  40. }
  41. static int
  42. stupid_strncasecmp (const char *s1, const char *s2, size_t max)
  43. {
  44. size_t ns1 = strlen (s1) + 1;
  45. size_t ns2 = strlen (s2) + 1;
  46. size_t n = ns1 < ns2 ? ns1 : ns2;
  47. if (n > max)
  48. n = max;
  49. int ret = 0;
  50. while (n--)
  51. {
  52. if ((ret = ((unsigned char) tolower (*s1)
  53. - (unsigned char) tolower (*s2))) != 0)
  54. break;
  55. ++s1;
  56. ++s2;
  57. }
  58. return ret;
  59. }
  60. static int
  61. check_result (impl_t *impl, const char *s1, const char *s2, size_t n,
  62. int exp_result)
  63. {
  64. int result = CALL (impl, s1, s2, n);
  65. if ((exp_result == 0 && result != 0)
  66. || (exp_result < 0 && result >= 0)
  67. || (exp_result > 0 && result <= 0))
  68. {
  69. error (0, 0, "Wrong result in function %s %d %d", impl->name,
  70. result, exp_result);
  71. ret = 1;
  72. return -1;
  73. }
  74. return 0;
  75. }
  76. static void
  77. do_one_test (impl_t *impl, const char *s1, const char *s2, size_t n,
  78. int exp_result)
  79. {
  80. if (check_result (impl, s1, s2, n, exp_result) < 0)
  81. return;
  82. if (HP_TIMING_AVAIL)
  83. {
  84. hp_timing_t start __attribute ((unused));
  85. hp_timing_t stop __attribute ((unused));
  86. hp_timing_t best_time = ~ (hp_timing_t) 0;
  87. size_t i;
  88. for (i = 0; i < 32; ++i)
  89. {
  90. HP_TIMING_NOW (start);
  91. CALL (impl, s1, s2, n);
  92. HP_TIMING_NOW (stop);
  93. HP_TIMING_BEST (best_time, start, stop);
  94. }
  95. printf ("\t%zd", (size_t) best_time);
  96. }
  97. }
  98. static void
  99. do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
  100. int exp_result)
  101. {
  102. size_t i;
  103. char *s1, *s2;
  104. if (len == 0)
  105. return;
  106. align1 &= 7;
  107. if (align1 + len + 1 >= page_size)
  108. return;
  109. align2 &= 7;
  110. if (align2 + len + 1 >= page_size)
  111. return;
  112. s1 = (char *) (buf1 + align1);
  113. s2 = (char *) (buf2 + align2);
  114. for (i = 0; i < len; i++)
  115. {
  116. s1[i] = toupper (1 + 23 * i % max_char);
  117. s2[i] = tolower (s1[i]);
  118. }
  119. s1[len] = s2[len] = 0;
  120. s1[len + 1] = 23;
  121. s2[len + 1] = 24 + exp_result;
  122. if ((s2[len - 1] == 'z' && exp_result == -1)
  123. || (s2[len - 1] == 'a' && exp_result == 1))
  124. s1[len - 1] += exp_result;
  125. else
  126. s2[len - 1] -= exp_result;
  127. if (HP_TIMING_AVAIL)
  128. printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
  129. FOR_EACH_IMPL (impl, 0)
  130. do_one_test (impl, s1, s2, n, exp_result);
  131. if (HP_TIMING_AVAIL)
  132. putchar ('\n');
  133. }
  134. static void
  135. do_random_tests (void)
  136. {
  137. size_t i, j, n, align1, align2, pos, len1, len2;
  138. int result;
  139. long r;
  140. unsigned char *p1 = buf1 + page_size - 512;
  141. unsigned char *p2 = buf2 + page_size - 512;
  142. for (n = 0; n < ITERATIONS; n++)
  143. {
  144. align1 = random () & 31;
  145. if (random () & 1)
  146. align2 = random () & 31;
  147. else
  148. align2 = align1 + (random () & 24);
  149. pos = random () & 511;
  150. j = align1 > align2 ? align1 : align2;
  151. if (pos + j >= 511)
  152. pos = 510 - j - (random () & 7);
  153. len1 = random () & 511;
  154. if (pos >= len1 && (random () & 1))
  155. len1 = pos + (random () & 7);
  156. if (len1 + j >= 512)
  157. len1 = 511 - j - (random () & 7);
  158. if (pos >= len1)
  159. len2 = len1;
  160. else
  161. len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
  162. j = (pos > len2 ? pos : len2) + align1 + 64;
  163. if (j > 512)
  164. j = 512;
  165. for (i = 0; i < j; ++i)
  166. {
  167. p1[i] = tolower (random () & 255);
  168. if (i < len1 + align1 && !p1[i])
  169. {
  170. p1[i] = tolower (random () & 255);
  171. if (!p1[i])
  172. p1[i] = tolower (1 + (random () & 127));
  173. }
  174. }
  175. for (i = 0; i < j; ++i)
  176. {
  177. p2[i] = toupper (random () & 255);
  178. if (i < len2 + align2 && !p2[i])
  179. {
  180. p2[i] = toupper (random () & 255);
  181. if (!p2[i])
  182. toupper (p2[i] = 1 + (random () & 127));
  183. }
  184. }
  185. result = 0;
  186. memcpy (p2 + align2, p1 + align1, pos);
  187. if (pos < len1)
  188. {
  189. if (tolower (p2[align2 + pos]) == p1[align1 + pos])
  190. {
  191. p2[align2 + pos] = toupper (random () & 255);
  192. if (tolower (p2[align2 + pos]) == p1[align1 + pos])
  193. p2[align2 + pos] = toupper (p1[align1 + pos]
  194. + 3 + (random () & 127));
  195. }
  196. if (p1[align1 + pos] < tolower (p2[align2 + pos]))
  197. result = -1;
  198. else
  199. result = 1;
  200. }
  201. p1[len1 + align1] = 0;
  202. p2[len2 + align2] = 0;
  203. FOR_EACH_IMPL (impl, 1)
  204. {
  205. r = CALL (impl, (char *) (p1 + align1), (char *) (p2 + align2),
  206. pos + 1 + (random () & 255));
  207. /* Test whether on 64-bit architectures where ABI requires
  208. callee to promote has the promotion been done. */
  209. asm ("" : "=g" (r) : "0" (r));
  210. if ((r == 0 && result)
  211. || (r < 0 && result >= 0)
  212. || (r > 0 && result <= 0))
  213. {
  214. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd, %zd, %zd) %ld != %d, p1 %p p2 %p",
  215. n, impl->name, align1, align2, len1, len2, pos, r, result, p1, p2);
  216. ret = 1;
  217. }
  218. }
  219. }
  220. }
  221. /* Regression test for BZ #12205 */
  222. static void
  223. bz12205 (void)
  224. {
  225. static char cp [4096+16] __attribute__ ((aligned(4096)));
  226. static char gotrel[4096] __attribute__ ((aligned(4096)));
  227. char *s1 = cp + 0xffa;
  228. char *s2 = gotrel + 0xcbe;
  229. int exp_result;
  230. size_t n = 6;
  231. strcpy (s1, "gottpoff");
  232. strcpy (s2, "GOTPLT");
  233. exp_result = simple_strncasecmp (s1, s2, n);
  234. FOR_EACH_IMPL (impl, 0)
  235. check_result (impl, s1, s2, n, exp_result);
  236. }
  237. /* Regression test for BZ #14195 */
  238. static void
  239. bz14195 (void)
  240. {
  241. const char *empty_string = "";
  242. FOR_EACH_IMPL (impl, 0)
  243. check_result (impl, empty_string, "", 5, 0);
  244. }
  245. int
  246. test_main (void)
  247. {
  248. size_t i;
  249. test_init ();
  250. bz12205 ();
  251. bz14195 ();
  252. printf ("%23s", "");
  253. FOR_EACH_IMPL (impl, 0)
  254. printf ("\t%s", impl->name);
  255. putchar ('\n');
  256. for (i = 1; i < 16; ++i)
  257. {
  258. do_test (i, i, i - 1, i, 127, 0);
  259. do_test (i, i, i, i, 127, 0);
  260. do_test (i, i, i, i, 127, 1);
  261. do_test (i, i, i, i, 127, -1);
  262. do_test (i, i, i + 1, i, 127, 0);
  263. do_test (i, i, i + 1, i, 127, 1);
  264. do_test (i, i, i + 1, i, 127, -1);
  265. }
  266. for (i = 1; i < 10; ++i)
  267. {
  268. do_test (0, 0, (2 << i) - 1, 2 << i, 127, 0);
  269. do_test (0, 0, 2 << i, 2 << i, 254, 0);
  270. do_test (0, 0, (2 << i) + 1, 2 << i, 127, 0);
  271. do_test (0, 0, (2 << i) + 1, 2 << i, 254, 0);
  272. do_test (0, 0, 2 << i, 2 << i, 127, 1);
  273. do_test (0, 0, (2 << i) + 10, 2 << i, 127, 1);
  274. do_test (0, 0, 2 << i, 2 << i, 254, 1);
  275. do_test (0, 0, (2 << i) + 10, 2 << i, 254, 1);
  276. do_test (0, 0, 2 << i, 2 << i, 127, -1);
  277. do_test (0, 0, (2 << i) + 10, 2 << i, 127, -1);
  278. do_test (0, 0, 2 << i, 2 << i, 254, -1);
  279. do_test (0, 0, (2 << i) + 10, 2 << i, 254, -1);
  280. }
  281. for (i = 1; i < 8; ++i)
  282. {
  283. do_test (i, 2 * i, (8 << i) - 1, 8 << i, 127, 0);
  284. do_test (i, 2 * i, 8 << i, 8 << i, 127, 0);
  285. do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 0);
  286. do_test (2 * i, i, (8 << i) - 1, 8 << i, 254, 0);
  287. do_test (2 * i, i, 8 << i, 8 << i, 254, 0);
  288. do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 0);
  289. do_test (i, 2 * i, 8 << i, 8 << i, 127, 1);
  290. do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, 1);
  291. do_test (2 * i, i, 8 << i, 8 << i, 254, 1);
  292. do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, 1);
  293. do_test (i, 2 * i, 8 << i, 8 << i, 127, -1);
  294. do_test (i, 2 * i, (8 << i) + 100, 8 << i, 127, -1);
  295. do_test (2 * i, i, 8 << i, 8 << i, 254, -1);
  296. do_test (2 * i, i, (8 << i) + 100, 8 << i, 254, -1);
  297. }
  298. do_random_tests ();
  299. return ret;
  300. }
  301. #include "../test-skeleton.c"