UtilTest.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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(testComputeFastSet);
  16. CPPUNIT_TEST_SUITE_END();
  17. private:
  18. public:
  19. void setUp() {
  20. }
  21. void testTrim();
  22. void testSplit();
  23. void testSlice();
  24. void testEndsWith();
  25. void testReplace();
  26. void testStartsWith();
  27. void testComputeFastSet();
  28. // may be moved to other helper class in the future.
  29. void testGetContentDispositionFilename();
  30. };
  31. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  32. void UtilTest::testTrim() {
  33. string str1 = "aria2";
  34. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("aria2"));
  35. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2"));
  36. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  37. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  38. string str2 = "aria2 debut";
  39. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  40. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  41. string str3 = "";
  42. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  43. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  44. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  45. string str4 = "A";
  46. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  47. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  48. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  49. }
  50. void UtilTest::testSplit() {
  51. pair<string, string> p1;
  52. Util::split(p1, "name=value", '=');
  53. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  54. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  55. Util::split(p1, " name = value ", '=');
  56. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  57. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  58. Util::split(p1, "=value", '=');
  59. CPPUNIT_ASSERT_EQUAL(string(""), p1.first);
  60. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  61. Util::split(p1, "name=", '=');
  62. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  63. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  64. Util::split(p1, "name", '=');
  65. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  66. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  67. }
  68. void UtilTest::testSlice() {
  69. Strings v1;
  70. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';');
  71. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  72. v1.clear();
  73. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';');
  74. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  75. Strings::iterator itr = v1.begin();
  76. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  77. CPPUNIT_ASSERT_EQUAL(string("name2=value2"), *itr++);
  78. CPPUNIT_ASSERT_EQUAL(string("name3=value3"), *itr++);
  79. }
  80. void UtilTest::testEndsWith() {
  81. string target = "abcdefg";
  82. string part = "fg";
  83. CPPUNIT_ASSERT(Util::endsWith(target, part));
  84. target = "abdefg";
  85. part = "g";
  86. CPPUNIT_ASSERT(Util::endsWith(target, part));
  87. target = "abdefg";
  88. part = "eg";
  89. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  90. target = "g";
  91. part = "eg";
  92. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  93. target = "g";
  94. part = "g";
  95. CPPUNIT_ASSERT(Util::endsWith(target, part));
  96. target = "g";
  97. part = "";
  98. CPPUNIT_ASSERT(Util::endsWith(target, part));
  99. target = "";
  100. part = "";
  101. CPPUNIT_ASSERT(Util::endsWith(target, part));
  102. target = "";
  103. part = "g";
  104. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  105. }
  106. void UtilTest::testReplace() {
  107. CPPUNIT_ASSERT_EQUAL(string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  108. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  109. CPPUNIT_ASSERT_EQUAL(string(""), Util::replace("", "\r\n", ""));
  110. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc", "", "a"));
  111. CPPUNIT_ASSERT_EQUAL(string("xbc"), Util::replace("abc", "a", "x"));
  112. }
  113. void UtilTest::testStartsWith() {
  114. string target;
  115. string part;
  116. target = "abcdefg";
  117. part = "abc";
  118. CPPUNIT_ASSERT(Util::startsWith(target, part));
  119. target = "abcdefg";
  120. part = "abx";
  121. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  122. target = "abcdefg";
  123. part = "bcd";
  124. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  125. target = "";
  126. part = "a";
  127. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  128. target = "";
  129. part = "";
  130. CPPUNIT_ASSERT(Util::startsWith(target, part));
  131. target = "a";
  132. part = "";
  133. CPPUNIT_ASSERT(Util::startsWith(target, part));
  134. target = "a";
  135. part = "a";
  136. CPPUNIT_ASSERT(Util::startsWith(target, part));
  137. }
  138. void UtilTest::testGetContentDispositionFilename() {
  139. string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  140. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h1));
  141. string h2 = "attachment; filename=\"\"";
  142. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h2));
  143. string h3 = "attachment; filename=\"";
  144. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h3));
  145. string h4 = "attachment;";
  146. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h4));
  147. }
  148. class Printer {
  149. public:
  150. template<class T>
  151. void operator()(T t) {
  152. cerr << t << ", ";
  153. }
  154. };
  155. void UtilTest::testComputeFastSet() {
  156. string ipaddr = "192.168.0.1";
  157. unsigned char infoHash[20];
  158. memset(infoHash, 0, sizeof(infoHash));
  159. infoHash[0] = 0xff;
  160. int pieces = 1000;
  161. int fastSetSize = 10;
  162. Integers fastSet = Util::computeFastSet(ipaddr, infoHash, pieces, fastSetSize);
  163. //for_each(fastSet.begin(), fastSet.end(), Printer());
  164. //cerr << endl;
  165. int ans1[] = { 686, 459, 278, 200, 404, 834, 64, 203, 760, 950 };
  166. Integers ansSet1(&ans1[0], &ans1[10]);
  167. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet1.begin()));
  168. ipaddr = "10.0.0.1";
  169. fastSet = Util::computeFastSet(ipaddr, infoHash, pieces, fastSetSize);
  170. int ans2[] = { 568, 188, 466, 452, 550, 662, 109, 226, 398, 11 };
  171. Integers ansSet2(&ans2[0], &ans2[10]);
  172. CPPUNIT_ASSERT(equal(fastSet.begin(), fastSet.end(), ansSet2.begin()));
  173. }