UtilTest.cc 11 KB

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