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. test_filevercmp
  5. START_TEST
  6. START_TEST
  7. START_TEST
  8. START_TEST
  9. START_TEST
  10. main

   1 /*
   2    lib/strutil - tests for lib/strutil/fileverscmp function.
   3 
   4    Copyright (C) 2019-2024
   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 #include "lib/util.h"           /* _GL_CMP() */
  32 
  33 /* --------------------------------------------------------------------------------------------- */
  34 
  35 /* @Before */
  36 static void
  37 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  38 {
  39 }
  40 
  41 /* --------------------------------------------------------------------------------------------- */
  42 
  43 /* @After */
  44 static void
  45 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  46 {
  47 }
  48 
  49 /* --------------------------------------------------------------------------------------------- */
  50 
  51 static int
  52 sign (int n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54     return _GL_CMP (n, 0);
  55 }
  56 
  57 /* --------------------------------------------------------------------------------------------- */
  58 
  59 /*
  60  * Return filevercmp (a, a), checking that a similar result is gotten after replacing all '\1's
  61  * with '\0's and calling filenvercmp with the embedded '\0's.
  62  */
  63 static int
  64 test_filevercmp (char const *a, char const *b)
     /* [previous][next][first][last][top][bottom][index][help]  */
  65 {
  66     int result;
  67     char buffer[BUF_1K];
  68     size_t alen, blen;
  69     size_t i;
  70     int nresult;
  71 
  72     result = filevercmp (a, b);
  73 
  74     alen = strlen (a);
  75     blen = strlen (b);
  76 
  77     ck_assert_int_le (alen + blen, sizeof (buffer));
  78     memcpy (buffer, a, alen);
  79     memcpy (buffer + alen, b, blen);
  80 
  81     for (i = 0; i < alen + blen; i++)
  82         if (buffer[i] == '\1')
  83             buffer[i] = '\0';
  84 
  85     nresult = filenvercmp (buffer, alen, buffer + alen, blen);
  86     ck_assert_int_eq (sign (nresult), sign (result));
  87 
  88     return result;
  89 }
  90 
  91 /* --------------------------------------------------------------------------------------------- */
  92 
  93 /* @DataSource("filevercmp_test_ds1") */
  94 /* Testcases are taken from Gnulib */
  95 /* *INDENT-OFF* */
  96 static const struct filevercmp_test_struct
  97 {
  98     const char *s1;
  99     const char *s2;
 100     int expected_result;
 101 } filevercmp_test_ds1[] =
 102 {
 103     { "", "", 0 },
 104     { "a", "a", 0 },
 105     { "a", "b", -1 },
 106     { "b", "a", 1 },
 107     { "00", "01", -1 },
 108     { "01", "010", -1 },
 109     { "9", "10", -1 },
 110     { "0a", "0", 1 }
 111 };
 112 /* *INDENT-ON* */
 113 
 114 
 115 /* @Test(dataSource = "filevercmp_test_ds1") */
 116 /* *INDENT-OFF* */
 117 START_TEST (filevercmp_test1)
     /* [previous][next][first][last][top][bottom][index][help]  */
 118 /* *INDENT-ON* */
 119 {
 120     /* given */
 121     int actual_result;
 122     const struct filevercmp_test_struct *data = &filevercmp_test_ds1[_i];
 123 
 124     /* when */
 125     actual_result = filevercmp (data->s1, data->s2);
 126 
 127     /* then */
 128     ck_assert_int_eq (sign (actual_result), sign (data->expected_result));
 129 }
 130 /* *INDENT-OFF* */
 131 END_TEST
 132 /* *INDENT-ON* */
 133 
 134 /* --------------------------------------------------------------------------------------------- */
 135 
 136 /* @DataSource("filevercmp_test_ds2") */
 137 /* Testcases are taken from Gnulib */
 138 static const char *filevercmp_test_ds2[] = {
 139     "",
 140     ".",
 141     "..",
 142     ".0",
 143     ".9",
 144     ".A",
 145     ".Z",
 146     ".a~",
 147     ".a",
 148     ".b~",
 149     ".b",
 150     ".z",
 151     ".zz~",
 152     ".zz",
 153     ".zz.~1~",
 154     ".zz.0",
 155     ".\1",
 156     ".\1.txt",
 157     ".\1x",
 158     ".\1x\1",
 159     ".\1.0",
 160     "0",
 161     "9",
 162     "A",
 163     "Z",
 164     "a~",
 165     "a",
 166     "a.b~",
 167     "a.b",
 168     "a.bc~",
 169     "a.bc",
 170     "a+",
 171     "a.",
 172     "a..a",
 173     "a.+",
 174     "b~",
 175     "b",
 176     "gcc-c++-10.fc9.tar.gz",
 177     "gcc-c++-10.fc9.tar.gz.~1~",
 178     "gcc-c++-10.fc9.tar.gz.~2~",
 179     "gcc-c++-10.8.12-0.7rc2.fc9.tar.bz2",
 180     "gcc-c++-10.8.12-0.7rc2.fc9.tar.bz2.~1~",
 181     "glibc-2-0.1.beta1.fc10.rpm",
 182     "glibc-common-5-0.2.beta2.fc9.ebuild",
 183     "glibc-common-5-0.2b.deb",
 184     "glibc-common-11b.ebuild",
 185     "glibc-common-11-0.6rc2.ebuild",
 186     "libstdc++-0.5.8.11-0.7rc2.fc10.tar.gz",
 187     "libstdc++-4a.fc8.tar.gz",
 188     "libstdc++-4.10.4.20040204svn.rpm",
 189     "libstdc++-devel-3.fc8.ebuild",
 190     "libstdc++-devel-3a.fc9.tar.gz",
 191     "libstdc++-devel-8.fc8.deb",
 192     "libstdc++-devel-8.6.2-0.4b.fc8",
 193     "nss_ldap-1-0.2b.fc9.tar.bz2",
 194     "nss_ldap-1-0.6rc2.fc8.tar.gz",
 195     "nss_ldap-1.0-0.1a.tar.gz",
 196     "nss_ldap-10beta1.fc8.tar.gz",
 197     "nss_ldap-10.11.8.6.20040204cvs.fc10.ebuild",
 198     "z",
 199     "zz~",
 200     "zz",
 201     "zz.~1~",
 202     "zz.0",
 203     "zz.0.txt",
 204     "\1",
 205     "\1.txt",
 206     "\1x",
 207     "\1x\1",
 208     "\1.0",
 209     "#\1.b#",
 210     "#.b#"
 211 };
 212 
 213 const size_t filevercmp_test_ds2_len = G_N_ELEMENTS (filevercmp_test_ds2);
 214 
 215 /* @Test(dataSource = "filevercmp_test_ds2") */
 216 /* *INDENT-OFF* */
 217 START_TEST (filevercmp_test2)
     /* [previous][next][first][last][top][bottom][index][help]  */
 218 /* *INDENT-ON* */
 219 {
 220     const char *i = filevercmp_test_ds2[_i];
 221     size_t _j;
 222 
 223     for (_j = 0; _j < filevercmp_test_ds2_len; _j++)
 224     {
 225         const char *j = filevercmp_test_ds2[_j];
 226         int result;
 227 
 228         result = test_filevercmp (i, j);
 229 
 230         if (result < 0)
 231             ck_assert_int_lt ((size_t) _i, _j);
 232         else if (result > 0)
 233             ck_assert_int_gt ((size_t) _i, _j);
 234         else
 235             ck_assert_int_eq ((size_t) _i, _j);
 236     }
 237 }
 238 /* *INDENT-OFF* */
 239 END_TEST
 240 /* *INDENT-ON* */
 241 
 242 
 243 /* @DataSource("filevercmp_test_ds3") */
 244 /* Ticket #3959 */
 245 static const char *filevercmp_test_ds3[] = {
 246     "application-1.10.tar.gz",
 247     "application-1.10.1.tar.gz"
 248 };
 249 
 250 const size_t filevercmp_test_ds3_len = G_N_ELEMENTS (filevercmp_test_ds3);
 251 
 252 /* @Test(dataSource = "filevercmp_test_ds3") */
 253 /* *INDENT-OFF* */
 254 START_TEST (filevercmp_test3)
     /* [previous][next][first][last][top][bottom][index][help]  */
 255 /* *INDENT-ON* */
 256 {
 257     const char *i = filevercmp_test_ds3[_i];
 258     size_t _j;
 259 
 260     for (_j = 0; _j < filevercmp_test_ds3_len; _j++)
 261     {
 262         const char *j = filevercmp_test_ds3[_j];
 263         int result;
 264 
 265         result = filevercmp (i, j);
 266 
 267         if (result < 0)
 268             ck_assert_int_lt ((size_t) _i, _j);
 269         else if (result > 0)
 270             ck_assert_int_gt ((size_t) _i, _j);
 271         else
 272             ck_assert_int_eq ((size_t) _i, _j);
 273     }
 274 }
 275 /* *INDENT-OFF* */
 276 END_TEST
 277 /* *INDENT-ON* */
 278 
 279 
 280 /* @DataSource("filevercmp_test_ds4") */
 281 /* Ticket #3905 */
 282 static const char *filevercmp_test_ds4[] = {
 283     "firefox-58.0.1+build1.tar.gz",
 284     "firefox-59.0~b14+build1.tar.gz",
 285     "firefox-59.0.1+build1.tar.gz"
 286 };
 287 
 288 const size_t filevercmp_test_ds4_len = G_N_ELEMENTS (filevercmp_test_ds4);
 289 
 290 /* @Test(dataSource = "filevercmp_test_ds4") */
 291 /* *INDENT-OFF* */
 292 START_TEST (filevercmp_test4)
     /* [previous][next][first][last][top][bottom][index][help]  */
 293 /* *INDENT-ON* */
 294 {
 295     const char *i = filevercmp_test_ds4[_i];
 296     size_t _j;
 297 
 298     for (_j = 0; _j < filevercmp_test_ds4_len; _j++)
 299     {
 300         const char *j = filevercmp_test_ds4[_j];
 301         int result;
 302 
 303         result = filevercmp (i, j);
 304 
 305         if (result < 0)
 306             ck_assert_int_lt ((size_t) _i, _j);
 307         else if (result > 0)
 308             ck_assert_int_gt ((size_t) _i, _j);
 309         else
 310             ck_assert_int_eq ((size_t) _i, _j);
 311     }
 312 }
 313 /* *INDENT-OFF* */
 314 END_TEST
 315 /* *INDENT-ON* */
 316 
 317 
 318 /* @DataSource("filevercmp_test_ds5") */
 319 /* Testcases are taken from Gnulib */
 320 static const char *filevercmp_test_ds5[] = {
 321     "a",
 322     "a0",
 323     "a0000",
 324     NULL,
 325     "a\1c-27.txt",
 326     "a\1c-027.txt",
 327     "a\1c-00000000000000000000000000000000000000000000000000000027.txt",
 328     NULL,
 329     ".a\1c-27.txt",
 330     ".a\1c-027.txt",
 331     ".a\1c-00000000000000000000000000000000000000000000000000000027.txt",
 332     NULL,
 333     "a\1c-",
 334     "a\1c-0",
 335     "a\1c-00",
 336     NULL,
 337     ".a\1c-",
 338     ".a\1c-0",
 339     ".a\1c-00",
 340     NULL,
 341     "a\1c-0.txt",
 342     "a\1c-00.txt",
 343     NULL,
 344     ".a\1c-1\1.txt",
 345     ".a\1c-001\1.txt",
 346     NULL
 347 };
 348 
 349 const size_t filevercmp_test_ds5_len = G_N_ELEMENTS (filevercmp_test_ds5);
 350 
 351 /* @Test(dataSource = "filevercmp_test_ds5") */
 352 /* *INDENT-OFF* */
 353 START_TEST (filevercmp_test5)
     /* [previous][next][first][last][top][bottom][index][help]  */
 354 /* *INDENT-ON* */
 355 {
 356     size_t ii;
 357 
 358     for (ii = 0; ii < filevercmp_test_ds5_len; ii++)
 359         for (; filevercmp_test_ds5[ii] != NULL; ii++)
 360         {
 361             const char *i = filevercmp_test_ds5[ii];
 362             size_t jj;
 363 
 364             for (jj = ii; filevercmp_test_ds5[jj] != NULL; jj++)
 365             {
 366                 const char *j = filevercmp_test_ds5[jj];
 367 
 368                 ck_assert_int_eq (test_filevercmp (i, j), 0);
 369                 ck_assert_int_eq (test_filevercmp (j, i), 0);
 370             }
 371         }
 372 }
 373 /* *INDENT-OFF* */
 374 END_TEST
 375 /* *INDENT-ON* */
 376 
 377 /* --------------------------------------------------------------------------------------------- */
 378 
 379 int
 380 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 381 {
 382     TCase *tc_core;
 383 
 384     tc_core = tcase_create ("Core");
 385 
 386     tcase_add_checked_fixture (tc_core, setup, teardown);
 387 
 388     /* Add new tests here: *************** */
 389     mctest_add_parameterized_test (tc_core, filevercmp_test1, filevercmp_test_ds1);
 390     tcase_add_loop_test (tc_core, filevercmp_test2, 0, filevercmp_test_ds2_len);
 391     tcase_add_loop_test (tc_core, filevercmp_test3, 0, filevercmp_test_ds3_len);
 392     tcase_add_loop_test (tc_core, filevercmp_test4, 0, filevercmp_test_ds4_len);
 393     tcase_add_test (tc_core, filevercmp_test5);
 394     /* *********************************** */
 395 
 396     return mctest_run_all (tc_core);
 397 }
 398 
 399 /* --------------------------------------------------------------------------------------------- */

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