filevercmp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. lib/strutil - tests for lib/strutil/fileverscmp function.
  3. Copyright (C) 2019
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2019
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/strutil"
  20. #include "tests/mctest.h"
  21. #include "lib/strutil.h"
  22. /* --------------------------------------------------------------------------------------------- */
  23. /* @Before */
  24. static void
  25. setup (void)
  26. {
  27. }
  28. /* --------------------------------------------------------------------------------------------- */
  29. /* @After */
  30. static void
  31. teardown (void)
  32. {
  33. }
  34. /* --------------------------------------------------------------------------------------------- */
  35. static int
  36. sign (int n)
  37. {
  38. return ((n < 0) ? -1 : (n == 0) ? 0 : 1);
  39. }
  40. /* --------------------------------------------------------------------------------------------- */
  41. /* @DataSource("filevercmp_test_ds1") */
  42. /* Testcases are taken from Gnulib */
  43. /* *INDENT-OFF* */
  44. static const struct filevercmp_test_struct
  45. {
  46. const char *s1;
  47. const char *s2;
  48. int expected_result;
  49. } filevercmp_test_ds1[] =
  50. {
  51. { "", "", 0 },
  52. { "a", "a", 0 },
  53. { "a", "b", -1 },
  54. { "b", "a", 1 },
  55. { "a0", "a", 1 },
  56. { "00", "01", -1 },
  57. { "01", "010", -1 },
  58. { "9", "10", -1 },
  59. { "0a", "0", 1 }
  60. };
  61. /* *INDENT-ON* */
  62. /* @Test(dataSource = "filevercmp_test_ds1") */
  63. /* *INDENT-OFF* */
  64. START_TEST (filevercmp_test1)
  65. /* *INDENT-ON* */
  66. {
  67. /* given */
  68. int actual_result;
  69. const struct filevercmp_test_struct *data = &filevercmp_test_ds1[_i];
  70. /* when */
  71. actual_result = filevercmp (data->s1, data->s2);
  72. /* then */
  73. mctest_assert_int_eq (sign (actual_result), sign (data->expected_result));
  74. }
  75. /* *INDENT-OFF* */
  76. END_TEST
  77. /* *INDENT-ON* */
  78. /* --------------------------------------------------------------------------------------------- */
  79. /* @DataSource("filevercmp_test_ds2") */
  80. /* Testcases are taken from Gnulib */
  81. static const char *filevercmp_test_ds2[] = {
  82. "",
  83. ".",
  84. "..",
  85. ".0",
  86. ".9",
  87. ".A",
  88. ".Z",
  89. ".a~",
  90. ".a",
  91. ".b~",
  92. ".b",
  93. ".z",
  94. ".zz~",
  95. ".zz",
  96. ".zz.~1~",
  97. ".zz.0",
  98. "0",
  99. "9",
  100. "A",
  101. "Z",
  102. "a~",
  103. "a",
  104. "a.b~",
  105. "a.b",
  106. "a.bc~",
  107. "a.bc",
  108. "b~",
  109. "b",
  110. "gcc-c++-10.fc9.tar.gz",
  111. "gcc-c++-10.fc9.tar.gz.~1~",
  112. "gcc-c++-10.fc9.tar.gz.~2~",
  113. "gcc-c++-10.8.12-0.7rc2.fc9.tar.bz2",
  114. "gcc-c++-10.8.12-0.7rc2.fc9.tar.bz2.~1~",
  115. "glibc-2-0.1.beta1.fc10.rpm",
  116. "glibc-common-5-0.2.beta2.fc9.ebuild",
  117. "glibc-common-5-0.2b.deb",
  118. "glibc-common-11b.ebuild",
  119. "glibc-common-11-0.6rc2.ebuild",
  120. "libstdc++-0.5.8.11-0.7rc2.fc10.tar.gz",
  121. "libstdc++-4a.fc8.tar.gz",
  122. "libstdc++-4.10.4.20040204svn.rpm",
  123. "libstdc++-devel-3.fc8.ebuild",
  124. "libstdc++-devel-3a.fc9.tar.gz",
  125. "libstdc++-devel-8.fc8.deb",
  126. "libstdc++-devel-8.6.2-0.4b.fc8",
  127. "nss_ldap-1-0.2b.fc9.tar.bz2",
  128. "nss_ldap-1-0.6rc2.fc8.tar.gz",
  129. "nss_ldap-1.0-0.1a.tar.gz",
  130. "nss_ldap-10beta1.fc8.tar.gz",
  131. "nss_ldap-10.11.8.6.20040204cvs.fc10.ebuild",
  132. "z",
  133. "zz~",
  134. "zz",
  135. "zz.~1~",
  136. "zz.0",
  137. "#.b#"
  138. };
  139. const size_t filevercmp_test_ds2_len = G_N_ELEMENTS (filevercmp_test_ds2);
  140. /* @Test(dataSource = "filevercmp_test_ds2") */
  141. /* *INDENT-OFF* */
  142. START_TEST (filevercmp_test2)
  143. /* *INDENT-ON* */
  144. {
  145. const char *i = filevercmp_test_ds2[_i];
  146. size_t _j;
  147. for (_j = 0; _j < filevercmp_test_ds2_len; _j++)
  148. {
  149. const char *j = filevercmp_test_ds2[_j];
  150. int result;
  151. result = filevercmp (i, j);
  152. if (result < 0)
  153. ck_assert_int_eq (! !((size_t) _i < _j), 1);
  154. else if (0 < result)
  155. ck_assert_int_eq (! !(_j < (size_t) _i), 1);
  156. else
  157. ck_assert_int_eq (! !(_j == (size_t) _i), 1);
  158. }
  159. }
  160. /* *INDENT-OFF* */
  161. END_TEST
  162. /* *INDENT-ON* */
  163. /* @DataSource("filevercmp_test_ds3") */
  164. /* Ticket #3959 */
  165. static const char *filevercmp_test_ds3[] = {
  166. "application-1.10.tar.gz",
  167. "application-1.10.1.tar.gz"
  168. };
  169. const size_t filevercmp_test_ds3_len = G_N_ELEMENTS (filevercmp_test_ds3);
  170. /* @Test(dataSource = "filevercmp_test_ds3") */
  171. /* *INDENT-OFF* */
  172. START_TEST (filevercmp_test3)
  173. /* *INDENT-ON* */
  174. {
  175. const char *i = filevercmp_test_ds3[_i];
  176. size_t _j;
  177. for (_j = 0; _j < filevercmp_test_ds3_len; _j++)
  178. {
  179. const char *j = filevercmp_test_ds3[_j];
  180. int result;
  181. result = filevercmp (i, j);
  182. if (result < 0)
  183. ck_assert_int_eq (! !((size_t) _i < _j), 1);
  184. else if (0 < result)
  185. ck_assert_int_eq (! !(_j < (size_t) _i), 1);
  186. else
  187. ck_assert_int_eq (! !(_j == (size_t) _i), 1);
  188. }
  189. }
  190. /* *INDENT-OFF* */
  191. END_TEST
  192. /* *INDENT-ON* */
  193. /* @DataSource("filevercmp_test_ds4") */
  194. /* Ticket #3905 */
  195. static const char *filevercmp_test_ds4[] = {
  196. "firefox-58.0.1+build1.tar.gz",
  197. "firefox-59.0~b14+build1.tar.gz",
  198. "firefox-59.0.1+build1.tar.gz"
  199. };
  200. const size_t filevercmp_test_ds4_len = G_N_ELEMENTS (filevercmp_test_ds4);
  201. /* @Test(dataSource = "filevercmp_test_ds4") */
  202. /* *INDENT-OFF* */
  203. START_TEST (filevercmp_test4)
  204. /* *INDENT-ON* */
  205. {
  206. const char *i = filevercmp_test_ds4[_i];
  207. size_t _j;
  208. for (_j = 0; _j < filevercmp_test_ds4_len; _j++)
  209. {
  210. const char *j = filevercmp_test_ds4[_j];
  211. int result;
  212. result = filevercmp (i, j);
  213. if (result < 0)
  214. ck_assert_int_eq (! !((size_t) _i < _j), 1);
  215. else if (0 < result)
  216. ck_assert_int_eq (! !(_j < (size_t) _i), 1);
  217. else
  218. ck_assert_int_eq (! !(_j == (size_t) _i), 1);
  219. }
  220. }
  221. /* *INDENT-OFF* */
  222. END_TEST
  223. /* *INDENT-ON* */
  224. /* --------------------------------------------------------------------------------------------- */
  225. int
  226. main (void)
  227. {
  228. int number_failed;
  229. Suite *s = suite_create (TEST_SUITE_NAME);
  230. TCase *tc_core = tcase_create ("Core");
  231. SRunner *sr;
  232. tcase_add_checked_fixture (tc_core, setup, teardown);
  233. /* Add new tests here: *************** */
  234. mctest_add_parameterized_test (tc_core, filevercmp_test1, filevercmp_test_ds1);
  235. tcase_add_loop_test (tc_core, filevercmp_test2, 0, filevercmp_test_ds2_len);
  236. tcase_add_loop_test (tc_core, filevercmp_test3, 0, filevercmp_test_ds3_len);
  237. tcase_add_loop_test (tc_core, filevercmp_test4, 0, filevercmp_test_ds4_len);
  238. /* *********************************** */
  239. suite_add_tcase (s, tc_core);
  240. sr = srunner_create (s);
  241. srunner_set_log (sr, "filevercmp.log");
  242. srunner_run_all (sr, CK_ENV);
  243. number_failed = srunner_ntests_failed (sr);
  244. srunner_free (sr);
  245. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  246. }
  247. /* --------------------------------------------------------------------------------------------- */