test_string.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 "core/io/ip_address.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. //TODO: Do replacement RegEx test
  280. return true;
  281. };
  282. struct test_27_data {
  283. char const * data;
  284. char const * begin;
  285. bool expected;
  286. };
  287. bool test_27() {
  288. OS::get_singleton()->print("\n\nTest 27: begins_with\n");
  289. test_27_data tc[] = {
  290. {"res://foobar", "res://", true},
  291. {"res", "res://", false},
  292. {"abc", "abc", true}
  293. };
  294. size_t count = sizeof(tc) / sizeof(tc[0]);
  295. bool state = true;
  296. for (size_t i = 0;state && i < count; ++i) {
  297. String s = tc[i].data;
  298. state = s.begins_with(tc[i].begin) == tc[i].expected;
  299. if (state) {
  300. String sb = tc[i].begin;
  301. state = s.begins_with(sb) == tc[i].expected;
  302. }
  303. if (!state) {
  304. 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");
  305. break;
  306. }
  307. };
  308. return state;
  309. };
  310. bool test_28() {
  311. OS::get_singleton()->print("\n\nTest 28: sprintf\n");
  312. bool success, state = true;
  313. char output_format[] = "\tTest:\t%ls => %ls (%s)\n";
  314. String format, output;
  315. Array args;
  316. bool error;
  317. // %%
  318. format = "fish %% frog";
  319. args.clear();
  320. output = format.sprintf(args, &error);
  321. success = (output == String("fish % frog") && !error);
  322. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  323. if (!success) state = false;
  324. //////// INTS
  325. // Int
  326. format = "fish %d frog";
  327. args.clear();
  328. args.push_back(5);
  329. output = format.sprintf(args, &error);
  330. success = (output == String("fish 5 frog") && !error);
  331. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  332. if (!success) state = false;
  333. // Int left padded with zeroes.
  334. format = "fish %05d frog";
  335. args.clear();
  336. args.push_back(5);
  337. output = format.sprintf(args, &error);
  338. success = (output == String("fish 00005 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 spaces.
  342. format = "fish %5d frog";
  343. args.clear();
  344. args.push_back(5);
  345. output = format.sprintf(args, &error);
  346. success = (output == String("fish 5 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 right 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 with sign (positive).
  358. format = "fish %+d 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. // Negative int.
  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. // Hex (lower)
  374. format = "fish %x frog";
  375. args.clear();
  376. args.push_back(45);
  377. output = format.sprintf(args, &error);
  378. success = (output == String("fish 2d 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 (upper)
  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. // Octal
  390. format = "fish %o frog";
  391. args.clear();
  392. args.push_back(99);
  393. output = format.sprintf(args, &error);
  394. success = (output == String("fish 143 frog") && !error);
  395. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  396. if (!success) state = false;
  397. ////// REALS
  398. // Real
  399. format = "fish %f frog";
  400. args.clear();
  401. args.push_back(99.99);
  402. output = format.sprintf(args, &error);
  403. success = (output == String("fish 99.990000 frog") && !error);
  404. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  405. if (!success) state = false;
  406. // Real left-padded
  407. format = "fish %11f 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 right-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 given int.
  423. format = "fish %f frog";
  424. args.clear();
  425. args.push_back(99);
  426. output = format.sprintf(args, &error);
  427. success = (output == String("fish 99.000000 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 with sign (positive).
  431. format = "fish %+f frog";
  432. args.clear();
  433. args.push_back(99.99);
  434. output = format.sprintf(args, &error);
  435. success = (output == String("fish +99.990000 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 1 decimals.
  439. format = "fish %.1f frog";
  440. args.clear();
  441. args.push_back(99.99);
  442. output = format.sprintf(args, &error);
  443. success = (output == String("fish 100.0 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 12 decimals.
  447. format = "fish %.12f frog";
  448. args.clear();
  449. args.push_back(99.99);
  450. output = format.sprintf(args, &error);
  451. success = (output == String("fish 99.990000000000 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 no decimals.
  455. format = "fish %.f frog";
  456. args.clear();
  457. args.push_back(99.99);
  458. output = format.sprintf(args, &error);
  459. success = (output == String("fish 100 frog") && !error);
  460. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  461. if (!success) state = false;
  462. /////// Strings.
  463. // String
  464. format = "fish %s frog";
  465. args.clear();
  466. args.push_back("cheese");
  467. output = format.sprintf(args, &error);
  468. success = (output == String("fish cheese frog") && !error);
  469. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  470. if (!success) state = false;
  471. // String left-padded
  472. format = "fish %10s 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 right-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. ///// Characters
  488. // Character as string.
  489. format = "fish %c frog";
  490. args.clear();
  491. args.push_back("A");
  492. output = format.sprintf(args, &error);
  493. success = (output == String("fish A frog") && !error);
  494. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  495. if (!success) state = false;
  496. // Character as int.
  497. format = "fish %c frog";
  498. args.clear();
  499. args.push_back(65);
  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. ///// Dynamic width
  505. // String dynamic width
  506. format = "fish %*s frog";
  507. args.clear();
  508. args.push_back(10);
  509. args.push_back("cheese");
  510. output = format.sprintf(args, &error);
  511. success = (output == String("fish cheese frog") && !error);
  512. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  513. if (!success) state = false;
  514. // Int dynamic width
  515. format = "fish %*d frog";
  516. args.clear();
  517. args.push_back(10);
  518. args.push_back(99);
  519. output = format.sprintf(args, &error);
  520. success = (output == String("fish 99 frog") && !error);
  521. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  522. if (!success) state = false;
  523. // Float dynamic width
  524. format = "fish %*.*f frog";
  525. args.clear();
  526. args.push_back(10);
  527. args.push_back(3);
  528. args.push_back(99.99);
  529. output = format.sprintf(args, &error);
  530. success = (output == String("fish 99.990 frog") && !error);
  531. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  532. if (!success) state = false;
  533. ///// Errors
  534. // More formats than arguments.
  535. format = "fish %s %s frog";
  536. args.clear();
  537. args.push_back("cheese");
  538. output = format.sprintf(args, &error);
  539. success = (output == "not enough arguments for format string" && error);
  540. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  541. if (!success) state = false;
  542. // More arguments than formats.
  543. format = "fish %s frog";
  544. args.clear();
  545. args.push_back("hello");
  546. args.push_back("cheese");
  547. output = format.sprintf(args, &error);
  548. success = (output == "not all arguments converted during string formatting" && error);
  549. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  550. if (!success) state = false;
  551. // Incomplete format.
  552. format = "fish %10";
  553. args.clear();
  554. args.push_back("cheese");
  555. output = format.sprintf(args, &error);
  556. success = (output == "incomplete format" && error);
  557. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  558. if (!success) state = false;
  559. // Bad character in format string
  560. format = "fish %&f frog";
  561. args.clear();
  562. args.push_back("cheese");
  563. output = format.sprintf(args, &error);
  564. success = (output == "unsupported format character" && error);
  565. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  566. if (!success) state = false;
  567. // Too many decimals.
  568. format = "fish %2.2.2f frog";
  569. args.clear();
  570. args.push_back(99.99);
  571. output = format.sprintf(args, &error);
  572. success = (output == "too many decimal points in format" && error);
  573. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  574. if (!success) state = false;
  575. // * not a number
  576. format = "fish %*f frog";
  577. args.clear();
  578. args.push_back("cheese");
  579. args.push_back(99.99);
  580. output = format.sprintf(args, &error);
  581. success = (output == "* wants number" && error);
  582. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  583. if (!success) state = false;
  584. // Character too long.
  585. format = "fish %c frog";
  586. args.clear();
  587. args.push_back("sc");
  588. output = format.sprintf(args, &error);
  589. success = (output == "%c requires number or single-character string" && error);
  590. OS::get_singleton()->print(output_format, format.c_str(), output.c_str(), success ? "OK" : "FAIL");
  591. if (!success) state = false;
  592. // Character bad type.
  593. format = "fish %c frog";
  594. args.clear();
  595. args.push_back(Array());
  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. return state;
  601. }
  602. bool test_29() {
  603. bool error = false;
  604. bool state = true;
  605. bool success = false;
  606. IP_Address ip0("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
  607. OS::get_singleton()->print("ip0 is %ls\n", String(ip0).c_str());
  608. IP_Address ip(0x0123, 0x4567, 0x89ab, 0xcdef, true);
  609. OS::get_singleton()->print("ip6 is %ls\n", String(ip).c_str());
  610. IP_Address ip2("fe80::52e5:49ff:fe93:1baf");
  611. OS::get_singleton()->print("ip6 is %ls\n", String(ip2).c_str());
  612. IP_Address ip3("::ffff:192.168.0.1");
  613. OS::get_singleton()->print("ip6 is %ls\n", String(ip3).c_str());
  614. String ip4 = "192.168.0.1";
  615. success = ip4.is_valid_ip_address();
  616. OS::get_singleton()->print("Is valid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL");
  617. if (!success) state = false;
  618. ip4 = "192.368.0.1";
  619. success = (!ip4.is_valid_ip_address());
  620. OS::get_singleton()->print("Is invalid ipv4: %ls, %s\n", ip4.c_str(), success ? "OK" : "FAIL");
  621. if (!success) state = false;
  622. String ip6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
  623. success = ip6.is_valid_ip_address();
  624. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  625. if (!success) state = false;
  626. ip6 = "2001:0db8:85j3:0000:0000:8a2e:0370:7334";
  627. success = (!ip6.is_valid_ip_address());
  628. OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  629. if (!success) state = false;
  630. ip6 = "2001:0db8:85f345:0000:0000:8a2e:0370:7334";
  631. success = (!ip6.is_valid_ip_address());
  632. OS::get_singleton()->print("Is invalid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  633. if (!success) state = false;
  634. ip6 = "2001:0db8::0:8a2e:370:7334";
  635. success = (ip6.is_valid_ip_address());
  636. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  637. if (!success) state = false;
  638. ip6 = "::ffff:192.168.0.1";
  639. success = (ip6.is_valid_ip_address());
  640. OS::get_singleton()->print("Is valid ipv6: %ls, %s\n", ip6.c_str(), success ? "OK" : "FAIL");
  641. if (!success) state = false;
  642. return state;
  643. };
  644. typedef bool (*TestFunc)(void);
  645. TestFunc test_funcs[] = {
  646. test_1,
  647. test_2,
  648. test_3,
  649. test_4,
  650. test_5,
  651. test_6,
  652. test_7,
  653. test_8,
  654. test_9,
  655. test_10,
  656. test_11,
  657. test_12,
  658. test_13,
  659. test_14,
  660. test_15,
  661. test_16,
  662. test_17,
  663. test_18,
  664. test_19,
  665. test_20,
  666. test_21,
  667. test_22,
  668. test_23,
  669. test_24,
  670. test_25,
  671. test_26,
  672. test_27,
  673. test_28,
  674. test_29,
  675. 0
  676. };
  677. MainLoop* test() {
  678. /** A character length != wchar_t may be forced, so the tests wont work */
  679. ERR_FAIL_COND_V( sizeof(CharType) != sizeof(wchar_t), NULL );
  680. int count=0;
  681. int passed=0;
  682. while(true) {
  683. if (!test_funcs[count])
  684. break;
  685. bool pass=test_funcs[count]();
  686. if (pass)
  687. passed++;
  688. OS::get_singleton()->print("\t%s\n",pass?"PASS":"FAILED");
  689. count++;
  690. }
  691. OS::get_singleton()->print("\n\n\n");
  692. OS::get_singleton()->print("*************\n");
  693. OS::get_singleton()->print("***TOTALS!***\n");
  694. OS::get_singleton()->print("*************\n");
  695. OS::get_singleton()->print("Passed %i of %i tests\n", passed, count);
  696. return NULL;
  697. }
  698. }