parse_integer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. lib/strutil - tests for lib/strutil/parse_integer function.
  3. Copyright (C) 2013-2024
  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 <http://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. /* *INDENT-OFF* */
  38. static const struct parse_integer_test_ds
  39. {
  40. const char *haystack;
  41. uintmax_t expected_result;
  42. gboolean invalid;
  43. } parse_integer_test_ds[] =
  44. {
  45. {
  46. /* too big */
  47. "99999999999999999999999999999999999999999999999999999999999999999999",
  48. 0,
  49. TRUE
  50. },
  51. {
  52. "x",
  53. 0,
  54. TRUE
  55. },
  56. {
  57. "9x",
  58. 0,
  59. TRUE
  60. },
  61. {
  62. "1",
  63. 1,
  64. FALSE
  65. },
  66. {
  67. "-1",
  68. 0,
  69. TRUE
  70. },
  71. {
  72. "1k",
  73. 1024,
  74. FALSE
  75. },
  76. {
  77. "1K",
  78. 1024,
  79. FALSE
  80. },
  81. {
  82. "1M",
  83. 1024 * 1024,
  84. FALSE
  85. },
  86. {
  87. "1m",
  88. 0,
  89. TRUE
  90. },
  91. {
  92. "64M",
  93. 64 * 1024 * 1024,
  94. FALSE
  95. },
  96. {
  97. "1G",
  98. 1 * 1024 * 1024 * 1024,
  99. FALSE
  100. }
  101. };
  102. /* *INDENT-ON* */
  103. /* @Test(dataSource = "parse_integer_test_ds") */
  104. /* *INDENT-OFF* */
  105. START_PARAMETRIZED_TEST (parse_integer_test, parse_integer_test_ds)
  106. /* *INDENT-ON* */
  107. {
  108. /* given */
  109. uintmax_t actual_result;
  110. gboolean invalid = FALSE;
  111. /* when */
  112. actual_result = parse_integer (data->haystack, &invalid);
  113. /* then */
  114. ck_assert_msg (invalid == data->invalid && actual_result == data->expected_result,
  115. "actual ( %" PRIuMAX ") not equal to\nexpected (%" PRIuMAX ")",
  116. actual_result, data->expected_result);
  117. }
  118. /* *INDENT-OFF* */
  119. END_PARAMETRIZED_TEST
  120. /* *INDENT-ON* */
  121. /* --------------------------------------------------------------------------------------------- */
  122. int
  123. main (void)
  124. {
  125. TCase *tc_core;
  126. tc_core = tcase_create ("Core");
  127. tcase_add_checked_fixture (tc_core, setup, teardown);
  128. /* Add new tests here: *************** */
  129. mctest_add_parameterized_test (tc_core, parse_integer_test, parse_integer_test_ds);
  130. /* *********************************** */
  131. return mctest_run_all (tc_core);
  132. }
  133. /* --------------------------------------------------------------------------------------------- */