irrString.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. #include <irrlicht.h>
  5. using namespace irr;
  6. using namespace core;
  7. static bool testSelfAssignment()
  8. {
  9. core::stringw myString(L"foo");
  10. myString = myString;
  11. return myString == core::stringw(L"foo");
  12. }
  13. static bool testSplit()
  14. {
  15. logTestString("Test stringw::split()\n");
  16. core::stringw teststring(L"[b]this [/b] is a [color=0xff000000]test[/color].");
  17. core::list<core::stringw> parts1;
  18. teststring.split<core::list<core::stringw> >(parts1, L"[");
  19. core::array<core::stringw> parts2;
  20. teststring.split<core::array<core::stringw> >(parts2, L"[", 1, false, true);
  21. return (parts1.size()==4) && (parts2.size()==9);
  22. }
  23. static bool testFastAlloc()
  24. {
  25. core::string<wchar_t, core::irrAllocatorFast<wchar_t> > FastString(L"abc");
  26. core::string<wchar_t, core::irrAllocatorFast<wchar_t> > FastStringLong(L"longer");
  27. FastString = L"test";
  28. // cause a reallocation
  29. FastString = FastStringLong;
  30. // this test should either not compile or crash when the allocaters are messed up
  31. return true;
  32. }
  33. static bool testReplace()
  34. {
  35. // test string getting longer
  36. core::stringw str = L"no";
  37. str.replace(L"no", L"yes");
  38. if ( str != L"yes" )
  39. return false;
  40. str = L"nonono";
  41. str.replace(L"no", L"yes");
  42. if ( str != L"yesyesyes" )
  43. return false;
  44. str = L"nomaybenomaybeno";
  45. str.replace(L"no", L"yes");
  46. if ( str != L"yesmaybeyesmaybeyes" )
  47. return false;
  48. // test string staying same length
  49. str = L"one";
  50. str.replace(L"one", L"two");
  51. if ( str != L"two" )
  52. return false;
  53. str = L"oneone";
  54. str.replace(L"one", L"two");
  55. if ( str != L"twotwo" )
  56. return false;
  57. // test string getting shorter
  58. str = L"yes";
  59. str.replace(L"yes", L"no");
  60. if ( str != L"no" )
  61. return false;
  62. str = L"yesyes";
  63. str.replace(L"yes", L"no");
  64. if ( str != L"nono" )
  65. return false;
  66. // remove string-parts completely
  67. str = L"killme";
  68. str.replace(L"killme", L"");
  69. if ( str != L"" )
  70. return false;
  71. str = L"killmenow";
  72. str.replace(L"killme", L"");
  73. if ( str != L"now" )
  74. return false;
  75. str = L"nowkillme";
  76. str.replace(L"killme", L"");
  77. if ( str != L"now" )
  78. return false;
  79. // remove nothing
  80. str = L"keepme";
  81. str.replace(L"", L"whatever");
  82. if ( str != L"keepme" )
  83. return false;
  84. str = L"keepme";
  85. str.replace(L"", L"");
  86. if ( str != L"keepme" )
  87. return false;
  88. return true;
  89. }
  90. bool testAppendStringc()
  91. {
  92. core::stringc str;
  93. // Test with character
  94. if (str != "")
  95. return false;
  96. str += 'W';
  97. if (str != "W")
  98. return false;
  99. str += 'i';
  100. if (str != "Wi")
  101. return false;
  102. str="";
  103. if (str != "")
  104. return false;
  105. // Test with C-style string
  106. str += "Another Test";
  107. if (str != "Another Test")
  108. return false;
  109. str="";
  110. str += 'A';
  111. str += "nother Test";
  112. if (str != "Another Test")
  113. return false;
  114. str="";
  115. // Test with int
  116. str += 10;
  117. if (str != "10")
  118. return false;
  119. str += 0;
  120. if (str != "100")
  121. return false;
  122. str="";
  123. str += "-32";
  124. if (str != "-32")
  125. return false;
  126. str="";
  127. // Test with unsigned int
  128. str += 21u;
  129. if (str != "21")
  130. return false;
  131. str += 0u;
  132. if (str != "210")
  133. return false;
  134. str="";
  135. // Test with long int
  136. str += 456l;
  137. if (str != "456")
  138. return false;
  139. str += 0l;
  140. if (str != "4560")
  141. return false;
  142. str="";
  143. str += -456l;
  144. if (str != "-456")
  145. return false;
  146. str="";
  147. // Test with unsigned long
  148. str += 994ul;
  149. if (str != "994")
  150. return false;
  151. str += 0ul;
  152. if (str != "9940")
  153. return false;
  154. str="";
  155. return true;
  156. }
  157. bool testInsert()
  158. {
  159. core::stringc str;
  160. str.insert(0, "something", 4);
  161. if (str != "some")
  162. return false;
  163. str.insert(4, "thing", 5);
  164. if (str != "something")
  165. return false;
  166. str.insert(0, "is ", 3);
  167. if (str != "is something")
  168. return false;
  169. str.insert(3, "there ", 6);
  170. if (str != "is there something")
  171. return false;
  172. return true;
  173. }
  174. bool testLowerUpper()
  175. {
  176. irr::core::array <irr::core::stringc> stringsOrig, targetLower, targetUpper;
  177. stringsOrig.push_back("abc");
  178. targetLower.push_back("abc");
  179. targetUpper.push_back("ABC");
  180. stringsOrig.push_back("ABC");
  181. targetLower.push_back("abc");
  182. targetUpper.push_back("ABC");
  183. stringsOrig.push_back("Abc");
  184. targetLower.push_back("abc");
  185. targetUpper.push_back("ABC");
  186. stringsOrig.push_back("aBBc");
  187. targetLower.push_back("abbc");
  188. targetUpper.push_back("ABBC");
  189. stringsOrig.push_back("abC");
  190. targetLower.push_back("abc");
  191. targetUpper.push_back("ABC");
  192. stringsOrig.push_back("");
  193. targetLower.push_back("");
  194. targetUpper.push_back("");
  195. /* TODO: those are not supported so far
  196. stringsOrig.push_back("ßäöü");
  197. targetLower.push_back("ßäöü");
  198. targetUpper.push_back("ßÄÖÜ");
  199. stringsOrig.push_back("ßÄÖÜ");
  200. targetLower.push_back("ßäöü");
  201. targetUpper.push_back("ßÄÖÜ");
  202. */
  203. for ( irr::u32 i=0; i<stringsOrig.size(); ++i )
  204. {
  205. irr::core::stringc c = stringsOrig[i];
  206. c.make_lower();
  207. if ( c != targetLower[i] )
  208. {
  209. logTestString("make_lower for stringc failed in test %d %s\n", i, stringsOrig[i].c_str());
  210. return false;
  211. }
  212. c = stringsOrig[i];
  213. c.make_upper();
  214. if ( c != targetUpper[i] )
  215. {
  216. logTestString("make_upper for stringc failed in test %d %s %s\n", i, stringsOrig[i].c_str(), c.c_str());
  217. return false;
  218. }
  219. irr::core::stringw w = irr::core::stringw(stringsOrig[i]);
  220. c.make_lower();
  221. if ( c != irr::core::stringw(targetLower[i]) )
  222. {
  223. logTestString("make_lower for stringw failed in test %d %s\n", i, stringsOrig[i].c_str());
  224. return false;
  225. }
  226. c = irr::core::stringw(stringsOrig[i]);
  227. c.make_upper();
  228. if ( c != irr::core::stringw(targetUpper[i]) )
  229. {
  230. logTestString("make_upper for stringw failed in test %d %s\n", i, stringsOrig[i].c_str());
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. bool testFindFunctions()
  237. {
  238. irr::core::stringc dot(".");
  239. irr::s32 p = dot.findFirst(0);
  240. if ( p >= 0 )
  241. return false;
  242. irr::core::stringc empty("");
  243. p = empty.findLastCharNotInList("x",1);
  244. if ( p >= 0 )
  245. return false;
  246. irr::core::stringc lastX("max");
  247. p = lastX.findLastCharNotInList("x",1);
  248. if ( p != 1 )
  249. return false;
  250. p = lastX.findLastCharNotInList("y",1);
  251. if ( p != 2 )
  252. return false;
  253. p = empty.findLast('x');
  254. if ( p >= 0 )
  255. return false;
  256. p = dot.findLast('.');
  257. if ( p != 0 )
  258. return false;
  259. p = empty.findLastChar("ab", 2);
  260. if ( p >= 0 )
  261. return false;
  262. p = dot.findLastChar("-.", 2);
  263. if ( p != 0 )
  264. return false;
  265. return true;
  266. }
  267. bool testErase()
  268. {
  269. if ( stringc(1.f).eraseTrailingFloatZeros() != stringc("1") )
  270. return false;
  271. if ( stringc("0.100000").eraseTrailingFloatZeros() != stringc("0.1") )
  272. return false;
  273. if ( stringc("10.000000").eraseTrailingFloatZeros() != stringc("10") )
  274. return false;
  275. if ( stringc("foo 3.140000").eraseTrailingFloatZeros() != stringc("foo 3.14") )
  276. return false;
  277. if ( stringc("no_num.000").eraseTrailingFloatZeros() != stringc("no_num.000") )
  278. return false;
  279. if ( stringc("1.").eraseTrailingFloatZeros() != stringc("1.") )
  280. return false;
  281. return true;
  282. }
  283. // Test the functionality of irrString
  284. /** Validation is done with assert_log() against expected results. */
  285. bool testIrrString(void)
  286. {
  287. bool allExpected = true;
  288. logTestString("Test stringc\n");
  289. {
  290. // Check empty string
  291. core::stringc empty;
  292. assert_log(empty.size()==0);
  293. assert_log(empty[0]==0);
  294. assert_log(empty.c_str()!=0);
  295. assert_log(*(empty.c_str())==0);
  296. // Assign content
  297. empty = "Test";
  298. assert_log(empty.size()==4);
  299. assert_log(empty[0]=='T');
  300. assert_log(empty[3]=='t');
  301. assert_log(*(empty.c_str())=='T');
  302. //Assign empty string, should be same as in the beginning
  303. empty = "";
  304. assert_log(empty.size()==0);
  305. assert_log(empty[0]==0);
  306. assert_log(*(empty.c_str())==0);
  307. }
  308. logTestString("Test stringw\n");
  309. {
  310. core::stringw empty;
  311. assert_log(empty.size()==0);
  312. assert_log(empty[0]==0);
  313. assert_log(empty.c_str()!=0);
  314. assert_log(*(empty.c_str())==0);
  315. empty = L"Test";
  316. assert_log(empty.size()==4);
  317. assert_log(empty[0]==L'T');
  318. assert_log(empty[3]=='t');
  319. assert_log(*(empty.c_str())==L'T');
  320. empty = L"";
  321. assert_log(empty.size()==0);
  322. assert_log(empty[0]==0);
  323. assert_log(*(empty.c_str())==0);
  324. assert_log(allExpected &= testSplit());
  325. }
  326. allExpected &= testAppendStringc();
  327. allExpected &= testInsert();
  328. logTestString("Test io::path\n");
  329. {
  330. // Only test that this type exists, it's one from above
  331. io::path myPath;
  332. myPath = "Some text"; // Only to avoid wrong optimizations
  333. }
  334. logTestString("Test self assignment\n");
  335. allExpected &= testSelfAssignment();
  336. logTestString("test fast alloc\n");
  337. allExpected &= testFastAlloc();
  338. logTestString("test replace\n");
  339. allExpected &= testReplace();
  340. logTestString("test make_lower and make_uppers\n");
  341. allExpected &= testLowerUpper();
  342. logTestString("test find functions\n");
  343. allExpected &= testFindFunctions();
  344. logTestString("test erase functions\n");
  345. allExpected &= testErase();
  346. if(allExpected)
  347. logTestString("\nAll tests passed\n");
  348. else
  349. logTestString("\nFAIL!\n");
  350. return allExpected;
  351. }