UtilTest.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(testStartsWith);
  13. // may be moved to other helper class in the future.
  14. CPPUNIT_TEST(testGetContentDispositionFilename);
  15. CPPUNIT_TEST_SUITE_END();
  16. private:
  17. public:
  18. void setUp() {
  19. }
  20. void testTrim();
  21. void testSplit();
  22. void testSlice();
  23. void testEndsWith();
  24. void testReplace();
  25. void testStartsWith();
  26. // may be moved to other helper class in the future.
  27. void testGetContentDispositionFilename();
  28. };
  29. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  30. void UtilTest::testTrim() {
  31. string str1 = "aria2";
  32. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("aria2"));
  33. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2"));
  34. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  35. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  36. string str2 = "aria2 debut";
  37. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  38. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  39. string str3 = "";
  40. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  41. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  42. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  43. string str4 = "A";
  44. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  45. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  46. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  47. }
  48. void UtilTest::testSplit() {
  49. pair<string, string> p1;
  50. Util::split(p1, "name=value", '=');
  51. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  52. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  53. Util::split(p1, " name = value ", '=');
  54. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  55. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  56. Util::split(p1, "=value", '=');
  57. CPPUNIT_ASSERT_EQUAL(string(""), p1.first);
  58. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  59. Util::split(p1, "name=", '=');
  60. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  61. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  62. Util::split(p1, "name", '=');
  63. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  64. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  65. }
  66. void UtilTest::testSlice() {
  67. Strings v1;
  68. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';');
  69. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  70. v1.clear();
  71. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';');
  72. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  73. Strings::iterator itr = v1.begin();
  74. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  75. CPPUNIT_ASSERT_EQUAL(string("name2=value2"), *itr++);
  76. CPPUNIT_ASSERT_EQUAL(string("name3=value3"), *itr++);
  77. }
  78. void UtilTest::testEndsWith() {
  79. string target = "abcdefg";
  80. string part = "fg";
  81. CPPUNIT_ASSERT(Util::endsWith(target, part));
  82. target = "abdefg";
  83. part = "g";
  84. CPPUNIT_ASSERT(Util::endsWith(target, part));
  85. target = "abdefg";
  86. part = "eg";
  87. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  88. target = "g";
  89. part = "eg";
  90. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  91. target = "g";
  92. part = "g";
  93. CPPUNIT_ASSERT(Util::endsWith(target, part));
  94. target = "g";
  95. part = "";
  96. CPPUNIT_ASSERT(Util::endsWith(target, part));
  97. target = "";
  98. part = "";
  99. CPPUNIT_ASSERT(Util::endsWith(target, part));
  100. target = "";
  101. part = "g";
  102. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  103. }
  104. void UtilTest::testReplace() {
  105. CPPUNIT_ASSERT_EQUAL(string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  106. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  107. CPPUNIT_ASSERT_EQUAL(string(""), Util::replace("", "\r\n", ""));
  108. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc", "", "a"));
  109. CPPUNIT_ASSERT_EQUAL(string("xbc"), Util::replace("abc", "a", "x"));
  110. }
  111. void UtilTest::testStartsWith() {
  112. string target;
  113. string part;
  114. target = "abcdefg";
  115. part = "abc";
  116. CPPUNIT_ASSERT(Util::startsWith(target, part));
  117. target = "abcdefg";
  118. part = "abx";
  119. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  120. target = "abcdefg";
  121. part = "bcd";
  122. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  123. target = "";
  124. part = "a";
  125. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  126. target = "";
  127. part = "";
  128. CPPUNIT_ASSERT(Util::startsWith(target, part));
  129. target = "a";
  130. part = "";
  131. CPPUNIT_ASSERT(Util::startsWith(target, part));
  132. target = "a";
  133. part = "a";
  134. CPPUNIT_ASSERT(Util::startsWith(target, part));
  135. }
  136. void UtilTest::testGetContentDispositionFilename() {
  137. string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  138. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h1));
  139. string h2 = "attachment; filename=\"\"";
  140. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h2));
  141. string h3 = "attachment; filename=\"";
  142. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h3));
  143. string h4 = "attachment;";
  144. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h4));
  145. }