TestMisc.hx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. package unit;
  2. import unit.MyClass;
  3. class MyDynamicClass {
  4. var v : Int;
  5. public function new(v) {
  6. this.v = v;
  7. }
  8. public function get() {
  9. return v;
  10. }
  11. public dynamic function add(x,y) {
  12. return v + x + y;
  13. }
  14. public inline function iadd(x,y) {
  15. return v + x + y;
  16. }
  17. static var Z = 10;
  18. public dynamic static function staticDynamic(x,y) {
  19. return Z + x + y;
  20. }
  21. @:isVar public static var W(get, set) : Int = 55;
  22. static function get_W() return W + 2;
  23. static function set_W(v) { W = v; return v; }
  24. }
  25. class MyDynamicSubClass extends MyDynamicClass {
  26. override function add(x,y) {
  27. return (v + x + y) * 2;
  28. }
  29. }
  30. class MyDynamicSubClass2 extends MyDynamicClass {
  31. override dynamic function add(x,y) {
  32. return (v + x + y) * 2;
  33. }
  34. }
  35. class MyOtherDynamicClass extends MyDynamicClass {
  36. public function new(v) {
  37. add = function(x,y) return x + y + 10;
  38. super(v);
  39. }
  40. }
  41. interface IDefArgs {
  42. public function get( x : Int = 5 ) : Int;
  43. }
  44. class BaseDefArgs {
  45. public function get( x = 3 ) {
  46. return x;
  47. }
  48. }
  49. class ExtDefArgs extends BaseDefArgs implements IDefArgs {
  50. public function new() {
  51. }
  52. override function get( x = 7 ) {
  53. return x;
  54. }
  55. }
  56. class BaseConstrOpt {
  57. public var s : String;
  58. public var i : Int;
  59. public var b : Bool;
  60. public function new( s = "test", i = -5, b = true ) {
  61. this.s = s;
  62. this.i = i;
  63. this.b = b;
  64. }
  65. }
  66. class SubConstrOpt extends BaseConstrOpt {
  67. public function new() {
  68. super();
  69. }
  70. }
  71. class SubConstrOpt2 extends BaseConstrOpt {
  72. // default inherited constructor
  73. }
  74. class SubConstrOpt3 extends BaseConstrOpt {
  75. public function new( s = "test2", i = -6 ) {
  76. super(s,i);
  77. }
  78. }
  79. class TestMisc extends Test {
  80. static var unit = "testing package conflict";
  81. function testPackageConflict()
  82. {
  83. eq( unit, "testing package conflict" );
  84. var unit = unit;
  85. eq( unit, TestMisc.unit );
  86. }
  87. function testDate() {
  88. var d = new Date(2012, 07, 17, 01, 02, 03);
  89. eq( d.getDay(), 5 );
  90. eq( d.getDate(), 17 );
  91. eq( d.getMonth(), 7 );
  92. eq( d.getFullYear(), 2012 );
  93. eq( d.getHours(), 1 );
  94. eq( d.getMinutes(), 2 );
  95. eq( d.getSeconds(), 3 );
  96. //seems to be system-dependent?
  97. //eq( d.getTime(), 1345158123000 );
  98. eq( d.toString(), "2012-08-17 01:02:03" );
  99. }
  100. function testClosure() {
  101. var c = new MyClass(100);
  102. var add = c.add;
  103. eq( c.add(1,2), 103 );
  104. eq( c.add.bind(1)(2), 103 );
  105. eq( add(1,2), 103 );
  106. var x = 4;
  107. var f = function() return x;
  108. eq( f(), 4 );
  109. x++;
  110. eq( f(), 5 );
  111. var o = { f : f };
  112. eq( o.f(), 5 );
  113. eq( o.f, o.f ); // we shouldn't create a new closure here
  114. var o = { add : c.add };
  115. eq( o.add(1,2), 103 );
  116. eq( o.add, o.add ); // we shouldn't create a new closure here
  117. var o = { cos : Math.cos };
  118. eq( o.cos(0), 1. );
  119. // check enum
  120. var c = MyEnum.C;
  121. t( Type.enumEq(MyEnum.C(1,"hello"), c(1,"hello")) );
  122. }
  123. // make sure that captured variables does not overlap each others even if in different scopes
  124. function testCaptureUnique() {
  125. var foo = null, bar = null;
  126. var flag = true;
  127. if( flag ) {
  128. var x = 1;
  129. foo = function() return x;
  130. }
  131. if( flag ) {
  132. var x = 2;
  133. bar = function() return x;
  134. }
  135. eq( foo(), 1);
  136. eq( bar(), 2);
  137. }
  138. function testCaptureUnique2() {
  139. // another more specialized test (was actually the original broken code - but not reproducible when optimization is off)
  140. var foo = id.bind(3);
  141. var bar = sq.bind(5);
  142. eq( foo(), 3 );
  143. eq( bar(), 25 );
  144. }
  145. function testSelfRef() {
  146. // check for self-name binding
  147. var bla = 55;
  148. var bla = function() return bla;
  149. eq( bla(), 55);
  150. }
  151. function testHiddenType() {
  152. var haxe = 20;
  153. eq( std.haxe.crypto.Md5.encode(""), "d41d8cd98f00b204e9800998ecf8427e");
  154. eq( haxe, 20);
  155. var Std = 50;
  156. eq( std.Std.int(45.3), 45);
  157. eq( Std, 50);
  158. }
  159. function testHiddenTypeScope() {
  160. var flag = true;
  161. if( flag ) {
  162. var haxe = 20;
  163. var Std = 50;
  164. eq( haxe, 20);
  165. eq( Std, 50);
  166. }
  167. eq( std.haxe.crypto.Md5.encode(""), "d41d8cd98f00b204e9800998ecf8427e");
  168. eq( std.Std.int(45.3), 45);
  169. }
  170. function testHiddenTypeCapture() {
  171. var flag = true;
  172. var foo = null, bar = null;
  173. if( flag ) {
  174. var haxe = 20;
  175. var Std = 50;
  176. foo = function() return haxe;
  177. bar = function() return Std;
  178. }
  179. eq( std.haxe.crypto.Md5.encode(""), "d41d8cd98f00b204e9800998ecf8427e");
  180. eq( std.Std.int(45.3), 45);
  181. eq( foo(), 20);
  182. eq( bar(), 50);
  183. }
  184. function id(x) {
  185. return x;
  186. }
  187. function sq(x) {
  188. return x * x;
  189. }
  190. function testPropertyInit() {
  191. eq(MyDynamicClass.W, 57);
  192. }
  193. function testInlineClosure() {
  194. var inst = new MyDynamicClass(100);
  195. var add = inst.iadd;
  196. eq( inst.iadd(1,2), 103 );
  197. eq( add(1,2), 103 );
  198. }
  199. function testDynamicClosure() {
  200. var inst = new MyDynamicClass(100);
  201. var add = inst.add;
  202. eq( inst.add(1,2), 103 );
  203. eq( inst.add.bind(1)(2), 103 );
  204. eq( add(1,2), 103 );
  205. // check overriden dynamic method
  206. var inst = new MyDynamicSubClass(100);
  207. var add = inst.add;
  208. eq( inst.add(1,2), 206 );
  209. eq( inst.add.bind(1)(2), 206 );
  210. eq( add(1,2), 206 );
  211. // check overriden dynamic method
  212. var inst = new MyDynamicSubClass2(100);
  213. var add = inst.add;
  214. eq( inst.add(1,2), 206 );
  215. eq( inst.add.bind(1)(2), 206 );
  216. eq( add(1,2), 206 );
  217. // check redefined dynamic method
  218. inst.add = function(x,y) return inst.get() * 2 + x + y;
  219. var add = inst.add;
  220. eq( inst.add(1,2), 203 );
  221. eq( inst.add.bind(1)(2), 203 );
  222. eq( add(1,2), 203 );
  223. // check inherited dynamic method
  224. var inst = new MyOtherDynamicClass(0);
  225. var add = inst.add;
  226. #if (!cs && !java) //see https://groups.google.com/d/msg/haxedev/TUaUykoTpq8/Q4XwcL4UyNUJ
  227. eq( inst.add(1,2), 13 );
  228. eq( inst.add.bind(1)(2), 13 );
  229. eq( add(1,2), 13 );
  230. #end
  231. // check static dynamic
  232. eq( MyDynamicClass.staticDynamic(1,2), 13 );
  233. MyDynamicClass.staticDynamic = function(x,y) return x + y + 100;
  234. eq( MyDynamicClass.staticDynamic(1,2), 103 );
  235. }
  236. function testMD5() {
  237. eq( haxe.crypto.Md5.encode(""), "d41d8cd98f00b204e9800998ecf8427e" );
  238. eq( haxe.crypto.Md5.encode("hello"), "5d41402abc4b2a76b9719d911017c592" );
  239. // depending of ISO/UTF8 native
  240. allow( haxe.crypto.Md5.encode("héllo"), ["1a722f7e6c801d9e470a10cb91ba406d", "be50e8478cf24ff3595bc7307fb91b50"] );
  241. eq( haxe.io.Bytes.ofString("héllo").toHex(), "68c3a96c6c6f");
  242. eq( haxe.crypto.Md5.make(haxe.io.Bytes.ofString("héllo")).toHex(), "be50e8478cf24ff3595bc7307fb91b50" );
  243. }
  244. function testSHA1() {
  245. eq( haxe.crypto.Sha1.encode(""), "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
  246. eq( haxe.crypto.Sha1.encode("hello"), "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d" );
  247. // depending of ISO/UTF8 native
  248. allow( haxe.crypto.Sha1.encode("héllo"), ["028db752c14604d624e8b1c121d600c427b8a3ba","35b5ea45c5e41f78b46a937cc74d41dfea920890"] );
  249. eq( haxe.crypto.Sha1.make(haxe.io.Bytes.ofString("héllo")).toHex(), "35b5ea45c5e41f78b46a937cc74d41dfea920890" );
  250. }
  251. function testBaseCode() {
  252. var b = new haxe.crypto.BaseCode(haxe.io.Bytes.ofString("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"));
  253. eq( b.encodeString("Héllow"), "iceFr6NLtM" );
  254. eq( b.decodeString("iceFr6NLtM"), "Héllow" );
  255. }
  256. function testUrlEncode() {
  257. eq( StringTools.urlEncode("é"), "%C3%A9" );
  258. eq( StringTools.urlDecode("%C3%A9"), "é" );
  259. }
  260. function opt1( ?x : Int, ?y : String ) {
  261. return { x : x, y : y };
  262. }
  263. function opt2( ?x = 5, ?y = "hello" ) {
  264. return { x : x, y : y };
  265. }
  266. function opt3( ?x : Null<Int> = 5, ?y : Null<Float> = 6 ) {
  267. return { x : x, y : y };
  268. }
  269. function opt4( x = 10 ) : Null<Int> {
  270. return x + 1;
  271. }
  272. function testOptionalParams() {
  273. eq( opt1().x, null );
  274. eq( opt1().y, null );
  275. eq( opt1(55).x, 55 );
  276. eq( opt1(55).y, null );
  277. eq( opt1("str").x, null );
  278. eq( opt1("str").y, "str" );
  279. eq( opt1(66,"hello").x, 66 );
  280. eq( opt1(66, "hello").y, "hello" );
  281. eq( opt2().x, 5 );
  282. eq( opt2().y, "hello" );
  283. #if !(flash9 || cpp || cs || java)
  284. eq( opt2(null, null).x, 5 );
  285. #end
  286. eq( opt2(0, null).y, "hello" );
  287. eq( opt3().x, 5 );
  288. eq( opt3().y, 6 );
  289. eq( opt3(9).x, 9 );
  290. eq( opt3(9).y, 6 );
  291. eq( opt3(9,10).x, 9 );
  292. eq( opt3(9,10).y, 10 );
  293. eq( opt3(null,null).x, 5 );
  294. eq( opt3(null,null).y, 6 );
  295. eq( opt3(null).x, 5 );
  296. eq( opt3(null).y, 6 );
  297. eq( opt3(null,7).x, 5 );
  298. eq( opt3(null, 7).y, 7 );
  299. // skipping
  300. eq( opt3(7.4).x, 5 );
  301. eq( opt3(7.4).y, 7.4 );
  302. eq( opt4(), 11 );
  303. #if !(flash9 || cpp || cs || java)
  304. eq( opt4(null), 11 );
  305. #end
  306. var opt4b : ?Int -> Null<Int> = opt4;
  307. eq( opt4b(), 11 );
  308. eq( opt4b(3), 4 );
  309. #if !(flash9 || cpp || cs || java)
  310. eq( opt4b(null), 11 );
  311. #end
  312. // don't compile because we restrict nullability of function param or return type
  313. // var opt4c : ?Null<Int> -> Null<Int> = opt4;
  314. // var opt4c : ?Int -> Int = opt4;
  315. var opt5 = function(a:Int, ?b = 2) return a + b;
  316. eq(3, opt5(1));
  317. eq(3, opt5(1, 2));
  318. eq(3, opt5(1, null));
  319. }
  320. function testIncr() {
  321. var z = 0;
  322. eq( z++, 0 );
  323. eq( z, 1 );
  324. eq( ++z, 2 );
  325. eq( z, 2 );
  326. z++;
  327. eq( z, 3 );
  328. ++z;
  329. eq( z, 4 );
  330. eq( z += 3, 7 );
  331. var x = 0;
  332. var arr = [3];
  333. eq( arr[x++]++, 3 );
  334. eq( x, 1 );
  335. eq( arr[0], 4 );
  336. x = 0;
  337. eq( arr[x++] += 3, 7 );
  338. eq( arr[0], 7 );
  339. var x = 0;
  340. var arr = [{ v : 3 }];
  341. eq( arr[x++].v++, 3 );
  342. eq( x, 1 );
  343. eq( arr[0].v, 4 );
  344. #if !as3
  345. x = 0;
  346. eq( arr[x++].v += 3, 7 );
  347. eq( arr[0].v, 7 );
  348. #end
  349. x = 0;
  350. var arr:Dynamic = [{ v : 3 }];
  351. eq( arr[x++].v++, 3 );
  352. eq( x, 1 );
  353. eq( arr[0].v, 4 );
  354. #if !as3
  355. x = 0;
  356. eq( arr[x++].v += 3, 7 );
  357. eq( arr[0].v, 7 );
  358. #end
  359. }
  360. function testInitOrder() {
  361. var i = 0;
  362. var o = {
  363. y : i++,
  364. x : i++,
  365. z : i++,
  366. blabla : i++,
  367. };
  368. eq(o.y,0);
  369. eq(o.x,1);
  370. eq(o.z,2);
  371. eq(o.blabla,3);
  372. }
  373. static inline function foo(x) return x + 5;
  374. function testInline() {
  375. // check that operations are correctly generated
  376. var x = 3; // prevent optimization
  377. eq( 2 * foo(x), 16 );
  378. eq( -foo(x), -8 );
  379. }
  380. function testEvalAccessOrder() {
  381. var a = [0,0];
  382. var x = 0;
  383. a[x++]++;
  384. eq(a[0],1);
  385. eq(a[1],0);
  386. var x = 0;
  387. var a = new Array();
  388. a[x++] = x++;
  389. eq(a[0],1);
  390. var x = 0;
  391. var foo = function() return x++;
  392. a[foo()] = foo();
  393. eq(a[0],1);
  394. }
  395. static var add = function (x, y) return x + y;
  396. function testStaticVarFun() {
  397. eq( add(2,3), 5);
  398. }
  399. function testDefArgs() {
  400. var e = new ExtDefArgs();
  401. eq( e.get(), 7 );
  402. var b : BaseDefArgs = e;
  403. eq( b.get(), 7 );
  404. var i : IDefArgs = e;
  405. eq( i.get(), 7 );
  406. }
  407. function testStringBuf() {
  408. var b = new StringBuf();
  409. b.add( -45);
  410. b.add(1.456);
  411. b.add(null);
  412. b.add(true);
  413. b.add(false);
  414. b.add("Hello!");
  415. b.addSub("Bla", 1, 2);
  416. b.addChar("R".code);
  417. eq(b.toString(), "-451.456nulltruefalseHello!laR");
  418. }
  419. function testToString():Void
  420. {
  421. var x = { toString : function() return "foo" };
  422. eq( Std.string(x), "foo" );
  423. //var x1:Dynamic = new MyDynamicChildWithToString();
  424. //eq( Std.string(x1), "Custom toString" );
  425. //
  426. //var x2:Dynamic = new MyDynamicChildWithoutToString();
  427. //x2.toString = function() return "foo";
  428. //eq( Std.string(x2), "foo" );
  429. }
  430. #if !macro
  431. function testFormat()
  432. {
  433. var x = 5;
  434. var y = 6;
  435. eq('$x${x+y}', "511");
  436. }
  437. #end
  438. function testJSon() {
  439. var str = haxe.Json.stringify( { x : -4500, y : 1.456, a : ["hello", "wor'\"\n\t\rd"] } );
  440. str = str.substr(1, str.length - 2); // remove {}
  441. var parts = str.split(",");
  442. t( parts.remove('"x":-4500') );
  443. t( parts.remove('"y":1.456') );
  444. t( parts.remove('"a":["hello"') );
  445. t( parts.remove('"wor\'\\"\\n\\t\\rd"]') );
  446. eq( parts.join("#"), "" );
  447. // no support for regexps
  448. #if flash8
  449. return;
  450. #end
  451. function id(v:Dynamic,?pos:haxe.PosInfos) eq(haxe.Json.parse(haxe.Json.stringify(v)),v);
  452. function deepId(v:Dynamic) {
  453. var str = haxe.Json.stringify(v);
  454. eq(haxe.Json.stringify(haxe.Json.parse(str)), str);
  455. }
  456. id(true);
  457. id(false);
  458. id(null);
  459. id(0);
  460. id(145);
  461. id( -145 );
  462. id(0.15461);
  463. id( -485.15461);
  464. id( 1e10 );
  465. id( -1e-10 );
  466. id( "" );
  467. id( "hello" );
  468. id( "he\n\r\t\\\\llo");
  469. deepId( {field: 4} );
  470. deepId( { test: { nested: null }} );
  471. var mix : Array<Dynamic> = [1, 2, 3, "str"];
  472. deepId( {array: mix} );
  473. eq( haxe.Json.parse('"\\u00E9"'), "é" );
  474. eq(haxe.Json.stringify(Math.POSITIVE_INFINITY), "null");
  475. eq(haxe.Json.stringify(Math.NEGATIVE_INFINITY), "null");
  476. eq(haxe.Json.stringify(Math.NaN), "null");
  477. }
  478. function testConstructorsOpts() {
  479. var b = new BaseConstrOpt();
  480. eq(b.s, "test");
  481. eq(b.i, -5);
  482. eq(b.b, true);
  483. var b = new BaseConstrOpt(null, 99);
  484. eq(b.s, "test");
  485. eq(b.i, 99);
  486. eq(b.b, true);
  487. var b = new SubConstrOpt();
  488. eq(b.s, "test");
  489. eq(b.i, -5);
  490. eq(b.b, true);
  491. var b = new SubConstrOpt2();
  492. eq(b.s, "test");
  493. eq(b.i, -5);
  494. eq(b.b, true);
  495. var b = new SubConstrOpt3();
  496. eq(b.s, "test2");
  497. eq(b.i, -6);
  498. eq(b.b, true);
  499. }
  500. }