UtilTest.cc 17 KB

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