16 static const char *task_bins[] = {
29 static FILE **task_popen(const char *command, const char *mode) {
35 popen_t *data = (popen_t*)mem_a(sizeof(popen_t));
37 char *line = (char*)command;
38 std::vector<char *> argv;
41 while (*line != '\0') {
42 while (*line == ' ' || *line == '\t' || *line == '\n')
45 while (*line != '\0' && *line != ' ' &&
46 *line != '\t' && *line != '\n') line++;
48 argv.push_back((char *)0);
51 if ((trypipe = pipe(inhandle)) < 0) goto task_popen_error_0;
52 if ((trypipe = pipe(outhandle)) < 0) goto task_popen_error_1;
53 if ((trypipe = pipe(errhandle)) < 0) goto task_popen_error_2;
55 if ((data->pid = fork()) > 0) {
60 data->pipes[0] = inhandle [1];
61 data->pipes[1] = outhandle[0];
62 data->pipes[2] = errhandle[0];
63 data->handles[0] = fdopen(inhandle [1], "w");
64 data->handles[1] = fdopen(outhandle[0], mode);
65 data->handles[2] = fdopen(errhandle[0], mode);
67 } else if (data->pid == 0) {
73 /* see piping documentation for this sillyness :P */
74 dup2(inhandle [0], 0);
75 dup2(outhandle[1], 1);
76 dup2(errhandle[1], 2);
78 execvp(argv[0], &argv[0]);
82 goto task_popen_error_3;
85 task_popen_error_3: close(errhandle[0]), close(errhandle[1]);
86 task_popen_error_2: close(outhandle[0]), close(outhandle[1]);
87 task_popen_error_1: close(inhandle [0]), close(inhandle [1]);
93 static int task_pclose(FILE **handles) {
94 popen_t *data = (popen_t*)handles;
97 close(data->pipes[0]); /* stdin */
98 close(data->pipes[1]); /* stdout */
99 close(data->pipes[2]); /* stderr */
101 if (data->pid != waitpid(data->pid, &status, 0)) {
104 if (!WIFEXITED(status))
106 if (WIFSIGNALED(status))
107 con_out("got signaled!\n");
111 return status ? 1 : 0;
114 #define TASK_COMPILE 0
115 #define TASK_EXECUTE 1
117 * Task template system:
118 * templates are rules for a specific test, used to create a "task" that
119 * is executed with those set of rules (arguments, and what not). Tests
120 * that don't have a template with them cannot become tasks, since without
121 * the information for that test there is no way to properly "test" them.
122 * Rules for these templates are described in a template file, using a
123 * task template language.
125 * The language is a basic finite statemachine, top-down single-line
126 * description language.
128 * The languge is composed entierly of "tags" which describe a string of
129 * text for a task. Think of it much like a configuration file. Except
130 * it's been designed to allow flexibility and future support for prodecual
133 * The following "tags" are suported by the language
136 * Used to set a description of the current test, this must be
137 * provided, this tag is NOT optional.
140 * Used to set the procedure for the given task, there are two
143 * This simply performs compilation only
145 * This will perform compilation and execution
147 * This will perform compilation, but requires
148 * the compilation to fail in order to succeed.
150 * This must be provided, this tag is NOT optional.
153 * Used to set the compilation flags for the given task, this
154 * must be provided, this tag is NOT optional.
156 * F: Used to set some test suite flags, currently the only option
157 * is -no-defs (to including of defs.qh)
160 * Used to set the execution flags for the given task. This tag
161 * must be provided if T == -execute, otherwise it's erroneous
162 * as compilation only takes place.
165 * Used to describe a string of text that should be matched from
166 * the output of executing the task. If this doesn't match the
167 * task fails. This tag must be provided if T == -execute, otherwise
168 * it's erroneous as compilation only takes place.
171 * Used to specify the INPUT source file to operate on, this must be
172 * provided, this tag is NOT optional
176 * These tags have one-time use, using them more than once will result
177 * in template compilation errors.
179 * Lines beginning with # or // in the template file are comments and
180 * are ignored by the template parser.
182 * Whitespace is optional, with exception to the colon ':' between the
183 * tag and it's assignment value/
185 * The template compiler will detect erronrous tags (optional tags
186 * that need not be set), as well as missing tags, and error accordingly
187 * this will result in the task failing.
189 struct task_template_t {
196 std::vector<char *> comparematch;
202 * This is very much like a compiler code generator :-). This generates
203 * a value from some data observed from the compiler.
205 static bool task_template_generate(task_template_t *tmpl, char tag, const char *file, size_t line, char *value, size_t *pad) {
208 char **destval = nullptr;
214 case 'D': destval = &tmpl->description; break;
215 case 'T': destval = &tmpl->proceduretype; break;
216 case 'C': destval = &tmpl->compileflags; break;
217 case 'E': destval = &tmpl->executeflags; break;
218 case 'I': destval = &tmpl->sourcefile; break;
219 case 'F': destval = &tmpl->testflags; break;
221 con_printmsg(LVL_ERROR, __FILE__, __LINE__, 0, "internal error",
222 "invalid tag `%c:` during code generation\n",
229 * Ensure if for the given tag, there already exists a
233 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "compile error",
234 "tag `%c:` already assigned value: %s\n",
241 * Strip any whitespace that might exist in the value for assignments
244 if (value && *value && (*value == ' ' || *value == '\t'))
250 * Value will contain a newline character at the end, we need to strip
251 * this otherwise kaboom, seriously, kaboom :P
253 if (strchr(value, '\n'))
254 *strrchr(value, '\n')='\0';
257 * Now allocate and set the actual value for the specific tag. Which
258 * was properly selected and can be accessed with *destval.
260 *destval = util_strdup(value);
263 if (*destval == tmpl->description) {
265 * Create some padding for the description to align the
266 * printing of the rules file.
268 if ((desclen = strlen(tmpl->description)) > pad[0])
272 if ((filelen = strlen(file)) > pad[2])
278 static bool task_template_parse(const char *file, task_template_t *tmpl, FILE *fp, size_t *pad) {
279 char *data = nullptr;
280 char *back = nullptr;
287 /* top down parsing */
288 while (util_getline(&back, &size, fp) != EOF) {
289 /* skip whitespace */
291 if (*data && (*data == ' ' || *data == '\t'))
296 * Handle comments inside task tmpl files. We're strict
297 * about the language for fun :-)
300 if (data[1] != '/') {
301 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
302 "invalid character `/`, perhaps you meant `//` ?");
311 * Empty newlines are acceptable as well, so we handle that here
312 * despite being just odd since there should't be that many
313 * empty lines to begin with.
321 * Now begin the actual "tag" stuff. This works as you expect
330 if (data[1] != ':') {
331 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
332 "expected `:` after `%c`",
337 if (!task_template_generate(tmpl, *data, file, line, &data[3], pad)) {
338 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl compile error",
339 "failed to generate for given task\n"
346 * Match requires it's own system since we allow multiple M's
347 * for multi-line matching.
351 char *value = &data[3];
352 if (data[1] != ':') {
353 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
354 "expected `:` after `%c`",
361 * Value will contain a newline character at the end, we need to strip
362 * this otherwise kaboom, seriously, kaboom :P
364 if (strrchr(value, '\n'))
365 *strrchr(value, '\n')='\0';
366 else /* cppcheck: possible null pointer dereference */
369 tmpl->comparematch.push_back(util_strdup(value));
375 con_printmsg(LVL_ERROR, file, line, 0, /*TODO: column for match*/ "tmpl parse error",
376 "invalid tag `%c`", *data
379 /* no break required */
382 /* update line and free old sata */
397 * Nullifies the template data: used during initialization of a new
400 static void task_template_nullify(task_template_t *tmpl) {
404 tmpl->description = nullptr;
405 tmpl->proceduretype = nullptr;
406 tmpl->compileflags = nullptr;
407 tmpl->executeflags = nullptr;
408 tmpl->sourcefile = nullptr;
409 tmpl->tempfilename = nullptr;
410 tmpl->rulesfile = nullptr;
411 tmpl->testflags = nullptr;
414 static task_template_t *task_template_compile(const char *file, const char *dir, size_t *pad) {
415 /* a page should be enough */
418 FILE *tempfile = nullptr;
419 task_template_t *tmpl = nullptr;
421 util_snprintf(fullfile, sizeof(fullfile), "%s/%s", dir, file);
423 tempfile = fopen(fullfile, "r");
424 tmpl = (task_template_t*)mem_a(sizeof(task_template_t));
425 new (tmpl) task_template_t();
426 task_template_nullify(tmpl);
429 * Create some padding for the printing to align the
430 * printing of the rules file to the console.
432 if ((filepadd = strlen(fullfile)) > pad[1])
435 tmpl->rulesfile = util_strdup(fullfile);
438 * Esnure the file even exists for the task, this is pretty useless
442 con_err("template file: %s does not exist or invalid permissions\n",
448 if (!task_template_parse(file, tmpl, tempfile, pad)) {
449 con_err("template parse error: error during parsing\n");
454 * Regardless procedure type, the following tags must exist:
460 if (!tmpl->description) {
461 con_err("template compile error: %s missing `D:` tag\n", file);
464 if (!tmpl->proceduretype) {
465 con_err("template compile error: %s missing `T:` tag\n", file);
468 if (!tmpl->compileflags) {
469 con_err("template compile error: %s missing `C:` tag\n", file);
472 if (!tmpl->sourcefile) {
473 con_err("template compile error: %s missing `I:` tag\n", file);
478 * Now lets compile the template, compilation is really just
479 * the process of validating the input.
481 if (!strcmp(tmpl->proceduretype, "-compile")) {
482 if (tmpl->executeflags)
483 con_err("template compile warning: %s erroneous tag `E:` when only compiling\n", file);
484 if (tmpl->comparematch.size())
485 con_err("template compile warning: %s erroneous tag `M:` when only compiling\n", file);
487 } else if (!strcmp(tmpl->proceduretype, "-execute")) {
488 if (!tmpl->executeflags) {
489 /* default to $null */
490 tmpl->executeflags = util_strdup("$null");
492 if (tmpl->comparematch.empty()) {
493 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
496 } else if (!strcmp(tmpl->proceduretype, "-fail")) {
497 if (tmpl->executeflags)
498 con_err("template compile warning: %s erroneous tag `E:` when only failing\n", file);
499 if (tmpl->comparematch.size())
500 con_err("template compile warning: %s erroneous tag `M:` when only failing\n", file);
501 } else if (!strcmp(tmpl->proceduretype, "-diagnostic")) {
502 if (tmpl->executeflags)
503 con_err("template compile warning: %s erroneous tag `E:` when only diagnostic\n", file);
504 if (tmpl->comparematch.empty()) {
505 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
508 } else if (!strcmp(tmpl->proceduretype, "-pp")) {
509 if (tmpl->executeflags)
510 con_err("template compile warning: %s erroneous tag `E:` when only preprocessing\n", file);
511 if (tmpl->comparematch.empty()) {
512 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
516 con_err("template compile error: %s invalid procedure type: %s\n", file, tmpl->proceduretype);
526 * The file might not exist and we jump here when that doesn't happen
527 * so the check to see if it's not null here is required.
536 static void task_template_destroy(task_template_t *tmpl) {
540 if (tmpl->description) mem_d(tmpl->description);
541 if (tmpl->proceduretype) mem_d(tmpl->proceduretype);
542 if (tmpl->compileflags) mem_d(tmpl->compileflags);
543 if (tmpl->executeflags) mem_d(tmpl->executeflags);
544 if (tmpl->sourcefile) mem_d(tmpl->sourcefile);
545 if (tmpl->rulesfile) mem_d(tmpl->rulesfile);
546 if (tmpl->testflags) mem_d(tmpl->testflags);
549 for (auto &it : tmpl->comparematch)
553 * Nullify all the template members otherwise nullptr comparision
554 * checks will fail if tmpl pointer is reused.
556 mem_d(tmpl->tempfilename);
561 * Now comes the task manager, this system allows adding tasks in and out
562 * of a task list. This is the executor of the tasks essentially as well.
565 task_template_t *tmpl;
574 static std::vector<task_t> task_tasks;
577 * Read a directory and searches for all template files in it
578 * which is later used to run all tests.
580 static bool task_propagate(const char *curdir, size_t *pad, const char *defs) {
583 struct dirent *files;
584 struct stat directory;
587 std::vector<char *> directories;
588 char *claim = util_strdup(curdir);
590 directories.push_back(claim);
591 dir = opendir(claim);
594 * Generate a list of subdirectories since we'll be checking them too
597 while ((files = readdir(dir))) {
598 util_asprintf(&claim, "%s/%s", curdir, files->d_name);
599 if (stat(claim, &directory) == -1) {
605 if (S_ISDIR(directory.st_mode) && files->d_name[0] != '.') {
606 directories.push_back(claim);
615 * Now do all the work, by touching all the directories inside
616 * test as well and compile the task templates into data we can
617 * use to run the tests.
619 for (auto &it : directories) {
621 while ((files = readdir(dir))) {
622 util_snprintf(buffer, sizeof(buffer), "%s/%s", it, files->d_name);
623 if (stat(buffer, &directory) == -1) {
624 con_err("internal error: stat failed, aborting\n");
628 if (S_ISDIR(directory.st_mode))
632 * We made it here, which concludes the file/directory is not
633 * actually a directory, so it must be a file :)
635 if (strcmp(files->d_name + strlen(files->d_name) - 5, ".tmpl") == 0) {
636 task_template_t *tmpl = task_template_compile(files->d_name, it, pad);
637 char buf[4096]; /* one page should be enough */
638 const char *qcflags = nullptr;
641 memset(&task, 0, sizeof(task));
645 con_err("error compiling task template: %s\n", files->d_name);
650 * Generate a temportary file name for the output binary
651 * so we don't trample over an existing one.
653 tmpl->tempfilename = nullptr;
654 util_asprintf(&tmpl->tempfilename, "%s/TMPDAT.%s.dat", it, files->d_name);
657 * Additional QCFLAGS enviroment variable may be used
658 * to test compile flags for all tests. This needs to be
659 * BEFORE other flags (so that the .tmpl can override them)
661 qcflags = getenv("QCFLAGS");
664 * Generate the command required to open a pipe to a process
665 * which will be refered to with a handle in the task for
666 * reading the data from the pipe.
668 if (strcmp(tmpl->proceduretype, "-pp")) {
670 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
671 util_snprintf(buf, sizeof(buf), "%s %s/%s %s %s -o %s",
672 task_bins[TASK_COMPILE],
680 util_snprintf(buf, sizeof(buf), "%s %s/%s %s/%s %s %s -o %s",
681 task_bins[TASK_COMPILE],
692 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
693 util_snprintf(buf, sizeof(buf), "%s %s/%s %s -o %s",
694 task_bins[TASK_COMPILE],
701 util_snprintf(buf, sizeof(buf), "%s %s/%s %s/%s %s -o %s",
702 task_bins[TASK_COMPILE],
713 /* Preprocessing (qcflags mean shit all here we don't allow them) */
714 if (tmpl->testflags && !strcmp(tmpl->testflags, "-no-defs")) {
715 util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s -o %s",
716 task_bins[TASK_COMPILE],
723 util_snprintf(buf, sizeof(buf), "%s -E %s/%s %s/%s %s -o %s",
724 task_bins[TASK_COMPILE],
736 * The task template was compiled, now lets create a task from
737 * the template data which has now been propagated.
740 if (!(task.runhandles = task_popen(buf, "r"))) {
741 con_err("error opening pipe to process for test: %s\n", tmpl->description);
747 * Open up some file desciptors for logging the stdout/stderr
750 util_snprintf(buf, sizeof(buf), "%s.stdout", tmpl->tempfilename);
751 task.stdoutlogfile = util_strdup(buf);
752 if (!(task.stdoutlog = fopen(buf, "w"))) {
753 con_err("error opening %s for stdout\n", buf);
757 util_snprintf(buf, sizeof(buf), "%s.stderr", tmpl->tempfilename);
758 task.stderrlogfile = util_strdup(buf);
759 if (!(task.stderrlog = fopen(buf, "w"))) {
760 con_err("error opening %s for stderr\n", buf);
763 task_tasks.push_back(task);
767 mem_d(it); /* free claimed memory */
773 * Task precleanup removes any existing temporary files or log files
774 * left behind from a previous invoke of the test-suite.
776 static void task_precleanup(const char *curdir) {
778 struct dirent *files;
781 dir = opendir(curdir);
783 while ((files = readdir(dir))) {
784 if (strstr(files->d_name, "TMP") ||
785 strstr(files->d_name, ".stdout") ||
786 strstr(files->d_name, ".stderr") ||
787 strstr(files->d_name, ".dat"))
789 util_snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name);
791 con_err("error removing temporary file: %s\n", buffer);
798 static void task_destroy(void) {
800 * Free all the data in the task list and finally the list itself
801 * then proceed to cleanup anything else outside the program like
804 for (auto &it : task_tasks) {
806 * Close any open handles to files or processes here. It's mighty
807 * annoying to have to do all this cleanup work.
809 if (it.stdoutlog) fclose(it.stdoutlog);
810 if (it.stderrlog) fclose(it.stderrlog);
813 * Only remove the log files if the test actually compiled otherwise
814 * forget about it (or if it didn't compile, and the procedure type
815 * was set to -fail (meaning it shouldn't compile) .. stil remove)
817 if (it.compiled || !strcmp(it.tmpl->proceduretype, "-fail")) {
818 if (remove(it.stdoutlogfile))
819 con_err("error removing stdout log file: %s\n", it.stdoutlogfile);
820 if (remove(it.stderrlogfile))
821 con_err("error removing stderr log file: %s\n", it.stderrlogfile);
823 (void)!remove(it.tmpl->tempfilename);
826 /* free util_strdup data for log files */
827 mem_d(it.stdoutlogfile);
828 mem_d(it.stderrlogfile);
830 task_template_destroy(it.tmpl);
835 * This executes the QCVM task for a specificly compiled progs.dat
836 * using the template passed into it for call-flags and user defined
837 * messages IF the procedure type is -execute, otherwise it matches
838 * the preprocessor output.
840 static bool task_trymatch(task_t &task, std::vector<char *> &line) {
843 int retval = EXIT_SUCCESS;
846 task_template_t *tmpl = task.tmpl;
848 memset(buffer,0,sizeof(buffer));
850 if (!strcmp(tmpl->proceduretype, "-execute")) {
852 * Drop the execution flags for the QCVM if none where
853 * actually specified.
855 if (!strcmp(tmpl->executeflags, "$null")) {
856 util_snprintf(buffer, sizeof(buffer), "%s %s",
857 task_bins[TASK_EXECUTE],
861 util_snprintf(buffer, sizeof(buffer), "%s %s %s",
862 task_bins[TASK_EXECUTE],
868 execute = popen(buffer, "r");
871 } else if (!strcmp(tmpl->proceduretype, "-pp")) {
873 * we're preprocessing, which means we need to read int
874 * the produced file and do some really weird shit.
876 if (!(execute = fopen(tmpl->tempfilename, "r")))
881 * we're testing diagnostic output, which means it will be
882 * in runhandles[2] (stderr) since that is where the compiler
885 if (!(execute = fopen(task.stderrlogfile, "r")))
891 * Now lets read the lines and compare them to the matches we expect
892 * and handle accordingly.
895 char *data = nullptr;
899 while (util_getline(&data, &size, execute) != EOF) {
900 if (!strcmp(data, "No main function found\n")) {
901 con_err("test failure: `%s` (No main function found) [%s]\n",
908 pclose((FILE*)execute);
913 * Trim newlines from data since they will just break our
914 * ability to properly validate matches.
916 if (strrchr(data, '\n'))
917 *strrchr(data, '\n') = '\0';
920 * We remove the file/directory and stuff from the error
921 * match messages when testing diagnostics.
923 if(!strcmp(tmpl->proceduretype, "-diagnostic")) {
924 if (strstr(data, "there have been errors, bailing out"))
925 continue; /* ignore it */
926 if (strstr(data, ": error: ")) {
927 char *claim = util_strdup(data + (strstr(data, ": error: ") - data) + 9);
934 * We need to ignore null lines for when -pp is used (preprocessor), since
935 * the preprocessor is likely to create empty newlines in certain macro
936 * instantations, otherwise it's in the wrong nature to ignore empty newlines.
938 if (!strcmp(tmpl->proceduretype, "-pp") && !*data)
941 if (tmpl->comparematch.size() > compare) {
942 if (strcmp(data, tmpl->comparematch[compare++])) {
949 line.push_back(data);
956 if (compare != tmpl->comparematch.size())
964 retval = pclose((FILE*)execute);
968 return success && retval == EXIT_SUCCESS;
971 static const char *task_type(task_template_t *tmpl) {
972 if (!strcmp(tmpl->proceduretype, "-pp"))
973 return "type: preprocessor";
974 if (!strcmp(tmpl->proceduretype, "-execute"))
975 return "type: execution";
976 if (!strcmp(tmpl->proceduretype, "-compile"))
977 return "type: compile";
978 if (!strcmp(tmpl->proceduretype, "-diagnostic"))
979 return "type: diagnostic";
984 * This schedualizes all tasks and actually runs them individually
985 * this is generally easy for just -compile variants. For compile and
986 * execution this takes more work since a task needs to be generated
987 * from thin air and executed INLINE.
990 static size_t task_schedualize(size_t *pad) {
992 bool execute = false;
993 char *data = nullptr;
994 std::vector<char *> match;
1000 util_snprintf(space[0], sizeof(space[0]), "%d", (int)task_tasks.size());
1002 for (auto &it : task_tasks) {
1004 memset(space[1], 0, sizeof(space[1]));
1005 util_snprintf(space[1], sizeof(space[1]), "%d", (int)(i));
1007 con_out("test #%u %*s", i, strlen(space[0]) - strlen(space[1]), "");
1008 //con_out("[[%*s]]",
1009 // (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1010 // it.tmpl->rulesfile);
1014 * Generate a task from thin air if it requires execution in
1018 /* diagnostic is not executed, but compare tested instead, like preproessor */
1019 execute = !! (!strcmp(it.tmpl->proceduretype, "-execute")) ||
1020 (!strcmp(it.tmpl->proceduretype, "-pp")) ||
1021 (!strcmp(it.tmpl->proceduretype, "-diagnostic"));
1024 * We assume it compiled before we actually compiled :). On error
1025 * we change the value
1030 * Read data from stdout first and pipe that stuff into a log file
1031 * then we do the same for stderr.
1033 while (util_getline(&data, &size, it.runhandles[1]) != EOF) {
1034 fputs(data, it.stdoutlog);
1036 if (strstr(data, "failed to open file")) {
1037 it.compiled = false;
1041 while (util_getline(&data, &size, it.runhandles[2]) != EOF) {
1043 * If a string contains an error we just dissalow execution
1046 * TODO: make this more percise, e.g if we print a warning
1047 * that refers to a variable named error, or something like
1048 * that .. then this will blowup :P
1050 if (strstr(data, "error") && strcmp(it.tmpl->proceduretype, "-diagnostic")) {
1052 it.compiled = false;
1055 fputs(data, it.stderrlog);
1056 fflush(it.stderrlog); /* fast flush for read */
1059 if (!it.compiled && strcmp(it.tmpl->proceduretype, "-fail")) {
1060 con_out("failure: `%s` %*s %*s\n",
1061 it.tmpl->description,
1062 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1064 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen("(failed to compile)") - pad[2]),
1065 "(failed to compile)"
1071 status = task_pclose(it.runhandles);
1072 if (status != 0 && status != 1) {
1073 con_out("compiler failure (returned: %i): `%s` %*s\n",
1075 it.tmpl->description,
1076 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1082 if ((!strcmp(it.tmpl->proceduretype, "-fail") && status == EXIT_SUCCESS)
1083 || ( strcmp(it.tmpl->proceduretype, "-fail") && status == EXIT_FAILURE)) {
1084 con_out("failure: `%s` %*s %*s\n",
1085 it.tmpl->description,
1086 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1088 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen("(compiler didn't return exit success)") - pad[2]),
1089 "(compiler didn't return exit success)"
1096 con_out("succeeded: `%s` %*s %*s\n",
1097 it.tmpl->description,
1098 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1100 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen(task_type(it.tmpl)) - pad[2]),
1108 * If we made it here that concludes the task is to be executed
1109 * in the virtual machine (or the preprocessor output needs to
1112 if (!task_trymatch(it, match)) {
1115 con_out("failure: `%s` %*s %*s\n",
1116 it.tmpl->description,
1117 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1119 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen(
1120 (strcmp(it.tmpl->proceduretype, "-pp"))
1121 ? "(invalid results from execution)"
1122 : (strcmp(it.tmpl->proceduretype, "-diagnostic"))
1123 ? "(invalid results from preprocessing)"
1124 : "(invalid results from compiler diagnsotics)"
1126 (strcmp(it.tmpl->proceduretype, "-pp"))
1127 ? "(invalid results from execution)"
1128 : (strcmp(it.tmpl->proceduretype, "-diagnostic"))
1129 ? "(invalid results from preprocessing)"
1130 : "(invalid results from compiler diagnsotics)"
1134 * Print nicely formatted expected match lists to console error
1135 * handler for the all the given matches in the template file and
1136 * what was actually returned from executing.
1138 con_out(" Expected From %u Matches: (got %u Matches)\n",
1139 it.tmpl->comparematch.size(),
1142 for (; d < it.tmpl->comparematch.size(); d++) {
1143 char *select = it.tmpl->comparematch[d];
1144 size_t length = 60 - strlen(select);
1145 con_out(" Expected: \"%s\"", select);
1148 con_out("| Got: \"%s\"\n", (d >= match.size()) ? "<<nothing else to compare>>" : match[d]);
1152 * Print the non-expected out (since we are simply not expecting it)
1153 * This will help track down bugs in template files that fail to match
1156 if (match.size() > it.tmpl->comparematch.size()) {
1157 for (d = 0; d < match.size() - it.tmpl->comparematch.size(); d++) {
1158 con_out(" Expected: Nothing | Got: \"%s\"\n",
1159 match[d + it.tmpl->comparematch.size()]
1164 for (auto &it : match)
1171 for (auto &it : match)
1175 con_out("succeeded: `%s` %*s %*s\n",
1176 it.tmpl->description,
1177 (pad[0] + pad[1] - strlen(it.tmpl->description)) + (strlen(it.tmpl->rulesfile) - pad[1]),
1179 (pad[1] + pad[2] - strlen(it.tmpl->rulesfile)) + (strlen(task_type(it.tmpl))- pad[2]),
1189 * This is the heart of the whole test-suite process. This cleans up
1190 * any existing temporary files left behind as well as log files left
1191 * behind. Then it propagates a list of tests from `curdir` by scaning
1192 * it for template files and compiling them into tasks, in which it
1193 * schedualizes them (executes them) and actually reports errors and
1194 * what not. It then proceeds to destroy the tasks and return memory
1195 * it's the engine :)
1197 * It returns true of tests could be propagated, otherwise it returns
1200 * It expects con_init() was called before hand.
1202 static GMQCC_WARN bool test_perform(const char *curdir, const char *defs) {
1203 size_t failed = false;
1204 static const char *default_defs = "defs.qh";
1207 /* test ### [succeed/fail]: `description` [tests/template.tmpl] [type] */
1212 * If the default definition file isn't set to anything. We will
1213 * use the default_defs here, which is "defs.qc"
1216 defs = default_defs;
1220 task_precleanup(curdir);
1221 if (!task_propagate(curdir, pad, defs)) {
1222 con_err("error: failed to propagate tasks\n");
1227 * If we made it here all tasks where propagated from their resultant
1228 * template file. So we can start the FILO scheduler, this has been
1229 * designed in the most thread-safe way possible for future threading
1230 * it's designed to prevent lock contention, and possible syncronization
1233 failed = task_schedualize(pad);
1235 con_out("%u out of %u tests failed\n", failed, task_tasks.size());
1238 return (failed) ? false : true;
1242 * Fancy GCC-like LONG parsing allows things like --opt=param with
1243 * assignment operator. This is used for redirecting stdout/stderr
1244 * console to specific files of your choice.
1246 static bool parsecmd(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
1248 char **argv = *argv_;
1250 size_t len = strlen(optname);
1252 if (strncmp(argv[0]+ds, optname, len))
1255 /* it's --optname, check how the parameter is supplied */
1256 if (argv[0][ds+len] == '=') {
1257 *out = argv[0]+ds+len+1;
1261 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
1264 /* using --opt param */
1271 int main(int argc, char **argv) {
1272 bool succeed = false;
1273 char *defs = nullptr;
1278 * Command line option parsing commences now We only need to support
1279 * a few things in the test suite.
1285 if (argv[0][0] == '-') {
1286 if (parsecmd("defs", &argc, &argv, &defs, 1, false))
1289 if (!strcmp(argv[0]+1, "debug")) {
1290 OPTS_OPTION_BOOL(OPTION_DEBUG) = true;
1293 if (!strcmp(argv[0]+1, "nocolor")) {
1298 con_err("invalid argument %s\n", argv[0]+1);
1302 succeed = test_perform("tests", defs);
1304 return (succeed) ? EXIT_SUCCESS : EXIT_FAILURE;