parse_integer.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. lib/strutil - tests for lib/strutil/parse_integer function.
  3. Copyright (C) 2013
  4. The 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("str_replace_all_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 = "str_replace_all_test_ds") */
  104. /* *INDENT-OFF* */
  105. START_TEST (parse_integer_test)
  106. /* *INDENT-ON* */
  107. {
  108. /* given */
  109. uintmax_t actual_result;
  110. gboolean invalid = FALSE;
  111. const struct parse_integer_test_ds *data = &parse_integer_test_ds[_i];
  112. /* when */
  113. actual_result = parse_integer (data->haystack, &invalid);
  114. /* then */
  115. fail_unless (invalid == data->invalid && actual_result == data->expected_result,
  116. "actial ( %" PRIuMAX ") not equal to\nexpected (%" PRIuMAX ")",
  117. actual_result, data->expected_result);
  118. }
  119. /* *INDENT-OFF* */
  120. END_TEST
  121. /* *INDENT-ON* */
  122. /* --------------------------------------------------------------------------------------------- */
  123. int
  124. main (void)
  125. {
  126. int number_failed;
  127. Suite *s = suite_create (TEST_SUITE_NAME);
  128. TCase *tc_core = tcase_create ("Core");
  129. SRunner *sr;
  130. tcase_add_checked_fixture (tc_core, setup, teardown);
  131. /* Add new tests here: *************** */
  132. tcase_add_loop_test (tc_core, parse_integer_test, 0, G_N_ELEMENTS (parse_integer_test_ds));
  133. /* *********************************** */
  134. suite_add_tcase (s, tc_core);
  135. sr = srunner_create (s);
  136. srunner_set_log (sr, "parse_integer.log");
  137. srunner_run_all (sr, CK_NOFORK);
  138. number_failed = srunner_ntests_failed (sr);
  139. srunner_free (sr);
  140. return (number_failed == 0) ? 0 : 1;
  141. }
  142. /* --------------------------------------------------------------------------------------------- */