Test.hx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package unit;
  2. @:expose
  3. @:keepSub
  4. #if as3
  5. @:publicFields
  6. #end
  7. class Test #if swf_mark implements mt.Protect #end {
  8. public function new() {
  9. }
  10. function eq<T>( v : T, v2 : T, ?pos ) {
  11. count++;
  12. if( v != v2 ) report(Std.string(v)+" should be "+Std.string(v2),pos);
  13. }
  14. function feq( v : Float, v2 : Float, ?pos ) {
  15. count++;
  16. if (!Math.isFinite(v) || !Math.isFinite(v2))
  17. eq(v, v2, pos);
  18. else if ( Math.abs(v - v2) > 1e-15 )
  19. report(v+" should be "+v2,pos);
  20. }
  21. function t( v, ?pos ) {
  22. eq(v,true,pos);
  23. }
  24. function f( v, ?pos ) {
  25. eq(v,false,pos);
  26. }
  27. function assert( ?pos ) {
  28. report("Assert",pos);
  29. }
  30. function exc( f : Void -> Void, ?pos ) {
  31. count++;
  32. try {
  33. f();
  34. report("No exception occured",pos);
  35. } catch( e : Dynamic ) {
  36. }
  37. }
  38. function unspec( f : Void -> Void, ?pos ) {
  39. count++;
  40. try {
  41. f();
  42. } catch( e : Dynamic ) {
  43. }
  44. }
  45. function allow<T>( v : T, values : Array<T>, ?pos ) {
  46. count++;
  47. for( v2 in values )
  48. if( v == v2 )
  49. return;
  50. report(v+" not in "+Std.string(values),pos);
  51. }
  52. function hf(c:Class<Dynamic>, n:String, ?pos:haxe.PosInfos) {
  53. Test.count++;
  54. if (!Lambda.has(Type.getInstanceFields(c), n))
  55. Test.report(Type.getClassName(c) + " should have member field " +n, pos);
  56. }
  57. function nhf(c:Class<Dynamic>, n:String, ?pos:haxe.PosInfos) {
  58. Test.count++;
  59. if (Lambda.has(Type.getInstanceFields(c), n))
  60. Test.report(Type.getClassName(c) + " should not have member field " +n, pos);
  61. }
  62. function hsf(c:Class<Dynamic> , n:String, ?pos:haxe.PosInfos) {
  63. Test.count++;
  64. if (!Lambda.has(Type.getClassFields(c), n))
  65. Test.report(Type.getClassName(c) + " should have static field " +n, pos);
  66. }
  67. function nhsf(c:Class<Dynamic> , n:String, ?pos:haxe.PosInfos) {
  68. Test.count++;
  69. if (Lambda.has(Type.getClassFields(c), n))
  70. Test.report(Type.getClassName(c) + " should not have static field " +n, pos);
  71. }
  72. function infos( m : String ) {
  73. reportInfos = m;
  74. }
  75. function async<Args,T>( f : Args -> (T -> Void) -> Void, args : Args, v : T, ?pos : haxe.PosInfos ) {
  76. if( asyncWaits.length >= AMAX ) {
  77. asyncCache.push(async.bind(f,args,v,pos));
  78. return;
  79. }
  80. asyncWaits.push(pos);
  81. f(args,function(v2) {
  82. count++;
  83. if( !asyncWaits.remove(pos) ) {
  84. report("Double async result",pos);
  85. return;
  86. }
  87. if( v != v2 )
  88. report(v2+" should be "+v,pos);
  89. checkDone();
  90. });
  91. }
  92. function asyncExc<Args>( seterror : (Dynamic -> Void) -> Void, f : Args -> (Dynamic -> Void) -> Void, args : Args, ?pos : haxe.PosInfos ) {
  93. if( asyncWaits.length >= AMAX ) {
  94. asyncCache.push(asyncExc.bind(seterror,f,args,pos));
  95. return;
  96. }
  97. asyncWaits.push(pos);
  98. seterror(function(e) {
  99. count++;
  100. if( asyncWaits.remove(pos) )
  101. checkDone();
  102. else
  103. report("Multiple async events",pos);
  104. });
  105. f(args,function(v) {
  106. count++;
  107. if( asyncWaits.remove(pos) ) {
  108. report("No exception occured",pos);
  109. checkDone();
  110. } else
  111. report("Multiple async events",pos);
  112. });
  113. }
  114. function log( msg, ?pos : haxe.PosInfos ) {
  115. haxe.Log.trace(msg,pos);
  116. }
  117. static var count = 0;
  118. static var reportInfos = null;
  119. static var reportCount = 0;
  120. static var checkCount = 0;
  121. static var asyncWaits = new Array<haxe.PosInfos>();
  122. static var asyncCache = new Array<Void -> Void>();
  123. static var AMAX = 3;
  124. static var timer : haxe.Timer;
  125. dynamic static function report( msg : String, ?pos : haxe.PosInfos ) {
  126. if( reportInfos != null ) {
  127. msg += " ("+reportInfos+")";
  128. reportInfos = null;
  129. }
  130. haxe.Log.trace(msg,pos);
  131. reportCount++;
  132. #if !(java || cs)
  133. if( reportCount == 50 ) {
  134. trace("Too many errors");
  135. report = function(msg,?pos) {};
  136. }
  137. #end
  138. }
  139. static function checkDone() {
  140. if( asyncWaits.length != 0 ) return;
  141. if( asyncCache.length == 0 ) {
  142. report("DONE ["+count+" tests]");
  143. return;
  144. }
  145. resetTimer();
  146. while( asyncCache.length > 0 && asyncWaits.length < AMAX )
  147. asyncCache.shift()();
  148. }
  149. static function asyncTimeout() {
  150. if( asyncWaits.length == 0 )
  151. return;
  152. for( pos in asyncWaits )
  153. report("TIMEOUT",pos);
  154. asyncWaits = new Array();
  155. checkDone();
  156. }
  157. static function resetTimer() {
  158. #if (neko || php || cpp)
  159. #else
  160. if( timer != null ) timer.stop();
  161. timer = new haxe.Timer(10000);
  162. timer.run = asyncTimeout;
  163. #end
  164. }
  165. static function onError( e : Dynamic, msg : String, context : String ) {
  166. var msg = "???";
  167. var stack :String = #if js
  168. e.stack;
  169. #else
  170. haxe.CallStack.toString(haxe.CallStack.exceptionStack());
  171. #end
  172. try msg = Std.string(e) catch( e : Dynamic ) {};
  173. reportCount = 0;
  174. report("ABORTED : "+msg+" in "+context);
  175. reportInfos = null;
  176. trace("STACK :\n"+stack);
  177. }
  178. static function main() {
  179. #if neko
  180. if( neko.Web.isModNeko )
  181. neko.Web.setHeader("Content-Type","text/plain");
  182. #elseif php
  183. if( php.Web.isModNeko )
  184. php.Web.setHeader("Content-Type","text/plain");
  185. #end
  186. resetTimer();
  187. #if !macro
  188. trace("Generated at: " + TestType.getCompilationDate());
  189. #end
  190. trace("START");
  191. #if flash9
  192. var tf : flash.text.TextField = untyped flash.Boot.getTrace();
  193. tf.selectable = true;
  194. tf.mouseEnabled = true;
  195. #elseif flash
  196. var tf : flash.TextField = untyped flash.Boot.getTrace();
  197. tf.selectable = true;
  198. #end
  199. var classes = [
  200. new TestOps(),
  201. new TestBasetypes(),
  202. new TestBytes(),
  203. new TestIO(),
  204. new TestLocals(),
  205. new TestEReg(),
  206. new TestXML(),
  207. new TestMisc(),
  208. new TestResource(),
  209. new TestInt64(),
  210. new TestReflect(),
  211. new TestSerialize(),
  212. new TestMeta(),
  213. new TestType(),
  214. new TestOrder(),
  215. new TestGADT(),
  216. #if !no_pattern_matching
  217. new TestMatch(),
  218. #end
  219. new TestSpecification(),
  220. #if cs
  221. new TestCSharp(),
  222. #end
  223. #if java
  224. new TestJava(),
  225. #end
  226. #if php
  227. new TestPhp(),
  228. #end
  229. #if (java || cs)
  230. new TestOverloads(),
  231. #end
  232. #if ((dce == "full") && !interp && !as3)
  233. new TestDCE(),
  234. #end
  235. //new TestUnspecified(),
  236. //new TestRemoting(),
  237. ];
  238. var current = null;
  239. #if (!fail_eager)
  240. try
  241. #end
  242. {
  243. asyncWaits.push(null);
  244. for( inst in classes ) {
  245. current = Type.getClass(inst);
  246. for( f in Type.getInstanceFields(current) )
  247. if( f.substr(0,4) == "test" ) {
  248. #if fail_eager
  249. Reflect.callMethod(inst,Reflect.field(inst,f),[]);
  250. #else
  251. try {
  252. Reflect.callMethod(inst,Reflect.field(inst,f),[]);
  253. }
  254. #if !as3
  255. catch( e : Dynamic ) {
  256. onError(e,"EXCEPTION",Type.getClassName(current)+"."+f);
  257. }
  258. #end
  259. #end
  260. reportInfos = null;
  261. }
  262. }
  263. asyncWaits.remove(null);
  264. checkDone();
  265. }
  266. #if (!as3 && !(fail_eager))
  267. catch( e : Dynamic ) {
  268. asyncWaits.remove(null);
  269. onError(e,"ABORTED",Type.getClassName(current));
  270. }
  271. #end
  272. }
  273. }