UtilTest.cc 6.1 KB

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