-
Notifications
You must be signed in to change notification settings - Fork 3
/
runtests.d
224 lines (210 loc) · 4.97 KB
/
runtests.d
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Written in the D programming language
// Author: Timon Gehr
// License: http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0
// (This is a quick hack.)
import std.stdio, std.file;
import std.process, std.string, std.array;
import std.algorithm, std.conv;
void main(){
auto sources=shell("ls test/*.d").splitLines;
Summary total;
foreach(source;sources){
writeln("running "~source);
auto expected=source.getExpected;
auto actual=source.getActual;
auto comparison = compare(expected, actual);
auto summary = comparison.summarize;
total+=summary;
if(summary.isInteresting) writeln(summary);
}
writeln("TOTAL:");
writeln(total);
}
struct Summary{
int unexpectedErrors;
int expectedErrors;
int missingErrors;
int todos;
int obsoleteTodos;
auto combine(Summary rhs){
Summary r;
foreach(i,t;this.tupleof){
r.tupleof[i]=t+rhs.tupleof[i];
}
return r;
}
string toString(){
return "regressions: "~(unexpectedErrors+missingErrors).to!string~"\n"~
"TODOs: "~todos.to!string~"\n"~
"fixed: "~obsoleteTodos.to!string~"\n";
}
bool isInteresting(){
foreach(i,x;this.tupleof) if(i!=1&&x) return true;
return false;
}
void opOpAssign(string op:"+")(Summary rhs){
foreach(i,ref x;this.tupleof) x+=rhs.tupleof[i];
}
}
auto summarize(Comparison[] comp){
Summary result;
foreach(c;comp){
final switch(c.status) with(Status){
case expected:
if(c.info.error){
if(c.info.todo){
result.obsoleteTodos++;
writeln("FIX AT LINE ",c.info.line);
}else result.expectedErrors++;
}else{
if(c.info.todo) result.todos++;
else{
result.missingErrors++;
writeln("REGRESSION AT LINE ",c.info.line);
}
}
break;
case unexpected:
assert(c.info.error);
result.unexpectedErrors++;
if(c.info.line!=-1) writeln("REGRESSION AT LINE ",c.info.line);
else writeln("REGRESSION: AssertError");
break;
case missing:
if(c.info.todo){
if(c.info.error) result.todos++;
else{
result.obsoleteTodos++;
writeln("FIX AT LINE ",c.info.line);
}
}else{
result.missingErrors++;
writeln("REGRESSION AT LINE ",c.info.line);
}
break;
}
}
return result;
}
enum Status{
expected,
unexpected,
missing,
}
struct Comparison{
Status status;
Info info;
}
auto compare(Info[] expected, int[] actual){
int ai=0;
Comparison[] result;
foreach(i,x;expected){
while(ai<actual.length&&actual[ai]<x.line) ai++;
if(ai==actual.length){
foreach(xx;expected[i..$])
result~=Comparison(Status.missing, xx);
break;
}
result~=Comparison(x.line==actual[ai]?Status.expected:Status.missing,x);
}
ai=0;
foreach(i,a;actual){
while(ai<expected.length&&expected[ai].line<a) ai++;
if(ai==expected.length){
foreach(aa;actual[i..$])
result~=Comparison(Status.unexpected, Info(aa,true,false));
break;
}
if(expected[ai].line!=a) result~=Comparison(Status.unexpected, Info(a, true, false));
}
return result;
}
struct Info{
int line;
bool error;
bool todo;
}
struct Comment{
int line;
string text;
}
auto comments(string code){
Comment[] result;
int line=1;
for(;;){
if(code.startsWith("//")){
code.popFront(); code.popFront();
auto start = code.ptr;
while(!code.empty&&code.front!='\n')
code.popFront();
auto text = start[0..code.ptr-start];
result~=Comment(line,text);
}
if(code.startsWith("\n")) line++;
if(code.startsWith("/*"))
while(!code.startsWith("*/")){
if(code.startsWith("\n")) line++;
code.popFront();
}
if(code.startsWith("/+")){
int nest=1;
code.popFront();
while(nest){
code.popFront();
if(code.startsWith("\n")) line++;
else if(code.startsWith("/+")){
code.popFront();
nest++;
}else if(code.startsWith("+/")){
code.popFront();
nest--;
}
}
}
if(code.empty) break;
code.popFront();
}
return result;
}
auto analyze(Comment comment){
Info result;
result.line=comment.line;
result.error=comment.text.canFind("error");
result.todo=comment.text.canFind("TODO")&&!comment.text.canFind("// TODO");
return result;
}
auto getExpected(string source){
Info[] result;
auto code = source.readText;
foreach(comment;code.comments){
auto info = comment.analyze;
if(info.error||info.todo)
result~=info;
}
return result;
}
int[] getActual(string source){
auto fin=File(source,"r");
auto options=fin.readln();
if(options.startsWith("// options: "))
options=options["// options: ".length..$].strip()~" ";
else options="";
auto output = shell("./d "~options~source~" 2>&1").splitLines;
int[] result;
static bool isBad(string x){
if(x.canFind(": error")) return true;
if(x.startsWith("core.exception.AssertError"))
return true;
return false;
}
foreach(err;output.filter!isBad){
while(err.startsWith("<mixin@")) err=err["<mixin@".length..$];
if(err.startsWith(source~":")){
auto line = err[(source~":").length..$].parse!int;
result~=line;
}else if(err.startsWith("core.exception.AssertError"))
result~=-1;
}
result=result.sort.uniq.array;
return result;
}