root/tests/lib/strutil/parse_integer.c

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

DEFINITIONS

This source file includes following definitions.
  1. setup
  2. teardown
  3. START_PARAMETRIZED_TEST
  4. main

   1 /*
   2    lib/strutil - tests for lib/strutil/parse_integer function.
   3 
   4    Copyright (C) 2013-2024
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Andrew Borodin <aborodin@vmail.ru>, 2013
   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 <inttypes.h>
  31 
  32 #include "lib/strutil.h"
  33 
  34 /* --------------------------------------------------------------------------------------------- */
  35 
  36 /* @Before */
  37 static void
  38 setup (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  39 {
  40 }
  41 
  42 /* --------------------------------------------------------------------------------------------- */
  43 
  44 /* @After */
  45 static void
  46 teardown (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  47 {
  48 }
  49 
  50 /* --------------------------------------------------------------------------------------------- */
  51 
  52 /* @DataSource("parse_integer_test_ds") */
  53 /* *INDENT-OFF* */
  54 static const struct parse_integer_test_ds
  55 {
  56     const char *haystack;
  57     uintmax_t expected_result;
  58     gboolean invalid;
  59 } parse_integer_test_ds[] =
  60 {
  61     {
  62         /* too big */
  63         "99999999999999999999999999999999999999999999999999999999999999999999",
  64         0,
  65         TRUE
  66     },
  67     {
  68         "x",
  69         0,
  70         TRUE
  71     },
  72     {
  73         "9x",
  74         0,
  75         TRUE
  76     },
  77     {
  78         "1",
  79         1,
  80         FALSE
  81     },
  82     {
  83         "-1",
  84         0,
  85         TRUE
  86     },
  87     {
  88         "1k",
  89         1024,
  90         FALSE
  91     },
  92     {
  93         "1K",
  94         1024,
  95         FALSE
  96     },
  97     {
  98         "1M",
  99         1024 * 1024,
 100         FALSE
 101     },
 102     {
 103         "1m",
 104         0,
 105         TRUE
 106     },
 107     {
 108         "64M",
 109         64 * 1024 * 1024,
 110         FALSE
 111     },
 112     {
 113         "1G",
 114         1 * 1024 * 1024 * 1024,
 115         FALSE
 116     }
 117 };
 118 /* *INDENT-ON* */
 119 
 120 /* @Test(dataSource = "parse_integer_test_ds") */
 121 /* *INDENT-OFF* */
 122 START_PARAMETRIZED_TEST (parse_integer_test, parse_integer_test_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
 123 /* *INDENT-ON* */
 124 {
 125     /* given */
 126     uintmax_t actual_result;
 127     gboolean invalid = FALSE;
 128 
 129     /* when */
 130     actual_result = parse_integer (data->haystack, &invalid);
 131 
 132     /* then */
 133     ck_assert_msg (invalid == data->invalid && actual_result == data->expected_result,
 134                    "actual ( %" PRIuMAX ") not equal to\nexpected (%" PRIuMAX ")",
 135                    actual_result, data->expected_result);
 136 }
 137 /* *INDENT-OFF* */
 138 END_PARAMETRIZED_TEST
 139 /* *INDENT-ON* */
 140 
 141 /* --------------------------------------------------------------------------------------------- */
 142 
 143 int
 144 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 145 {
 146     TCase *tc_core;
 147 
 148     tc_core = tcase_create ("Core");
 149 
 150     tcase_add_checked_fixture (tc_core, setup, teardown);
 151 
 152     /* Add new tests here: *************** */
 153     mctest_add_parameterized_test (tc_core, parse_integer_test, parse_integer_test_ds);
 154     /* *********************************** */
 155 
 156     return mctest_run_all (tc_core);
 157 }
 158 
 159 /* --------------------------------------------------------------------------------------------- */

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