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
|
/*
* Copyright © 2020 Valve Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#include <map>
#include <set>
#include <string>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <unistd.h>
#include <stdarg.h>
#include <llvm-c/Target.h>
#include "aco_ir.h"
#include "framework.h"
static const char *help_message =
"Usage: %s [-h] [-l --list] [--no-check] [TEST [TEST ...]]\n"
"\n"
"Run ACO unit test(s). If TEST is not provided, all tests are run.\n"
"\n"
"positional arguments:\n"
" TEST Run TEST. If TEST ends with a '.', run tests with names\n"
" starting with TEST. The test variant (after the '/') can\n"
" be omitted to run all variants\n"
"\n"
"optional arguments:\n"
" -h, --help Show this help message and exit.\n"
" -l --list List unit tests.\n"
" --no-check Print test output instead of checking it.\n";
std::map<std::string, TestDef> tests;
FILE *output = NULL;
static TestDef current_test;
static unsigned tests_written = 0;
static FILE *checker_stdin = NULL;
static char *checker_stdin_data = NULL;
static size_t checker_stdin_size = 0;
static char *output_data = NULL;
static size_t output_size = 0;
static size_t output_offset = 0;
static char current_variant[64] = {0};
static std::set<std::string> *variant_filter = NULL;
bool test_failed = false;
bool test_skipped = false;
static char fail_message[256] = {0};
void write_test()
{
if (!checker_stdin) {
/* not entirely correct, but shouldn't matter */
tests_written++;
return;
}
fflush(output);
if (output_offset == output_size && !test_skipped && !test_failed)
return;
char *data = output_data + output_offset;
uint32_t size = output_size - output_offset;
fwrite("test", 1, 4, checker_stdin);
fwrite(current_test.name, 1, strlen(current_test.name)+1, checker_stdin);
fwrite(current_variant, 1, strlen(current_variant)+1, checker_stdin);
fwrite(current_test.source_file, 1, strlen(current_test.source_file)+1, checker_stdin);
if (test_failed || test_skipped) {
const char *res = test_failed ? "failed" : "skipped";
fwrite("\x01", 1, 1, checker_stdin);
fwrite(res, 1, strlen(res)+1, checker_stdin);
fwrite(fail_message, 1, strlen(fail_message)+1, checker_stdin);
} else {
fwrite("\x00", 1, 1, checker_stdin);
}
fwrite(&size, 4, 1, checker_stdin);
fwrite(data, 1, size, checker_stdin);
tests_written++;
output_offset += size;
}
bool set_variant(const char *name)
{
if (variant_filter && !variant_filter->count(name))
return false;
write_test();
test_failed = false;
test_skipped = false;
strncpy(current_variant, name, sizeof(current_variant) - 1);
printf("Running '%s/%s'\n", current_test.name, name);
return true;
}
void fail_test(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
test_failed = true;
vsnprintf(fail_message, sizeof(fail_message), fmt, args);
va_end(args);
}
void skip_test(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
test_skipped = true;
vsnprintf(fail_message, sizeof(fail_message), fmt, args);
va_end(args);
}
void run_test(TestDef def)
{
current_test = def;
output_data = NULL;
output_size = 0;
output_offset = 0;
test_failed = false;
test_skipped = false;
memset(current_variant, 0, sizeof(current_variant));
if (checker_stdin)
output = open_memstream(&output_data, &output_size);
else
output = stdout;
current_test.func();
write_test();
if (checker_stdin)
fclose(output);
free(output_data);
}
int check_output(char **argv)
{
fflush(stdout);
fflush(stderr);
fclose(checker_stdin);
int stdin_pipe[2];
pipe(stdin_pipe);
pid_t child_pid = fork();
if (child_pid == -1) {
fprintf(stderr, "%s: fork() failed: %s\n", argv[0], strerror(errno));
return 99;
} else if (child_pid != 0) {
/* Evaluate test output externally using Python */
dup2(stdin_pipe[0], STDIN_FILENO);
close(stdin_pipe[0]);
close(stdin_pipe[1]);
execlp(ACO_TEST_PYTHON_BIN, ACO_TEST_PYTHON_BIN, ACO_TEST_SOURCE_DIR "/check_output.py", NULL);
fprintf(stderr, "%s: execlp() failed: %s\n", argv[0], strerror(errno));
return 99;
} else {
/* Feed input data to the Python process. Writing large streams to
* stdin will block eventually, so this is done in a forked process
* to let the test checker process chunks of data as they arrive */
write(stdin_pipe[1], checker_stdin_data, checker_stdin_size);
close(stdin_pipe[0]);
close(stdin_pipe[1]);
_exit(0);
}
}
bool match_test(std::string name, std::string pattern)
{
if (name.length() < pattern.length())
return false;
if (pattern.back() == '.')
name.resize(pattern.length());
return name == pattern;
}
int main(int argc, char **argv)
{
int print_help = 0;
int do_list = 0;
int do_check = 1;
const struct option opts[] = {
{ "help", no_argument, &print_help, 1 },
{ "list", no_argument, &do_list, 1 },
{ "no-check", no_argument, &do_check, 0 },
{ NULL, 0, NULL, 0 }
};
int c;
while ((c = getopt_long(argc, argv, "hl", opts, NULL)) != -1) {
switch (c) {
case 'h':
print_help = 1;
break;
case 'l':
do_list = 1;
break;
case 0:
break;
case '?':
default:
fprintf(stderr, "%s: Invalid argument\n", argv[0]);
return 99;
}
}
if (print_help) {
fprintf(stderr, help_message, argv[0]);
return 99;
}
if (do_list) {
for (auto test : tests)
printf("%s\n", test.first.c_str());
return 99;
}
std::vector<std::pair<std::string, std::string>> names;
for (int i = optind; i < argc; i++) {
std::string name = argv[i];
std::string variant;
size_t pos = name.find('/');
if (pos != std::string::npos) {
variant = name.substr(pos + 1);
name = name.substr(0, pos);
}
names.emplace_back(std::pair<std::string, std::string>(name, variant));
}
if (do_check)
checker_stdin = open_memstream(&checker_stdin_data, &checker_stdin_size);
LLVMInitializeAMDGPUTargetInfo();
LLVMInitializeAMDGPUTarget();
LLVMInitializeAMDGPUTargetMC();
LLVMInitializeAMDGPUDisassembler();
aco::init();
for (auto pair : tests) {
bool found = names.empty();
bool all_variants = names.empty();
std::set<std::string> variants;
for (const std::pair<std::string, std::string>& name : names) {
if (match_test(pair.first, name.first)) {
found = true;
if (name.second.empty())
all_variants = true;
else
variants.insert(name.second);
}
}
if (found) {
variant_filter = all_variants ? NULL : &variants;
printf("Running '%s'\n", pair.first.c_str());
run_test(pair.second);
}
}
if (!tests_written) {
fprintf(stderr, "%s: No matching tests\n", argv[0]);
return 99;
}
if (checker_stdin) {
printf("\n");
return check_output(argv);
} else {
printf("Tests ran\n");
return 99;
}
}
|