1
0

testStringAlgorithms.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include <cmConfigure.h> // IWYU pragma: keep
  4. #include "cm_string_view.hxx"
  5. #include <iostream>
  6. #include <sstream>
  7. #include <string>
  8. #include <vector>
  9. #include "cmStringAlgorithms.h"
  10. int testStringAlgorithms(int /*unused*/, char* /*unused*/ [])
  11. {
  12. int failed = 0;
  13. auto assert_ok = [&failed](bool test, cm::string_view title) {
  14. if (test) {
  15. std::cout << "Passed: " << title << "\n";
  16. } else {
  17. std::cout << "Failed: " << title << "\n";
  18. ++failed;
  19. }
  20. };
  21. auto assert_string = [&failed](cm::string_view generated,
  22. cm::string_view expected,
  23. cm::string_view title) {
  24. if (generated == expected) {
  25. std::cout << "Passed: " << title << "\n";
  26. } else {
  27. std::cout << "Failed: " << title << "\n";
  28. std::cout << "Expected: " << expected << "\n";
  29. std::cout << "Got: " << generated << "\n";
  30. ++failed;
  31. }
  32. };
  33. // ----------------------------------------------------------------------
  34. // Test cmTrimWhitespace
  35. {
  36. std::string base = "base";
  37. std::string spaces = " \f\f\n\n\r\r\t\t\v\v";
  38. assert_string(cmTrimWhitespace(spaces + base), base,
  39. "cmTrimWhitespace front");
  40. assert_string(cmTrimWhitespace(base + spaces), base,
  41. "cmTrimWhitespace back");
  42. assert_string(cmTrimWhitespace(spaces + base + spaces), base,
  43. "cmTrimWhitespace front and back");
  44. }
  45. // ----------------------------------------------------------------------
  46. // Test cmEscapeQuotes
  47. {
  48. assert_string(cmEscapeQuotes("plain"), "plain", "cmEscapeQuotes plain");
  49. std::string base = "\"base\"\"";
  50. std::string result = "\\\"base\\\"\\\"";
  51. assert_string(cmEscapeQuotes(base), result, "cmEscapeQuotes escaped");
  52. }
  53. // ----------------------------------------------------------------------
  54. // Test cmJoin
  55. {
  56. typedef std::string ST;
  57. typedef std::vector<std::string> VT;
  58. assert_string(cmJoin(ST("abc"), ";"), "a;b;c", "cmJoin std::string");
  59. assert_string(cmJoin(VT{}, ";"), "", "cmJoin std::vector empty");
  60. assert_string(cmJoin(VT{ "a" }, ";"), "a", "cmJoin std::vector single");
  61. assert_string(cmJoin(VT{ "a", "b", "c" }, ";"), "a;b;c",
  62. "cmJoin std::vector multiple");
  63. assert_string(cmJoin(VT{ "a", "b", "c" }, "<=>"), "a<=>b<=>c",
  64. "cmJoin std::vector long sep");
  65. }
  66. // ----------------------------------------------------------------------
  67. // Test cmTokenize
  68. {
  69. typedef std::vector<std::string> VT;
  70. assert_ok(cmTokenize("", ";") == VT{ "" }, "cmTokenize empty");
  71. assert_ok(cmTokenize(";", ";") == VT{ "" }, "cmTokenize sep");
  72. assert_ok(cmTokenize("abc", ";") == VT{ "abc" }, "cmTokenize item");
  73. assert_ok(cmTokenize("abc;", ";") == VT{ "abc" }, "cmTokenize item sep");
  74. assert_ok(cmTokenize(";abc", ";") == VT{ "abc" }, "cmTokenize sep item");
  75. assert_ok(cmTokenize("abc;;efg", ";") == VT{ "abc", "efg" },
  76. "cmTokenize item sep sep item");
  77. assert_ok(cmTokenize("a1;a2;a3;a4", ";") == VT{ "a1", "a2", "a3", "a4" },
  78. "cmTokenize multiple items");
  79. }
  80. // ----------------------------------------------------------------------
  81. // Test cmStrCat
  82. {
  83. int ni = -1100;
  84. unsigned int nui = 1100u;
  85. long int nli = -12000l;
  86. unsigned long int nuli = 12000ul;
  87. long long int nlli = -130000ll;
  88. unsigned long long int nulli = 130000ull;
  89. std::string val =
  90. cmStrCat("<test>", ni, ',', nui, ',', nli, ",", nuli, ", ", nlli,
  91. std::string(", "), nulli, cm::string_view("</test>"));
  92. std::string expect =
  93. "<test>-1100,1100,-12000,12000, -130000, 130000</test>";
  94. assert_string(val, expect, "cmStrCat strings and integers");
  95. }
  96. {
  97. float const val = 1.5f;
  98. float const div = 0.00001f;
  99. float f = 0.0f;
  100. std::istringstream(cmStrCat("", val)) >> f;
  101. f -= val;
  102. assert_ok((f < div) && (f > -div), "cmStrCat float");
  103. }
  104. {
  105. double const val = 1.5;
  106. double const div = 0.00001;
  107. double d = 0.0;
  108. std::istringstream(cmStrCat("", val)) >> d;
  109. d -= val;
  110. assert_ok((d < div) && (d > -div), "cmStrCat double");
  111. }
  112. // ----------------------------------------------------------------------
  113. // Test cmWrap
  114. {
  115. typedef std::vector<std::string> VT;
  116. assert_string(cmWrap("<", VT{}, ">", "; "), //
  117. "", //
  118. "cmWrap empty, string prefix and suffix");
  119. assert_string(cmWrap("<", VT{ "abc" }, ">", "; "), //
  120. "<abc>", //
  121. "cmWrap single, string prefix and suffix");
  122. assert_string(cmWrap("<", VT{ "a1", "a2", "a3" }, ">", "; "), //
  123. "<a1>; <a2>; <a3>", //
  124. "cmWrap multiple, string prefix and suffix");
  125. assert_string(cmWrap('<', VT{}, '>', "; "), //
  126. "", //
  127. "cmWrap empty, char prefix and suffix");
  128. assert_string(cmWrap('<', VT{ "abc" }, '>', "; "), //
  129. "<abc>", //
  130. "cmWrap single, char prefix and suffix");
  131. assert_string(cmWrap('<', VT{ "a1", "a2", "a3" }, '>', "; "), //
  132. "<a1>; <a2>; <a3>", //
  133. "cmWrap multiple, char prefix and suffix");
  134. }
  135. // ----------------------------------------------------------------------
  136. // Test cmHas(Literal)Prefix and cmHas(Literal)Suffix
  137. {
  138. std::string str("abc");
  139. assert_ok(cmHasPrefix(str, 'a'), "cmHasPrefix char");
  140. assert_ok(!cmHasPrefix(str, 'c'), "cmHasPrefix char not");
  141. assert_ok(cmHasPrefix(str, "ab"), "cmHasPrefix string");
  142. assert_ok(!cmHasPrefix(str, "bc"), "cmHasPrefix string not");
  143. assert_ok(cmHasPrefix(str, str), "cmHasPrefix complete string");
  144. assert_ok(cmHasLiteralPrefix(str, "ab"), "cmHasLiteralPrefix string");
  145. assert_ok(!cmHasLiteralPrefix(str, "bc"), "cmHasLiteralPrefix string not");
  146. assert_ok(cmHasSuffix(str, 'c'), "cmHasSuffix char");
  147. assert_ok(!cmHasSuffix(str, 'a'), "cmHasSuffix char not");
  148. assert_ok(cmHasSuffix(str, "bc"), "cmHasSuffix string");
  149. assert_ok(!cmHasSuffix(str, "ab"), "cmHasSuffix string not");
  150. assert_ok(cmHasSuffix(str, str), "cmHasSuffix complete string");
  151. assert_ok(cmHasLiteralSuffix(str, "bc"), "cmHasLiteralSuffix string");
  152. assert_ok(!cmHasLiteralSuffix(str, "ab"), "cmHasLiteralPrefix string not");
  153. }
  154. return failed;
  155. }