test_string.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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-2016 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/nrex/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. int res = regexp.find("name:password");
  282. printf("\tmatch: %s\n", (res>=0)?"true":"false");
  283. printf("\t%i captures:\n", regexp.get_capture_count());
  284. for (int i = 0; i<regexp.get_capture_count(); i++)
  285. {
  286. printf("%ls\n", regexp.get_capture(i).c_str());
  287. }
  288. return res;
  289. };
  290. struct test_27_data {
  291. char const * data;
  292. char const * begin;
  293. bool expected;
  294. };
  295. bool test_27() {
  296. OS::get_singleton()->print("\n\nTest 27: begins_with\n");
  297. test_27_data tc[] = {
  298. {"res://foobar", "res://", true},
  299. {"res", "res://", false},
  300. {"abc", "abc", true}
  301. };
  302. size_t count = sizeof(tc) / sizeof(tc[0]);
  303. bool state = true;
  304. for (size_t i = 0;state && i < count; ++i) {
  305. String s = tc[i].data;
  306. state = s.begins_with(tc[i].begin) == tc[i].expected;
  307. if (state) {
  308. String sb = tc[i].begin;
  309. state = s.begins_with(sb) == tc[i].expected;
  310. }
  311. if (!state) {
  312. 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");
  313. break;
  314. }
  315. };
  316. return state;
  317. };
  318. bool test_28() {
  319. OS::get_singleton()->print("\n\nTest 28: sprintf\n");
  320. bool success, state = true;
  321. char output_format[] = "\tTest:\t%ls => %ls (%s)\n";
  322. String format, output;
  323. Array args;
  324. bool error;
  325. // %%
  326. format = "fish %% frog";
  327. args.clear();
  328. output = format.sprintf(args, &error);
  329. success = (output == String("fish % frog") && !error);
  330. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  331. if (!success) state = false;
  332. //////// INTS
  333. // Int
  334. format = "fish %d frog";
  335. args.clear();
  336. args.push_back(5);
  337. output = format.sprintf(args, &error);
  338. success = (output == String("fish 5 frog") && !error);
  339. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  340. if (!success) state = false;
  341. // Int left padded with zeroes.
  342. format = "fish %05d frog";
  343. args.clear();
  344. args.push_back(5);
  345. output = format.sprintf(args, &error);
  346. success = (output == String("fish 00005 frog") && !error);
  347. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  348. if (!success) state = false;
  349. // Int left padded with spaces.
  350. format = "fish %5d frog";
  351. args.clear();
  352. args.push_back(5);
  353. output = format.sprintf(args, &error);
  354. success = (output == String("fish 5 frog") && !error);
  355. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  356. if (!success) state = false;
  357. // Int right padded with spaces.
  358. format = "fish %-5d frog";
  359. args.clear();
  360. args.push_back(5);
  361. output = format.sprintf(args, &error);
  362. success = (output == String("fish 5 frog") && !error);
  363. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  364. if (!success) state = false;
  365. // Int with sign (positive).
  366. format = "fish %+d frog";
  367. args.clear();
  368. args.push_back(5);
  369. output = format.sprintf(args, &error);
  370. success = (output == String("fish +5 frog") && !error);
  371. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  372. if (!success) state = false;
  373. // Negative int.
  374. format = "fish %d frog";
  375. args.clear();
  376. args.push_back(-5);
  377. output = format.sprintf(args, &error);
  378. success = (output == String("fish -5 frog") && !error);
  379. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  380. if (!success) state = false;
  381. // Hex (lower)
  382. format = "fish %x frog";
  383. args.clear();
  384. args.push_back(45);
  385. output = format.sprintf(args, &error);
  386. success = (output == String("fish 2d frog") && !error);
  387. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  388. if (!success) state = false;
  389. // Hex (upper)
  390. format = "fish %X frog";
  391. args.clear();
  392. args.push_back(45);
  393. output = format.sprintf(args, &error);
  394. success = (output == String("fish 2D frog") && !error);
  395. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  396. if (!success) state = false;
  397. // Octal
  398. format = "fish %o frog";
  399. args.clear();
  400. args.push_back(99);
  401. output = format.sprintf(args, &error);
  402. success = (output == String("fish 143 frog") && !error);
  403. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  404. if (!success) state = false;
  405. ////// REALS
  406. // Real
  407. format = "fish %f frog";
  408. args.clear();
  409. args.push_back(99.99);
  410. output = format.sprintf(args, &error);
  411. success = (output == String("fish 99.990000 frog") && !error);
  412. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  413. if (!success) state = false;
  414. // Real left-padded
  415. format = "fish %11f frog";
  416. args.clear();
  417. args.push_back(99.99);
  418. output = format.sprintf(args, &error);
  419. success = (output == String("fish 99.990000 frog") && !error);
  420. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  421. if (!success) state = false;
  422. // Real right-padded
  423. format = "fish %-11f frog";
  424. args.clear();
  425. args.push_back(99.99);
  426. output = format.sprintf(args, &error);
  427. success = (output == String("fish 99.990000 frog") && !error);
  428. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  429. if (!success) state = false;
  430. // Real given int.
  431. format = "fish %f frog";
  432. args.clear();
  433. args.push_back(99);
  434. output = format.sprintf(args, &error);
  435. success = (output == String("fish 99.000000 frog") && !error);
  436. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  437. if (!success) state = false;
  438. // Real with sign (positive).
  439. format = "fish %+f frog";
  440. args.clear();
  441. args.push_back(99.99);
  442. output = format.sprintf(args, &error);
  443. success = (output == String("fish +99.990000 frog") && !error);
  444. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  445. if (!success) state = false;
  446. // Real with 1 decimals.
  447. format = "fish %.1f frog";
  448. args.clear();
  449. args.push_back(99.99);
  450. output = format.sprintf(args, &error);
  451. success = (output == String("fish 100.0 frog") && !error);
  452. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  453. if (!success) state = false;
  454. // Real with 12 decimals.
  455. format = "fish %.12f frog";
  456. args.clear();
  457. args.push_back(99.99);
  458. output = format.sprintf(args, &error);
  459. success = (output == String("fish 99.990000000000 frog") && !error);
  460. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  461. if (!success) state = false;
  462. // Real with no decimals.
  463. format = "fish %.f frog";
  464. args.clear();
  465. args.push_back(99.99);
  466. output = format.sprintf(args, &error);
  467. success = (output == String("fish 100 frog") && !error);
  468. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  469. if (!success) state = false;
  470. /////// Strings.
  471. // String
  472. format = "fish %s frog";
  473. args.clear();
  474. args.push_back("cheese");
  475. output = format.sprintf(args, &error);
  476. success = (output == String("fish cheese frog") && !error);
  477. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  478. if (!success) state = false;
  479. // String left-padded
  480. format = "fish %10s frog";
  481. args.clear();
  482. args.push_back("cheese");
  483. output = format.sprintf(args, &error);
  484. success = (output == String("fish cheese frog") && !error);
  485. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  486. if (!success) state = false;
  487. // String right-padded
  488. format = "fish %-10s frog";
  489. args.clear();
  490. args.push_back("cheese");
  491. output = format.sprintf(args, &error);
  492. success = (output == String("fish cheese frog") && !error);
  493. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  494. if (!success) state = false;
  495. ///// Characters
  496. // Character as string.
  497. format = "fish %c frog";
  498. args.clear();
  499. args.push_back("A");
  500. output = format.sprintf(args, &error);
  501. success = (output == String("fish A frog") && !error);
  502. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  503. if (!success) state = false;
  504. // Character as int.
  505. format = "fish %c frog";
  506. args.clear();
  507. args.push_back(65);
  508. output = format.sprintf(args, &error);
  509. success = (output == String("fish A frog") && !error);
  510. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  511. if (!success) state = false;
  512. ///// Dynamic width
  513. // String dynamic width
  514. format = "fish %*s frog";
  515. args.clear();
  516. args.push_back(10);
  517. args.push_back("cheese");
  518. output = format.sprintf(args, &error);
  519. success = (output == String("fish cheese frog") && !error);
  520. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  521. if (!success) state = false;
  522. // Int dynamic width
  523. format = "fish %*d frog";
  524. args.clear();
  525. args.push_back(10);
  526. args.push_back(99);
  527. output = format.sprintf(args, &error);
  528. success = (output == String("fish 99 frog") && !error);
  529. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  530. if (!success) state = false;
  531. // Float dynamic width
  532. format = "fish %*.*f frog";
  533. args.clear();
  534. args.push_back(10);
  535. args.push_back(3);
  536. args.push_back(99.99);
  537. output = format.sprintf(args, &error);
  538. success = (output == String("fish 99.990 frog") && !error);
  539. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  540. if (!success) state = false;
  541. ///// Errors
  542. // More formats than arguments.
  543. format = "fish %s %s frog";
  544. args.clear();
  545. args.push_back("cheese");
  546. output = format.sprintf(args, &error);
  547. success = (output == "not enough arguments for format string" && error);
  548. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  549. if (!success) state = false;
  550. // More arguments than formats.
  551. format = "fish %s frog";
  552. args.clear();
  553. args.push_back("hello");
  554. args.push_back("cheese");
  555. output = format.sprintf(args, &error);
  556. success = (output == "not all arguments converted during string formatting" && error);
  557. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  558. if (!success) state = false;
  559. // Incomplete format.
  560. format = "fish %10";
  561. args.clear();
  562. args.push_back("cheese");
  563. output = format.sprintf(args, &error);
  564. success = (output == "incomplete format" && error);
  565. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  566. if (!success) state = false;
  567. // Bad character in format string
  568. format = "fish %&f frog";
  569. args.clear();
  570. args.push_back("cheese");
  571. output = format.sprintf(args, &error);
  572. success = (output == "unsupported format character" && error);
  573. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  574. if (!success) state = false;
  575. // Too many decimals.
  576. format = "fish %2.2.2f frog";
  577. args.clear();
  578. args.push_back(99.99);
  579. output = format.sprintf(args, &error);
  580. success = (output == "too many decimal points in format" && error);
  581. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  582. if (!success) state = false;
  583. // * not a number
  584. format = "fish %*f frog";
  585. args.clear();
  586. args.push_back("cheese");
  587. args.push_back(99.99);
  588. output = format.sprintf(args, &error);
  589. success = (output == "* wants number" && error);
  590. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  591. if (!success) state = false;
  592. // Character too long.
  593. format = "fish %c frog";
  594. args.clear();
  595. args.push_back("sc");
  596. output = format.sprintf(args, &error);
  597. success = (output == "%c requires number or single-character string" && error);
  598. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  599. if (!success) state = false;
  600. // Character bad type.
  601. format = "fish %c frog";
  602. args.clear();
  603. args.push_back(Array());
  604. output = format.sprintf(args, &error);
  605. success = (output == "%c requires number or single-character string" && error);
  606. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  607. if (!success) state = false;
  608. return state;
  609. }
  610. typedef bool (*TestFunc)(void);
  611. TestFunc test_funcs[] = {
  612. test_1,
  613. test_2,
  614. test_3,
  615. test_4,
  616. test_5,
  617. test_6,
  618. test_7,
  619. test_8,
  620. test_9,
  621. test_10,
  622. test_11,
  623. test_12,
  624. test_13,
  625. test_14,
  626. test_15,
  627. test_16,
  628. test_17,
  629. test_18,
  630. test_19,
  631. test_20,
  632. test_21,
  633. test_22,
  634. test_23,
  635. test_24,
  636. test_25,
  637. test_26,
  638. test_27,
  639. test_28,
  640. 0
  641. };
  642. MainLoop* test() {
  643. /** A character length != wchar_t may be forced, so the tests wont work */
  644. ERR_FAIL_COND_V( sizeof(CharType) != sizeof(wchar_t), NULL );
  645. int count=0;
  646. int passed=0;
  647. while(true) {
  648. if (!test_funcs[count])
  649. break;
  650. bool pass=test_funcs[count]();
  651. if (pass)
  652. passed++;
  653. OS::get_singleton()->print("\t%s\n",pass?"PASS":"FAILED");
  654. count++;
  655. }
  656. OS::get_singleton()->print("\n\n\n");
  657. OS::get_singleton()->print("*************\n");
  658. OS::get_singleton()->print("***TOTALS!***\n");
  659. OS::get_singleton()->print("*************\n");
  660. OS::get_singleton()->print("Passed %i of %i tests\n",count,passed);
  661. return NULL;
  662. }
  663. }