Merge pull request #2174 from stukselbax/master
[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
136 /// Provide tests with the ability to find out how much time they have to run before being timed out.
137 public class TestTimeout {
138         const string ENV_TIMEOUT = "TEST_DRIVER_TIMEOUT_SEC";
139         private readonly TimeSpan availableTime;
140         private TimeSpan slack;
141         private DateTime startTime;
142
143         /// <summary>
144         ///   How much time the test runner provided for us or TimeSpan.Zero if there is no bound.
145         /// </summary>
146         public TimeSpan AvailableTime { get { return availableTime; } }
147
148         public DateTime StartTime { get { return startTime; } }
149
150         /// <summary> Extra time to add when deciding if there
151         ///   is still time to run.  Bigger slack means less
152         ///   time left.
153         /// </summary>
154         public TimeSpan Slack {
155                 get { return slack; }
156                 set { slack = value; }
157         }
158
159         public TestTimeout () {
160                 availableTime = initializeAvailableTime ();
161                 slack = defaultSlack ();
162         }
163
164         /// <summary>
165         ///    Consider the test started.
166         /// </summary>
167         public void Start ()
168         {
169                 startTime = DateTime.UtcNow;
170         }
171
172         public bool HaveTimeLeft ()
173         {
174                 if (availableTime == TimeSpan.Zero)
175                         return true;
176                 var t = DateTime.UtcNow;
177                 var finishTime = startTime + availableTime - slack;
178                 return (t < finishTime);
179         }
180
181         private TimeSpan defaultSlack ()
182         {
183                 return TimeSpan.FromSeconds (5);
184         }
185
186         private TimeSpan initializeAvailableTime ()
187         {
188                 var e = System.Environment.GetEnvironmentVariable(ENV_TIMEOUT);
189                 double d;
190                 if (Double.TryParse(e, out d)) {
191                         return TimeSpan.FromSeconds(d);
192                 } else {
193                         return TimeSpan.Zero;
194                 }
195         }
196
197 }