test_string.cpp 26 KB

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