Merge pull request #225 from mistoll/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                                         j += 1;
44                                 } else if (args [j] == "--exclude") {
45                                         exclude [args [j + 1]] = args [j + 1];
46                                         j += 2;
47                                 } else if (args [j] == "--run-only") {
48                                         run_only.Add (args [j + 1]);
49                                         j += 2;
50                                 } else {
51                                         Console.WriteLine ("Unknown argument: " + args [j]);
52                                         return 1;
53                                 }
54                         }
55                 }
56                 int nskipped = 0;
57                 methods = type.GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static);
58                 for (int iter = 0; iter < iterations; ++iter) {
59                         for (i = 0; i < methods.Length; ++i) {
60                                 name = methods [i].Name;
61                                 if (!name.StartsWith ("test_", StringComparison.Ordinal))
62                                         continue;
63                                 if (run_only.Count > 0) {
64                                         bool found = false;
65                                         for (j = 0; j < run_only.Count; j++) {
66                                                 if (name.EndsWith (run_only [j])) {
67                                                         found = true;
68                                                         break;
69                                                 }
70                                         }
71                                         if (!found)
72                                                 continue;
73                                 }
74                                 if (exclude.Count > 0) {
75                                         var attrs = methods [i].GetCustomAttributes (typeof (CategoryAttribute), false);
76                                         bool skip = false;
77                                         foreach (CategoryAttribute attr in attrs) {
78                                                 if (exclude.ContainsKey (attr.Category))
79                                                         skip = true;
80                                         }
81                                         if (skip) {
82                                                 if (verbose)
83                                                         Console.WriteLine ("Skipping '{0}'.", name);
84                                                 nskipped ++;
85                                                 continue;
86                                         }
87                                 }
88                                 for (j = 5; j < name.Length; ++j)
89                                         if (!Char.IsDigit (name [j]))
90                                                 break;
91                                 if (verbose)
92                                         Console.WriteLine ("Running '{0}' ...", name);
93                                 expected = Int32.Parse (name.Substring (5, j - 5));
94                                 start = DateTime.Now;
95                                 result = (int)methods [i].Invoke (null, null);
96                                 if (do_timings) {
97                                         end = DateTime.Now;
98                                         long tdiff = end.Ticks - start.Ticks;
99                                         int mdiff = (int)tdiff/10000;
100                                         tms += mdiff;
101                                         Console.WriteLine ("{0} took {1} ms", name, mdiff);
102                                 }
103                                 ran++;
104                                 if (result != expected) {
105                                         failed++;
106                                         Console.WriteLine ("{0} failed: got {1}, expected {2}", name, result, expected);
107                                 }
108                         }
109                 
110                         if (do_timings) {
111                                 Console.WriteLine ("Total ms: {0}", tms);
112                         }
113                         if (nskipped > 0)
114                                 Console.WriteLine ("Regression tests: {0} ran, {1} skipped, {2} failed in {3}", ran, nskipped, failed, type);
115                         else
116                                 Console.WriteLine ("Regression tests: {0} ran, {1} failed in {2}", ran, failed, type);
117                 }
118
119                 //Console.WriteLine ("Regression tests: {0} ran, {1} failed in [{2}]{3}", ran, failed, type.Assembly.GetName().Name, type);
120                 return failed;
121         }
122         static public int RunTests (Type type) {
123                 return RunTests (type, null);
124         }
125 }
126