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