string.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <iostream>
  2. #include "../core/containers/Str.h"
  3. using namespace Crown;
  4. using namespace std;
  5. template<typename T>
  6. void TestResult(T actual, T expected);
  7. void SplitTest();
  8. void RemoveTest();
  9. void ReplaceTest();
  10. int tests = 0;
  11. int successes = 0;
  12. int main() {
  13. SplitTest();
  14. cout << endl;
  15. RemoveTest();
  16. cout << endl;
  17. ReplaceTest();
  18. cout << endl;
  19. cout << "Test result: " << successes << "/" << tests << endl;
  20. cout << endl;
  21. return 0;
  22. }
  23. void SplitTest()
  24. {
  25. Str str = " abc def ghi,e asdgfe ", rebuilt;
  26. List<Str> split;
  27. str.Split(' ', split);
  28. for(int i = 0; i < split.GetSize(); i++)
  29. {
  30. rebuilt += split[i];
  31. if (i < split.GetSize()-1)
  32. rebuilt += " ";
  33. }
  34. cout << " = Split test = " << endl;
  35. cout << "- Split count ";
  36. TestResult<int>(split.GetSize(), 6);
  37. cout << "- Rebuilding split string ";
  38. TestResult<const Str&>(rebuilt, str);
  39. }
  40. void RemoveTest()
  41. {
  42. Str str = "il gatto salta di notte";
  43. Str strBegin = str;
  44. Str strEnd = str;
  45. Str strMiddle = str;
  46. strBegin.Remove(0, 3);
  47. strEnd.Remove(strEnd.GetLength()-6, strEnd.GetLength());
  48. strMiddle.Remove(3, 9);
  49. cout << " = Remove test = " << endl;
  50. cout << "- Remove from begin ";
  51. TestResult<const Str&>(strBegin, "gatto salta di notte");
  52. cout << str.c_str() << " -> " << strBegin.c_str() << endl << endl;
  53. cout << "- Remove from end ";
  54. TestResult<const Str&>(strEnd, "il gatto salta di");
  55. cout << str.c_str() << " -> " << strEnd.c_str() << endl << endl;
  56. cout << "- Remove from middle ";
  57. TestResult<const Str&>(strMiddle, "il salta di notte");
  58. cout << str.c_str() << " -> " << strMiddle.c_str() << endl << endl;
  59. }
  60. void ReplaceTest()
  61. {
  62. Str str = "il gatto salta di notte";
  63. Str strBegin = str;
  64. Str strEnd = str;
  65. Str strMiddle = str;
  66. strBegin.Replace("il gatto", "la gatta non");
  67. strEnd.Replace("notte", "largo anticipo");
  68. strMiddle.Replace("salta", "dorme");
  69. cout << " = Replace test = " << endl;
  70. cout << "- Replace from begin ";
  71. TestResult<const Str&>(strBegin, "la gatta non salta di notte");
  72. cout << str.c_str() << " -> " << strBegin.c_str() << endl << endl;
  73. cout << "- Replace from end ";
  74. TestResult<const Str&>(strEnd, "il gatto salta di largo anticipo");
  75. cout << str.c_str() << " -> " << strEnd.c_str() << endl << endl;
  76. cout << "- Replace from middle ";
  77. TestResult<const Str&>(strMiddle, "il gatto dorme di notte");
  78. cout << str.c_str() << " -> " << strMiddle.c_str() << endl << endl;
  79. }
  80. template<typename T>
  81. void TestResult(T actual, T expected)
  82. {
  83. tests++;
  84. if (actual == expected)
  85. {
  86. cout << "[Ok]" << endl;
  87. successes++;
  88. }
  89. else
  90. cout << "[Failure]" << endl;
  91. }