UtilTest.cc 20 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(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.stackTrace();
  270. }
  271. try {
  272. Util::getRealSize("foo");
  273. CPPUNIT_FAIL("exception must be thrown.");
  274. } catch(Exception& e) {
  275. std::cerr << e.stackTrace();
  276. }
  277. try {
  278. Util::getRealSize("-1");
  279. CPPUNIT_FAIL("exception must be thrown.");
  280. } catch(Exception& e) {
  281. std::cerr << e.stackTrace();
  282. }
  283. try {
  284. Util::getRealSize("9223372036854775807K");
  285. CPPUNIT_FAIL("exception must be thrown.");
  286. } catch(Exception& e) {
  287. std::cerr << e.stackTrace();
  288. }
  289. try {
  290. Util::getRealSize("9223372036854775807M");
  291. CPPUNIT_FAIL("exception must be thrown.");
  292. } catch(Exception& e) {
  293. std::cerr << e.stackTrace();
  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(0U, Util::alphaToNum("a"));
  353. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("aa"));
  354. CPPUNIT_ASSERT_EQUAL(1U, Util::alphaToNum("b"));
  355. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("zz")); // 25*26+25
  356. CPPUNIT_ASSERT_EQUAL(675U, Util::alphaToNum("ZZ")); // 25*26+25
  357. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum(""));
  358. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::alphaToNum("NXMRLXV"));
  359. CPPUNIT_ASSERT_EQUAL(0U, Util::alphaToNum("NXMRLXW")); // uint32_t overflow
  360. }
  361. void UtilTest::testMkdirs()
  362. {
  363. std::string dir = "/tmp/aria2-UtilTest-testMkdirs";
  364. File d(dir);
  365. if(d.exists()) {
  366. CPPUNIT_ASSERT(d.remove());
  367. }
  368. CPPUNIT_ASSERT(!d.exists());
  369. Util::mkdirs(dir);
  370. CPPUNIT_ASSERT(d.isDir());
  371. std::string file = "./UtilTest.cc";
  372. File f(file);
  373. CPPUNIT_ASSERT(f.isFile());
  374. try {
  375. Util::mkdirs(file);
  376. CPPUNIT_FAIL("exception must be thrown.");
  377. } catch(DlAbortEx& ex) {
  378. std::cerr << ex.stackTrace() << std::endl;
  379. }
  380. }
  381. void UtilTest::testConvertBitfield()
  382. {
  383. BitfieldMan srcBitfield(384*1024, 256*1024*256+1);
  384. BitfieldMan destBitfield(512*1024, srcBitfield.getTotalLength());
  385. srcBitfield.setAllBit();
  386. srcBitfield.unsetBit(2);// <- range [768, 1152)
  387. // which corresponds to the index [1,2] in destBitfield
  388. Util::convertBitfield(&destBitfield, &srcBitfield);
  389. CPPUNIT_ASSERT_EQUAL(std::string("9fffffffffffffffffffffffffffffff80"),
  390. Util::toHex(destBitfield.getBitfield(),
  391. destBitfield.getBitfieldLength()));
  392. }
  393. void UtilTest::testParseIntRange()
  394. {
  395. IntSequence seq = Util::parseIntRange("1,3-8,10");
  396. CPPUNIT_ASSERT(seq.hasNext());
  397. CPPUNIT_ASSERT_EQUAL((int32_t)1, seq.next());
  398. CPPUNIT_ASSERT(seq.hasNext());
  399. CPPUNIT_ASSERT_EQUAL((int32_t)3, seq.next());
  400. CPPUNIT_ASSERT(seq.hasNext());
  401. CPPUNIT_ASSERT_EQUAL((int32_t)4, seq.next());
  402. CPPUNIT_ASSERT(seq.hasNext());
  403. CPPUNIT_ASSERT_EQUAL((int32_t)5, seq.next());
  404. CPPUNIT_ASSERT(seq.hasNext());
  405. CPPUNIT_ASSERT_EQUAL((int32_t)6, seq.next());
  406. CPPUNIT_ASSERT(seq.hasNext());
  407. CPPUNIT_ASSERT_EQUAL((int32_t)7, seq.next());
  408. CPPUNIT_ASSERT(seq.hasNext());
  409. CPPUNIT_ASSERT_EQUAL((int32_t)8, seq.next());
  410. CPPUNIT_ASSERT(seq.hasNext());
  411. CPPUNIT_ASSERT_EQUAL((int32_t)10, seq.next());
  412. CPPUNIT_ASSERT(!seq.hasNext());
  413. CPPUNIT_ASSERT_EQUAL((int32_t)0, seq.next());
  414. }
  415. void UtilTest::testParseIntRange_invalidRange()
  416. {
  417. try {
  418. IntSequence seq = Util::parseIntRange("-1");
  419. CPPUNIT_FAIL("exception must be thrown.");
  420. } catch(Exception& e) {
  421. std::cerr << e.stackTrace();
  422. }
  423. try {
  424. IntSequence seq = Util::parseIntRange("2147483648");
  425. CPPUNIT_FAIL("exception must be thrown.");
  426. } catch(Exception& e) {
  427. std::cerr << e.stackTrace();
  428. }
  429. try {
  430. IntSequence seq = Util::parseIntRange("2147483647-2147483648");
  431. CPPUNIT_FAIL("exception must be thrown.");
  432. } catch(Exception& e) {
  433. std::cerr << e.stackTrace();
  434. }
  435. try {
  436. IntSequence seq = Util::parseIntRange("1-2x");
  437. CPPUNIT_FAIL("exception must be thrown.");
  438. } catch(Exception& e) {
  439. std::cerr << e.stackTrace();
  440. }
  441. try {
  442. IntSequence seq = Util::parseIntRange("3x-4");
  443. CPPUNIT_FAIL("exception must be thrown.");
  444. } catch(Exception& e) {
  445. std::cerr << e.stackTrace();
  446. }
  447. }
  448. void UtilTest::testParseInt()
  449. {
  450. CPPUNIT_ASSERT_EQUAL(-1, Util::parseInt(" -1 "));
  451. CPPUNIT_ASSERT_EQUAL(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.stackTrace();
  457. }
  458. try {
  459. Util::parseInt("-2147483649");
  460. CPPUNIT_FAIL("exception must be thrown.");
  461. } catch(Exception& e) {
  462. std::cerr << e.stackTrace();
  463. }
  464. try {
  465. Util::parseInt("12x");
  466. CPPUNIT_FAIL("exception must be thrown.");
  467. } catch(Exception& e) {
  468. std::cerr << e.stackTrace();
  469. }
  470. try {
  471. Util::parseInt("");
  472. CPPUNIT_FAIL("exception must be thrown.");
  473. } catch(Exception& e) {
  474. std::cerr << e.stackTrace();
  475. }
  476. }
  477. void UtilTest::testParseUInt()
  478. {
  479. CPPUNIT_ASSERT_EQUAL(4294967295U, Util::parseUInt(" 4294967295 "));
  480. try {
  481. Util::parseUInt("-1");
  482. CPPUNIT_FAIL("exception must be thrown.");
  483. } catch(Exception& e) {
  484. std::cerr << e.stackTrace();
  485. }
  486. try {
  487. Util::parseUInt("4294967296");
  488. CPPUNIT_FAIL("exception must be thrown.");
  489. } catch(Exception& e) {
  490. std::cerr << e.stackTrace();
  491. }
  492. }
  493. void UtilTest::testParseLLInt()
  494. {
  495. CPPUNIT_ASSERT_EQUAL((int64_t)-1LL, Util::parseLLInt(" -1 "));
  496. CPPUNIT_ASSERT_EQUAL((int64_t)9223372036854775807LL,
  497. Util::parseLLInt("9223372036854775807"));
  498. try {
  499. Util::parseLLInt("9223372036854775808");
  500. CPPUNIT_FAIL("exception must be thrown.");
  501. } catch(Exception& e) {
  502. std::cerr << e.stackTrace();
  503. }
  504. try {
  505. Util::parseLLInt("-9223372036854775809");
  506. CPPUNIT_FAIL("exception must be thrown.");
  507. } catch(Exception& e) {
  508. std::cerr << e.stackTrace();
  509. }
  510. try {
  511. Util::parseLLInt("12x");
  512. CPPUNIT_FAIL("exception must be thrown.");
  513. } catch(Exception& e) {
  514. std::cerr << e.stackTrace();
  515. }
  516. try {
  517. Util::parseLLInt("");
  518. CPPUNIT_FAIL("exception must be thrown.");
  519. } catch(Exception& e) {
  520. std::cerr << e.stackTrace();
  521. }
  522. }
  523. void UtilTest::testParseULLInt()
  524. {
  525. CPPUNIT_ASSERT_EQUAL((uint64_t)18446744073709551615ULL,
  526. Util::parseULLInt("18446744073709551615"));
  527. try {
  528. Util::parseUInt("-1");
  529. CPPUNIT_FAIL("exception must be thrown.");
  530. } catch(Exception& e) {
  531. std::cerr << e.stackTrace();
  532. }
  533. try {
  534. Util::parseLLInt("18446744073709551616");
  535. CPPUNIT_FAIL("exception must be thrown.");
  536. } catch(Exception& e) {
  537. std::cerr << e.stackTrace();
  538. }
  539. }
  540. void UtilTest::testToString_binaryStream()
  541. {
  542. SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
  543. std::string data(16*1024+256, 'a');
  544. dw->initAndOpenFile("dummy");
  545. dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
  546. std::string readData = Util::toString(dw);
  547. CPPUNIT_ASSERT_EQUAL(data, readData);
  548. }
  549. void UtilTest::testItos()
  550. {
  551. {
  552. int i = 0;
  553. CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::itos(i));
  554. }
  555. {
  556. int i = 100;
  557. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i, true));
  558. }
  559. {
  560. int i = 100;
  561. CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i));
  562. }
  563. {
  564. int i = 12345;
  565. CPPUNIT_ASSERT_EQUAL(std::string("12,345"), Util::itos(i, true));
  566. }
  567. {
  568. int i = 12345;
  569. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::itos(i));
  570. }
  571. {
  572. int i = -12345;
  573. CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), Util::itos(i, true));
  574. }
  575. {
  576. int64_t i = INT64_MAX;
  577. CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), Util::itos(i, true));
  578. }
  579. }
  580. void UtilTest::testUitos()
  581. {
  582. {
  583. uint16_t i = 12345;
  584. CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::uitos(i));
  585. }
  586. {
  587. int16_t i = -12345;
  588. CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), Util::uitos(i));
  589. }
  590. }
  591. } // namespace aria2