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