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