UtilTest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "Util.h"
  2. #include <string>
  3. #include <cppunit/extensions/HelperMacros.h>
  4. using namespace std;
  5. class UtilTest:public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(UtilTest);
  7. CPPUNIT_TEST(testTrim);
  8. CPPUNIT_TEST(testSplit);
  9. CPPUNIT_TEST(testSlice);
  10. CPPUNIT_TEST(testEndsWith);
  11. CPPUNIT_TEST(testReplace);
  12. CPPUNIT_TEST_SUITE_END();
  13. private:
  14. public:
  15. void setUp() {
  16. }
  17. void testTrim();
  18. void testSplit();
  19. void testSlice();
  20. void testEndsWith();
  21. void testReplace();
  22. };
  23. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  24. void UtilTest::testTrim() {
  25. string str1 = "aria2";
  26. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("aria2"));
  27. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2"));
  28. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  29. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  30. string str2 = "aria2 debut";
  31. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  32. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  33. string str3 = "";
  34. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  35. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  36. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  37. string str4 = "A";
  38. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  39. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  40. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  41. }
  42. void UtilTest::testSplit() {
  43. pair<string, string> p1;
  44. Util::split(p1, "name=value", '=');
  45. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  46. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  47. Util::split(p1, " name = value ", '=');
  48. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  49. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  50. Util::split(p1, "=value", '=');
  51. CPPUNIT_ASSERT_EQUAL(string(""), p1.first);
  52. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  53. Util::split(p1, "name=", '=');
  54. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  55. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  56. Util::split(p1, "name", '=');
  57. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  58. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  59. }
  60. void UtilTest::testSlice() {
  61. vector<string> v1;
  62. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';');
  63. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  64. v1.clear();
  65. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';');
  66. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  67. vector<string>::iterator itr = v1.begin();
  68. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  69. CPPUNIT_ASSERT_EQUAL(string("name2=value2"), *itr++);
  70. CPPUNIT_ASSERT_EQUAL(string("name3=value3"), *itr++);
  71. }
  72. void UtilTest::testEndsWith() {
  73. string target = "abcdefg";
  74. string part = "fg";
  75. CPPUNIT_ASSERT(Util::endsWith(target, part));
  76. target = "abdefg";
  77. part = "g";
  78. CPPUNIT_ASSERT(Util::endsWith(target, part));
  79. target = "abdefg";
  80. part = "eg";
  81. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  82. target = "g";
  83. part = "eg";
  84. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  85. target = "g";
  86. part = "g";
  87. CPPUNIT_ASSERT(Util::endsWith(target, part));
  88. target = "g";
  89. part = "";
  90. CPPUNIT_ASSERT(Util::endsWith(target, part));
  91. target = "";
  92. part = "";
  93. CPPUNIT_ASSERT(Util::endsWith(target, part));
  94. target = "";
  95. part = "g";
  96. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  97. }
  98. void UtilTest::testReplace() {
  99. CPPUNIT_ASSERT_EQUAL(string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  100. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  101. CPPUNIT_ASSERT_EQUAL(string(""), Util::replace("", "\r\n", ""));
  102. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc", "", "a"));
  103. CPPUNIT_ASSERT_EQUAL(string("xbc"), Util::replace("abc", "a", "x"));
  104. }