TestCSharp.hx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package unit;
  2. //C#-specific tests, like unsafe code
  3. class TestCSharp extends Test
  4. {
  5. #if cs
  6. @:skipReflection private function refTest(i:cs.Ref<Int>):Void
  7. {
  8. i *= 2;
  9. }
  10. @:skipReflection private function outTest(out:cs.Out<Int>, x:Int):Void
  11. {
  12. out = x * 2;
  13. }
  14. public function testRef()
  15. {
  16. var i = 10;
  17. refTest(i);
  18. eq(i, 20);
  19. var cl:NativeClass = new HxClass();
  20. cl.refTest(i);
  21. eq(i, 80);
  22. }
  23. public function testOut()
  24. {
  25. var i = 0;
  26. outTest(i, 10);
  27. eq(i, 20);
  28. var cl:NativeClass = new HxClass();
  29. cl.outTest(i, 10);
  30. eq(i, 40);
  31. }
  32. public function testChecked()
  33. {
  34. exc(function()
  35. {
  36. cs.Lib.checked({
  37. var x = 1000;
  38. while(true)
  39. {
  40. x *= x;
  41. }
  42. });
  43. });
  44. }
  45. #if unsafe
  46. @:unsafe public function testUnsafe()
  47. {
  48. var x:cs.NativeArray<Int> = new cs.NativeArray(10);
  49. cs.Lib.fixed({
  50. var p = cs.Lib.pointerOfArray(x);
  51. for (i in 0...10)
  52. {
  53. p[0] = i;
  54. p = p.add(1);
  55. }
  56. });
  57. cs.Lib.fixed( {
  58. var p = cs.Lib.pointerOfArray(x);
  59. for (i in 0...10)
  60. {
  61. eq(p[i], i);
  62. }
  63. });
  64. var x:Int = 0;
  65. var addr = cs.Lib.addressOf(x);
  66. eq(cs.Lib.valueOf(addr), 0);
  67. eq(addr[0], 0);
  68. addr[0] = 42;
  69. eq(cs.Lib.valueOf(addr), 42);
  70. eq(addr[0], 42);
  71. eq(x, 42);
  72. }
  73. #end
  74. #end
  75. }
  76. @:nativeGen private class NativeClass
  77. {
  78. public function outTest(out:cs.Out<Int>, x:Int):Void
  79. {
  80. out = x * 2;
  81. }
  82. public function refTest(i:cs.Ref<Int>):Void
  83. {
  84. i *= 2;
  85. }
  86. }
  87. private class HxClass extends NativeClass
  88. {
  89. public function new()
  90. {
  91. }
  92. //here it would normally fail due to the added fast reflection field
  93. override public function outTest(out:cs.Out<Int>, x:Int):Void
  94. {
  95. out = x * 4;
  96. }
  97. override public function refTest(i:cs.Ref<Int>):Void
  98. {
  99. super.refTest(i);
  100. i *= 2;
  101. }
  102. }