UtilTest.cc 20 KB

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