test_string.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*************************************************************************/
  2. /* test_string.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "ustring.h"
  30. #include <wchar.h>
  31. //#include "math_funcs.h"
  32. #include <stdio.h>
  33. #include "os/os.h"
  34. #include "drivers/trex/regex.h"
  35. #include "test_string.h"
  36. namespace TestString {
  37. bool test_1() {
  38. OS::get_singleton()->print("\n\nTest 1: Assign from cstr\n");
  39. String s = "Hello";
  40. OS::get_singleton()->print("\tExpected: Hello\n");
  41. OS::get_singleton()->print("\tResulted: %ls\n",s.c_str());
  42. return (wcscmp(s.c_str(),L"Hello")==0);
  43. }
  44. bool test_2() {
  45. OS::get_singleton()->print("\n\nTest 2: Assign from string (operator=)\n");
  46. String s = "Dolly";
  47. String t = s;
  48. OS::get_singleton()->print("\tExpected: Dolly\n");
  49. OS::get_singleton()->print("\tResulted: %ls\n",t.c_str());
  50. return (wcscmp(t.c_str(),L"Dolly")==0);
  51. }
  52. bool test_3() {
  53. OS::get_singleton()->print("\n\nTest 3: Assign from c-string (copycon)\n");
  54. String s("Sheep");
  55. String t(s);
  56. OS::get_singleton()->print("\tExpected: Sheep\n");
  57. OS::get_singleton()->print("\tResulted: %ls\n",t.c_str());
  58. return (wcscmp(t.c_str(),L"Sheep")==0);
  59. }
  60. bool test_4() {
  61. OS::get_singleton()->print("\n\nTest 4: Assign from c-widechar (operator=)\n");
  62. String s(L"Give me");
  63. OS::get_singleton()->print("\tExpected: Give me\n");
  64. OS::get_singleton()->print("\tResulted: %ls\n",s.c_str());
  65. return (wcscmp(s.c_str(),L"Give me")==0);
  66. }
  67. bool test_5() {
  68. OS::get_singleton()->print("\n\nTest 5: Assign from c-widechar (copycon)\n");
  69. String s(L"Wool");
  70. OS::get_singleton()->print("\tExpected: Wool\n");
  71. OS::get_singleton()->print("\tResulted: %ls\n",s.c_str());
  72. return (wcscmp(s.c_str(),L"Wool")==0);
  73. }
  74. bool test_6() {
  75. OS::get_singleton()->print("\n\nTest 6: comparisons (equal)\n");
  76. String s="Test Compare";
  77. OS::get_singleton()->print("\tComparing to \"Test Compare\"\n");
  78. if (! ( s=="Test Compare" ) )
  79. return false;
  80. if (! ( s==L"Test Compare" ) )
  81. return false;
  82. if (! ( s==String("Test Compare") ) )
  83. return false;
  84. return true;
  85. }
  86. bool test_7() {
  87. OS::get_singleton()->print("\n\nTest 7: comparisons (unequal)\n");
  88. String s="Test Compare";
  89. OS::get_singleton()->print("\tComparing to \"Test Compare\"\n");
  90. if (! ( s!="Peanut" ) )
  91. return false;
  92. if (! ( s!=L"Coconut" ) )
  93. return false;
  94. if (! ( s!=String("Butter") ) )
  95. return false;
  96. return true;
  97. }
  98. bool test_8() {
  99. OS::get_singleton()->print("\n\nTest 8: comparisons (operator<)\n");
  100. String s="Bees";
  101. OS::get_singleton()->print("\tComparing to \"Bees\"\n");
  102. if ( ! (s < "Elephant") )
  103. return false;
  104. if ( s < L"Amber" )
  105. return false;
  106. if ( s < String("Beatrix") )
  107. return false;
  108. return true;
  109. }
  110. bool test_9() {
  111. OS::get_singleton()->print("\n\nTest 9: Concatenation\n");
  112. String s;
  113. s+="Have";
  114. s+=' ';
  115. s+='a';
  116. s+=String(" ");
  117. s = s + L"Nice";
  118. s = s + " ";
  119. s = s + String("Day");
  120. OS::get_singleton()->print("\tComparing to \"Have a Nice Day\"\n");
  121. return (s == "Have a Nice Day");
  122. }
  123. bool test_10() {
  124. OS::get_singleton()->print("\n\nTest 10: Misc funcs (size/length/empty/etc)\n");
  125. if (! String("").empty())
  126. return false;
  127. if (String("Mellon").size() != 7)
  128. return false;
  129. if (String("Oranges").length() != 7)
  130. return false;
  131. return true;
  132. }
  133. bool test_11() {
  134. OS::get_singleton()->print("\n\nTest 11: Operator[]\n");
  135. String a="Kugar Sane";
  136. a[0]='S';
  137. a[6]='C';
  138. if (a != "Sugar Cane")
  139. return false;
  140. if (a[1]!='u')
  141. return false;
  142. return true;
  143. }
  144. bool test_12() {
  145. OS::get_singleton()->print("\n\nTest 12: case functions\n");
  146. String a="MoMoNgA";
  147. if (a.to_upper() != "MOMONGA")
  148. return false;
  149. if (a.nocasecmp_to("momonga")!=0)
  150. return false;
  151. return true;
  152. }
  153. bool test_13() {
  154. OS::get_singleton()->print("\n\nTest 13: UTF8\n");
  155. /* how can i embed UTF in here? */
  156. static const CharType ustr[] = { 0x304A , 0x360F, 0x3088, 0x3046, 0 };
  157. // static const wchar_t ustr[] = { 'P', 0xCE, 'p',0xD3, 0 };
  158. String s=ustr;
  159. OS::get_singleton()->print("\tUnicode: %ls\n",ustr);
  160. s.parse_utf8( s.utf8().get_data() );
  161. OS::get_singleton()->print("\tConvert/Parse UTF8: %ls\n",s.c_str());
  162. return (s==ustr);
  163. }
  164. bool test_14() {
  165. OS::get_singleton()->print("\n\nTest 14: ASCII\n");
  166. String s = L"Primero Leche";
  167. OS::get_singleton()->print("\tAscii: %s\n",s.ascii().get_data());
  168. String t=s.ascii().get_data();
  169. return (s==t);
  170. }
  171. bool test_15() {
  172. OS::get_singleton()->print("\n\nTest 15: substr\n");
  173. String s="Killer Baby";
  174. OS::get_singleton()->print("\tsubstr(3,4) of \"%ls\" is \"%ls\"\n",s.c_str(),s.substr(3,4).c_str());
  175. return (s.substr(3,4)=="ler ");
  176. }
  177. bool test_16() {
  178. OS::get_singleton()->print("\n\nTest 16: find\n");
  179. String s="Pretty Woman";
  180. OS::get_singleton()->print("\tString: %ls\n",s.c_str());
  181. OS::get_singleton()->print("\t\"tty\" is at %i pos.\n",s.find("tty"));
  182. OS::get_singleton()->print("\t\"Revenge of the Monster Truck\" is at %i pos.\n",s.find("Revenge of the Monster Truck"));
  183. if (s.find("tty")!=3)
  184. return false;
  185. if (s.find("Revenge of the Monster Truck")!=-1)
  186. return false;
  187. return true;
  188. }
  189. bool test_17() {
  190. OS::get_singleton()->print("\n\nTest 17: find no case\n");
  191. String s="Pretty Whale";
  192. OS::get_singleton()->print("\tString: %ls\n",s.c_str());
  193. OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n",s.findn("WHA"));
  194. OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n",s.findn("Revenge of the Monster Truck"));
  195. if (s.findn("WHA")!=7)
  196. return false;
  197. if (s.findn("Revenge of the Monster SawFish")!=-1)
  198. return false;
  199. return true;
  200. }
  201. bool test_18() {
  202. OS::get_singleton()->print("\n\nTest 18: find no case\n");
  203. String s="Pretty Whale";
  204. OS::get_singleton()->print("\tString: %ls\n",s.c_str());
  205. OS::get_singleton()->print("\t\"WHA\" is at %i pos.\n",s.findn("WHA"));
  206. OS::get_singleton()->print("\t\"Revenge of the Monster SawFish\" is at %i pos.\n",s.findn("Revenge of the Monster Truck"));
  207. if (s.findn("WHA")!=7)
  208. return false;
  209. if (s.findn("Revenge of the Monster SawFish")!=-1)
  210. return false;
  211. return true;
  212. }
  213. bool test_19() {
  214. OS::get_singleton()->print("\n\nTest 19: Search & replace\n");
  215. String s="Happy Birthday, Anna!";
  216. OS::get_singleton()->print("\tString: %ls\n",s.c_str());
  217. s=s.replace("Birthday","Halloween");
  218. OS::get_singleton()->print("\tReplaced Birthday/Halloween: %ls.\n",s.c_str());
  219. return (s=="Happy Halloween, Anna!");
  220. }
  221. bool test_20() {
  222. OS::get_singleton()->print("\n\nTest 20: Insertion\n");
  223. String s="Who is Frederic?";
  224. OS::get_singleton()->print("\tString: %ls\n",s.c_str());
  225. s=s.insert( s.find("?")," Chopin" );
  226. OS::get_singleton()->print("\tInserted Chopin: %ls.\n",s.c_str());
  227. return (s=="Who is Frederic Chopin?");
  228. }
  229. bool test_21() {
  230. OS::get_singleton()->print("\n\nTest 21: Number -> String\n");
  231. OS::get_singleton()->print("\tPi is %f\n",33.141593);
  232. OS::get_singleton()->print("\tPi String is %ls\n",String::num(3.141593).c_str());
  233. return String::num(3.141593)=="3.141593";
  234. }
  235. bool test_22() {
  236. OS::get_singleton()->print("\n\nTest 22: String -> Int\n");
  237. static const char* nums[4]={ "1237461283", "- 22", "0", " - 1123412" };
  238. static const int num[4]={ 1237461283, -22, 0, -1123412 };
  239. for (int i=0;i<4;i++) {
  240. OS::get_singleton()->print("\tString: \"%s\" as Int is %i\n",nums[i],String(nums[i]).to_int());
  241. if (String(nums[i]).to_int()!=num[i])
  242. return false;
  243. }
  244. return true;
  245. }
  246. bool test_23() {
  247. OS::get_singleton()->print("\n\nTest 23: String -> Float\n");
  248. static const char* nums[4]={ "-12348298412.2", "0.05", "2.0002", " -0.0001" };
  249. static const double num[4]={ -12348298412.2, 0.05, 2.0002, -0.0001 };
  250. for (int i=0;i<4;i++) {
  251. OS::get_singleton()->print("\tString: \"%s\" as Float is %f\n",nums[i],String(nums[i]).to_double());
  252. if ( ABS(String(nums[i]).to_double()-num[i])>0.00001)
  253. return false;
  254. }
  255. return true;
  256. }
  257. bool test_24() {
  258. OS::get_singleton()->print("\n\nTest 24: Slicing\n");
  259. String s="Mars,Jupiter,Saturn,Uranus";
  260. const char*slices[4]={"Mars","Jupiter","Saturn","Uranus"};
  261. OS::get_singleton()->print("\tSlicing \"%ls\" by \"%s\"..\n",s.c_str(),",");
  262. for (int i=0;i<s.get_slice_count(",");i++) {
  263. OS::get_singleton()->print("\t\t%i- %ls\n",i+1,s.get_slice(",",i).c_str());
  264. if (s.get_slice(",",i)!=slices[i])
  265. return false;
  266. }
  267. return true;
  268. }
  269. bool test_25() {
  270. OS::get_singleton()->print("\n\nTest 25: Erasing\n");
  271. String s="Josephine is such a cute girl!";
  272. OS::get_singleton()->print("\tString: %ls\n",s.c_str());
  273. OS::get_singleton()->print("\tRemoving \"cute\"\n");
  274. s.erase(s.find("cute "),String("cute ").length());
  275. OS::get_singleton()->print("\tResult: %ls\n",s.c_str());
  276. return (s=="Josephine is such a girl!");
  277. }
  278. bool test_26() {
  279. OS::get_singleton()->print("\n\nTest 26: RegEx\n");
  280. RegEx regexp("(.*):(.*)");
  281. List<String> captures;
  282. bool match = regexp.match("name:password", &captures);
  283. printf("\tmatch: %s\n", match?"true":"false");
  284. printf("\t%i captures:\n", captures.size());
  285. List<String>::Element *I = captures.front();
  286. while (I) {
  287. printf("%ls\n", I->get().c_str());
  288. I = I->next();
  289. };
  290. return captures.size();
  291. };
  292. struct test_27_data {
  293. char const * data;
  294. char const * begin;
  295. bool expected;
  296. };
  297. bool test_27() {
  298. OS::get_singleton()->print("\n\nTest 27: begins_with\n");
  299. test_27_data tc[] = {
  300. {"res://foobar", "res://", true},
  301. {"res", "res://", false},
  302. {"abc", "abc", true}
  303. };
  304. size_t count = sizeof(tc) / sizeof(tc[0]);
  305. bool state = true;
  306. for (size_t i = 0;state && i < count; ++i) {
  307. String s = tc[i].data;
  308. state = s.begins_with(tc[i].begin) == tc[i].expected;
  309. if (state) {
  310. String sb = tc[i].begin;
  311. state = s.begins_with(sb) == tc[i].expected;
  312. }
  313. if (!state) {
  314. OS::get_singleton()->print("\n\t Failure on:\n\t\tstring: ", tc[i].data, "\n\t\tbegin: ", tc[i].begin, "\n\t\texpected: ", tc[i].expected ? "true" : "false", "\n");
  315. break;
  316. }
  317. };
  318. return state;
  319. };
  320. bool test_28() {
  321. OS::get_singleton()->print("\n\nTest 28: sprintf\n");
  322. bool success, state = true;
  323. char output_format[] = "\tTest:\t%ls => %ls (%s)\n";
  324. String format, output;
  325. Array args;
  326. bool error;
  327. // %%
  328. format = "fish %% frog";
  329. args.clear();
  330. output = format.sprintf(args, &error);
  331. success = (output == String("fish % frog") && !error);
  332. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  333. if (!success) state = false;
  334. //////// INTS
  335. // Int
  336. format = "fish %d frog";
  337. args.clear();
  338. args.push_back(5);
  339. output = format.sprintf(args, &error);
  340. success = (output == String("fish 5 frog") && !error);
  341. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  342. if (!success) state = false;
  343. // Int left padded with zeroes.
  344. format = "fish %05d frog";
  345. args.clear();
  346. args.push_back(5);
  347. output = format.sprintf(args, &error);
  348. success = (output == String("fish 00005 frog") && !error);
  349. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  350. if (!success) state = false;
  351. // Int left padded with spaces.
  352. format = "fish %5d frog";
  353. args.clear();
  354. args.push_back(5);
  355. output = format.sprintf(args, &error);
  356. success = (output == String("fish 5 frog") && !error);
  357. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  358. if (!success) state = false;
  359. // Int right padded with spaces.
  360. format = "fish %-5d frog";
  361. args.clear();
  362. args.push_back(5);
  363. output = format.sprintf(args, &error);
  364. success = (output == String("fish 5 frog") && !error);
  365. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  366. if (!success) state = false;
  367. // Int with sign (positive).
  368. format = "fish %+d frog";
  369. args.clear();
  370. args.push_back(5);
  371. output = format.sprintf(args, &error);
  372. success = (output == String("fish +5 frog") && !error);
  373. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  374. if (!success) state = false;
  375. // Negative int.
  376. format = "fish %d frog";
  377. args.clear();
  378. args.push_back(-5);
  379. output = format.sprintf(args, &error);
  380. success = (output == String("fish -5 frog") && !error);
  381. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  382. if (!success) state = false;
  383. // Hex (lower)
  384. format = "fish %x frog";
  385. args.clear();
  386. args.push_back(45);
  387. output = format.sprintf(args, &error);
  388. success = (output == String("fish 2d frog") && !error);
  389. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  390. if (!success) state = false;
  391. // Hex (upper)
  392. format = "fish %X frog";
  393. args.clear();
  394. args.push_back(45);
  395. output = format.sprintf(args, &error);
  396. success = (output == String("fish 2D frog") && !error);
  397. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  398. if (!success) state = false;
  399. // Octal
  400. format = "fish %o frog";
  401. args.clear();
  402. args.push_back(99);
  403. output = format.sprintf(args, &error);
  404. success = (output == String("fish 143 frog") && !error);
  405. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  406. if (!success) state = false;
  407. ////// REALS
  408. // Real
  409. format = "fish %f frog";
  410. args.clear();
  411. args.push_back(99.99);
  412. output = format.sprintf(args, &error);
  413. success = (output == String("fish 99.990000 frog") && !error);
  414. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  415. if (!success) state = false;
  416. // Real left-padded
  417. format = "fish %11f frog";
  418. args.clear();
  419. args.push_back(99.99);
  420. output = format.sprintf(args, &error);
  421. success = (output == String("fish 99.990000 frog") && !error);
  422. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  423. if (!success) state = false;
  424. // Real right-padded
  425. format = "fish %-11f frog";
  426. args.clear();
  427. args.push_back(99.99);
  428. output = format.sprintf(args, &error);
  429. success = (output == String("fish 99.990000 frog") && !error);
  430. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  431. if (!success) state = false;
  432. // Real given int.
  433. format = "fish %f frog";
  434. args.clear();
  435. args.push_back(99);
  436. output = format.sprintf(args, &error);
  437. success = (output == String("fish 99.000000 frog") && !error);
  438. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  439. if (!success) state = false;
  440. // Real with sign (positive).
  441. format = "fish %+f frog";
  442. args.clear();
  443. args.push_back(99.99);
  444. output = format.sprintf(args, &error);
  445. success = (output == String("fish +99.990000 frog") && !error);
  446. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  447. if (!success) state = false;
  448. // Real with 1 decimals.
  449. format = "fish %.1f frog";
  450. args.clear();
  451. args.push_back(99.99);
  452. output = format.sprintf(args, &error);
  453. success = (output == String("fish 100.0 frog") && !error);
  454. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  455. if (!success) state = false;
  456. // Real with 12 decimals.
  457. format = "fish %.12f frog";
  458. args.clear();
  459. args.push_back(99.99);
  460. output = format.sprintf(args, &error);
  461. success = (output == String("fish 99.990000000000 frog") && !error);
  462. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  463. if (!success) state = false;
  464. // Real with no decimals.
  465. format = "fish %.f frog";
  466. args.clear();
  467. args.push_back(99.99);
  468. output = format.sprintf(args, &error);
  469. success = (output == String("fish 100 frog") && !error);
  470. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  471. if (!success) state = false;
  472. /////// Strings.
  473. // String
  474. format = "fish %s frog";
  475. args.clear();
  476. args.push_back("cheese");
  477. output = format.sprintf(args, &error);
  478. success = (output == String("fish cheese frog") && !error);
  479. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  480. if (!success) state = false;
  481. // String left-padded
  482. format = "fish %10s frog";
  483. args.clear();
  484. args.push_back("cheese");
  485. output = format.sprintf(args, &error);
  486. success = (output == String("fish cheese frog") && !error);
  487. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  488. if (!success) state = false;
  489. // String right-padded
  490. format = "fish %-10s frog";
  491. args.clear();
  492. args.push_back("cheese");
  493. output = format.sprintf(args, &error);
  494. success = (output == String("fish cheese frog") && !error);
  495. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  496. if (!success) state = false;
  497. ///// Characters
  498. // Character as string.
  499. format = "fish %c frog";
  500. args.clear();
  501. args.push_back("A");
  502. output = format.sprintf(args, &error);
  503. success = (output == String("fish A frog") && !error);
  504. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  505. if (!success) state = false;
  506. // Character as int.
  507. format = "fish %c frog";
  508. args.clear();
  509. args.push_back(65);
  510. output = format.sprintf(args, &error);
  511. success = (output == String("fish A frog") && !error);
  512. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  513. if (!success) state = false;
  514. ///// Dynamic width
  515. // String dynamic width
  516. format = "fish %*s frog";
  517. args.clear();
  518. args.push_back(10);
  519. args.push_back("cheese");
  520. output = format.sprintf(args, &error);
  521. success = (output == String("fish cheese frog") && !error);
  522. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  523. if (!success) state = false;
  524. // Int dynamic width
  525. format = "fish %*d frog";
  526. args.clear();
  527. args.push_back(10);
  528. args.push_back(99);
  529. output = format.sprintf(args, &error);
  530. success = (output == String("fish 99 frog") && !error);
  531. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  532. if (!success) state = false;
  533. // Float dynamic width
  534. format = "fish %*.*f frog";
  535. args.clear();
  536. args.push_back(10);
  537. args.push_back(3);
  538. args.push_back(99.99);
  539. output = format.sprintf(args, &error);
  540. success = (output == String("fish 99.990 frog") && !error);
  541. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  542. if (!success) state = false;
  543. ///// Errors
  544. // More formats than arguments.
  545. format = "fish %s %s frog";
  546. args.clear();
  547. args.push_back("cheese");
  548. output = format.sprintf(args, &error);
  549. success = (output == "not enough arguments for format string" && error);
  550. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  551. if (!success) state = false;
  552. // More arguments than formats.
  553. format = "fish %s frog";
  554. args.clear();
  555. args.push_back("hello");
  556. args.push_back("cheese");
  557. output = format.sprintf(args, &error);
  558. success = (output == "not all arguments converted during string formatting" && error);
  559. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  560. if (!success) state = false;
  561. // Incomplete format.
  562. format = "fish %10";
  563. args.clear();
  564. args.push_back("cheese");
  565. output = format.sprintf(args, &error);
  566. success = (output == "incomplete format" && error);
  567. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  568. if (!success) state = false;
  569. // Bad character in format string
  570. format = "fish %&f frog";
  571. args.clear();
  572. args.push_back("cheese");
  573. output = format.sprintf(args, &error);
  574. success = (output == "unsupported format character" && error);
  575. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  576. if (!success) state = false;
  577. // Too many decimals.
  578. format = "fish %2.2.2f frog";
  579. args.clear();
  580. args.push_back(99.99);
  581. output = format.sprintf(args, &error);
  582. success = (output == "too many decimal points in format" && error);
  583. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  584. if (!success) state = false;
  585. // * not a number
  586. format = "fish %*f frog";
  587. args.clear();
  588. args.push_back("cheese");
  589. args.push_back(99.99);
  590. output = format.sprintf(args, &error);
  591. success = (output == "* wants number" && error);
  592. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  593. if (!success) state = false;
  594. // Character too long.
  595. format = "fish %c frog";
  596. args.clear();
  597. args.push_back("sc");
  598. output = format.sprintf(args, &error);
  599. success = (output == "%c requires number or single-character string" && error);
  600. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  601. if (!success) state = false;
  602. // Character bad type.
  603. format = "fish %c frog";
  604. args.clear();
  605. args.push_back(Array());
  606. output = format.sprintf(args, &error);
  607. success = (output == "%c requires number or single-character string" && error);
  608. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  609. if (!success) state = false;
  610. return state;
  611. }
  612. typedef bool (*TestFunc)(void);
  613. TestFunc test_funcs[] = {
  614. test_1,
  615. test_2,
  616. test_3,
  617. test_4,
  618. test_5,
  619. test_6,
  620. test_7,
  621. test_8,
  622. test_9,
  623. test_10,
  624. test_11,
  625. test_12,
  626. test_13,
  627. test_14,
  628. test_15,
  629. test_16,
  630. test_17,
  631. test_18,
  632. test_19,
  633. test_20,
  634. test_21,
  635. test_22,
  636. test_23,
  637. test_24,
  638. test_25,
  639. test_26,
  640. test_27,
  641. test_28,
  642. 0
  643. };
  644. MainLoop* test() {
  645. /** A character length != wchar_t may be forced, so the tests wont work */
  646. ERR_FAIL_COND_V( sizeof(CharType) != sizeof(wchar_t), NULL );
  647. int count=0;
  648. int passed=0;
  649. while(true) {
  650. if (!test_funcs[count])
  651. break;
  652. bool pass=test_funcs[count]();
  653. if (pass)
  654. passed++;
  655. OS::get_singleton()->print("\t%s\n",pass?"PASS":"FAILED");
  656. count++;
  657. }
  658. OS::get_singleton()->print("\n\n\n");
  659. OS::get_singleton()->print("*************\n");
  660. OS::get_singleton()->print("***TOTALS!***\n");
  661. OS::get_singleton()->print("*************\n");
  662. OS::get_singleton()->print("Passed %i of %i tests\n",count,passed);
  663. return NULL;
  664. }
  665. }