UtilTest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. #include "Util.h"
  2. #include "FixedNumberRandomizer.h"
  3. #include "DlAbortEx.h"
  4. #include "BitfieldMan.h"
  5. #include "ByteArrayDiskWriter.h"
  6. #include "FileEntry.h"
  7. #include "File.h"
  8. #include <string>
  9. #include <iostream>
  10. #include <cppunit/extensions/HelperMacros.h>
  11. namespace aria2 {
  12. class UtilTest:public CppUnit::TestFixture {
  13. CPPUNIT_TEST_SUITE(UtilTest);
  14. CPPUNIT_TEST(testTrim);
  15. CPPUNIT_TEST(testSplit);
  16. CPPUNIT_TEST(testSlice);
  17. CPPUNIT_TEST(testEndsWith);
  18. CPPUNIT_TEST(testReplace);
  19. CPPUNIT_TEST(testStartsWith);
  20. // may be moved to other helper class in the future.
  21. CPPUNIT_TEST(testGetContentDispositionFilename);
  22. CPPUNIT_TEST(testRandomAlpha);
  23. CPPUNIT_TEST(testToUpper);
  24. CPPUNIT_TEST(testToLower);
  25. CPPUNIT_TEST(testUrldecode);
  26. CPPUNIT_TEST(testCountBit);
  27. CPPUNIT_TEST(testGetRealSize);
  28. CPPUNIT_TEST(testAbbrevSize);
  29. CPPUNIT_TEST(testToStream);
  30. CPPUNIT_TEST(testIsNumber);
  31. CPPUNIT_TEST(testIsLowercase);
  32. CPPUNIT_TEST(testIsUppercase);
  33. CPPUNIT_TEST(testAlphaToNum);
  34. CPPUNIT_TEST(testMkdirs);
  35. CPPUNIT_TEST(testConvertBitfield);
  36. CPPUNIT_TEST(testParseIntRange);
  37. CPPUNIT_TEST(testParseIntRange_invalidRange);
  38. CPPUNIT_TEST(testParseInt);
  39. CPPUNIT_TEST(testParseUInt);
  40. CPPUNIT_TEST(testParseLLInt);
  41. CPPUNIT_TEST(testParseULLInt);
  42. CPPUNIT_TEST(testToString_binaryStream);
  43. CPPUNIT_TEST(testItos);
  44. CPPUNIT_TEST(testUitos);
  45. CPPUNIT_TEST(testHttpGMT);
  46. CPPUNIT_TEST_SUITE_END();
  47. private:
  48. public:
  49. void setUp() {
  50. }
  51. void testTrim();
  52. void testSplit();
  53. void testSlice();
  54. void testEndsWith();
  55. void testReplace();
  56. void testStartsWith();
  57. // may be moved to other helper class in the future.
  58. void testGetContentDispositionFilename();
  59. void testRandomAlpha();
  60. void testToUpper();
  61. void testToLower();
  62. void testUrldecode();
  63. void testCountBit();
  64. void testGetRealSize();
  65. void testAbbrevSize();
  66. void testToStream();
  67. void testIsNumber();
  68. void testIsLowercase();
  69. void testIsUppercase();
  70. void testAlphaToNum();
  71. void testMkdirs();
  72. void testConvertBitfield();
  73. void testParseIntRange();
  74. void testParseIntRange_invalidRange();
  75. void testParseInt();
  76. void testParseUInt();
  77. void testParseLLInt();
  78. void testParseULLInt();
  79. void testToString_binaryStream();
  80. void testItos();
  81. void testUitos();
  82. void testHttpGMT();
  83. };
  84. CPPUNIT_TEST_SUITE_REGISTRATION( UtilTest );
  85. void UtilTest::testTrim() {
  86. std::string str1 = "aria2";
  87. CPPUNIT_ASSERT_EQUAL(str1, Util::trim("aria2"));
  88. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2"));
  89. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  90. CPPUNIT_ASSERT_EQUAL(str1, Util::trim(" aria2 "));
  91. std::string str2 = "aria2 debut";
  92. CPPUNIT_ASSERT_EQUAL(str2, Util::trim("aria2 debut"));
  93. CPPUNIT_ASSERT_EQUAL(str2, Util::trim(" aria2 debut "));
  94. std::string str3 = "";
  95. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(""));
  96. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  97. CPPUNIT_ASSERT_EQUAL(str3, Util::trim(" "));
  98. std::string str4 = "A";
  99. CPPUNIT_ASSERT_EQUAL(str4, Util::trim("A"));
  100. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  101. CPPUNIT_ASSERT_EQUAL(str4, Util::trim(" A "));
  102. }
  103. void UtilTest::testSplit() {
  104. std::pair<std::string, std::string> p1;
  105. Util::split(p1, "name=value", '=');
  106. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  107. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  108. Util::split(p1, " name = value ", '=');
  109. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  110. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  111. Util::split(p1, "=value", '=');
  112. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.first);
  113. CPPUNIT_ASSERT_EQUAL(std::string("value"), p1.second);
  114. Util::split(p1, "name=", '=');
  115. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  116. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
  117. Util::split(p1, "name", '=');
  118. CPPUNIT_ASSERT_EQUAL(std::string("name"), p1.first);
  119. CPPUNIT_ASSERT_EQUAL(std::string(""), p1.second);
  120. }
  121. void UtilTest::testSlice() {
  122. std::deque<std::string> v1;
  123. Util::slice(v1, "name1=value1; name2=value2; name3=value3;", ';', true);
  124. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  125. v1.clear();
  126. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', true);
  127. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  128. std::deque<std::string>::iterator itr = v1.begin();
  129. CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
  130. CPPUNIT_ASSERT_EQUAL(std::string("name2=value2"), *itr++);
  131. CPPUNIT_ASSERT_EQUAL(std::string("name3=value3"), *itr++);
  132. v1.clear();
  133. Util::slice(v1, "name1=value1; name2=value2; name3=value3", ';', false);
  134. CPPUNIT_ASSERT_EQUAL(3, (int)v1.size());
  135. itr = v1.begin();
  136. CPPUNIT_ASSERT_EQUAL(std::string("name1=value1"), *itr++);
  137. CPPUNIT_ASSERT_EQUAL(std::string(" name2=value2"), *itr++);
  138. CPPUNIT_ASSERT_EQUAL(std::string(" name3=value3"), *itr++);
  139. }
  140. void UtilTest::testEndsWith() {
  141. std::string target = "abcdefg";
  142. std::string part = "fg";
  143. CPPUNIT_ASSERT(Util::endsWith(target, part));
  144. target = "abdefg";
  145. part = "g";
  146. CPPUNIT_ASSERT(Util::endsWith(target, part));
  147. target = "abdefg";
  148. part = "eg";
  149. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  150. target = "g";
  151. part = "eg";
  152. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  153. target = "g";
  154. part = "g";
  155. CPPUNIT_ASSERT(Util::endsWith(target, part));
  156. target = "g";
  157. part = "";
  158. CPPUNIT_ASSERT(Util::endsWith(target, part));
  159. target = "";
  160. part = "";
  161. CPPUNIT_ASSERT(Util::endsWith(target, part));
  162. target = "";
  163. part = "g";
  164. CPPUNIT_ASSERT(!Util::endsWith(target, part));
  165. }
  166. void UtilTest::testReplace() {
  167. CPPUNIT_ASSERT_EQUAL(std::string("abc\n"), Util::replace("abc\r\n", "\r", ""));
  168. CPPUNIT_ASSERT_EQUAL(std::string("abc"), Util::replace("abc\r\n", "\r\n", ""));
  169. CPPUNIT_ASSERT_EQUAL(std::string(""), Util::replace("", "\r\n", ""));
  170. CPPUNIT_ASSERT_EQUAL(std::string("abc"), Util::replace("abc", "", "a"));
  171. CPPUNIT_ASSERT_EQUAL(std::string("xbc"), Util::replace("abc", "a", "x"));
  172. }
  173. void UtilTest::testStartsWith() {
  174. std::string target;
  175. std::string part;
  176. target = "abcdefg";
  177. part = "abc";
  178. CPPUNIT_ASSERT(Util::startsWith(target, part));
  179. target = "abcdefg";
  180. part = "abx";
  181. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  182. target = "abcdefg";
  183. part = "bcd";
  184. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  185. target = "";
  186. part = "a";
  187. CPPUNIT_ASSERT(!Util::startsWith(target, part));
  188. target = "";
  189. part = "";
  190. CPPUNIT_ASSERT(Util::startsWith(target, part));
  191. target = "a";
  192. part = "";
  193. CPPUNIT_ASSERT(Util::startsWith(target, part));
  194. target = "a";
  195. part = "a";
  196. CPPUNIT_ASSERT(Util::startsWith(target, part));
  197. }
  198. void UtilTest::testGetContentDispositionFilename() {
  199. std::string h1 = "attachment; filename=\"aria2.tar.bz2\"";
  200. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), Util::getContentDispositionFilename(h1));
  201. std::string h2 = "attachment; filename=\"\"";
  202. CPPUNIT_ASSERT_EQUAL(std::string(""), Util::getContentDispositionFilename(h2));
  203. std::string h3 = "attachment; filename=\"";
  204. CPPUNIT_ASSERT_EQUAL(std::string(""), Util::getContentDispositionFilename(h3));
  205. std::string h4 = "attachment;";
  206. CPPUNIT_ASSERT_EQUAL(std::string(""), Util::getContentDispositionFilename(h4));
  207. std::string h5 = "attachment; filename=aria2.tar.bz2";
  208. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), Util::getContentDispositionFilename(h5));
  209. std::string h6 = "attachment; filename='aria2.tar.bz2'";
  210. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), Util::getContentDispositionFilename(h6));
  211. std::string h7 = "attachment; filename='aria2.tar.bz2";
  212. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), Util::getContentDispositionFilename(h7));
  213. std::string h8 = "attachment; filename=aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT";
  214. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2"), Util::getContentDispositionFilename(h8));
  215. std::string h9 = "attachment; filename=\"aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT\"";
  216. CPPUNIT_ASSERT_EQUAL(std::string("aria2.tar.bz2; creation-date=20 Jun 2007 00:00:00 GMT"), Util::getContentDispositionFilename(h9));
  217. std::string h10 = "attachment; filename=";
  218. CPPUNIT_ASSERT_EQUAL(std::string(""), Util::getContentDispositionFilename(h10));
  219. std::string h11 = "attachment; filename=;";
  220. CPPUNIT_ASSERT_EQUAL(std::string(""), Util::getContentDispositionFilename(h11));
  221. }
  222. class Printer {
  223. public:
  224. template<class T>
  225. void operator()(T t) {
  226. std::cerr << t << ", ";
  227. }
  228. };
  229. void UtilTest::testRandomAlpha() {
  230. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  231. std::string s = Util::randomAlpha(8, rand);
  232. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  233. }
  234. void UtilTest::testToUpper() {
  235. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  236. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  237. CPPUNIT_ASSERT_EQUAL(upp, Util::toUpper(src));
  238. }
  239. void UtilTest::testToLower() {
  240. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  241. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  242. CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src));
  243. }
  244. void UtilTest::testUrldecode() {
  245. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  246. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  247. Util::urldecode(src));
  248. std::string src2 = "aria2+aria2";
  249. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"), Util::urldecode(src2));
  250. std::string src3 = "%5t%20";
  251. CPPUNIT_ASSERT_EQUAL(std::string("%5t "), Util::urldecode(src3));
  252. std::string src4 = "%";
  253. CPPUNIT_ASSERT_EQUAL(std::string("%"), Util::urldecode(src4));
  254. std::string src5 = "%3";
  255. CPPUNIT_ASSERT_EQUAL(std::string("%3"), Util::urldecode(src5));
  256. std::string src6 = "%2f";
  257. CPPUNIT_ASSERT_EQUAL(std::string("/"), Util::urldecode(src6));
  258. }
  259. void UtilTest::testCountBit() {
  260. CPPUNIT_ASSERT_EQUAL((unsigned int)32, Util::countBit(UINT32_MAX));
  261. CPPUNIT_ASSERT_EQUAL((unsigned int)8, Util::countBit(255));
  262. }
  263. void UtilTest::testGetRealSize()
  264. {
  265. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  266. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  267. try {
  268. Util::getRealSize("");
  269. CPPUNIT_FAIL("exception must be thrown.");
  270. } catch(Exception& e) {
  271. std::cerr << e.stackTrace();
  272. }
  273. try {
  274. Util::getRealSize("foo");
  275. CPPUNIT_FAIL("exception must be thrown.");
  276. } catch(Exception& e) {
  277. std::cerr << e.stackTrace();
  278. }
  279. try {
  280. Util::getRealSize("-1");
  281. CPPUNIT_FAIL("exception must be thrown.");
  282. } catch(Exception& e) {
  283. std::cerr << e.stackTrace();
  284. }
  285. try {
  286. Util::getRealSize("9223372036854775807K");
  287. CPPUNIT_FAIL("exception must be thrown.");
  288. } catch(Exception& e) {
  289. std::cerr << e.stackTrace();
  290. }
  291. try {
  292. Util::getRealSize("9223372036854775807M");
  293. CPPUNIT_FAIL("exception must be thrown.");
  294. } catch(Exception& e) {
  295. std::cerr << e.stackTrace();
  296. }
  297. }
  298. void UtilTest::testAbbrevSize()
  299. {
  300. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), Util::abbrevSize(4294967296LL));
  301. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), Util::abbrevSize(1024));
  302. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), Util::abbrevSize(1023));
  303. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::abbrevSize(0));
  304. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), Util::abbrevSize(1127));
  305. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), Util::abbrevSize(1572864));
  306. }
  307. void UtilTest::testToStream()
  308. {
  309. std::ostringstream os;
  310. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  311. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  312. std::deque<SharedHandle<FileEntry> > entries;
  313. entries.push_back(f1);
  314. entries.push_back(f2);
  315. Util::toStream(os, entries);
  316. CPPUNIT_ASSERT_EQUAL(
  317. std::string("Files:\n"
  318. "idx|path/length\n"
  319. "===+===========================================================================\n"
  320. " 1|aria2.tar.bz2\n"
  321. " |12.0KiB\n"
  322. "---+---------------------------------------------------------------------------\n"
  323. " 2|aria2.txt\n"
  324. " |556B\n"
  325. "---+---------------------------------------------------------------------------\n"),
  326. os.str());
  327. }
  328. void UtilTest::testIsNumber()
  329. {
  330. CPPUNIT_ASSERT_EQUAL(true, Util::isNumber("000"));
  331. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("a"));
  332. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("0a"));
  333. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(""));
  334. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(" "));
  335. }
  336. void UtilTest::testIsLowercase()
  337. {
  338. CPPUNIT_ASSERT_EQUAL(true, Util::isLowercase("alpha"));
  339. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("Alpha"));
  340. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("1alpha"));
  341. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(""));
  342. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(" "));
  343. }
  344. void UtilTest::testIsUppercase()
  345. {
  346. CPPUNIT_ASSERT_EQUAL(true, Util::isUppercase("ALPHA"));
  347. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("Alpha"));
  348. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("1ALPHA"));
  349. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(""));
  350. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(" "));
  351. }
  352. void UtilTest::testAlphaToNum()
  353. {
  354. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("a"));
  355. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("aa"));
  356. CPPUNIT_ASSERT_EQUAL(1U, Util::alphaToNum("b"));
  357. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("zz")); // 25*26+25
  358. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("ZZ")); // 25*26+25
  359. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum(""));
  360. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::alphaToNum("NXMRLXV"));
  361. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("NXMRLXW")); // uint32_t overflow
  362. }
  363. void UtilTest::testMkdirs()
  364. {
  365. std::string dir = "/tmp/aria2-UtilTest-testMkdirs";
  366. File d(dir);
  367. if(d.exists()) {
  368. CPPUNIT_ASSERT(d.remove());
  369. }
  370. CPPUNIT_ASSERT(!d.exists());
  371. Util::mkdirs(dir);
  372. CPPUNIT_ASSERT(d.isDir());
  373. std::string file = "./UtilTest.cc";
  374. File f(file);
  375. CPPUNIT_ASSERT(f.isFile());
  376. try {
  377. Util::mkdirs(file);
  378. CPPUNIT_FAIL("exception must be thrown.");
  379. } catch(DlAbortEx& ex) {
  380. std::cerr << ex.stackTrace() << std::endl;
  381. }
  382. }
  383. void UtilTest::testConvertBitfield()
  384. {
  385. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  386. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  387. srcBitfield.setAllBit();
  388. srcBitfield.unsetBit(2);// <- range [768, 1152)
  389. // which corresponds to the index [1,2] in destBitfield
  390. Util::convertBitfield(&destBitfield, &srcBitfield);
  391. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  392. Util::toHex(destBitfield.getBitfield(),
  393. destBitfield.getBitfieldLength()));
  394. }
  395. void UtilTest::testParseIntRange()
  396. {
  397. IntSequence seq = Util::parseIntRange("1,3-8,10");
  398. CPPUNIT_ASSERT(seq.hasNext());
  399. CPPUNIT_ASSERT_EQUAL((int32_t)1, seq.next());
  400. CPPUNIT_ASSERT(seq.hasNext());
  401. CPPUNIT_ASSERT_EQUAL((int32_t)3, seq.next());
  402. CPPUNIT_ASSERT(seq.hasNext());
  403. CPPUNIT_ASSERT_EQUAL((int32_t)4, seq.next());
  404. CPPUNIT_ASSERT(seq.hasNext());
  405. CPPUNIT_ASSERT_EQUAL((int32_t)5, seq.next());
  406. CPPUNIT_ASSERT(seq.hasNext());
  407. CPPUNIT_ASSERT_EQUAL((int32_t)6, seq.next());
  408. CPPUNIT_ASSERT(seq.hasNext());
  409. CPPUNIT_ASSERT_EQUAL((int32_t)7, seq.next());
  410. CPPUNIT_ASSERT(seq.hasNext());
  411. CPPUNIT_ASSERT_EQUAL((int32_t)8, seq.next());
  412. CPPUNIT_ASSERT(seq.hasNext());
  413. CPPUNIT_ASSERT_EQUAL((int32_t)10, seq.next());
  414. CPPUNIT_ASSERT(!seq.hasNext());
  415. CPPUNIT_ASSERT_EQUAL((int32_t)0, seq.next());
  416. }
  417. void UtilTest::testParseIntRange_invalidRange()
  418. {
  419. try {
  420. IntSequence seq = Util::parseIntRange("-1");
  421. CPPUNIT_FAIL("exception must be thrown.");
  422. } catch(Exception& e) {
  423. std::cerr << e.stackTrace();
  424. }
  425. try {
  426. IntSequence seq = Util::parseIntRange("2147483648");
  427. CPPUNIT_FAIL("exception must be thrown.");
  428. } catch(Exception& e) {
  429. std::cerr << e.stackTrace();
  430. }
  431. try {
  432. IntSequence seq = Util::parseIntRange("2147483647-2147483648");
  433. CPPUNIT_FAIL("exception must be thrown.");
  434. } catch(Exception& e) {
  435. std::cerr << e.stackTrace();
  436. }
  437. try {
  438. IntSequence seq = Util::parseIntRange("1-2x");
  439. CPPUNIT_FAIL("exception must be thrown.");
  440. } catch(Exception& e) {
  441. std::cerr << e.stackTrace();
  442. }
  443. try {
  444. IntSequence seq = Util::parseIntRange("3x-4");
  445. CPPUNIT_FAIL("exception must be thrown.");
  446. } catch(Exception& e) {
  447. std::cerr << e.stackTrace();
  448. }
  449. }
  450. void UtilTest::testParseInt()
  451. {
  452. CPPUNIT_ASSERT_EQUAL(-1, Util::parseInt(" -1 "));
  453. CPPUNIT_ASSERT_EQUAL(2147483647, Util::parseInt("2147483647"));
  454. try {
  455. Util::parseInt("2147483648");
  456. CPPUNIT_FAIL("exception must be thrown.");
  457. } catch(Exception& e) {
  458. std::cerr << e.stackTrace();
  459. }
  460. try {
  461. Util::parseInt("-2147483649");
  462. CPPUNIT_FAIL("exception must be thrown.");
  463. } catch(Exception& e) {
  464. std::cerr << e.stackTrace();
  465. }
  466. try {
  467. Util::parseInt("12x");
  468. CPPUNIT_FAIL("exception must be thrown.");
  469. } catch(Exception& e) {
  470. std::cerr << e.stackTrace();
  471. }
  472. try {
  473. Util::parseInt("");
  474. CPPUNIT_FAIL("exception must be thrown.");
  475. } catch(Exception& e) {
  476. std::cerr << e.stackTrace();
  477. }
  478. }
  479. void UtilTest::testParseUInt()
  480. {
  481. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::parseUInt(" 4294967295 "));
  482. try {
  483. Util::parseUInt("-1");
  484. CPPUNIT_FAIL("exception must be thrown.");
  485. } catch(Exception& e) {
  486. std::cerr << e.stackTrace();
  487. }
  488. try {
  489. Util::parseUInt("4294967296");
  490. CPPUNIT_FAIL("exception must be thrown.");
  491. } catch(Exception& e) {
  492. std::cerr << e.stackTrace();
  493. }
  494. }
  495. void UtilTest::testParseLLInt()
  496. {
  497. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, Util::parseLLInt(" -1 "));
  498. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  499. Util::parseLLInt("9223372036854775807"));
  500. try {
  501. Util::parseLLInt("9223372036854775808");
  502. CPPUNIT_FAIL("exception must be thrown.");
  503. } catch(Exception& e) {
  504. std::cerr << e.stackTrace();
  505. }
  506. try {
  507. Util::parseLLInt("-9223372036854775809");
  508. CPPUNIT_FAIL("exception must be thrown.");
  509. } catch(Exception& e) {
  510. std::cerr << e.stackTrace();
  511. }
  512. try {
  513. Util::parseLLInt("12x");
  514. CPPUNIT_FAIL("exception must be thrown.");
  515. } catch(Exception& e) {
  516. std::cerr << e.stackTrace();
  517. }
  518. try {
  519. Util::parseLLInt("");
  520. CPPUNIT_FAIL("exception must be thrown.");
  521. } catch(Exception& e) {
  522. std::cerr << e.stackTrace();
  523. }
  524. }
  525. void UtilTest::testParseULLInt()
  526. {
  527. CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
  528. Util::parseULLInt("18446744073709551615"));
  529. try {
  530. Util::parseUInt("-1");
  531. CPPUNIT_FAIL("exception must be thrown.");
  532. } catch(Exception& e) {
  533. std::cerr << e.stackTrace();
  534. }
  535. try {
  536. Util::parseLLInt("18446744073709551616");
  537. CPPUNIT_FAIL("exception must be thrown.");
  538. } catch(Exception& e) {
  539. std::cerr << e.stackTrace();
  540. }
  541. }
  542. void UtilTest::testToString_binaryStream()
  543. {
  544. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  545. std::string data(16*1024+256, 'a');
  546. dw->initAndOpenFile("dummy");
  547. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  548. std::string readData = Util::toString(dw);
  549. CPPUNIT_ASSERT_EQUAL(data, readData);
  550. }
  551. void UtilTest::testItos()
  552. {
  553. {
  554. int i = 0;
  555. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::itos(i));
  556. }
  557. {
  558. int i = 100;
  559. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i, true));
  560. }
  561. {
  562. int i = 100;
  563. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i));
  564. }
  565. {
  566. int i = 12345;
  567. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), Util::itos(i, true));
  568. }
  569. {
  570. int i = 12345;
  571. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::itos(i));
  572. }
  573. {
  574. int i = -12345;
  575. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), Util::itos(i, true));
  576. }
  577. {
  578. int64_t i = INT64_MAX;
  579. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), Util::itos(i, true));
  580. }
  581. }
  582. void UtilTest::testUitos()
  583. {
  584. {
  585. uint16_t i = 12345;
  586. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::uitos(i));
  587. }
  588. {
  589. int16_t i = -12345;
  590. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), Util::uitos(i));
  591. }
  592. }
  593. void UtilTest::testHttpGMT()
  594. {
  595. CPPUNIT_ASSERT_EQUAL((time_t)0, Util::httpGMT("Thu, 1970-01-01 0:0:0 GMT"));
  596. CPPUNIT_ASSERT_EQUAL((time_t)2147483647,
  597. Util::httpGMT("Tue, 2038-01-19 3:14:7 GMT"));
  598. CPPUNIT_ASSERT_EQUAL((time_t)2147483647,
  599. Util::httpGMT("Tue, 2038-01-19 3:14:8 GMT"));
  600. CPPUNIT_ASSERT_EQUAL((time_t)-1,
  601. Util::httpGMT("Tue, 2008/10/10 23:33:33 UTC"));
  602. }
  603. } // namespace aria2