UtilTest.cc 20 KB

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