root/tests/lib/strutil/str_verscmp.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. main

   1 /*
   2    lib/strutil - tests for lib/strutil/str_verscmp function.
   3    Testcases are taken from Gnulib.
   4 
   5    Copyright (C) 2019
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Andrew Borodin <aborodin@vmail.ru>, 2019
  10 
  11    This file is part of the Midnight Commander.
  12 
  13    The Midnight Commander is free software: you can redistribute it
  14    and/or modify it under the terms of the GNU General Public License as
  15    published by the Free Software Foundation, either version 3 of the License,
  16    or (at your option) any later version.
  17 
  18    The Midnight Commander is distributed in the hope that it will be useful,
  19    but WITHOUT ANY WARRANTY; without even the implied warranty of
  20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21    GNU General Public License for more details.
  22 
  23    You should have received a copy of the GNU General Public License
  24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25  */
  26 
  27 #define TEST_SUITE_NAME "/lib/strutil"
  28 
  29 #include "tests/mctest.h"
  30 
  31 #include "lib/strutil.h"
  32 
  33 /* --------------------------------------------------------------------------------------------- */
  34 
  35 /* From glibc bug 9913 */
  36 static char const a[] = "B0075022800016.gbp.corp.com";
  37 static char const b[] = "B007502280067.gbp.corp.com";
  38 static char const c[] = "B007502357019.GBP.CORP.COM";
  39 
  40 /* --------------------------------------------------------------------------------------------- */
  41 
  42 /* @Before */
  43 static void
  44 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  45 {
  46 }
  47 
  48 /* --------------------------------------------------------------------------------------------- */
  49 
  50 /* @After */
  51 static void
  52 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  53 {
  54 }
  55 
  56 /* --------------------------------------------------------------------------------------------- */
  57 
  58 static int
  59 sign (int n)
     /* [previous][next][first][last][top][bottom][index][help]  */
  60 {
  61     return ((n < 0) ? -1 : (n == 0) ? 0 : 1);
  62 }
  63 
  64 /* --------------------------------------------------------------------------------------------- */
  65 
  66 /* @DataSource("str_verscmp_test_ds") */
  67 /* *INDENT-OFF* */
  68 static const struct str_verscmp_test_struct
  69 {
  70     const char *s1;
  71     const char *s2;
  72     int expected_result;
  73 } str_verscmp_test_ds[] =
  74 {
  75     { "", "", 0 },
  76     { "a", "a", 0 },
  77     { "a", "b", -1 },
  78     { "b", "a", 1 },
  79     { "000", "00", -1 },
  80     { "00", "000", 1 },
  81     { "a0", "a", 1 },
  82     { "00", "01", -1 },
  83     { "01", "010", -1 },
  84     { "010", "09", -1 },
  85     { "09", "0", -1 },
  86     { "9", "10", -1 },
  87     { "0a", "0", 1 },
  88     /* From glibc bug 9913 */
  89     { a, b, -1 },
  90     { b, c, -1 },
  91     { a, c, -1 },
  92     { b, a, 1 },
  93     { c, b, 1 },
  94     { c, a, 1 }
  95 };
  96 /* *INDENT-ON* */
  97 
  98 /* @Test(dataSource = "str_verscmp_test_ds") */
  99 /* *INDENT-OFF* */
 100 START_TEST (str_verscmp_test)
     /* [previous][next][first][last][top][bottom][index][help]  */
 101 /* *INDENT-ON* */
 102 {
 103     /* given */
 104     int actual_result;
 105     const struct str_verscmp_test_struct *data = &str_verscmp_test_ds[_i];
 106 
 107     /* when */
 108     actual_result = str_verscmp (data->s1, data->s2);
 109 
 110     /* then */
 111     mctest_assert_int_eq (sign (actual_result), sign (data->expected_result));
 112 }
 113 /* *INDENT-OFF* */
 114 END_TEST
 115 /* *INDENT-ON* */
 116 
 117 /* --------------------------------------------------------------------------------------------- */
 118 
 119 int
 120 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 121 {
 122     int number_failed;
 123 
 124     Suite *s = suite_create (TEST_SUITE_NAME);
 125     TCase *tc_core = tcase_create ("Core");
 126     SRunner *sr;
 127 
 128     tcase_add_checked_fixture (tc_core, setup, teardown);
 129 
 130     /* Add new tests here: *************** */
 131     mctest_add_parameterized_test (tc_core, str_verscmp_test, str_verscmp_test_ds);
 132     /* *********************************** */
 133 
 134     suite_add_tcase (s, tc_core);
 135     sr = srunner_create (s);
 136     srunner_set_log (sr, "str_verscmp.log");
 137     srunner_run_all (sr, CK_ENV);
 138     number_failed = srunner_ntests_failed (sr);
 139     srunner_free (sr);
 140     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 141 }
 142 
 143 /* --------------------------------------------------------------------------------------------- */

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