UtilTest.cc 19 KB

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