forked from thomasvandoren/chapel-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapelTest.chpl
226 lines (186 loc) · 4.72 KB
/
ChapelTest.chpl
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
225
226
use Exec;
use FileSystem;
use IO;
config const testDir = "/Users/tvandoren/src/chapel/test/release/examples/programs";
config const testverbose = false;
config const printEnv = testverbose;
config const noRecurse = false;
proc main() {
var testRunner = new TestRunner(testDir, !noRecurse);
testRunner.runTests();
delete testRunner;
}
class TestRunner {
const testDir, chplHome: string,
recurse: bool;
proc TestRunner(testDir, recurse) {
this.testDir = testDir;
this.recurse = recurse;
this.chplHome = this.getChplHome();
}
proc runTests() {
setEnv("CHPL_HOME", this.chplHome);
this.printEnvironment();
// TODO: forall test in this.findTests() {
for test in this.findTests() {
this.runTest(test);
}
}
proc getChplHome() {
const output = run("chpl --print-chpl-home");
var chplHome: string;
for i in 1..output.length {
if output.substring(i) == "\t" {
chplHome = output.substring(1..i-1);
break;
}
}
if testverbose then
writeln("CHPL_HOME: ", chplHome);
return chplHome;
}
proc printEnvironment() {
if printEnv {
const chplEnv = run("$CHPL_HOME/util/printchplenv");
writeln("### Chapel Environment ###");
write(chplEnv);
writeln("##########################");
writeln();
}
}
proc runTest(test) {
writeln("testing: ", test);
var t = new Test(test);
t.runTest();
delete t;
}
iter findTests() {
for filename in findfiles(this.testDir, recursive=this.recurse) {
if this.isChapelFile(filename) {
yield filename;
}
}
}
iter findTests(param tag: iterKind)
where tag == iterKind.standalone
{
forall filename in findfiles(this.testDir, recursive=this.recurse) {
if this.isChapelFile(filename) {
yield filename;
}
}
}
proc isChapelFile(filename) {
return getTestBasename(filename) + ".chpl" == filename;
}
proc writeThis(w: Writer) {
w <~> "TestRunner(";
w <~> "testDir=";
w <~> testDir;
w <~> ")";
}
}
class Test {
const test, testDir, testBasename, testName: string;
var diff: string,
success: bool;
proc Test(test) {
this.test = test;
(testDir, testBasename) = splitFilename(test);
testName = getTestBasename(testBasename);
this.validateTestFiles();
}
proc validateTestFiles() {
// TODO: implement this!
}
proc runTest() {
const testOutput = this.compile() + this.execute();
if this.checkOutput(testOutput) {
writeln("SUCCESS: " + this.test);
} else {
writeln("ERROR: matching output: " + this.test);
}
return this.result;
}
proc result {
// return pass/fail as bool, then diff
return (this.success, this.diff);
}
proc fullTestName {
return this.testDir + "/" + this.testName;
}
proc goodFile {
return this.fullTestName + ".good";
}
proc expectedOutput {
var f = open(this.goodFile, iomode.r),
r = f.reader();
var o: string;
r.readstring(o);
r.close();
f.close();
return o;
}
proc checkOutput(testOutput) {
const expectedOut = this.expectedOutput;
this.success = testOutput == expectedOut;
if !this.success {
this.diff = this.generateDiff(expectedOut, testOutput);
}
return this.success;
}
proc actualOutputFile {
return this.fullTestName + ".out.tmp";
}
proc generateDiff(expectedOutput, actualOutput) {
var f = open(this.actualOutputFile, iomode.cw),
w = f.writer();
w.write(actualOutput);
w.close();
f.close();
const cmd = "diff " + this.goodFile + " " + this.actualOutputFile;
if testverbose then
writeln("Running diff command: " + cmd);
return run(cmd);
}
proc compile() {
const cmd = "chpl -o " + this.fullTestName + " " + this.test;
if testverbose then
writeln("Running compiler: " + cmd);
return run(cmd);
}
proc execute() {
const cmd = this.fullTestName;
if testverbose then
writeln("Running binary: " + cmd);
const output = run(cmd);
if testverbose then
writeln("Deleting " + this.fullTestName);
remove(this.fullTestName);
return output;
}
proc writeThis(w: Writer) {
w <~> "Test(";
w <~> "test=";
w <~> test;
w <~> ", testDir=";
w <~> testDir;
w <~> ", testBasename=";
w <~> testBasename;
w <~> ", testName=";
w <~> testName;
w <~> ")";
}
}
proc getTestBasename(filename) {
return filename.substring(..filename.length-5);
}
/* Return tuple that is dirname and basename. */
proc splitFilename(filename) {
for i in 1..filename.length by -1 {
if filename.substring(i) == "/" {
return (filename.substring(1..i-1), filename.substring(i+1..));
}
}
return ("", filename);
}