Test.hx 7.1 KB

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