UtilTest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_SUITE_END();
  12. private:
  13. public:
  14. void setUp() {
  15. }
  16. void testTrim();
  17. void testSplit();
  18. void testSlice();
  19. void testEndsWith();
  20. };
  21. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  22. void UtilTest::testTrim() {
  23. string str1 = "aria2";
  24. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("aria2"));
  25. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2"));
  26. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  27. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  28. string str2 = "aria2 debut";
  29. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  30. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  31. string str3 = "";
  32. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  33. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  34. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  35. string str4 = "A";
  36. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  37. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  38. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  39. }
  40. void UtilTest::testSplit() {
  41. pair<string, string> p1;
  42. Util::split(p1, "name=value", '=');
  43. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  44. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  45. Util::split(p1, " name = value ", '=');
  46. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  47. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  48. Util::split(p1, "=value", '=');
  49. CPPUNIT_ASSERT_EQUAL(string(""), p1.first);
  50. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  51. Util::split(p1, "name=", '=');
  52. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  53. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  54. Util::split(p1, "name", '=');
  55. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  56. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  57. }
  58. void UtilTest::testSlice() {
  59. vector<string> v1;
  60. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';');
  61. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  62. v1.clear();
  63. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';');
  64. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  65. vector<string>::iterator itr = v1.begin();
  66. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  67. CPPUNIT_ASSERT_EQUAL(string("name2=value2"), *itr++);
  68. CPPUNIT_ASSERT_EQUAL(string("name3=value3"), *itr++);
  69. }
  70. void UtilTest::testEndsWith() {
  71. string target = "abcdefg";
  72. string part = "fg";
  73. CPPUNIT_ASSERT(Util::endsWith(target, part));
  74. target = "abdefg";
  75. part = "g";
  76. CPPUNIT_ASSERT(Util::endsWith(target, part));
  77. target = "abdefg";
  78. part = "eg";
  79. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  80. target = "g";
  81. part = "eg";
  82. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  83. target = "g";
  84. part = "g";
  85. CPPUNIT_ASSERT(Util::endsWith(target, part));
  86. target = "g";
  87. part = "";
  88. CPPUNIT_ASSERT(Util::endsWith(target, part));
  89. target = "";
  90. part = "";
  91. CPPUNIT_ASSERT(Util::endsWith(target, part));
  92. target = "";
  93. part = "g";
  94. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  95. }