str_rstrip_eol.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. lib/strutil - tests for lib/strutil.c:str_rstrip_eol function
  3. Copyright (C) 2025
  4. Free Software Foundation, Inc.
  5. This file is part of the Midnight Commander.
  6. The Midnight Commander is free software: you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation, either version 3 of the License,
  9. or (at your option) any later version.
  10. The Midnight Commander is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. #define TEST_SUITE_NAME "/lib/strutil"
  18. #include "tests/mctest.h"
  19. #include "lib/strutil.h"
  20. /* --------------------------------------------------------------------------------------------- */
  21. /* @Before */
  22. static void
  23. setup (void)
  24. {
  25. }
  26. /* --------------------------------------------------------------------------------------------- */
  27. /* @After */
  28. static void
  29. teardown (void)
  30. {
  31. }
  32. /* --------------------------------------------------------------------------------------------- */
  33. /* @DataSource("str_rstrip_eol_test_ds1") */
  34. /* Testcases are taken from Glib */
  35. static const struct str_rstrip_eol_test_struct
  36. {
  37. const char *input_string;
  38. const char *expected_result;
  39. } str_rstrip_eol_test_ds1[] = {
  40. {
  41. "",
  42. "",
  43. },
  44. {
  45. " \n\r",
  46. " \n",
  47. },
  48. {
  49. " \t\r\n",
  50. " \t",
  51. },
  52. {
  53. "a \r ",
  54. "a \r ",
  55. },
  56. {
  57. " a \n ",
  58. " a \n ",
  59. },
  60. {
  61. "a a\n\r\n",
  62. "a a\n",
  63. },
  64. {
  65. "\na a \r",
  66. "\na a ",
  67. },
  68. };
  69. /* @Test(dataSource = "str_rstrip_eol_test_ds1") */
  70. START_TEST (str_rstrip_eol_test1)
  71. {
  72. /* given */
  73. const struct str_rstrip_eol_test_struct *data = &str_rstrip_eol_test_ds1[_i];
  74. /* when */
  75. char *actual_result = g_strdup (data->input_string);
  76. str_rstrip_eol (actual_result);
  77. /* then */
  78. ck_assert_str_eq (actual_result, data->expected_result);
  79. g_free (actual_result);
  80. }
  81. END_TEST
  82. /* --------------------------------------------------------------------------------------------- */
  83. START_TEST (str_rstrip_eol_test_null)
  84. {
  85. char *ptr = NULL;
  86. str_rstrip_eol (ptr);
  87. ck_assert_ptr_null (ptr);
  88. }
  89. END_TEST
  90. /* --------------------------------------------------------------------------------------------- */
  91. int
  92. main (void)
  93. {
  94. TCase *tc_core;
  95. tc_core = tcase_create ("Core");
  96. tcase_add_checked_fixture (tc_core, setup, teardown);
  97. /* Add new tests here: *************** */
  98. mctest_add_parameterized_test (tc_core, str_rstrip_eol_test1, str_rstrip_eol_test_ds1);
  99. tcase_add_test (tc_core, str_rstrip_eol_test_null);
  100. /* *********************************** */
  101. return mctest_run_all (tc_core);
  102. }
  103. /* --------------------------------------------------------------------------------------------- */