-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.php
337 lines (283 loc) · 6.65 KB
/
test.php
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env php
<?php
/**
* @file test.php
* @author Jiri Furda (xfurda00)
*/
// --- Loading arguments ---
$testManager = new TestManager();
class Arguments
{
// @todo Test "./" if script is called from different location
public $testDir = "./";
public $recursive = false;
public $parsePath = "./parse.php";
public $interpretPath = "./interpret.py";
public function __construct()
{
global $argc;
// --- Read arguments ---
$options = array("help", "directory:", "recursive", "parse-script:", "int-script:");
$usedOptions = getopt(null, $options);
// --- Process --help option ---
if(isset($usedOptions["help"])) // If --help is used
{
if($argc == 2) // If its the only argument used
{
print("This is tool used for automatic testing of parse.php and interpret.py");
exit(0);
}
else
errorExit(10, "Option --help can't be combinated with other arguments");
}
if(isset($usedOptions["directory"]))
$this->testDir = $usedOptions["directory"];
if(substr($this->testDir, -1) != "/")
$this->testDir = $this->testDir."/"; // Dir must always end with "/"
if(isset($usedOptions["recursive"]))
$this->recursive = true;
if(isset($usedOptions["parse-script"]))
$this->parsePath = $usedOptions["parse-script"];
if(isset($usedOptions["int-script"]))
$this->interpretPath = $usedOptions["int-script"];
}
}
class TestManager
{
private $arguments;
private $folders;
private $results;
public function __construct()
{
$this->arguments = new Arguments;
$this->folders = array();
$this->results = array();
$this->scan($this->arguments->testDir);
}
private function scan($dir)
{
// --- Check if folder exists ---
if(!file_exists($dir))
return;
// --- Save information about folder ---
$folderID = count($this->folders);
$this->folders[$folderID]["name"] = $dir;
$this->folders[$folderID]["total"] = 0;
$this->folders[$folderID]["passed"] = 0;
// --- Scan folder ---
$files = scandir($dir);
// --- Loop throught every element ---
foreach($files as $file)
{
if(is_dir($dir.$file)) // Found directory
{
if($this->arguments->recursive == false)
continue;
else
{
if($file == "." || $file == "..")
continue; // Prevent loop
$this->scan($dir.$file."/"); // Use recursive scan
}
}
else // Found file
{
if(preg_match("/.src$/", $file))
$this->test(substr($file, 0, -4), $dir, $folderID); // "01" instead of "01.src"
else
continue; // Skips every file without .src suffix
}
}
}
private function test($name, $dir, $folderID)
{
$path = $dir.$name; // Shortcut
// --- Create missing files ---
if(!file_exists($path.".in"))
{
$succ = touch($path.".in");
if($succ == false)
errorExit(12, "Couldn't generate .in file");
}
if(!file_exists($path.".out"))
{
$succ = touch($path.".out");
if($succ == false)
errorExit(12, "Couldn't generate .out file");
}
if(!file_exists($path.".rc"))
{
$file = fopen($path.".rc", "w");
if($file == false)
errorExit(12, "Couldn't generate .rc file");
fwrite($file, "0");
fclose($file);
}
// --- Check .in file ---
exec("php5.6 ".$this->arguments->parsePath." <\"$path.src\" >\"$path.tmp.in\"");
exec("diff -q \"$path.in\" \"$path.tmp.in\"", $dump, $diff);
if($diff == 0)
$in = "OK";
else
$in = "NOK";
unset($diff);
// --- Check .rc file ---
exec("python3.6 ".$this->arguments->interpretPath." --source=\"$path.tmp.in\" >\"$path.tmp.out\"", $dump, $diff);
exec("printf $diff | diff -q - \"$path.rc\"", $dump, $diff);
if($diff == 0)
$rc = "OK";
else
$rc = "NOK";
unset($diff);
// --- Check .out file ---
exec("diff -q \"$path.out\" \"$path.tmp.out\"", $dump, $diff);
if($diff == 0)
$out = "OK";
else
$out = "NOK";
// --- Delete temporary files ---
unlink("$path.tmp.in");
unlink("$path.tmp.out");
// --- Save results ---
$this->results[$folderID][] = array($name, $in, $out, $rc);
$this->folders[$folderID]["total"]++;
if($in == "OK" && $out == "OK" && $rc == "OK")
$this->folders[$folderID]["passed"]++;
}
public function printResults()
{
// --- Check if some folder was scanned ---
if(count($this->folders) == 0)
{
print("Test folder opening wasn't successful\n>");
return;
}
$folderID = 0;
foreach($this->folders as $dir)
{
?>
<p>
<div class="folder">
<h2>Folder "<?php echo $dir["name"]; ?>" (passed: <?php echo $dir["passed"]."/".$dir["total"]; ?>)</h2>
<?php
if(!isset($this->results[$folderID]))
{
print("No tests found in this folder\n</div>\n");
$folderID++;
continue;
}
?>
<table>
<tr>
<th>Test name</th>
<th>IN</th>
<th>OUT</th>
<th>RC</th>
</tr>
<?php
foreach($this->results[$folderID] as $row)
{
?>
<tr>
<td><?php echo $row[0]; ?></td>
<td class="result" id="<?php echo $row[1]; ?>"> </td>
<td class="result" id="<?php echo $row[2]; ?>"> </td>
<td class="result" id="<?php echo $row[3]; ?>"> </td>
</tr>
<?php
}
?>
</table>
</div>
</p>
<?php
$folderID++;
}
}
}
?>
<!DOCTYPE HTML>
<html lang="cs">
<head>
<meta charset="utf-8" />
<style>
body
{
background-color: #b2c2bf;
margin: 30px;
}
h1
{
font-size: 40px;
font-family: "Segoe UI",Arial,sans-serif;
}
h2
{
line-height: 0px;
font-family: "Segoe UI",Arial,sans-serif;
}
.container
{
margin: auto;
width: 50%;
min-width: 800px;
box-shadow: 10px 10px 5px grey;
}
.header
{
background-color: #c0ded9;
text-align: center;
padding: 10px;
}
.content
{
background-color: #eaece5;
padding: 15px;
}
.folder
{
background-color: #ffffff;
padding: 15px;
}
table, th, td
{
border: 1px solid black;
}
table th
{
background-color: lightgrey;
}
table tr:nth-child(even)
{
background-color: #eee;
}
table tr:nth-child(odd)
{
background-color: #fff;
}
.result
{
width: 40px;
}
#OK
{
background-color: #6B8E23;
}
#NOK
{
background-color: #ff1700;
}
</style>
<title>IPP project tests results</title>
</head>
<body>
<div class="container">
<div class="header">
<h1>IPP project tests results</h1>
</div>
<div class="content">
<?php $testManager->printResults(); ?>
</div>
</div>
</body>
</html>