UtilTest.cc 18 KB

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