StringAlgorithm.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "Test.h"
  2. #include "StringAlgorithm.h"
  3. #include <Logging.h>
  4. #include <string>
  5. #include <sstream>
  6. #include <vector>
  7. namespace Utilities {
  8. namespace Test {
  9. bool TestSplitInstance(const std::string& subject, const char& delimiter, unsigned expectedSize)
  10. {
  11. std::stringstream ss;
  12. std::vector<std::string> vector = StringAlgorithm::split(subject, delimiter);
  13. ss << "Vector Size - " << vector.size() << std::endl;
  14. for (auto& item : vector)
  15. ss << item << std::endl;
  16. Logging::Log(Logging::Severity::Info, ss.str());
  17. if (vector.size() != expectedSize)
  18. return false;
  19. return true;
  20. }
  21. bool TestSplitInstance(const std::string& subject, const std::string& delimiters, unsigned expectedSize)
  22. {
  23. std::stringstream ss;
  24. std::vector<std::string> vector = StringAlgorithm::split(subject, delimiters);
  25. ss << "Vector Size - " << vector.size() << std::endl;
  26. for (auto& item : vector)
  27. ss << item << std::endl;
  28. Logging::Log(Logging::Severity::Info, ss.str());
  29. if (vector.size() != expectedSize)
  30. return false;
  31. return true;
  32. }
  33. bool TestSplit()
  34. {
  35. bool success = true;
  36. std::string testString = "a/b/c/d";
  37. success = success && TestSplitInstance(testString, '/', 4);
  38. testString = "a/ /c/ e";
  39. success = success && TestSplitInstance(testString, '/', 4);
  40. testString = "a//c/";
  41. success = success && TestSplitInstance(testString, '/', 4);
  42. testString = "a/.b/";
  43. success = success && TestSplitInstance(testString, "/.", 4);
  44. return success;
  45. }
  46. bool TestIsNumeric()
  47. {
  48. std::stringstream ss;
  49. ss << "\"Test\" != Numeric";
  50. if (StringAlgorithm::is_numeric("Test"))
  51. {
  52. ss << " -> FAIL";
  53. Logging::Log(Logging::Severity::Info, ss.str());
  54. return false;
  55. }
  56. ss << std::endl << "\"1\" == Numeric";
  57. if (!StringAlgorithm::is_numeric("1"))
  58. {
  59. ss << " -> FAIL";
  60. Logging::Log(Logging::Severity::Info, ss.str());
  61. return false;
  62. }
  63. ss << std::endl << "\"1.43\" == Numeric";
  64. if (!StringAlgorithm::is_numeric("1.43"))
  65. {
  66. ss << " -> FAIL";
  67. Logging::Log(Logging::Severity::Info, ss.str());
  68. return false;
  69. }
  70. ss << std::endl << "\"0\" == Numeric";
  71. if (!StringAlgorithm::is_numeric("0"))
  72. {
  73. ss << " -> FAIL";
  74. Logging::Log(Logging::Severity::Info, ss.str());
  75. return false;
  76. }
  77. ss << std::endl << "\"034dfj\" != Numeric";
  78. if (StringAlgorithm::is_numeric("034dfj"))
  79. {
  80. ss << " -> FAIL";
  81. Logging::Log(Logging::Severity::Info, ss.str());
  82. return false;
  83. }
  84. ss << std::endl << "\"dfj354\" != Numeric";
  85. if (StringAlgorithm::is_numeric("dfj354"))
  86. {
  87. ss << " -> FAIL";
  88. Logging::Log(Logging::Severity::Info, ss.str());
  89. return false;
  90. }
  91. Logging::Log(Logging::Severity::Info, ss.str());
  92. return true;
  93. }
  94. bool TestStringAlgorithm()
  95. {
  96. std::stringstream ss;
  97. ss << "----------------------------------" << std::endl;
  98. ss << "| TestStringAlgorithm |" << std::endl;
  99. ss << "----------------------------------" << std::endl;
  100. Logging::Log(Logging::Severity::Info, ss.str());
  101. bool success = TestSplit();
  102. ss.str("");
  103. ss << "----------------------------------" << std::endl;
  104. Logging::Log(Logging::Severity::Info, ss.str());
  105. success = TestIsNumeric() && success;
  106. ss.str("");
  107. ss << "==================================" << std::endl << std::endl;
  108. Logging::Log(Logging::Severity::Info, ss.str());
  109. return success;
  110. }
  111. } // namespace Test
  112. } // namespace Utilities