test-strcmp.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* Test and measure strcmp and wcscmp 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. Added wcscmp support by Liubov Dmitrieva <liubov.dmitrieva@gmail.com>, 2011.
  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. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #define TEST_MAIN
  18. #ifdef WIDE
  19. # define TEST_NAME "wcscmp"
  20. #else
  21. # define TEST_NAME "strcmp"
  22. #endif
  23. #include "test-string.h"
  24. #ifdef WIDE
  25. # include <wchar.h>
  26. # define L(str) L##str
  27. # define STRCMP wcscmp
  28. # define STRCPY wcscpy
  29. # define STRLEN wcslen
  30. # define MEMCPY wmemcpy
  31. # define SIMPLE_STRCMP simple_wcscmp
  32. # define STUPID_STRCMP stupid_wcscmp
  33. # define CHAR wchar_t
  34. # define UCHAR wchar_t
  35. # define CHARBYTES 4
  36. # define CHARBYTESLOG 2
  37. # define CHARALIGN __alignof__ (CHAR)
  38. # define MIDCHAR 0x7fffffff
  39. # define LARGECHAR 0xfffffffe
  40. # define CHAR__MAX WCHAR_MAX
  41. # define CHAR__MIN WCHAR_MIN
  42. /* Wcscmp uses signed semantics for comparison, not unsigned */
  43. /* Avoid using substraction since possible overflow */
  44. int
  45. simple_wcscmp (const wchar_t *s1, const wchar_t *s2)
  46. {
  47. wchar_t c1, c2;
  48. do
  49. {
  50. c1 = *s1++;
  51. c2 = *s2++;
  52. if (c2 == L'\0')
  53. return c1 - c2;
  54. }
  55. while (c1 == c2);
  56. return c1 < c2 ? -1 : 1;
  57. }
  58. int
  59. stupid_wcscmp (const wchar_t *s1, const wchar_t *s2)
  60. {
  61. size_t ns1 = wcslen (s1) + 1;
  62. size_t ns2 = wcslen (s2) + 1;
  63. size_t n = ns1 < ns2 ? ns1 : ns2;
  64. int ret = 0;
  65. wchar_t c1, c2;
  66. while (n--) {
  67. c1 = *s1++;
  68. c2 = *s2++;
  69. if ((ret = c1 < c2 ? -1 : c1 == c2 ? 0 : 1) != 0)
  70. break;
  71. }
  72. return ret;
  73. }
  74. #else
  75. # include <limits.h>
  76. # define L(str) str
  77. # define STRCMP strcmp
  78. # define STRCPY strcpy
  79. # define STRLEN strlen
  80. # define MEMCPY memcpy
  81. # define SIMPLE_STRCMP simple_strcmp
  82. # define STUPID_STRCMP stupid_strcmp
  83. # define CHAR char
  84. # define UCHAR unsigned char
  85. # define CHARBYTES 1
  86. # define CHARBYTESLOG 0
  87. # define CHARALIGN 1
  88. # define MIDCHAR 0x7f
  89. # define LARGECHAR 0xfe
  90. # define CHAR__MAX CHAR_MAX
  91. # define CHAR__MIN CHAR_MIN
  92. /* Strcmp uses unsigned semantics for comparison. */
  93. int
  94. simple_strcmp (const char *s1, const char *s2)
  95. {
  96. int ret;
  97. while ((ret = *(unsigned char *) s1 - *(unsigned char*) s2++) == 0 && *s1++);
  98. return ret;
  99. }
  100. int
  101. stupid_strcmp (const char *s1, const char *s2)
  102. {
  103. size_t ns1 = strlen (s1) + 1;
  104. size_t ns2 = strlen (s2) + 1;
  105. size_t n = ns1 < ns2 ? ns1 : ns2;
  106. int ret = 0;
  107. while (n--)
  108. if ((ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) != 0)
  109. break;
  110. return ret;
  111. }
  112. #endif
  113. typedef int (*proto_t) (const CHAR *, const CHAR *);
  114. IMPL (STUPID_STRCMP, 1)
  115. IMPL (SIMPLE_STRCMP, 1)
  116. IMPL (STRCMP, 1)
  117. static int
  118. check_result (impl_t *impl,
  119. const CHAR *s1, const CHAR *s2,
  120. int exp_result)
  121. {
  122. int result = CALL (impl, s1, s2);
  123. if ((exp_result == 0 && result != 0)
  124. || (exp_result < 0 && result >= 0)
  125. || (exp_result > 0 && result <= 0))
  126. {
  127. error (0, 0, "Wrong result in function %s %d %d", impl->name,
  128. result, exp_result);
  129. ret = 1;
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. static void
  135. do_one_test (impl_t *impl,
  136. const CHAR *s1, const CHAR *s2,
  137. int exp_result)
  138. {
  139. if (check_result (impl, s1, s2, exp_result) < 0)
  140. return;
  141. }
  142. static void
  143. do_test (size_t align1, size_t align2, size_t len, int max_char,
  144. int exp_result)
  145. {
  146. size_t i;
  147. CHAR *s1, *s2;
  148. if (len == 0)
  149. return;
  150. align1 &= 63;
  151. if (align1 + (len + 1) * CHARBYTES >= page_size)
  152. return;
  153. align2 &= 63;
  154. if (align2 + (len + 1) * CHARBYTES >= page_size)
  155. return;
  156. /* Put them close to the end of page. */
  157. i = align1 + CHARBYTES * (len + 2);
  158. s1 = (CHAR *) (buf1 + ((page_size - i) / 16 * 16) + align1);
  159. i = align2 + CHARBYTES * (len + 2);
  160. s2 = (CHAR *) (buf2 + ((page_size - i) / 16 * 16) + align2);
  161. for (i = 0; i < len; i++)
  162. s1[i] = s2[i] = 1 + (23 << ((CHARBYTES - 1) * 8)) * i % max_char;
  163. s1[len] = s2[len] = 0;
  164. s1[len + 1] = 23;
  165. s2[len + 1] = 24 + exp_result;
  166. s2[len - 1] -= exp_result;
  167. FOR_EACH_IMPL (impl, 0)
  168. do_one_test (impl, s1, s2, exp_result);
  169. }
  170. static void
  171. do_random_tests (void)
  172. {
  173. UCHAR *p1 = (UCHAR *) (buf1 + page_size - 512 * CHARBYTES);
  174. UCHAR *p2 = (UCHAR *) (buf2 + page_size - 512 * CHARBYTES);
  175. for (size_t n = 0; n < ITERATIONS; n++)
  176. {
  177. /* for wcscmp case align1 and align2 mean here alignment
  178. in wchar_t symbols, it equal 4*k alignment in bytes, we
  179. don't check other alignments like for example
  180. p1 = (wchar_t *)(buf1 + 1)
  181. because it's wrong using of wchar_t type. */
  182. size_t align1 = random () & 31;
  183. size_t align2;
  184. if (random () & 1)
  185. align2 = random () & 31;
  186. else
  187. align2 = align1 + (random () & 24);
  188. size_t pos = random () & 511;
  189. size_t j = align1 > align2 ? align1 : align2;
  190. if (pos + j >= 511)
  191. pos = 510 - j - (random () & 7);
  192. size_t len1 = random () & 511;
  193. if (pos >= len1 && (random () & 1))
  194. len1 = pos + (random () & 7);
  195. if (len1 + j >= 512)
  196. len1 = 511 - j - (random () & 7);
  197. size_t len2;
  198. if (pos >= len1)
  199. len2 = len1;
  200. else
  201. len2 = len1 + (len1 != 511 - j ? random () % (511 - j - len1) : 0);
  202. j = (pos > len2 ? pos : len2) + align1 + 64;
  203. if (j > 512)
  204. j = 512;
  205. for (size_t i = 0; i < j; ++i)
  206. {
  207. p1[i] = random () & 255;
  208. if (i < len1 + align1 && !p1[i])
  209. {
  210. p1[i] = random () & 255;
  211. if (!p1[i])
  212. p1[i] = 1 + (random () & 127);
  213. }
  214. }
  215. for (size_t i = 0; i < j; ++i)
  216. {
  217. p2[i] = random () & 255;
  218. if (i < len2 + align2 && !p2[i])
  219. {
  220. p2[i] = random () & 255;
  221. if (!p2[i])
  222. p2[i] = 1 + (random () & 127);
  223. }
  224. }
  225. int result = 0;
  226. MEMCPY (p2 + align2, p1 + align1, pos);
  227. if (pos < len1)
  228. {
  229. if (p2[align2 + pos] == p1[align1 + pos])
  230. {
  231. p2[align2 + pos] = random () & 255;
  232. if (p2[align2 + pos] == p1[align1 + pos])
  233. p2[align2 + pos] = p1[align1 + pos] + 3 + (random () & 127);
  234. }
  235. if (p1[align1 + pos] < p2[align2 + pos])
  236. result = -1;
  237. else
  238. result = 1;
  239. }
  240. p1[len1 + align1] = 0;
  241. p2[len2 + align2] = 0;
  242. FOR_EACH_IMPL (impl, 1)
  243. {
  244. int r = CALL (impl, (CHAR *) (p1 + align1), (CHAR *) (p2 + align2));
  245. /* Test whether on 64-bit architectures where ABI requires
  246. callee to promote has the promotion been done. */
  247. asm ("" : "=g" (r) : "0" (r));
  248. if ((r == 0 && result)
  249. || (r < 0 && result >= 0)
  250. || (r > 0 && result <= 0))
  251. {
  252. error (0, 0, "Iteration %zd - wrong result in function %s (align in bytes: %zd, align in bytes: %zd, len1: %zd, len2: %zd, pos: %zd) %d != %d, p1 %p p2 %p",
  253. n, impl->name, (size_t) (p1 + align1) & 63, (size_t) (p1 + align2) & 63, len1, len2, pos, r, result, p1, p2);
  254. ret = 1;
  255. }
  256. }
  257. }
  258. }
  259. static void
  260. check (void)
  261. {
  262. CHAR *s1 = (CHAR *) (buf1 + 0xb2c);
  263. CHAR *s2 = (CHAR *) (buf1 + 0xfd8);
  264. STRCPY(s1, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrs"));
  265. STRCPY(s2, L("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkLMNOPQRSTUV"));
  266. /* Check correct working for negatives values */
  267. s1[0] = 1;
  268. s2[0] = 1;
  269. s1[1] = 1;
  270. s2[1] = 1;
  271. s1[2] = -1;
  272. s2[2] = 3;
  273. s1[3] = 0;
  274. s2[3] = -1;
  275. /* Check possible overflow bug, actual more for wcscmp */
  276. s1[7] = CHAR__MIN;
  277. s2[7] = CHAR__MAX;
  278. size_t l1 = STRLEN (s1);
  279. size_t l2 = STRLEN (s2);
  280. for (size_t i1 = 0; i1 < l1; i1++)
  281. for (size_t i2 = 0; i2 < l2; i2++)
  282. {
  283. int exp_result = SIMPLE_STRCMP (s1 + i1, s2 + i2);
  284. FOR_EACH_IMPL (impl, 0)
  285. check_result (impl, s1 + i1, s2 + i2, exp_result);
  286. }
  287. /* Test cases where there are multiple zero bytes after the first. */
  288. for (size_t i = 0; i < 16 + 1; i++)
  289. {
  290. s1[i] = 0x00;
  291. s2[i] = 0x00;
  292. }
  293. for (size_t i = 0; i < 16; i++)
  294. {
  295. int exp_result;
  296. for (int val = 0x01; val < 0x100; val++)
  297. {
  298. for (size_t j = 0; j < i; j++)
  299. {
  300. s1[j] = val;
  301. s2[j] = val;
  302. }
  303. s2[i] = val;
  304. exp_result = SIMPLE_STRCMP (s1, s2);
  305. FOR_EACH_IMPL (impl, 0)
  306. check_result (impl, s1, s2, exp_result);
  307. }
  308. }
  309. }
  310. int
  311. test_main (void)
  312. {
  313. size_t i;
  314. test_init ();
  315. check();
  316. printf ("%23s", "");
  317. FOR_EACH_IMPL (impl, 0)
  318. printf ("\t%s", impl->name);
  319. putchar ('\n');
  320. for (i = 1; i < 32; ++i)
  321. {
  322. do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 0);
  323. do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, 1);
  324. do_test (CHARBYTES * i, CHARBYTES * i, i, MIDCHAR, -1);
  325. }
  326. for (i = 1; i < 10 + CHARBYTESLOG; ++i)
  327. {
  328. do_test (0, 0, 2 << i, MIDCHAR, 0);
  329. do_test (0, 0, 2 << i, LARGECHAR, 0);
  330. do_test (0, 0, 2 << i, MIDCHAR, 1);
  331. do_test (0, 0, 2 << i, LARGECHAR, 1);
  332. do_test (0, 0, 2 << i, MIDCHAR, -1);
  333. do_test (0, 0, 2 << i, LARGECHAR, -1);
  334. do_test (0, CHARBYTES * i, 2 << i, MIDCHAR, 1);
  335. do_test (CHARBYTES * i, CHARBYTES * (i + 1), 2 << i, LARGECHAR, 1);
  336. }
  337. for (i = 1; i < 8; ++i)
  338. {
  339. do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 0);
  340. do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 0);
  341. do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, 1);
  342. do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, 1);
  343. do_test (CHARBYTES * i, 2 * CHARBYTES * i, 8 << i, MIDCHAR, -1);
  344. do_test (2 * CHARBYTES * i, CHARBYTES * i, 8 << i, LARGECHAR, -1);
  345. }
  346. do_random_tests ();
  347. return ret;
  348. }
  349. #include <support/test-driver.c>