test_utilities.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "test.h"
  17. #include <cmath>
  18. #include "util/enriched_string.h"
  19. #include "util/numeric.h"
  20. #include "util/string.h"
  21. #include "util/base64.h"
  22. class TestUtilities : public TestBase {
  23. public:
  24. TestUtilities() { TestManager::registerTestModule(this); }
  25. const char *getName() { return "TestUtilities"; }
  26. void runTests(IGameDef *gamedef);
  27. void testAngleWrapAround();
  28. void testWrapDegrees_0_360_v3f();
  29. void testLowercase();
  30. void testTrim();
  31. void testIsYes();
  32. void testRemoveStringEnd();
  33. void testUrlEncode();
  34. void testUrlDecode();
  35. void testPadString();
  36. void testStartsWith();
  37. void testStrEqual();
  38. void testStrToIntConversion();
  39. void testStringReplace();
  40. void testStringAllowed();
  41. void testAsciiPrintableHelper();
  42. void testUTF8();
  43. void testRemoveEscapes();
  44. void testWrapRows();
  45. void testEnrichedString();
  46. void testIsNumber();
  47. void testIsPowerOfTwo();
  48. void testMyround();
  49. void testStringJoin();
  50. void testEulerConversion();
  51. void testBase64();
  52. void testSanitizeDirName();
  53. };
  54. static TestUtilities g_test_instance;
  55. void TestUtilities::runTests(IGameDef *gamedef)
  56. {
  57. TEST(testAngleWrapAround);
  58. TEST(testWrapDegrees_0_360_v3f);
  59. TEST(testLowercase);
  60. TEST(testTrim);
  61. TEST(testIsYes);
  62. TEST(testRemoveStringEnd);
  63. TEST(testUrlEncode);
  64. TEST(testUrlDecode);
  65. TEST(testPadString);
  66. TEST(testStartsWith);
  67. TEST(testStrEqual);
  68. TEST(testStrToIntConversion);
  69. TEST(testStringReplace);
  70. TEST(testStringAllowed);
  71. TEST(testAsciiPrintableHelper);
  72. TEST(testUTF8);
  73. TEST(testRemoveEscapes);
  74. TEST(testWrapRows);
  75. TEST(testEnrichedString);
  76. TEST(testIsNumber);
  77. TEST(testIsPowerOfTwo);
  78. TEST(testMyround);
  79. TEST(testStringJoin);
  80. TEST(testEulerConversion);
  81. TEST(testBase64);
  82. TEST(testSanitizeDirName);
  83. }
  84. ////////////////////////////////////////////////////////////////////////////////
  85. inline float ref_WrapDegrees180(float f)
  86. {
  87. // This is a slower alternative to the wrapDegrees_180() function;
  88. // used as a reference for testing
  89. float value = fmodf(f + 180, 360);
  90. if (value < 0)
  91. value += 360;
  92. return value - 180;
  93. }
  94. inline float ref_WrapDegrees_0_360(float f)
  95. {
  96. // This is a slower alternative to the wrapDegrees_0_360() function;
  97. // used as a reference for testing
  98. float value = fmodf(f, 360);
  99. if (value < 0)
  100. value += 360;
  101. return value < 0 ? value + 360 : value;
  102. }
  103. void TestUtilities::testAngleWrapAround() {
  104. UASSERT(fabs(modulo360f(100.0) - 100.0) < 0.001);
  105. UASSERT(fabs(modulo360f(720.5) - 0.5) < 0.001);
  106. UASSERT(fabs(modulo360f(-0.5) - (-0.5)) < 0.001);
  107. UASSERT(fabs(modulo360f(-365.5) - (-5.5)) < 0.001);
  108. for (float f = -720; f <= -360; f += 0.25) {
  109. UASSERT(std::fabs(modulo360f(f) - modulo360f(f + 360)) < 0.001);
  110. }
  111. for (float f = -1440; f <= 1440; f += 0.25) {
  112. UASSERT(std::fabs(modulo360f(f) - fmodf(f, 360)) < 0.001);
  113. UASSERT(std::fabs(wrapDegrees_180(f) - ref_WrapDegrees180(f)) < 0.001);
  114. UASSERT(std::fabs(wrapDegrees_0_360(f) - ref_WrapDegrees_0_360(f)) < 0.001);
  115. UASSERT(wrapDegrees_0_360(
  116. std::fabs(wrapDegrees_180(f) - wrapDegrees_0_360(f))) < 0.001);
  117. }
  118. }
  119. void TestUtilities::testWrapDegrees_0_360_v3f()
  120. {
  121. // only x test with little step
  122. for (float x = -720.f; x <= 720; x += 0.05) {
  123. v3f r = wrapDegrees_0_360_v3f(v3f(x, 0, 0));
  124. UASSERT(r.X >= 0.0f && r.X < 360.0f)
  125. UASSERT(r.Y == 0.0f)
  126. UASSERT(r.Z == 0.0f)
  127. }
  128. // only y test with little step
  129. for (float y = -720.f; y <= 720; y += 0.05) {
  130. v3f r = wrapDegrees_0_360_v3f(v3f(0, y, 0));
  131. UASSERT(r.X == 0.0f)
  132. UASSERT(r.Y >= 0.0f && r.Y < 360.0f)
  133. UASSERT(r.Z == 0.0f)
  134. }
  135. // only z test with little step
  136. for (float z = -720.f; z <= 720; z += 0.05) {
  137. v3f r = wrapDegrees_0_360_v3f(v3f(0, 0, z));
  138. UASSERT(r.X == 0.0f)
  139. UASSERT(r.Y == 0.0f)
  140. UASSERT(r.Z >= 0.0f && r.Z < 360.0f)
  141. }
  142. // test the whole coordinate translation
  143. for (float x = -720.f; x <= 720; x += 2.5) {
  144. for (float y = -720.f; y <= 720; y += 2.5) {
  145. for (float z = -720.f; z <= 720; z += 2.5) {
  146. v3f r = wrapDegrees_0_360_v3f(v3f(x, y, z));
  147. UASSERT(r.X >= 0.0f && r.X < 360.0f)
  148. UASSERT(r.Y >= 0.0f && r.Y < 360.0f)
  149. UASSERT(r.Z >= 0.0f && r.Z < 360.0f)
  150. }
  151. }
  152. }
  153. }
  154. void TestUtilities::testLowercase()
  155. {
  156. UASSERT(lowercase("Foo bAR") == "foo bar");
  157. UASSERT(lowercase("eeeeeeaaaaaaaaaaaààààà") == "eeeeeeaaaaaaaaaaaààààà");
  158. UASSERT(lowercase("MINETEST-powa") == "minetest-powa");
  159. }
  160. void TestUtilities::testTrim()
  161. {
  162. UASSERT(trim("") == "");
  163. UASSERT(trim("dirt_with_grass") == "dirt_with_grass");
  164. UASSERT(trim("\n \t\r Foo bAR \r\n\t\t ") == "Foo bAR");
  165. UASSERT(trim("\n \t\r \r\n\t\t ") == "");
  166. UASSERT(trim(" a") == "a");
  167. UASSERT(trim("a ") == "a");
  168. }
  169. void TestUtilities::testIsYes()
  170. {
  171. UASSERT(is_yes("YeS") == true);
  172. UASSERT(is_yes("") == false);
  173. UASSERT(is_yes("FAlse") == false);
  174. UASSERT(is_yes("-1") == true);
  175. UASSERT(is_yes("0") == false);
  176. UASSERT(is_yes("1") == true);
  177. UASSERT(is_yes("2") == true);
  178. }
  179. void TestUtilities::testRemoveStringEnd()
  180. {
  181. const char *ends[] = {"abc", "c", "bc", "", NULL};
  182. UASSERT(removeStringEnd("abc", ends) == "");
  183. UASSERT(removeStringEnd("bc", ends) == "b");
  184. UASSERT(removeStringEnd("12c", ends) == "12");
  185. UASSERT(removeStringEnd("foo", ends) == "");
  186. }
  187. void TestUtilities::testUrlEncode()
  188. {
  189. UASSERT(urlencode("\"Aardvarks lurk, OK?\"")
  190. == "%22Aardvarks%20lurk%2C%20OK%3F%22");
  191. }
  192. void TestUtilities::testUrlDecode()
  193. {
  194. UASSERT(urldecode("%22Aardvarks%20lurk%2C%20OK%3F%22")
  195. == "\"Aardvarks lurk, OK?\"");
  196. }
  197. void TestUtilities::testPadString()
  198. {
  199. UASSERT(padStringRight("hello", 8) == "hello ");
  200. }
  201. void TestUtilities::testStartsWith()
  202. {
  203. UASSERT(str_starts_with(std::string(), std::string()) == true);
  204. UASSERT(str_starts_with(std::string("the sharp pickaxe"),
  205. std::string()) == true);
  206. UASSERT(str_starts_with(std::string("the sharp pickaxe"),
  207. std::string("the")) == true);
  208. UASSERT(str_starts_with(std::string("the sharp pickaxe"),
  209. std::string("The")) == false);
  210. UASSERT(str_starts_with(std::string("the sharp pickaxe"),
  211. std::string("The"), true) == true);
  212. UASSERT(str_starts_with(std::string("T"), std::string("The")) == false);
  213. }
  214. void TestUtilities::testStrEqual()
  215. {
  216. UASSERT(str_equal(utf8_to_wide("abc"), utf8_to_wide("abc")));
  217. UASSERT(str_equal(utf8_to_wide("ABC"), utf8_to_wide("abc"), true));
  218. }
  219. void TestUtilities::testStrToIntConversion()
  220. {
  221. UASSERT(mystoi("123", 0, 1000) == 123);
  222. UASSERT(mystoi("123", 0, 10) == 10);
  223. }
  224. void TestUtilities::testStringReplace()
  225. {
  226. std::string test_str;
  227. test_str = "Hello there";
  228. str_replace(test_str, "there", "world");
  229. UASSERT(test_str == "Hello world");
  230. test_str = "ThisAisAaAtest";
  231. str_replace(test_str, 'A', ' ');
  232. UASSERT(test_str == "This is a test");
  233. }
  234. void TestUtilities::testStringAllowed()
  235. {
  236. UASSERT(string_allowed("hello", "abcdefghijklmno") == true);
  237. UASSERT(string_allowed("123", "abcdefghijklmno") == false);
  238. UASSERT(string_allowed_blacklist("hello", "123") == true);
  239. UASSERT(string_allowed_blacklist("hello123", "123") == false);
  240. }
  241. void TestUtilities::testAsciiPrintableHelper()
  242. {
  243. UASSERT(IS_ASCII_PRINTABLE_CHAR('e') == true);
  244. UASSERT(IS_ASCII_PRINTABLE_CHAR('\0') == false);
  245. // Ensures that there is no cutting off going on...
  246. // If there were, 331 would be cut to 75 in this example
  247. // and 73 is a valid ASCII char.
  248. int ch = 331;
  249. UASSERT(IS_ASCII_PRINTABLE_CHAR(ch) == false);
  250. }
  251. void TestUtilities::testUTF8()
  252. {
  253. UASSERT(utf8_to_wide("¤") == L"¤");
  254. UASSERT(wide_to_utf8(L"¤") == "¤");
  255. UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("")), "");
  256. UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("the shovel dug a crumbly node!")),
  257. "the shovel dug a crumbly node!");
  258. UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("-ä-")),
  259. "-ä-");
  260. UASSERTEQ(std::string, wide_to_utf8(utf8_to_wide("-\xF0\xA0\x80\x8B-")),
  261. "-\xF0\xA0\x80\x8B-");
  262. }
  263. void TestUtilities::testRemoveEscapes()
  264. {
  265. UASSERT(unescape_enriched<wchar_t>(
  266. L"abc\x1bXdef") == L"abcdef");
  267. UASSERT(unescape_enriched<wchar_t>(
  268. L"abc\x1b(escaped)def") == L"abcdef");
  269. UASSERT(unescape_enriched<wchar_t>(
  270. L"abc\x1b((escaped with parenthesis\\))def") == L"abcdef");
  271. UASSERT(unescape_enriched<wchar_t>(
  272. L"abc\x1b(incomplete") == L"abc");
  273. UASSERT(unescape_enriched<wchar_t>(
  274. L"escape at the end\x1b") == L"escape at the end");
  275. // Nested escapes not supported
  276. UASSERT(unescape_enriched<wchar_t>(
  277. L"abc\x1b(outer \x1b(inner escape)escape)def") == L"abcescape)def");
  278. }
  279. void TestUtilities::testWrapRows()
  280. {
  281. UASSERT(wrap_rows("12345678",4) == "1234\n5678");
  282. // test that wrap_rows doesn't wrap inside multibyte sequences
  283. {
  284. const unsigned char s[] = {
  285. 0x2f, 0x68, 0x6f, 0x6d, 0x65, 0x2f, 0x72, 0x61, 0x70, 0x74, 0x6f,
  286. 0x72, 0x2f, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0x2f,
  287. 0x6d, 0x69, 0x6e, 0x65, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x62, 0x69,
  288. 0x6e, 0x2f, 0x2e, 0x2e, 0};
  289. std::string str((char *)s);
  290. UASSERT(utf8_to_wide(wrap_rows(str, 20)) != L"<invalid UTF-8 string>");
  291. };
  292. {
  293. const unsigned char s[] = {
  294. 0x74, 0x65, 0x73, 0x74, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81,
  295. 0xd1, 0x82, 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82,
  296. 0x20, 0xd1, 0x82, 0xd0, 0xb5, 0xd1, 0x81, 0xd1, 0x82, 0};
  297. std::string str((char *)s);
  298. UASSERT(utf8_to_wide(wrap_rows(str, 8)) != L"<invalid UTF-8 string>");
  299. }
  300. }
  301. void TestUtilities::testEnrichedString()
  302. {
  303. EnrichedString str(L"Test bar");
  304. irr::video::SColor color(0xFF, 0, 0, 0xFF);
  305. UASSERT(str.substr(1, 3).getString() == L"est");
  306. str += L" BUZZ";
  307. UASSERT(str.substr(9, std::string::npos).getString() == L"BUZZ");
  308. str.setDefaultColor(color); // Blue foreground
  309. UASSERT(str.getColors()[5] == color);
  310. // Green background, then white and yellow text
  311. str = L"\x1b(b@#0F0)Regular \x1b(c@#FF0)yellow";
  312. UASSERT(str.getColors()[2] == 0xFFFFFFFF);
  313. str.setDefaultColor(color); // Blue foreground
  314. UASSERT(str.getColors()[13] == 0xFFFFFF00); // Still yellow text
  315. UASSERT(str.getBackground() == 0xFF00FF00); // Green background
  316. }
  317. void TestUtilities::testIsNumber()
  318. {
  319. UASSERT(is_number("123") == true);
  320. UASSERT(is_number("") == false);
  321. UASSERT(is_number("123a") == false);
  322. }
  323. void TestUtilities::testIsPowerOfTwo()
  324. {
  325. UASSERT(is_power_of_two(0) == false);
  326. UASSERT(is_power_of_two(1) == true);
  327. UASSERT(is_power_of_two(2) == true);
  328. UASSERT(is_power_of_two(3) == false);
  329. for (int exponent = 2; exponent <= 31; ++exponent) {
  330. UASSERT(is_power_of_two((1U << exponent) - 1) == false);
  331. UASSERT(is_power_of_two((1U << exponent)) == true);
  332. UASSERT(is_power_of_two((1U << exponent) + 1) == false);
  333. }
  334. UASSERT(is_power_of_two(U32_MAX) == false);
  335. }
  336. void TestUtilities::testMyround()
  337. {
  338. UASSERT(myround(4.6f) == 5);
  339. UASSERT(myround(1.2f) == 1);
  340. UASSERT(myround(-3.1f) == -3);
  341. UASSERT(myround(-6.5f) == -7);
  342. }
  343. void TestUtilities::testStringJoin()
  344. {
  345. std::vector<std::string> input;
  346. UASSERT(str_join(input, ",") == "");
  347. input.emplace_back("one");
  348. UASSERT(str_join(input, ",") == "one");
  349. input.emplace_back("two");
  350. UASSERT(str_join(input, ",") == "one,two");
  351. input.emplace_back("three");
  352. UASSERT(str_join(input, ",") == "one,two,three");
  353. input[1] = "";
  354. UASSERT(str_join(input, ",") == "one,,three");
  355. input[1] = "two";
  356. UASSERT(str_join(input, " and ") == "one and two and three");
  357. }
  358. static bool within(const f32 value1, const f32 value2, const f32 precision)
  359. {
  360. return std::fabs(value1 - value2) <= precision;
  361. }
  362. static bool within(const v3f &v1, const v3f &v2, const f32 precision)
  363. {
  364. return within(v1.X, v2.X, precision) && within(v1.Y, v2.Y, precision)
  365. && within(v1.Z, v2.Z, precision);
  366. }
  367. static bool within(const core::matrix4 &m1, const core::matrix4 &m2,
  368. const f32 precision)
  369. {
  370. const f32 *M1 = m1.pointer();
  371. const f32 *M2 = m2.pointer();
  372. for (int i = 0; i < 16; i++)
  373. if (! within(M1[i], M2[i], precision))
  374. return false;
  375. return true;
  376. }
  377. static bool roundTripsDeg(const v3f &v, const f32 precision)
  378. {
  379. core::matrix4 m;
  380. setPitchYawRoll(m, v);
  381. return within(v, getPitchYawRoll(m), precision);
  382. }
  383. void TestUtilities::testEulerConversion()
  384. {
  385. // This test may fail on non-IEEE systems.
  386. // Low tolerance is 4 ulp(1.0) for binary floats with 24 bit mantissa.
  387. // (ulp = unit in the last place; ulp(1.0) = 2^-23).
  388. const f32 tolL = 4.76837158203125e-7f;
  389. // High tolerance is 2 ulp(180.0), needed for numbers in degrees.
  390. // ulp(180.0) = 2^-16
  391. const f32 tolH = 3.0517578125e-5f;
  392. v3f v1, v2;
  393. core::matrix4 m1, m2;
  394. const f32 *M1 = m1.pointer();
  395. const f32 *M2 = m2.pointer();
  396. // Check that the radians version and the degrees version
  397. // produce the same results. Check also that the conversion
  398. // works both ways for these values.
  399. v1 = v3f(M_PI/3.0, M_PI/5.0, M_PI/4.0);
  400. v2 = v3f(60.0f, 36.0f, 45.0f);
  401. setPitchYawRollRad(m1, v1);
  402. setPitchYawRoll(m2, v2);
  403. UASSERT(within(m1, m2, tolL));
  404. UASSERT(within(getPitchYawRollRad(m1), v1, tolL));
  405. UASSERT(within(getPitchYawRoll(m2), v2, tolH));
  406. // Check the rotation matrix produced.
  407. UASSERT(within(M1[0], 0.932004869f, tolL));
  408. UASSERT(within(M1[1], 0.353553385f, tolL));
  409. UASSERT(within(M1[2], 0.0797927827f, tolL));
  410. UASSERT(within(M1[4], -0.21211791f, tolL));
  411. UASSERT(within(M1[5], 0.353553355f, tolL));
  412. UASSERT(within(M1[6], 0.911046684f, tolL));
  413. UASSERT(within(M1[8], 0.293892622f, tolL));
  414. UASSERT(within(M1[9], -0.866025448f, tolL));
  415. UASSERT(within(M1[10], 0.404508471f, tolL));
  416. // Check that the matrix is still homogeneous with no translation
  417. UASSERT(M1[3] == 0.0f);
  418. UASSERT(M1[7] == 0.0f);
  419. UASSERT(M1[11] == 0.0f);
  420. UASSERT(M1[12] == 0.0f);
  421. UASSERT(M1[13] == 0.0f);
  422. UASSERT(M1[14] == 0.0f);
  423. UASSERT(M1[15] == 1.0f);
  424. UASSERT(M2[3] == 0.0f);
  425. UASSERT(M2[7] == 0.0f);
  426. UASSERT(M2[11] == 0.0f);
  427. UASSERT(M2[12] == 0.0f);
  428. UASSERT(M2[13] == 0.0f);
  429. UASSERT(M2[14] == 0.0f);
  430. UASSERT(M2[15] == 1.0f);
  431. // Compare to Irrlicht's results. To be comparable, the
  432. // angles must come in a different order and the matrix
  433. // elements to compare are different too.
  434. m2.setRotationRadians(v3f(v1.Z, v1.X, v1.Y));
  435. UASSERT(within(M1[0], M2[5], tolL));
  436. UASSERT(within(M1[1], M2[6], tolL));
  437. UASSERT(within(M1[2], M2[4], tolL));
  438. UASSERT(within(M1[4], M2[9], tolL));
  439. UASSERT(within(M1[5], M2[10], tolL));
  440. UASSERT(within(M1[6], M2[8], tolL));
  441. UASSERT(within(M1[8], M2[1], tolL));
  442. UASSERT(within(M1[9], M2[2], tolL));
  443. UASSERT(within(M1[10], M2[0], tolL));
  444. // Check that Eulers that produce near gimbal-lock still round-trip
  445. UASSERT(roundTripsDeg(v3f(89.9999f, 17.f, 0.f), tolH));
  446. UASSERT(roundTripsDeg(v3f(89.9999f, 0.f, 19.f), tolH));
  447. UASSERT(roundTripsDeg(v3f(89.9999f, 17.f, 19.f), tolH));
  448. // Check that Eulers at an angle > 90 degrees may not round-trip...
  449. v1 = v3f(90.00001f, 1.f, 1.f);
  450. setPitchYawRoll(m1, v1);
  451. v2 = getPitchYawRoll(m1);
  452. //UASSERT(within(v1, v2, tolL)); // this is typically false
  453. // ... however the rotation matrix is the same for both
  454. setPitchYawRoll(m2, v2);
  455. UASSERT(within(m1, m2, tolL));
  456. }
  457. void TestUtilities::testBase64()
  458. {
  459. // Test character set
  460. UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  461. "abcdefghijklmnopqrstuvwxyz"
  462. "0123456789+/") == true);
  463. UASSERT(base64_is_valid("/+9876543210"
  464. "zyxwvutsrqponmlkjihgfedcba"
  465. "ZYXWVUTSRQPONMLKJIHGFEDCBA") == true);
  466. UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  467. "abcdefghijklmnopqrstuvwxyz"
  468. "0123456789+.") == false);
  469. UASSERT(base64_is_valid("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  470. "abcdefghijklmnopqrstuvwxyz"
  471. "0123456789 /") == false);
  472. // Test empty string
  473. UASSERT(base64_is_valid("") == true);
  474. // Test different lengths, with and without padding,
  475. // with correct and incorrect padding
  476. UASSERT(base64_is_valid("A") == false);
  477. UASSERT(base64_is_valid("AA") == true);
  478. UASSERT(base64_is_valid("AAA") == true);
  479. UASSERT(base64_is_valid("AAAA") == true);
  480. UASSERT(base64_is_valid("AAAAA") == false);
  481. UASSERT(base64_is_valid("AAAAAA") == true);
  482. UASSERT(base64_is_valid("AAAAAAA") == true);
  483. UASSERT(base64_is_valid("AAAAAAAA") == true);
  484. UASSERT(base64_is_valid("A===") == false);
  485. UASSERT(base64_is_valid("AA==") == true);
  486. UASSERT(base64_is_valid("AAA=") == true);
  487. UASSERT(base64_is_valid("AAAA") == true);
  488. UASSERT(base64_is_valid("AAAA====") == false);
  489. UASSERT(base64_is_valid("AAAAA===") == false);
  490. UASSERT(base64_is_valid("AAAAAA==") == true);
  491. UASSERT(base64_is_valid("AAAAAAA=") == true);
  492. UASSERT(base64_is_valid("AAAAAAA==") == false);
  493. UASSERT(base64_is_valid("AAAAAAA===") == false);
  494. UASSERT(base64_is_valid("AAAAAAA====") == false);
  495. UASSERT(base64_is_valid("AAAAAAAA") == true);
  496. UASSERT(base64_is_valid("AAAAAAAA=") == false);
  497. UASSERT(base64_is_valid("AAAAAAAA==") == false);
  498. UASSERT(base64_is_valid("AAAAAAAA===") == false);
  499. UASSERT(base64_is_valid("AAAAAAAA====") == false);
  500. // Test if canonical encoding
  501. // Last character limitations, length % 4 == 3
  502. UASSERT(base64_is_valid("AAB") == false);
  503. UASSERT(base64_is_valid("AAE") == true);
  504. UASSERT(base64_is_valid("AAQ") == true);
  505. UASSERT(base64_is_valid("AAB=") == false);
  506. UASSERT(base64_is_valid("AAE=") == true);
  507. UASSERT(base64_is_valid("AAQ=") == true);
  508. UASSERT(base64_is_valid("AAAAAAB=") == false);
  509. UASSERT(base64_is_valid("AAAAAAE=") == true);
  510. UASSERT(base64_is_valid("AAAAAAQ=") == true);
  511. // Last character limitations, length % 4 == 2
  512. UASSERT(base64_is_valid("AB") == false);
  513. UASSERT(base64_is_valid("AE") == false);
  514. UASSERT(base64_is_valid("AQ") == true);
  515. UASSERT(base64_is_valid("AB==") == false);
  516. UASSERT(base64_is_valid("AE==") == false);
  517. UASSERT(base64_is_valid("AQ==") == true);
  518. UASSERT(base64_is_valid("AAAAAB==") == false);
  519. UASSERT(base64_is_valid("AAAAAE==") == false);
  520. UASSERT(base64_is_valid("AAAAAQ==") == true);
  521. // Extraneous character present
  522. UASSERT(base64_is_valid(".") == false);
  523. UASSERT(base64_is_valid("A.") == false);
  524. UASSERT(base64_is_valid("AA.") == false);
  525. UASSERT(base64_is_valid("AAA.") == false);
  526. UASSERT(base64_is_valid("AAAA.") == false);
  527. UASSERT(base64_is_valid("AAAAA.") == false);
  528. UASSERT(base64_is_valid("A.A") == false);
  529. UASSERT(base64_is_valid("AA.A") == false);
  530. UASSERT(base64_is_valid("AAA.A") == false);
  531. UASSERT(base64_is_valid("AAAA.A") == false);
  532. UASSERT(base64_is_valid("AAAAA.A") == false);
  533. UASSERT(base64_is_valid("\xE1""AAA") == false);
  534. // Padding in wrong position
  535. UASSERT(base64_is_valid("A=A") == false);
  536. UASSERT(base64_is_valid("AA=A") == false);
  537. UASSERT(base64_is_valid("AAA=A") == false);
  538. UASSERT(base64_is_valid("AAAA=A") == false);
  539. UASSERT(base64_is_valid("AAAAA=A") == false);
  540. }
  541. void TestUtilities::testSanitizeDirName()
  542. {
  543. UASSERT(sanitizeDirName("a", "~") == "a");
  544. UASSERT(sanitizeDirName(" ", "~") == "__");
  545. UASSERT(sanitizeDirName(" a ", "~") == "_a_");
  546. UASSERT(sanitizeDirName("COM1", "~") == "~COM1");
  547. UASSERT(sanitizeDirName("COM1", ":") == "_COM1");
  548. UASSERT(sanitizeDirName("cOm\u00B2", "~") == "~cOm\u00B2");
  549. UASSERT(sanitizeDirName("cOnIn$", "~") == "~cOnIn$");
  550. UASSERT(sanitizeDirName(" cOnIn$ ", "~") == "_cOnIn$_");
  551. }