UtilTest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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(testRandomAlpha);
  16. CPPUNIT_TEST(testToUpper);
  17. CPPUNIT_TEST(testToLower);
  18. CPPUNIT_TEST(testUrldecode);
  19. CPPUNIT_TEST(testCountBit);
  20. CPPUNIT_TEST(testGetRealSize);
  21. CPPUNIT_TEST(testAbbrevSize);
  22. CPPUNIT_TEST(testToStream);
  23. CPPUNIT_TEST(testIsNumber);
  24. CPPUNIT_TEST(testIsLowercase);
  25. CPPUNIT_TEST(testIsUppercase);
  26. CPPUNIT_TEST(testAlphaToNum);
  27. CPPUNIT_TEST_SUITE_END();
  28. private:
  29. public:
  30. void setUp() {
  31. }
  32. void testTrim();
  33. void testSplit();
  34. void testSlice();
  35. void testEndsWith();
  36. void testReplace();
  37. void testStartsWith();
  38. // may be moved to other helper class in the future.
  39. void testGetContentDispositionFilename();
  40. void testRandomAlpha();
  41. void testToUpper();
  42. void testToLower();
  43. void testUrldecode();
  44. void testCountBit();
  45. void testGetRealSize();
  46. void testAbbrevSize();
  47. void testToStream();
  48. void testIsNumber();
  49. void testIsLowercase();
  50. void testIsUppercase();
  51. void testAlphaToNum();
  52. };
  53. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  54. void UtilTest::testTrim() {
  55. string str1 = "aria2";
  56. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("aria2"));
  57. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2"));
  58. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  59. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  60. string str2 = "aria2 debut";
  61. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  62. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  63. string str3 = "";
  64. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  65. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  66. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  67. string str4 = "A";
  68. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  69. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  70. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  71. }
  72. void UtilTest::testSplit() {
  73. pair<string, string> p1;
  74. Util::split(p1, "name=value", '=');
  75. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  76. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  77. Util::split(p1, " name = value ", '=');
  78. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  79. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  80. Util::split(p1, "=value", '=');
  81. CPPUNIT_ASSERT_EQUAL(string(""), p1.first);
  82. CPPUNIT_ASSERT_EQUAL(string("value"), p1.second);
  83. Util::split(p1, "name=", '=');
  84. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  85. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  86. Util::split(p1, "name", '=');
  87. CPPUNIT_ASSERT_EQUAL(string("name"), p1.first);
  88. CPPUNIT_ASSERT_EQUAL(string(""), p1.second);
  89. }
  90. void UtilTest::testSlice() {
  91. Strings v1;
  92. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';', true);
  93. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  94. v1.clear();
  95. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true);
  96. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  97. Strings::iterator itr = v1.begin();
  98. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  99. CPPUNIT_ASSERT_EQUAL(string("name2=value2"), *itr++);
  100. CPPUNIT_ASSERT_EQUAL(string("name3=value3"), *itr++);
  101. v1.clear();
  102. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', false);
  103. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  104. itr = v1.begin();
  105. CPPUNIT_ASSERT_EQUAL(string("name1=value1"), *itr++);
  106. CPPUNIT_ASSERT_EQUAL(string(" name2=value2"), *itr++);
  107. CPPUNIT_ASSERT_EQUAL(string(" name3=value3"), *itr++);
  108. }
  109. void UtilTest::testEndsWith() {
  110. string target = "abcdefg";
  111. string part = "fg";
  112. CPPUNIT_ASSERT(Util::endsWith(target, part));
  113. target = "abdefg";
  114. part = "g";
  115. CPPUNIT_ASSERT(Util::endsWith(target, part));
  116. target = "abdefg";
  117. part = "eg";
  118. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  119. target = "g";
  120. part = "eg";
  121. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  122. target = "g";
  123. part = "g";
  124. CPPUNIT_ASSERT(Util::endsWith(target, part));
  125. target = "g";
  126. part = "";
  127. CPPUNIT_ASSERT(Util::endsWith(target, part));
  128. target = "";
  129. part = "";
  130. CPPUNIT_ASSERT(Util::endsWith(target, part));
  131. target = "";
  132. part = "g";
  133. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  134. }
  135. void UtilTest::testReplace() {
  136. CPPUNIT_ASSERT_EQUAL(string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  137. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  138. CPPUNIT_ASSERT_EQUAL(string(""), Util::replace("", "\r\n", ""));
  139. CPPUNIT_ASSERT_EQUAL(string("abc"), Util::replace("abc", "", "a"));
  140. CPPUNIT_ASSERT_EQUAL(string("xbc"), Util::replace("abc", "a", "x"));
  141. }
  142. void UtilTest::testStartsWith() {
  143. string target;
  144. string part;
  145. target = "abcdefg";
  146. part = "abc";
  147. CPPUNIT_ASSERT(Util::startsWith(target, part));
  148. target = "abcdefg";
  149. part = "abx";
  150. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  151. target = "abcdefg";
  152. part = "bcd";
  153. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  154. target = "";
  155. part = "a";
  156. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  157. target = "";
  158. part = "";
  159. CPPUNIT_ASSERT(Util::startsWith(target, part));
  160. target = "a";
  161. part = "";
  162. CPPUNIT_ASSERT(Util::startsWith(target, part));
  163. target = "a";
  164. part = "a";
  165. CPPUNIT_ASSERT(Util::startsWith(target, part));
  166. }
  167. void UtilTest::testGetContentDispositionFilename() {
  168. string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  169. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h1));
  170. string h2 = "attachment; filename=\"\"";
  171. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h2));
  172. string h3 = "attachment; filename=\"";
  173. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h3));
  174. string h4 = "attachment;";
  175. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h4));
  176. string h5 = "attachment; filename=aria2.tar.bz2";
  177. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h5));
  178. string h6 = "attachment; filename='aria2.tar.bz2'";
  179. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h6));
  180. string h7 = "attachment; filename='aria2.tar.bz2";
  181. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h7));
  182. string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  183. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2"), Util::getContentDispositionFilename(h8));
  184. string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  185. CPPUNIT_ASSERT_EQUAL(string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"), Util::getContentDispositionFilename(h9));
  186. string h10 = "attachment; filename=";
  187. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h10));
  188. string h11 = "attachment; filename=;";
  189. CPPUNIT_ASSERT_EQUAL(string(""), Util::getContentDispositionFilename(h11));
  190. }
  191. class Printer {
  192. public:
  193. template<class T>
  194. void operator()(T t) {
  195. cerr << t << ", ";
  196. }
  197. };
  198. void UtilTest::testRandomAlpha() {
  199. CPPUNIT_ASSERT_EQUAL((size_t)8, Util::randomAlpha(8).size());
  200. }
  201. void UtilTest::testToUpper() {
  202. string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  203. string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  204. CPPUNIT_ASSERT_EQUAL(upp, Util::toUpper(src));
  205. }
  206. void UtilTest::testToLower() {
  207. string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  208. string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  209. CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src));
  210. }
  211. #include "SharedHandle.h"
  212. void UtilTest::testUrldecode() {
  213. string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  214. CPPUNIT_ASSERT_EQUAL(string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  215. Util::urldecode(src));
  216. string src2 = "aria2+aria2";
  217. CPPUNIT_ASSERT_EQUAL(string("aria2+aria2"), Util::urldecode(src2));
  218. string src3 = "%5t%20";
  219. CPPUNIT_ASSERT_EQUAL(string("%5t "), Util::urldecode(src3));
  220. string src4 = "%";
  221. CPPUNIT_ASSERT_EQUAL(string("%"), Util::urldecode(src4));
  222. string src5 = "%3";
  223. CPPUNIT_ASSERT_EQUAL(string("%3"), Util::urldecode(src5));
  224. string src6 = "%2f";
  225. CPPUNIT_ASSERT_EQUAL(string("/"), Util::urldecode(src6));
  226. }
  227. void UtilTest::testCountBit() {
  228. CPPUNIT_ASSERT_EQUAL((int32_t)32, Util::countBit(UINT32_MAX));
  229. CPPUNIT_ASSERT_EQUAL((int32_t)8, Util::countBit(255));
  230. }
  231. void UtilTest::testGetRealSize()
  232. {
  233. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  234. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  235. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize(""));
  236. CPPUNIT_ASSERT_EQUAL((int64_t)0, Util::getRealSize("foo"));
  237. }
  238. void UtilTest::testAbbrevSize()
  239. {
  240. CPPUNIT_ASSERT_EQUAL(string("4,096.0Mi"), Util::abbrevSize(4294967296LL));
  241. CPPUNIT_ASSERT_EQUAL(string("1.0Ki"), Util::abbrevSize(1024));
  242. CPPUNIT_ASSERT_EQUAL(string("1,023"), Util::abbrevSize(1023));
  243. CPPUNIT_ASSERT_EQUAL(string("0"), Util::abbrevSize(0));
  244. CPPUNIT_ASSERT_EQUAL(string("1.1Ki"), Util::abbrevSize(1127));
  245. CPPUNIT_ASSERT_EQUAL(string("1.5Mi"), Util::abbrevSize(1572864));
  246. }
  247. void UtilTest::testToStream()
  248. {
  249. ostringstream os;
  250. FileEntryHandle f1 = new FileEntry("aria2.tar.bz2", 12300, 0);
  251. FileEntryHandle f2 = new FileEntry("aria2.txt", 556, 0);
  252. FileEntries entries;
  253. entries.push_back(f1);
  254. entries.push_back(f2);
  255. Util::toStream(os, entries);
  256. CPPUNIT_ASSERT_EQUAL(
  257. string("Files:\n"
  258. "idx|path/length\n"
  259. "===+===========================================================================\n"
  260. " 1|aria2.tar.bz2\n"
  261. " |12,300 bytes\n"
  262. "---+---------------------------------------------------------------------------\n"
  263. " 2|aria2.txt\n"
  264. " |556 bytes\n"
  265. "---+---------------------------------------------------------------------------\n"),
  266. os.str());
  267. }
  268. void UtilTest::testIsNumber()
  269. {
  270. CPPUNIT_ASSERT_EQUAL(true, Util::isNumber("000"));
  271. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("a"));
  272. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("0a"));
  273. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(""));
  274. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(" "));
  275. }
  276. void UtilTest::testIsLowercase()
  277. {
  278. CPPUNIT_ASSERT_EQUAL(true, Util::isLowercase("alpha"));
  279. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("Alpha"));
  280. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("1alpha"));
  281. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(""));
  282. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(" "));
  283. }
  284. void UtilTest::testIsUppercase()
  285. {
  286. CPPUNIT_ASSERT_EQUAL(true, Util::isUppercase("ALPHA"));
  287. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("Alpha"));
  288. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("1ALPHA"));
  289. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(""));
  290. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(" "));
  291. }
  292. void UtilTest::testAlphaToNum()
  293. {
  294. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum("a"));
  295. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum("aa"));
  296. CPPUNIT_ASSERT_EQUAL((int32_t)1, Util::alphaToNum("b"));
  297. CPPUNIT_ASSERT_EQUAL((int32_t)675, Util::alphaToNum("zz")); // 25*26+25
  298. CPPUNIT_ASSERT_EQUAL((int32_t)675, Util::alphaToNum("ZZ")); // 25*26+25
  299. CPPUNIT_ASSERT_EQUAL((int32_t)0, Util::alphaToNum(""));
  300. }