Strings.cs bits
[mono.git] / mono / mini / TestDriver.cs
1 using System;
2 using System.Reflection;
3
4
5 public class TestDriver {
6
7         static public int RunTests (Type type, string[] args) {
8                 int failed = 0, ran = 0;
9                 int result, expected, elen;
10                 int i, j;
11                 string name;
12                 MethodInfo[] methods;
13                 bool do_timings = false;
14                 int tms = 0;
15                 DateTime start, end = DateTime.Now;
16
17                 if (args != null && args.Length > 0) {
18                         for (j = 0; j < args.Length; j++) {
19                                 if (args [j] == "--time") {
20                                         do_timings = true;
21                                         string[] new_args = new string [args.Length - 1];
22                                         for (i = 0; i < j; ++i)
23                                                 new_args [i] = args [i];
24                                         j++;
25                                         for (; j < args.Length; ++i, ++j)
26                                                 new_args [i] = args [j];
27                                         args = new_args;
28                                         break;
29                                 }
30                         }
31                 }
32                 methods = type.GetMethods (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Static);
33                 for (i = 0; i < methods.Length; ++i) {
34                         name = methods [i].Name;
35                         if (!name.StartsWith ("test_"))
36                                 continue;
37                         if (args != null && args.Length > 0) {
38                                 bool found = false;
39                                 for (j = 0; j < args.Length; j++) {
40                                         if (name.EndsWith (args [j])) {
41                                                 found = true;
42                                                 break;
43                                         }
44                                 }
45                                 if (!found)
46                                         continue;
47                         }
48                         for (j = 5; j < name.Length; ++j)
49                                 if (!Char.IsDigit (name [j]))
50                                         break;
51                         expected = Int32.Parse (name.Substring (5, j - 5));
52                         start = DateTime.Now;
53                         result = (int)methods [i].Invoke (null, null);
54                         if (do_timings) {
55                                 end = DateTime.Now;
56                                 long tdiff = end.Ticks - start.Ticks;
57                                 int mdiff = (int)tdiff/10000;
58                                 tms += mdiff;
59                                 Console.WriteLine ("{0} took {1} ms", name, mdiff);
60                         }
61                         ran++;
62                         if (result != expected) {
63                                 failed++;
64                                 Console.WriteLine ("{0} failed: got {1}, expected {2}", name, result, expected);
65                         }
66                 }
67                 
68                 if (do_timings) {
69                         Console.WriteLine ("Total ms: {0}", tms);
70                 }
71                 Console.WriteLine ("Regression tests: {0} ran, {1} failed in {2}", ran, failed, type);
72                 //Console.WriteLine ("Regression tests: {0} ran, {1} failed in [{2}]{3}", ran, failed, type.Assembly.GetName().Name, type);
73                 return failed;
74         }
75         static public int RunTests (Type type) {
76                 return RunTests (type, null);
77         }
78 }
79