UtilTest.cc 19 KB

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