testStringAlgorithms.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 cmJoin
  35. {
  36. typedef std::string ST;
  37. typedef std::vector<std::string> VT;
  38. assert_string(cmJoin(ST("abc"), ";"), "a;b;c", "cmJoin std::string");
  39. assert_string(cmJoin(VT{}, ";"), "", "cmJoin std::vector empty");
  40. assert_string(cmJoin(VT{ "a" }, ";"), "a", "cmJoin std::vector single");
  41. assert_string(cmJoin(VT{ "a", "b", "c" }, ";"), "a;b;c",
  42. "cmJoin std::vector multiple");
  43. assert_string(cmJoin(VT{ "a", "b", "c" }, "<=>"), "a<=>b<=>c",
  44. "cmJoin std::vector long sep");
  45. }
  46. // ----------------------------------------------------------------------
  47. // Test cmStrCat
  48. {
  49. int ni = -1100;
  50. unsigned int nui = 1100u;
  51. long int nli = -12000l;
  52. unsigned long int nuli = 12000ul;
  53. long long int nlli = -130000ll;
  54. unsigned long long int nulli = 130000ull;
  55. std::string val =
  56. cmStrCat("<test>", ni, ',', nui, ',', nli, ",", nuli, ", ", nlli,
  57. std::string(", "), nulli, cm::string_view("</test>"));
  58. std::string expect =
  59. "<test>-1100,1100,-12000,12000, -130000, 130000</test>";
  60. assert_string(val, expect, "cmStrCat strings and integers");
  61. }
  62. {
  63. float const val = 1.5f;
  64. float const div = 0.00001f;
  65. float f = 0.0f;
  66. std::istringstream(cmStrCat("", val)) >> f;
  67. f -= val;
  68. assert_ok((f < div) && (f > -div), "cmStrCat float");
  69. }
  70. {
  71. double const val = 1.5;
  72. double const div = 0.00001;
  73. double d = 0.0;
  74. std::istringstream(cmStrCat("", val)) >> d;
  75. d -= val;
  76. assert_ok((d < div) && (d > -div), "cmStrCat double");
  77. }
  78. // ----------------------------------------------------------------------
  79. // Test cmWrap
  80. {
  81. typedef std::vector<std::string> VT;
  82. assert_string(cmWrap("<", VT{}, ">", "; "), //
  83. "", //
  84. "cmWrap empty, string prefix and suffix");
  85. assert_string(cmWrap("<", VT{ "abc" }, ">", "; "), //
  86. "<abc>", //
  87. "cmWrap single, string prefix and suffix");
  88. assert_string(cmWrap("<", VT{ "a1", "a2", "a3" }, ">", "; "), //
  89. "<a1>; <a2>; <a3>", //
  90. "cmWrap multiple, string prefix and suffix");
  91. assert_string(cmWrap('<', VT{}, '>', "; "), //
  92. "", //
  93. "cmWrap empty, char prefix and suffix");
  94. assert_string(cmWrap('<', VT{ "abc" }, '>', "; "), //
  95. "<abc>", //
  96. "cmWrap single, char prefix and suffix");
  97. assert_string(cmWrap('<', VT{ "a1", "a2", "a3" }, '>', "; "), //
  98. "<a1>; <a2>; <a3>", //
  99. "cmWrap multiple, char prefix and suffix");
  100. }
  101. // ----------------------------------------------------------------------
  102. // Test cmHas(Literal)Prefix and cmHas(Literal)Suffix
  103. {
  104. std::string str("abc");
  105. assert_ok(cmHasPrefix(str, 'a'), "cmHasPrefix char");
  106. assert_ok(!cmHasPrefix(str, 'c'), "cmHasPrefix char not");
  107. assert_ok(cmHasPrefix(str, "ab"), "cmHasPrefix string");
  108. assert_ok(!cmHasPrefix(str, "bc"), "cmHasPrefix string not");
  109. assert_ok(cmHasPrefix(str, str), "cmHasPrefix complete string");
  110. assert_ok(cmHasLiteralPrefix(str, "ab"), "cmHasLiteralPrefix string");
  111. assert_ok(!cmHasLiteralPrefix(str, "bc"), "cmHasLiteralPrefix string not");
  112. assert_ok(cmHasSuffix(str, 'c'), "cmHasSuffix char");
  113. assert_ok(!cmHasSuffix(str, 'a'), "cmHasSuffix char not");
  114. assert_ok(cmHasSuffix(str, "bc"), "cmHasSuffix string");
  115. assert_ok(!cmHasSuffix(str, "ab"), "cmHasSuffix string not");
  116. assert_ok(cmHasSuffix(str, str), "cmHasSuffix complete string");
  117. assert_ok(cmHasLiteralSuffix(str, "bc"), "cmHasLiteralSuffix string");
  118. assert_ok(!cmHasLiteralSuffix(str, "ab"), "cmHasLiteralPrefix string not");
  119. }
  120. return failed;
  121. }