root/tests/lib/strutil/filevercmp.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. sign
  4. START_TEST
  5. START_TEST
  6. START_TEST
  7. START_TEST
  8. main

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

/* [previous][next][first][last][top][bottom][index][help]  */