parse_integer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. lib/strutil - tests for lib/strutil/parse_integer function.
  3. Copyright (C) 2013-2025
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2013
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/strutil"
  20. #include "tests/mctest.h"
  21. #include <inttypes.h>
  22. #include "lib/strutil.h"
  23. /* --------------------------------------------------------------------------------------------- */
  24. /* @Before */
  25. static void
  26. setup (void)
  27. {
  28. }
  29. /* --------------------------------------------------------------------------------------------- */
  30. /* @After */
  31. static void
  32. teardown (void)
  33. {
  34. }
  35. /* --------------------------------------------------------------------------------------------- */
  36. /* @DataSource("parse_integer_test_ds") */
  37. static const struct parse_integer_test_ds
  38. {
  39. const char *haystack;
  40. uintmax_t expected_result;
  41. gboolean invalid;
  42. } parse_integer_test_ds[] = {
  43. { // too big
  44. "99999999999999999999999999999999999999999999999999999999999999999999", 0, TRUE },
  45. { "x", 0, TRUE },
  46. { "9x", 0, TRUE },
  47. { "1", 1, FALSE },
  48. { "-1", 0, TRUE },
  49. { "1k", 1024, FALSE },
  50. { "1K", 1024, FALSE },
  51. { "1M", 1024 * 1024, FALSE },
  52. { "1m", 0, TRUE },
  53. { "64M", 64 * 1024 * 1024, FALSE },
  54. { "1G", 1 * 1024 * 1024 * 1024, FALSE },
  55. };
  56. /* @Test(dataSource = "parse_integer_test_ds") */
  57. START_PARAMETRIZED_TEST (parse_integer_test, parse_integer_test_ds)
  58. {
  59. // given
  60. uintmax_t actual_result;
  61. gboolean invalid = FALSE;
  62. // when
  63. actual_result = parse_integer (data->haystack, &invalid);
  64. // then
  65. ck_assert_msg (invalid == data->invalid && actual_result == data->expected_result,
  66. "actual ( %" PRIuMAX ") not equal to\nexpected (%" PRIuMAX ")", actual_result,
  67. data->expected_result);
  68. }
  69. END_PARAMETRIZED_TEST
  70. /* --------------------------------------------------------------------------------------------- */
  71. int
  72. main (void)
  73. {
  74. TCase *tc_core;
  75. tc_core = tcase_create ("Core");
  76. tcase_add_checked_fixture (tc_core, setup, teardown);
  77. // Add new tests here: ***************
  78. mctest_add_parameterized_test (tc_core, parse_integer_test, parse_integer_test_ds);
  79. // ***********************************
  80. return mctest_run_all (tc_core);
  81. }
  82. /* --------------------------------------------------------------------------------------------- */