PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/src/sys/dotnet/fanx/test/Test.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 380 lines | 248 code | 59 blank | 73 comment | 28 complexity | 5910b25694afd0af63320381035ea195 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 19 Sep 06 Andy Frank Creation
  7. //
  8. using System;
  9. using System.Reflection;
  10. using Duration = Fan.Sys.Duration;
  11. namespace Fanx.Test
  12. {
  13. /// <summary>
  14. /// Test is the base class of test cases as well as the
  15. /// main entry for test harness.
  16. /// </summary>
  17. public abstract class Test
  18. {
  19. //////////////////////////////////////////////////////////////////////////
  20. // Test Case List
  21. //////////////////////////////////////////////////////////////////////////
  22. public static string[] tests =
  23. {
  24. "UtilTest",
  25. };
  26. //////////////////////////////////////////////////////////////////////////
  27. // Test Cases
  28. //////////////////////////////////////////////////////////////////////////
  29. /// <summary>
  30. /// Get the test name.
  31. /// </summary>
  32. public string TestName()
  33. {
  34. return testName;
  35. }
  36. /// <summary>
  37. /// Return true if we should skip this test.
  38. /// </summary>
  39. public virtual bool Skip()
  40. {
  41. return false;
  42. }
  43. /// <summary>
  44. /// Run the test case - any exception thrown
  45. /// is considered a failed test.
  46. /// </summary>
  47. public abstract void Run();
  48. //////////////////////////////////////////////////////////////////////////
  49. // verify Utils
  50. //////////////////////////////////////////////////////////////////////////
  51. /// <summary>
  52. /// verify the condition is true, otherwise throw an exception.
  53. /// </summary>
  54. public void verify(bool b)
  55. {
  56. if (!b) throw new Exception("Test failed");
  57. verified++;
  58. totalVerified++;
  59. }
  60. /// <summary>
  61. /// verify a and b are equal taking into account nulls.
  62. /// </summary>
  63. public void verify(Object a, Object b)
  64. {
  65. try
  66. {
  67. if (a == null) verify(b == null);
  68. else if (b == null) verify(false);
  69. else verify(a.Equals(b));
  70. }
  71. catch (Exception e)
  72. {
  73. if (e.Message != "Test failed") throw e;
  74. throw new Exception("Test failed " + a + " != " + b);
  75. }
  76. }
  77. /// <summary>
  78. /// Force test failure
  79. /// </summary>
  80. public void Fail()
  81. {
  82. verify(false);
  83. }
  84. //////////////////////////////////////////////////////////////////////////
  85. // Misc Utils
  86. //////////////////////////////////////////////////////////////////////////
  87. /// <summary>
  88. /// Print a line to standard out.
  89. /// </summary>
  90. public static void WriteLine(Object s)
  91. {
  92. Console.WriteLine(s);
  93. }
  94. /// <summary>
  95. /// Print a line to standard out if in verbose mode.
  96. /// </summary>
  97. public static void Verbose(Object s)
  98. {
  99. if (verbose) Console.WriteLine(s);
  100. }
  101. /// <summary>
  102. /// Get the temporary directory to use for tests.
  103. /// </summary>
  104. //public static File Temp()
  105. //{
  106. // temp.mkdirs();
  107. // return temp;
  108. //}
  109. //////////////////////////////////////////////////////////////////////////
  110. // Main
  111. //////////////////////////////////////////////////////////////////////////
  112. static bool RunTest(string testName)
  113. {
  114. try
  115. {
  116. Type type = Type.GetType("Fanx.Test." + testName);
  117. Test test = (Test)Activator.CreateInstance(type);
  118. test.testName = testName;
  119. if (test.Skip())
  120. {
  121. WriteLine("-- Skip: sys::" + testName + " [skip]");
  122. }
  123. else
  124. {
  125. WriteLine("-- Run: sys::" + testName + "...");
  126. long start = Environment.TickCount;
  127. test.Run();
  128. long end = Environment.TickCount;
  129. WriteLine(" Pass: sys::" + testName + " [" + test.verified + "] " + (end-start) + "ms");
  130. }
  131. return true;
  132. }
  133. catch (Exception e)
  134. {
  135. WriteLine("### Failed: " + testName);
  136. WriteLine(e.ToString());
  137. return false;
  138. }
  139. }
  140. public static bool RunTests(string pattern)
  141. {
  142. //temp = new File("temp");
  143. if (pattern == null) pattern = "";
  144. bool allPassed = true;
  145. int testCount = 0;
  146. long start = Environment.TickCount;
  147. for (int i=0; i<tests.Length; i++)
  148. {
  149. if (tests[i].StartsWith(pattern))
  150. {
  151. testCount++;
  152. if (!RunTest(tests[i]))
  153. allPassed = false;
  154. }
  155. }
  156. long end = Environment.TickCount;
  157. double elapsed = (end-start) / (1000d * 60d);
  158. elapsed = Math.Round(elapsed, 2);
  159. if (allPassed)
  160. {
  161. WriteLine("\n*** All Tests Passed!!! [" + totalVerified + "] "
  162. + elapsed + "min (" + (end-start) + "ms)");
  163. }
  164. //CompileTest.Cleanup();
  165. //try { FileUtil.delete(temp); } catch(IOException e) { e.printStackTrace(); }
  166. return allPassed;
  167. }
  168. //////////////////////////////////////////////////////////////////////////
  169. // Array Utils
  170. //////////////////////////////////////////////////////////////////////////
  171. public bool[] MakeBools(bool a)
  172. {
  173. return new bool[] { a };
  174. }
  175. public bool[] MakeBools(bool a, bool b)
  176. {
  177. return new bool[] { a, b };
  178. }
  179. public bool[] MakeBools(bool a, bool b, bool c)
  180. {
  181. return new bool[] { a, b, c };
  182. }
  183. public long[] MakeInts(long a)
  184. {
  185. return new long[] { a };
  186. }
  187. public long[] MakeInts(long a, long b)
  188. {
  189. return new long[] { a, b };
  190. }
  191. public long[] MakeInts(long a, long b, long c)
  192. {
  193. return new long[] { a, b, c };
  194. }
  195. public long[] MakeInts(long a, long b, long c, long d)
  196. {
  197. return new long[] { a, b, c, d };
  198. }
  199. public long[] MakeInts(long a, long b, long c, long d, long e)
  200. {
  201. return new long[] { a, b, c, d, e };
  202. }
  203. public long[] MakeInts(long a, long b, long c, long d, long e, long f)
  204. {
  205. return new long[] { a, b, c, d, e, f };
  206. }
  207. public double[] MakeFloats(double a)
  208. {
  209. return new double[] { a };
  210. }
  211. public double[] MakeFloats(double a, double b)
  212. {
  213. return new double[] { a, b };
  214. }
  215. public double[] MakeFloats(double a, double b, double c)
  216. {
  217. return new double[] { a, c, b };
  218. }
  219. public string[] MakeStrs(string a)
  220. {
  221. return new string[] { a };
  222. }
  223. public string[] MakeStrs(string a, string b)
  224. {
  225. return new string[] { a, b };
  226. }
  227. public string[] MakeStrs(string a, string b, string c)
  228. {
  229. return new string[] { a, b, c };
  230. }
  231. public string[] MakeStrs(string a, string b, string c, string d)
  232. {
  233. return new string[] { a, b, c, d };
  234. }
  235. public string[] MakeStrs(string a, string b, string c, string d, string e)
  236. {
  237. return new string[] { a, b, c, d, e };
  238. }
  239. public Duration[] MakeDurs(long a)
  240. {
  241. return new Duration[] { Duration.make(a) };
  242. }
  243. public Duration[] MakeDurs(long a, long b)
  244. {
  245. return new Duration[] { Duration.make(a), Duration.make(b) };
  246. }
  247. public Duration[] MakeDurs(long a, long b, long c)
  248. {
  249. return new Duration[] { Duration.make(a), Duration.make(b), Duration.make(c) };
  250. }
  251. //////////////////////////////////////////////////////////////////////////
  252. // Reflection Utils
  253. //////////////////////////////////////////////////////////////////////////
  254. public object Make(System.Type type) { return Make(type, new object[0]); }
  255. public object Make(System.Type type, object[] args)
  256. {
  257. return type.InvokeMember("Make", GetStaticFlags(), null, null, args);
  258. }
  259. public object InvokeStatic(System.Type type, string name)
  260. {
  261. return InvokeStatic(type, name, new object[0]);
  262. }
  263. public object InvokeStatic(System.Type type, string name, object[] args)
  264. {
  265. return type.InvokeMember(name, GetStaticFlags(), null, null, args);
  266. }
  267. public object InvokeInstance(System.Type type, object obj, string name)
  268. {
  269. return InvokeInstance(type, obj, name, new object[0]);
  270. }
  271. public object InvokeInstance(System.Type type, object obj, string name, object[] args)
  272. {
  273. return type.InvokeMember(name, GetInstanceFlags(), null, obj, args);
  274. }
  275. public MethodInfo FindMethod(Type type, string name)
  276. {
  277. return FindMethod(type, name, -1);
  278. }
  279. public MethodInfo FindMethod(Type type, string name, int paramCount)
  280. {
  281. MethodInfo method;
  282. method = FindMethod(type, name, paramCount, type.GetMethods()); if (method != null) return method;
  283. //method = FindMethod(type, name, paramCount, type.GetDeclaredMethods()); if (method != null) return method;
  284. throw new Exception("No method " + name);
  285. }
  286. public MethodInfo FindMethod(Type type, string name, int paramCount, MethodInfo[] methods)
  287. {
  288. for (int i=0; i<methods.Length; ++i)
  289. if (methods[i].Name == name)
  290. {
  291. if (paramCount != -1 && methods[i].GetParameters().Length != paramCount) continue;
  292. return methods[i];
  293. }
  294. return null;
  295. }
  296. public Object Get(Object instance, string name)
  297. {
  298. BindingFlags flags = BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
  299. if (instance is Type)
  300. return ((Type)instance).GetField(name, flags).GetValue(null);
  301. else
  302. return instance.GetType().GetField(name, flags).GetValue(instance);
  303. }
  304. public BindingFlags GetStaticFlags()
  305. {
  306. return BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static;
  307. }
  308. public BindingFlags GetInstanceFlags()
  309. {
  310. return BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance;
  311. }
  312. //////////////////////////////////////////////////////////////////////////
  313. // Fields
  314. //////////////////////////////////////////////////////////////////////////
  315. public static bool verbose;
  316. public static int totalVerified;
  317. //private static File temp;
  318. private int verified;
  319. private string testName = null;
  320. }
  321. }