| 1 |
/* Copyright 2016 Google Inc. |
| 2 |
|
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. */ |
| 14 |
|
| 15 |
/* Run the tests listed on the command line. */ |
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <string.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#if defined(_WIN32) |
| 21 |
#include <Windows.h> |
| 22 |
#define popen _popen |
| 23 |
#define pclose _pclose |
| 24 |
#define sleep(n) Sleep ((n) * 1000) |
| 25 |
#define mkstemp(x) (-1) |
| 26 |
#define TEMP tmpnam (NULL) |
| 27 |
#define closefd(fd) |
| 28 |
#else |
| 29 |
#include <unistd.h> |
| 30 |
#define TEMP templ |
| 31 |
#define closefd(fd) close (fd) |
| 32 |
#endif |
| 33 |
|
| 34 |
/* If x is non-zero, print a usage message on stderr, and exit |
| 35 |
with a non-zero status. */ |
| 36 |
static void usage_if (int x) { |
| 37 |
if (x) { |
| 38 |
fprintf (stderr, |
| 39 |
"usage: run_tests [-p <N>] [-n <N>] [-bB] test...\n" |
| 40 |
" -p <N> runs <N> tests in parallel\n" |
| 41 |
" -n <N> runs <N> subtests in parallel\n" |
| 42 |
" -b benchmarks are run if tests pass\n" |
| 43 |
" -B only benchmarks are run\n" |
| 44 |
"Benchmarks are not run in parallel.\n"); |
| 45 |
exit (2); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/* Describe a run of a test */ |
| 50 |
struct test_run { |
| 51 |
char *name; /* test name (execuatble to run) */ |
| 52 |
char *cmd; /* command line with flags to be passed to popen() */ |
| 53 |
char *file; /* output file */ |
| 54 |
int fd; /* output file fd or -1 */ |
| 55 |
|
| 56 |
FILE *pfp; /* result of popen(); NULL when run is complete */ |
| 57 |
int status; /* result of pclose */ |
| 58 |
}; |
| 59 |
|
| 60 |
/* Return whether the file with nul-terminated name file[] can be read, and |
| 61 |
ends with a newline. Also returns true if file==NULL.*/ |
| 62 |
static int file_ready (const char *file) { |
| 63 |
int lastc = '\n'; |
| 64 |
if (file != NULL) { |
| 65 |
FILE *fp = fopen (file, "r"); |
| 66 |
lastc = -1; |
| 67 |
if (fp != NULL) { |
| 68 |
int c; |
| 69 |
lastc = '\n'; |
| 70 |
while ((c = getc (fp)) != EOF) { |
| 71 |
lastc = c; |
| 72 |
} |
| 73 |
fclose (fp); |
| 74 |
} |
| 75 |
} |
| 76 |
return (lastc == '\n'); |
| 77 |
} |
| 78 |
|
| 79 |
/* If file != NULL, output the contents of file with nul-terminated name file[] |
| 80 |
to ofp. Return 2 if I/O failed, and 0 otherwise. */ |
| 81 |
static int output (FILE *ofp, const char *file) { |
| 82 |
int rc = 0; |
| 83 |
if (file != NULL) { |
| 84 |
FILE *fp = fopen (file, "r"); |
| 85 |
if (fp == NULL) { |
| 86 |
perror (file); |
| 87 |
rc = 2; |
| 88 |
} else { |
| 89 |
int c; |
| 90 |
while ((c = getc (fp)) != EOF) { |
| 91 |
putc (c, ofp); |
| 92 |
} |
| 93 |
if (ferror (fp)) { |
| 94 |
perror (file); |
| 95 |
rc = 2; |
| 96 |
} |
| 97 |
if (fclose (fp) != 0) { |
| 98 |
perror (file); |
| 99 |
rc = 2; |
| 100 |
} |
| 101 |
} |
| 102 |
unlink (file); |
| 103 |
fflush (ofp); |
| 104 |
} |
| 105 |
return (rc); |
| 106 |
} |
| 107 |
|
| 108 |
/* Run the tests test[0, .., n-1], running as many as possible in parallel, |
| 109 |
Assumes that test[*].{name,cmd,file} are filled in, and other |
| 110 |
fields are uninitialized. |
| 111 |
On return, all fields are initialized. |
| 112 |
It is expected that all output has been directed to the file named by |
| 113 |
test[*].file[] (if parallelism > 1) or test[*].pfp (if parallelism==1). |
| 114 |
test[*].status is valid, and test[*].pfp == NULL. Any output from the |
| 115 |
commands is written to *ofp, and test[*].file[] is removed. |
| 116 |
Returns 0 on success, 2 if any I/O error occurred, and 1 if a test failed, |
| 117 |
and 3 if a test failed and there was an I/O error. |
| 118 |
|
| 119 |
Attempts to run "parallelism" tests in parallel. |
| 120 |
The parallelism is half-hearted as we avoid threads and |
| 121 |
non-blocking I/O for portability. */ |
| 122 |
static int run (FILE *ofp, int n, struct test_run *test, int parallelism) { |
| 123 |
int running = 0; |
| 124 |
int started = 0; |
| 125 |
int flushed = 0; |
| 126 |
int rc = 0; |
| 127 |
int i; |
| 128 |
while (flushed != n) { |
| 129 |
for (i = 0; i != started; i++) { |
| 130 |
if (test[i].pfp != NULL && |
| 131 |
file_ready (test[i].file)) { |
| 132 |
int c; |
| 133 |
int err; |
| 134 |
while ((c = getc (test[i].pfp)) != EOF) { |
| 135 |
putc (c, ofp); |
| 136 |
if (c == '\n' || c == ' ') { |
| 137 |
fflush (ofp); |
| 138 |
} |
| 139 |
} |
| 140 |
err = ferror (test[i].pfp); |
| 141 |
test[i].status = pclose (test[i].pfp); |
| 142 |
if (test[i].status != 0) { |
| 143 |
rc |= 1; |
| 144 |
} else if (err) { |
| 145 |
test[i].status = 1; |
| 146 |
rc |= 1; |
| 147 |
} |
| 148 |
test[i].pfp = NULL; |
| 149 |
running--; |
| 150 |
} |
| 151 |
} |
| 152 |
while (flushed != started && test[flushed].pfp == NULL) { |
| 153 |
rc |= output (ofp, test[flushed].file); |
| 154 |
flushed++; |
| 155 |
} |
| 156 |
while (running < parallelism && started != n) { |
| 157 |
test[started].pfp = popen (test[started].cmd, "r"); |
| 158 |
test[started].status = 2; |
| 159 |
running += (test[started].pfp != NULL); |
| 160 |
started++; |
| 161 |
} |
| 162 |
if (parallelism > 1) { |
| 163 |
sleep (1); |
| 164 |
} |
| 165 |
} |
| 166 |
if ((rc & 1) != 0) { |
| 167 |
fprintf (stderr, "failures:"); |
| 168 |
for (i = 0; i != n; i++) { |
| 169 |
if (test[i].status != 0) { |
| 170 |
fprintf (stderr, " %s\n", test[i].name); |
| 171 |
} |
| 172 |
} |
| 173 |
fprintf (stderr, "\n"); |
| 174 |
} |
| 175 |
return (rc); |
| 176 |
} |
| 177 |
|
| 178 |
/* For each test run test[0, .., n-1], test run, free its output file name and |
| 179 |
cmd string. */ |
| 180 |
static void cleanup (int n, struct test_run *test) { |
| 181 |
int i; |
| 182 |
for (i = 0; i != n; i++) { |
| 183 |
if (test[i].fd != -1) { |
| 184 |
closefd (test[i].fd); |
| 185 |
} |
| 186 |
if (test[i].file != NULL) { |
| 187 |
free (test[i].file); |
| 188 |
} |
| 189 |
free (test[i].cmd); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/* Run the tests in test[0, .., n-1] with the deisgnated parallism. |
| 194 |
Run Benchmarks instead of tests if is_benchmark != 0. |
| 195 |
On entry, only the test[*].name field is valid. |
| 196 |
Return a suitable exit code, 0 for success, and non-zero for failure. |
| 197 |
Output is written to *ofp. */ |
| 198 |
static int run_tests (FILE *ofp, int n, struct test_run *test, |
| 199 |
int parallelism, int subparallelism, int is_benchmark) { |
| 200 |
int i; |
| 201 |
int rc; |
| 202 |
for (i = 0; i != n; i++) { |
| 203 |
int cmd_len; |
| 204 |
char flags[64]; |
| 205 |
char *redirect; |
| 206 |
if (parallelism > 1) { |
| 207 |
int redirect_len; |
| 208 |
char templ[64]; |
| 209 |
snprintf (templ, sizeof (templ), |
| 210 |
"/tmp/run_tests.XXXXXXXXX"); |
| 211 |
test[i].fd = mkstemp (templ); |
| 212 |
test[i].file = strdup (TEMP); |
| 213 |
redirect_len = strlen (test[i].file) + 10; |
| 214 |
redirect = (char *) malloc (redirect_len); |
| 215 |
snprintf (redirect, redirect_len, "> \"%s\"", |
| 216 |
test[i].file); |
| 217 |
} else { |
| 218 |
test[i].file = NULL; |
| 219 |
test[i].fd = -1; |
| 220 |
redirect = strdup (""); |
| 221 |
} |
| 222 |
if (subparallelism == 1) { |
| 223 |
snprintf (flags, sizeof (flags), "%s", |
| 224 |
is_benchmark? "-B" : ""); |
| 225 |
} else { |
| 226 |
snprintf (flags, sizeof (flags), "%s -n %d", |
| 227 |
is_benchmark? "-B" : "", |
| 228 |
subparallelism); |
| 229 |
} |
| 230 |
cmd_len = strlen (test[i].name) + |
| 231 |
strlen (redirect) + strlen (flags) + 64; |
| 232 |
test[i].cmd = (char *) malloc (cmd_len); |
| 233 |
snprintf (test[i].cmd, cmd_len, "%s %s %s 2>&1", |
| 234 |
test[i].name, flags, redirect); |
| 235 |
free (redirect); |
| 236 |
} |
| 237 |
rc = run (ofp, n, test, parallelism); |
| 238 |
cleanup (n, test); |
| 239 |
return (rc); |
| 240 |
} |
| 241 |
|
| 242 |
int main (int argc, char *argv[]) { |
| 243 |
int do_run_benchmarks = 0; |
| 244 |
int do_run_tests = 1; |
| 245 |
int parallelism = 1; |
| 246 |
int subparallelism = 1; |
| 247 |
int test_n = 0; |
| 248 |
int test_max = 16; |
| 249 |
struct test_run *test = |
| 250 |
(struct test_run *) malloc (sizeof (test[0]) * test_max); |
| 251 |
int rc = 0; |
| 252 |
int argn; |
| 253 |
|
| 254 |
for (argn = 1; argn != argc; argn++) { |
| 255 |
const char *arg = argv[argn]; |
| 256 |
if (arg[0] == '-') { |
| 257 |
const char *f; |
| 258 |
for (f = &arg[1]; *f != 0; f++) { |
| 259 |
switch (*f) { |
| 260 |
case 'p': usage_if (argn+1 == argc); |
| 261 |
parallelism = atoi (argv[++argn]); |
| 262 |
break; |
| 263 |
case 'n': usage_if (argn+1 == argc); |
| 264 |
subparallelism = atoi (argv[++argn]); |
| 265 |
break; |
| 266 |
case 'b': do_run_benchmarks = 1; |
| 267 |
break; |
| 268 |
case 'B': do_run_benchmarks = 1; |
| 269 |
do_run_tests = 0; |
| 270 |
break; |
| 271 |
default: usage_if (1); |
| 272 |
} |
| 273 |
} |
| 274 |
} else { |
| 275 |
if (test_n == test_max) { |
| 276 |
test_max *= 2; |
| 277 |
test = (struct test_run *) realloc ( |
| 278 |
test, sizeof (test[0]) * test_max); |
| 279 |
} |
| 280 |
test[test_n++].name = argv[argn]; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
if (do_run_tests) { |
| 285 |
rc |= run_tests (stdout, test_n, test, |
| 286 |
parallelism, subparallelism, 0); |
| 287 |
} |
| 288 |
if (do_run_benchmarks && rc == 0) { |
| 289 |
rc |= run_tests (stdout, test_n, test, 1, 1, 1); |
| 290 |
} |
| 291 |
|
| 292 |
free (test); |
| 293 |
return (rc); |
| 294 |
} |