UtilTest.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. SharedHandle<Randomizer> rand(new FixedNumberRandomizer());
  229. std::string s = Util::randomAlpha(8, rand);
  230. CPPUNIT_ASSERT_EQUAL(std::string("AAAAAAAA"), s);
  231. }
  232. void UtilTest::testToUpper() {
  233. std::string src = "608cabc0f2fa18c260cafd974516865c772363d5";
  234. std::string upp = "608CABC0F2FA18C260CAFD974516865C772363D5";
  235. CPPUNIT_ASSERT_EQUAL(upp, Util::toUpper(src));
  236. }
  237. void UtilTest::testToLower() {
  238. std::string src = "608CABC0F2FA18C260CAFD974516865C772363D5";
  239. std::string upp = "608cabc0f2fa18c260cafd974516865c772363d5";
  240. CPPUNIT_ASSERT_EQUAL(upp, Util::toLower(src));
  241. }
  242. void UtilTest::testUrldecode() {
  243. std::string src = "http://aria2.sourceforge.net/aria2%200.7.0%20docs.html";
  244. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sourceforge.net/aria2 0.7.0 docs.html"),
  245. Util::urldecode(src));
  246. std::string src2 = "aria2+aria2";
  247. CPPUNIT_ASSERT_EQUAL(std::string("aria2+aria2"), Util::urldecode(src2));
  248. std::string src3 = "%5t%20";
  249. CPPUNIT_ASSERT_EQUAL(std::string("%5t "), Util::urldecode(src3));
  250. std::string src4 = "%";
  251. CPPUNIT_ASSERT_EQUAL(std::string("%"), Util::urldecode(src4));
  252. std::string src5 = "%3";
  253. CPPUNIT_ASSERT_EQUAL(std::string("%3"), Util::urldecode(src5));
  254. std::string src6 = "%2f";
  255. CPPUNIT_ASSERT_EQUAL(std::string("/"), Util::urldecode(src6));
  256. }
  257. void UtilTest::testCountBit() {
  258. CPPUNIT_ASSERT_EQUAL((unsigned int)32, Util::countBit(UINT32_MAX));
  259. CPPUNIT_ASSERT_EQUAL((unsigned int)8, Util::countBit(255));
  260. }
  261. void UtilTest::testGetRealSize()
  262. {
  263. CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, Util::getRealSize("4096M"));
  264. CPPUNIT_ASSERT_EQUAL((int64_t)1024, Util::getRealSize("1K"));
  265. try {
  266. Util::getRealSize("");
  267. CPPUNIT_FAIL("exception must be thrown.");
  268. } catch(Exception* e) {
  269. std::cerr << *e;
  270. delete e;
  271. }
  272. try {
  273. Util::getRealSize("foo");
  274. CPPUNIT_FAIL("exception must be thrown.");
  275. } catch(Exception* e) {
  276. std::cerr << *e;
  277. delete e;
  278. }
  279. try {
  280. Util::getRealSize("-1");
  281. CPPUNIT_FAIL("exception must be thrown.");
  282. } catch(Exception* e) {
  283. std::cerr << *e;
  284. delete e;
  285. }
  286. try {
  287. Util::getRealSize("9223372036854775807K");
  288. CPPUNIT_FAIL("exception must be thrown.");
  289. } catch(Exception* e) {
  290. std::cerr << *e;
  291. delete e;
  292. }
  293. try {
  294. Util::getRealSize("9223372036854775807M");
  295. CPPUNIT_FAIL("exception must be thrown.");
  296. } catch(Exception* e) {
  297. std::cerr << *e;
  298. delete e;
  299. }
  300. }
  301. void UtilTest::testAbbrevSize()
  302. {
  303. CPPUNIT_ASSERT_EQUAL(std::string("4,096.0Mi"), Util::abbrevSize(4294967296LL));
  304. CPPUNIT_ASSERT_EQUAL(std::string("1.0Ki"), Util::abbrevSize(1024));
  305. CPPUNIT_ASSERT_EQUAL(std::string("1,023"), Util::abbrevSize(1023));
  306. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::abbrevSize(0));
  307. CPPUNIT_ASSERT_EQUAL(std::string("1.1Ki"), Util::abbrevSize(1127));
  308. CPPUNIT_ASSERT_EQUAL(std::string("1.5Mi"), Util::abbrevSize(1572864));
  309. }
  310. void UtilTest::testToStream()
  311. {
  312. std::ostringstream os;
  313. SharedHandle<FileEntry> f1(new FileEntry("aria2.tar.bz2", 12300, 0));
  314. SharedHandle<FileEntry> f2(new FileEntry("aria2.txt", 556, 0));
  315. std::deque<SharedHandle<FileEntry> > entries;
  316. entries.push_back(f1);
  317. entries.push_back(f2);
  318. Util::toStream(os, entries);
  319. CPPUNIT_ASSERT_EQUAL(
  320. std::string("Files:\n"
  321. "idx|path/length\n"
  322. "===+===========================================================================\n"
  323. " 1|aria2.tar.bz2\n"
  324. " |12.0KiB\n"
  325. "---+---------------------------------------------------------------------------\n"
  326. " 2|aria2.txt\n"
  327. " |556B\n"
  328. "---+---------------------------------------------------------------------------\n"),
  329. os.str());
  330. }
  331. void UtilTest::testIsNumber()
  332. {
  333. CPPUNIT_ASSERT_EQUAL(true, Util::isNumber("000"));
  334. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("a"));
  335. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber("0a"));
  336. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(""));
  337. CPPUNIT_ASSERT_EQUAL(false, Util::isNumber(" "));
  338. }
  339. void UtilTest::testIsLowercase()
  340. {
  341. CPPUNIT_ASSERT_EQUAL(true, Util::isLowercase("alpha"));
  342. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("Alpha"));
  343. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase("1alpha"));
  344. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(""));
  345. CPPUNIT_ASSERT_EQUAL(false, Util::isLowercase(" "));
  346. }
  347. void UtilTest::testIsUppercase()
  348. {
  349. CPPUNIT_ASSERT_EQUAL(true, Util::isUppercase("ALPHA"));
  350. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("Alpha"));
  351. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase("1ALPHA"));
  352. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(""));
  353. CPPUNIT_ASSERT_EQUAL(false, Util::isUppercase(" "));
  354. }
  355. void UtilTest::testAlphaToNum()
  356. {
  357. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("a"));
  358. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("aa"));
  359. CPPUNIT_ASSERT_EQUAL(1U, Util::alphaToNum("b"));
  360. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("zz")); // 25*26+25
  361. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("ZZ")); // 25*26+25
  362. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum(""));
  363. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::alphaToNum("NXMRLXV"));
  364. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("NXMRLXW")); // uint32_t overflow
  365. }
  366. void UtilTest::testMkdirs()
  367. {
  368. std::string dir = "/tmp/aria2-UtilTest-testMkdirs";
  369. File d(dir);
  370. if(d.exists()) {
  371. CPPUNIT_ASSERT(d.remove());
  372. }
  373. CPPUNIT_ASSERT(!d.exists());
  374. Util::mkdirs(dir);
  375. CPPUNIT_ASSERT(d.isDir());
  376. std::string file = "./UtilTest.cc";
  377. File f(file);
  378. CPPUNIT_ASSERT(f.isFile());
  379. try {
  380. Util::mkdirs(file);
  381. CPPUNIT_FAIL("exception must be thrown.");
  382. } catch(DlAbortEx* ex) {
  383. std::cerr << ex->getMsg() << std::endl;
  384. delete ex;
  385. }
  386. }
  387. void UtilTest::testConvertBitfield()
  388. {
  389. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  390. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  391. srcBitfield.setAllBit();
  392. srcBitfield.unsetBit(2);// <- range [768, 1152)
  393. // which corresponds to the index [1,2] in destBitfield
  394. Util::convertBitfield(&destBitfield, &srcBitfield);
  395. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  396. Util::toHex(destBitfield.getBitfield(),
  397. destBitfield.getBitfieldLength()));
  398. }
  399. void UtilTest::testParseIntRange()
  400. {
  401. IntSequence seq = Util::parseIntRange("1,3-8,10");
  402. CPPUNIT_ASSERT(seq.hasNext());
  403. CPPUNIT_ASSERT_EQUAL((int32_t)1, seq.next());
  404. CPPUNIT_ASSERT(seq.hasNext());
  405. CPPUNIT_ASSERT_EQUAL((int32_t)3, seq.next());
  406. CPPUNIT_ASSERT(seq.hasNext());
  407. CPPUNIT_ASSERT_EQUAL((int32_t)4, seq.next());
  408. CPPUNIT_ASSERT(seq.hasNext());
  409. CPPUNIT_ASSERT_EQUAL((int32_t)5, seq.next());
  410. CPPUNIT_ASSERT(seq.hasNext());
  411. CPPUNIT_ASSERT_EQUAL((int32_t)6, seq.next());
  412. CPPUNIT_ASSERT(seq.hasNext());
  413. CPPUNIT_ASSERT_EQUAL((int32_t)7, seq.next());
  414. CPPUNIT_ASSERT(seq.hasNext());
  415. CPPUNIT_ASSERT_EQUAL((int32_t)8, seq.next());
  416. CPPUNIT_ASSERT(seq.hasNext());
  417. CPPUNIT_ASSERT_EQUAL((int32_t)10, seq.next());
  418. CPPUNIT_ASSERT(!seq.hasNext());
  419. CPPUNIT_ASSERT_EQUAL((int32_t)0, seq.next());
  420. }
  421. void UtilTest::testParseIntRange_invalidRange()
  422. {
  423. try {
  424. IntSequence seq = Util::parseIntRange("-1");
  425. CPPUNIT_FAIL("exception must be thrown.");
  426. } catch(Exception* e) {
  427. std::cerr << *e;
  428. delete e;
  429. }
  430. try {
  431. IntSequence seq = Util::parseIntRange("2147483648");
  432. CPPUNIT_FAIL("exception must be thrown.");
  433. } catch(Exception* e) {
  434. std::cerr << *e;
  435. delete e;
  436. }
  437. try {
  438. IntSequence seq = Util::parseIntRange("2147483647-2147483648");
  439. CPPUNIT_FAIL("exception must be thrown.");
  440. } catch(Exception* e) {
  441. std::cerr << *e;
  442. delete e;
  443. }
  444. try {
  445. IntSequence seq = Util::parseIntRange("1-2x");
  446. CPPUNIT_FAIL("exception must be thrown.");
  447. } catch(Exception* e) {
  448. std::cerr << *e;
  449. delete e;
  450. }
  451. try {
  452. IntSequence seq = Util::parseIntRange("3x-4");
  453. CPPUNIT_FAIL("exception must be thrown.");
  454. } catch(Exception* e) {
  455. std::cerr << *e;
  456. delete e;
  457. }
  458. }
  459. void UtilTest::testParseInt()
  460. {
  461. CPPUNIT_ASSERT_EQUAL(-1, Util::parseInt(" -1 "));
  462. CPPUNIT_ASSERT_EQUAL(2147483647, Util::parseInt("2147483647"));
  463. try {
  464. Util::parseInt("2147483648");
  465. CPPUNIT_FAIL("exception must be thrown.");
  466. } catch(Exception* e) {
  467. std::cerr << *e;
  468. delete e;
  469. }
  470. try {
  471. Util::parseInt("-2147483649");
  472. CPPUNIT_FAIL("exception must be thrown.");
  473. } catch(Exception* e) {
  474. std::cerr << *e;
  475. delete e;
  476. }
  477. try {
  478. Util::parseInt("12x");
  479. CPPUNIT_FAIL("exception must be thrown.");
  480. } catch(Exception* e) {
  481. std::cerr << *e;
  482. delete e;
  483. }
  484. try {
  485. Util::parseInt("");
  486. CPPUNIT_FAIL("exception must be thrown.");
  487. } catch(Exception* e) {
  488. std::cerr << *e;
  489. delete e;
  490. }
  491. }
  492. void UtilTest::testParseUInt()
  493. {
  494. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::parseUInt(" 4294967295 "));
  495. try {
  496. Util::parseUInt("-1");
  497. CPPUNIT_FAIL("exception must be thrown.");
  498. } catch(Exception* e) {
  499. std::cerr << *e;
  500. delete e;
  501. }
  502. try {
  503. Util::parseUInt("4294967296");
  504. CPPUNIT_FAIL("exception must be thrown.");
  505. } catch(Exception* e) {
  506. std::cerr << *e;
  507. delete e;
  508. }
  509. }
  510. void UtilTest::testParseLLInt()
  511. {
  512. CPPUNIT_ASSERT_EQUAL(-1LL, Util::parseLLInt(" -1 "));
  513. CPPUNIT_ASSERT_EQUAL(9223372036854775807LL,
  514. Util::parseLLInt("9223372036854775807"));
  515. try {
  516. Util::parseLLInt("9223372036854775808");
  517. CPPUNIT_FAIL("exception must be thrown.");
  518. } catch(Exception* e) {
  519. std::cerr << *e;
  520. delete e;
  521. }
  522. try {
  523. Util::parseLLInt("-9223372036854775809");
  524. CPPUNIT_FAIL("exception must be thrown.");
  525. } catch(Exception* e) {
  526. std::cerr << *e;
  527. delete e;
  528. }
  529. try {
  530. Util::parseLLInt("12x");
  531. CPPUNIT_FAIL("exception must be thrown.");
  532. } catch(Exception* e) {
  533. std::cerr << *e;
  534. delete e;
  535. }
  536. try {
  537. Util::parseLLInt("");
  538. CPPUNIT_FAIL("exception must be thrown.");
  539. } catch(Exception* e) {
  540. std::cerr << *e;
  541. delete e;
  542. }
  543. }
  544. void UtilTest::testParseULLInt()
  545. {
  546. CPPUNIT_ASSERT_EQUAL(18446744073709551615ULL,
  547. Util::parseULLInt("18446744073709551615"));
  548. try {
  549. Util::parseUInt("-1");
  550. CPPUNIT_FAIL("exception must be thrown.");
  551. } catch(Exception* e) {
  552. std::cerr << *e;
  553. delete e;
  554. }
  555. try {
  556. Util::parseLLInt("18446744073709551616");
  557. CPPUNIT_FAIL("exception must be thrown.");
  558. } catch(Exception* e) {
  559. std::cerr << *e;
  560. delete e;
  561. }
  562. }
  563. void UtilTest::testToString_binaryStream()
  564. {
  565. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  566. std::string data(16*1024+256, 'a');
  567. dw->initAndOpenFile("dummy");
  568. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  569. std::string readData = Util::toString(dw);
  570. CPPUNIT_ASSERT_EQUAL(data, readData);
  571. }
  572. void UtilTest::testItos()
  573. {
  574. {
  575. int i = 0;
  576. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::itos(i));
  577. }
  578. {
  579. int i = 100;
  580. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i, true));
  581. }
  582. {
  583. int i = 100;
  584. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i));
  585. }
  586. {
  587. int i = 12345;
  588. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), Util::itos(i, true));
  589. }
  590. {
  591. int i = 12345;
  592. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::itos(i));
  593. }
  594. {
  595. int i = -12345;
  596. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), Util::itos(i, true));
  597. }
  598. {
  599. int64_t i = INT64_MAX;
  600. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), Util::itos(i, true));
  601. }
  602. }
  603. void UtilTest::testUitos()
  604. {
  605. {
  606. uint16_t i = 12345;
  607. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::uitos(i));
  608. }
  609. {
  610. int16_t i = -12345;
  611. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), Util::uitos(i));
  612. }
  613. }
  614. } // namespace aria2