parse_integer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. lib/strutil - tests for lib/strutil/parse_integer function.
  3. Copyright (C) 2013-2019
  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. fail_unless (invalid == data->invalid && actual_result == data->expected_result,
  115. "actial ( %" 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. int number_failed;
  126. Suite *s = suite_create (TEST_SUITE_NAME);
  127. TCase *tc_core = tcase_create ("Core");
  128. SRunner *sr;
  129. tcase_add_checked_fixture (tc_core, setup, teardown);
  130. /* Add new tests here: *************** */
  131. mctest_add_parameterized_test (tc_core, parse_integer_test, parse_integer_test_ds);
  132. /* *********************************** */
  133. suite_add_tcase (s, tc_core);
  134. sr = srunner_create (s);
  135. srunner_set_log (sr, "parse_integer.log");
  136. srunner_run_all (sr, CK_ENV);
  137. number_failed = srunner_ntests_failed (sr);
  138. srunner_free (sr);
  139. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  140. }
  141. /* --------------------------------------------------------------------------------------------- */