root/tests/lib/strutil/filevercmp.c

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

DEFINITIONS

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

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