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-2025
   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 <https://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 static const struct parse_integer_test_ds
  54 {
  55     const char *haystack;
  56     uintmax_t expected_result;
  57     gboolean invalid;
  58 } parse_integer_test_ds[] = {
  59     { // too big
  60       "99999999999999999999999999999999999999999999999999999999999999999999", 0, TRUE },
  61     { "x", 0, TRUE },
  62     { "9x", 0, TRUE },
  63     { "1", 1, FALSE },
  64     { "-1", 0, TRUE },
  65     { "1k", 1024, FALSE },
  66     { "1K", 1024, FALSE },
  67     { "1M", 1024 * 1024, FALSE },
  68     { "1m", 0, TRUE },
  69     { "64M", 64 * 1024 * 1024, FALSE },
  70     { "1G", 1 * 1024 * 1024 * 1024, FALSE },
  71 };
  72 
  73 /* @Test(dataSource = "parse_integer_test_ds") */
  74 START_PARAMETRIZED_TEST (parse_integer_test, parse_integer_test_ds)
     /* [previous][next][first][last][top][bottom][index][help]  */
  75 {
  76     // given
  77     uintmax_t actual_result;
  78     gboolean invalid = FALSE;
  79 
  80     // when
  81     actual_result = parse_integer (data->haystack, &invalid);
  82 
  83     // then
  84     ck_assert_msg (invalid == data->invalid && actual_result == data->expected_result,
  85                    "actual ( %" PRIuMAX ") not equal to\nexpected (%" PRIuMAX ")", actual_result,
  86                    data->expected_result);
  87 }
  88 END_PARAMETRIZED_TEST
  89 
  90 /* --------------------------------------------------------------------------------------------- */
  91 
  92 int
  93 main (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  94 {
  95     TCase *tc_core;
  96 
  97     tc_core = tcase_create ("Core");
  98 
  99     tcase_add_checked_fixture (tc_core, setup, teardown);
 100 
 101     // Add new tests here: ***************
 102     mctest_add_parameterized_test (tc_core, parse_integer_test, parse_integer_test_ds);
 103     // ***********************************
 104 
 105     return mctest_run_all (tc_core);
 106 }
 107 
 108 /* --------------------------------------------------------------------------------------------- */

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