Merge pull request #199 from slide/master
[mono.git] / mono / mini / TestDriver.cs
1 using System;
2 using System.Reflection;
3 using System.Collections.Generic;
4
5 public class CategoryAttribute : Attribute
6 {
7         public CategoryAttribute (string category) {
8                 Category = category;
9         }
10
11         public string Category {
12                 get; set;
13         }
14 }
15
16 public class TestDriver {
17
18         static public int RunTests (Type type, string[] args) {
19                 int failed = 0, ran = 0;
20                 int result, expected;
21                 int i, j, iterations;
22                 string name;
23                 MethodInfo[] methods;
24                 bool do_timings = false;
25                 bool verbose = false;
26                 int tms = 0;
27                 DateTime start, end = DateTime.Now;
28
29                 iterations = 1;
30
31                 var exclude = new Dictionary<string, string> ();
32                 List<string> run_only = new List<string> ();
33                 if (args != null && args.Length > 0) {
34                         for (j = 0; j < args.Length;) {
35                                 if (args [j] == "--time") {
36                                         do_timings = true;
37                                         j ++;
38                                 } else if (args [j] == "--iter") {
39                                         iterations = Int32.Parse (args [j + 1]);
40                                         j += 2;
41                                 } else if ((args [j] == "-v") || (args [j] == "--verbose")) {
42                                         verbose = true;
43                                 } else if (args [j] == "--exclude") {
44                                         exclude [args [j + 1]] = args [j + 1];
45                                         j += 2;
46                                 } else if (args [j] == "--run-only") {
47                                         run_only.Add (args [j + 1]);
48                                         j += 2;
49                                 } else {
50                                         Console.WriteLine ("Unknown argument: " + args [j]);
51                                         return 1;
52                                 }
53                         }
54                 }
55                 int nskipped = 0;
56                 methods = type.GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static);
57                 for (int iter = 0; iter < iterations; ++iter) {
58                         for (i = 0; i < methods.Length; ++i) {
59                                 name = methods [i].Name;
60                                 if (!name.StartsWith ("test_", StringComparison.Ordinal))
61                                         continue;
62                                 if (run_only.Count > 0) {
63                                         bool found = false;
64                                         for (j = 0; j < run_only.Count; j++) {
65                                                 if (name.EndsWith (run_only [j])) {
66                                                         found = true;
67                                                         break;
68                                                 }
69                                         }
70                                         if (!found)
71                                                 continue;
72                                 }
73                                 if (exclude.Count > 0) {
74                                         var attrs = methods [i].GetCustomAttributes (typeof (CategoryAttribute), false);
75                                         bool skip = false;
76                                         foreach (CategoryAttribute attr in attrs) {
77                                                 if (exclude.ContainsKey (attr.Category))
78                                                         skip = true;
79                                         }
80                                         if (skip) {
81                                                 if (verbose)
82                                                         Console.WriteLine ("Skipping '{0}'.", name);
83                                                 nskipped ++;
84                                                 continue;
85                                         }
86                                 }
87                                 for (j = 5; j < name.Length; ++j)
88                                         if (!Char.IsDigit (name [j]))
89                                                 break;
90                                 if (verbose)
91                                         Console.WriteLine ("Running '{0}' ...", name);
92                                 expected = Int32.Parse (name.Substring (5, j - 5));
93                                 start = DateTime.Now;
94                                 result = (int)methods [i].Invoke (null, null);
95                                 if (do_timings) {
96                                         end = DateTime.Now;
97                                         long tdiff = end.Ticks - start.Ticks;
98                                         int mdiff = (int)tdiff/10000;
99                                         tms += mdiff;
100                                         Console.WriteLine ("{0} took {1} ms", name, mdiff);
101                                 }
102                                 ran++;
103                                 if (result != expected) {
104                                         failed++;
105                                         Console.WriteLine ("{0} failed: got {1}, expected {2}", name, result, expected);
106                                 }
107                         }
108                 
109                         if (do_timings) {
110                                 Console.WriteLine ("Total ms: {0}", tms);
111                         }
112                         if (nskipped > 0)
113                                 Console.WriteLine ("Regression tests: {0} ran, {1} skipped, {2} failed in {3}", ran, nskipped, failed, type);
114                         else
115                                 Console.WriteLine ("Regression tests: {0} ran, {1} failed in {2}", ran, failed, type);
116                 }
117
118                 //Console.WriteLine ("Regression tests: {0} ran, {1} failed in [{2}]{3}", ran, failed, type.Assembly.GetName().Name, type);
119                 return failed;
120         }
121         static public int RunTests (Type type) {
122                 return RunTests (type, null);
123         }
124 }
125