replace__str_replace_all.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. lib/strutil - tests for lib/strutil/replace:str_replace_all() function.
  3. Copyright (C) 2013
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 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 <config.h>
  21. #include <check.h>
  22. #include "lib/global.h"
  23. #include "lib/strutil.h"
  24. /* --------------------------------------------------------------------------------------------- */
  25. /* @Before */
  26. static void
  27. setup (void)
  28. {
  29. str_init_strings (NULL);
  30. }
  31. /* --------------------------------------------------------------------------------------------- */
  32. /* @After */
  33. static void
  34. teardown (void)
  35. {
  36. str_uninit_strings ();
  37. }
  38. /* --------------------------------------------------------------------------------------------- */
  39. /* @DataSource("str_replace_all_test_ds") */
  40. /* *INDENT-OFF* */
  41. static const struct str_replace_all_test_ds
  42. {
  43. const char *haystack;
  44. const char *needle;
  45. const char *replacement;
  46. const char *expected_result;
  47. } str_replace_all_test_ds[] =
  48. {
  49. {
  50. /*needle not found*/
  51. "needle not found",
  52. "blablablabla",
  53. "1234567890",
  54. "needle not found",
  55. },
  56. {
  57. /* replacement is less rather that needle */
  58. "some string blablablabla string",
  59. "blablablabla",
  60. "1234",
  61. "some string 1234 string",
  62. },
  63. {
  64. /* replacement is great rather that needle */
  65. "some string bla string",
  66. "bla",
  67. "1234567890",
  68. "some string 1234567890 string",
  69. },
  70. {
  71. /* replace few substrings in a start of string */
  72. "blabla blabla string",
  73. "blabla",
  74. "111111111",
  75. "111111111 111111111 string",
  76. },
  77. {
  78. /* replace few substrings in a middle of string */
  79. "some string blabla str blabla string",
  80. "blabla",
  81. "111111111",
  82. "some string 111111111 str 111111111 string",
  83. },
  84. {
  85. /* replace few substrings in an end of string */
  86. "some string blabla str blabla",
  87. "blabla",
  88. "111111111",
  89. "some string 111111111 str 111111111",
  90. },
  91. {
  92. /* escaped substring */
  93. "\\blabla blabla",
  94. "blabla",
  95. "111111111",
  96. "blabla 111111111",
  97. },
  98. {
  99. /* escaped substring */
  100. "str \\blabla blabla",
  101. "blabla",
  102. "111111111",
  103. "str blabla 111111111",
  104. },
  105. {
  106. /* escaped substring */
  107. "str \\\\\\blabla blabla",
  108. "blabla",
  109. "111111111",
  110. "str \\\\blabla 111111111",
  111. },
  112. {
  113. /* double-escaped substring (actually non-escaped) */
  114. "\\\\blabla blabla",
  115. "blabla",
  116. "111111111",
  117. "\\\\111111111 111111111",
  118. },
  119. {
  120. /* partial substring */
  121. "blablabla",
  122. "blabla",
  123. "111111111",
  124. "111111111bla",
  125. },
  126. {
  127. /* special symbols */
  128. "bla bla",
  129. "bla",
  130. "111\t1 1\n1111",
  131. "111\t1 1\n1111 111\t1 1\n1111",
  132. },
  133. };
  134. /* *INDENT-ON* */
  135. /* @Test(dataSource = "str_replace_all_test_ds") */
  136. /* *INDENT-OFF* */
  137. START_TEST (str_replace_all_test)
  138. /* *INDENT-ON* */
  139. {
  140. /* given */
  141. char *actual_result;
  142. const struct str_replace_all_test_ds *data = &str_replace_all_test_ds[_i];
  143. /* when */
  144. actual_result = str_replace_all (data->haystack, data->needle, data->replacement);
  145. /* then */
  146. g_assert_cmpstr (actual_result, ==, data->expected_result);
  147. g_free (actual_result);
  148. }
  149. /* *INDENT-OFF* */
  150. END_TEST
  151. /* *INDENT-ON* */
  152. /* --------------------------------------------------------------------------------------------- */
  153. int
  154. main (void)
  155. {
  156. int number_failed;
  157. Suite *s = suite_create (TEST_SUITE_NAME);
  158. TCase *tc_core = tcase_create ("Core");
  159. SRunner *sr;
  160. tcase_add_checked_fixture (tc_core, setup, teardown);
  161. /* Add new tests here: *************** */
  162. tcase_add_loop_test (tc_core, str_replace_all_test, 0,
  163. sizeof (str_replace_all_test_ds) / sizeof (str_replace_all_test_ds[0]));
  164. /* *********************************** */
  165. suite_add_tcase (s, tc_core);
  166. sr = srunner_create (s);
  167. srunner_set_log (sr, "replace__str_replace_all.log");
  168. srunner_run_all (sr, CK_NOFORK);
  169. number_failed = srunner_ntests_failed (sr);
  170. srunner_free (sr);
  171. return (number_failed == 0) ? 0 : 1;
  172. }
  173. /* --------------------------------------------------------------------------------------------- */