irrString.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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::list<core::stringw> parts2;
  20. teststring.split<core::list<core::stringw> >(parts2, L"[", 1, false, true);
  21. return (parts1.getSize()==4) && (parts2.getSize()==5);
  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 testLowerUpper()
  158. {
  159. irr::core::array <irr::core::stringc> stringsOrig, targetLower, targetUpper;
  160. stringsOrig.push_back("abc");
  161. targetLower.push_back("abc");
  162. targetUpper.push_back("ABC");
  163. stringsOrig.push_back("ABC");
  164. targetLower.push_back("abc");
  165. targetUpper.push_back("ABC");
  166. stringsOrig.push_back("Abc");
  167. targetLower.push_back("abc");
  168. targetUpper.push_back("ABC");
  169. stringsOrig.push_back("aBBc");
  170. targetLower.push_back("abbc");
  171. targetUpper.push_back("ABBC");
  172. stringsOrig.push_back("abC");
  173. targetLower.push_back("abc");
  174. targetUpper.push_back("ABC");
  175. stringsOrig.push_back("");
  176. targetLower.push_back("");
  177. targetUpper.push_back("");
  178. /* TODO: those are not supported so far
  179. stringsOrig.push_back("ßäöü");
  180. targetLower.push_back("ßäöü");
  181. targetUpper.push_back("ßÄÖÜ");
  182. stringsOrig.push_back("ßÄÖÜ");
  183. targetLower.push_back("ßäöü");
  184. targetUpper.push_back("ßÄÖÜ");
  185. */
  186. for ( irr::u32 i=0; i<stringsOrig.size(); ++i )
  187. {
  188. irr::core::stringc c = stringsOrig[i];
  189. c.make_lower();
  190. if ( c != targetLower[i] )
  191. {
  192. logTestString("make_lower for stringc failed in test %d %s\n", i, stringsOrig[i].c_str());
  193. return false;
  194. }
  195. c = stringsOrig[i];
  196. c.make_upper();
  197. if ( c != targetUpper[i] )
  198. {
  199. logTestString("make_upper for stringc failed in test %d %s %s\n", i, stringsOrig[i].c_str(), c.c_str());
  200. return false;
  201. }
  202. irr::core::stringw w = irr::core::stringw(stringsOrig[i]);
  203. c.make_lower();
  204. if ( c != irr::core::stringw(targetLower[i]) )
  205. {
  206. logTestString("make_lower for stringw failed in test %d %s\n", i, stringsOrig[i].c_str());
  207. return false;
  208. }
  209. c = irr::core::stringw(stringsOrig[i]);
  210. c.make_upper();
  211. if ( c != irr::core::stringw(targetUpper[i]) )
  212. {
  213. logTestString("make_upper for stringw failed in test %d %s\n", i, stringsOrig[i].c_str());
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. bool testFindFunctions()
  220. {
  221. irr::core::stringc dot(".");
  222. irr::s32 p = dot.findFirst(0);
  223. if ( p >= 0 )
  224. return false;
  225. irr::core::stringc empty("");
  226. p = empty.findLastCharNotInList("x",1);
  227. if ( p >= 0 )
  228. return false;
  229. p = empty.findLast('x');
  230. if ( p >= 0 )
  231. return false;
  232. p = dot.findLast('.');
  233. if ( p != 0 )
  234. return false;
  235. p = empty.findLastChar("ab", 2);
  236. if ( p >= 0 )
  237. return false;
  238. p = dot.findLastChar("-.", 2);
  239. if ( p != 0 )
  240. return false;
  241. return true;
  242. }
  243. // Test the functionality of irrString
  244. /** Validation is done with assert_log() against expected results. */
  245. bool testIrrString(void)
  246. {
  247. bool allExpected = true;
  248. logTestString("Test stringc\n");
  249. {
  250. // Check empty string
  251. core::stringc empty;
  252. assert_log(empty.size()==0);
  253. assert_log(empty[0]==0);
  254. assert_log(empty.c_str()!=0);
  255. assert_log(*(empty.c_str())==0);
  256. // Assign content
  257. empty = "Test";
  258. assert_log(empty.size()==4);
  259. assert_log(empty[0]=='T');
  260. assert_log(empty[3]=='t');
  261. assert_log(*(empty.c_str())=='T');
  262. //Assign empty string, should be same as in the beginning
  263. empty = "";
  264. assert_log(empty.size()==0);
  265. assert_log(empty[0]==0);
  266. assert_log(*(empty.c_str())==0);
  267. }
  268. logTestString("Test stringw\n");
  269. {
  270. core::stringw empty;
  271. assert_log(empty.size()==0);
  272. assert_log(empty[0]==0);
  273. assert_log(empty.c_str()!=0);
  274. assert_log(*(empty.c_str())==0);
  275. empty = L"Test";
  276. assert_log(empty.size()==4);
  277. assert_log(empty[0]==L'T');
  278. assert_log(empty[3]=='t');
  279. assert_log(*(empty.c_str())==L'T');
  280. empty = L"";
  281. assert_log(empty.size()==0);
  282. assert_log(empty[0]==0);
  283. assert_log(*(empty.c_str())==0);
  284. assert_log(allExpected &= testSplit());
  285. }
  286. allExpected &= testAppendStringc();
  287. logTestString("Test io::path\n");
  288. {
  289. // Only test that this type exists, it's one from above
  290. io::path myPath;
  291. myPath = "Some text"; // Only to avoid wrong optimizations
  292. }
  293. logTestString("Test self assignment\n");
  294. allExpected &= testSelfAssignment();
  295. logTestString("test fast alloc\n");
  296. allExpected &= testFastAlloc();
  297. logTestString("test replace\n");
  298. allExpected &= testReplace();
  299. logTestString("test make_lower and make_uppers\n");
  300. allExpected &= testLowerUpper();
  301. logTestString("test find functions\n");
  302. allExpected &= testFindFunctions();
  303. if(allExpected)
  304. logTestString("\nAll tests passed\n");
  305. else
  306. logTestString("\nFAIL!\n");
  307. return allExpected;
  308. }