UtilTest.cc 17 KB

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