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