]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - test.c
use the hashtables in the IR
[xonotic/gmqcc.git] / test.c
diff --git a/test.c b/test.c
index b59826a4139e64c0a6370b9111322ae73040b2e9..00080689540bdcc1744c52132aa9226052803706 100644 (file)
--- a/test.c
+++ b/test.c
@@ -239,15 +239,15 @@ int task_pclose(FILE **handles) {
  *      this will result in the task failing.
  */
 typedef struct {
-    char *description;
-    char *failuremessage;
-    char *successmessage;
-    char *compileflags;
-    char *executeflags;
-    char *comparematch;
-    char *proceduretype;
-    char *sourcefile;
-    char *tempfilename;
+    char  *description;
+    char  *failuremessage;
+    char  *successmessage;
+    char  *compileflags;
+    char  *executeflags;
+    char  *proceduretype;
+    char  *sourcefile;
+    char  *tempfilename;
+    char **comparematch;
 } task_template_t;
 
 /*
@@ -267,7 +267,6 @@ bool task_template_generate(task_template_t *template, char tag, const char *fil
         case 'T': destval = &template->proceduretype;  break;
         case 'C': destval = &template->compileflags;   break;
         case 'E': destval = &template->executeflags;   break;
-        case 'M': destval = &template->comparematch;   break;
         case 'I': destval = &template->sourcefile;     break;
         default:
             con_printmsg(LVL_ERROR, __FILE__, __LINE__, "internal error",
@@ -363,7 +362,6 @@ bool task_template_parse(const char *file, task_template_t *template, FILE *fp)
             case 'T':
             case 'C':
             case 'E':
-            case 'M':
             case 'I':
                 if (data[1] != ':') {
                     con_printmsg(LVL_ERROR, file, line, "template parse error",
@@ -380,6 +378,35 @@ bool task_template_parse(const char *file, task_template_t *template, FILE *fp)
                 }
                 break;
             
+            /*
+             * Match requires it's own system since we allow multiple M's
+             * for multi-line matching.
+             */
+            case 'M':
+            {
+                char *value = &data[3];
+                if (data[1] != ':') {
+                    con_printmsg(LVL_ERROR, file, line, "template parse error",
+                        "expected `:` after `%c`",
+                        *data
+                    );
+                    goto failure;
+                }
+                
+                if (value && *value && (*value == ' ' || *value == '\t'))
+                    value++;
+    
+                /*
+                 * Value will contain a newline character at the end, we need to strip
+                 * this otherwise kaboom, seriously, kaboom :P
+                 */
+                *strrchr(value, '\n')='\0';
+                
+                vec_push(template->comparematch, util_strdup(value));
+                
+                break;
+            }
+            
             default:
                 con_printmsg(LVL_ERROR, file, line, "template parse error",
                     "invalid tag `%c`", *data
@@ -487,8 +514,8 @@ task_template_t *task_template_compile(const char *file, const char *dir) {
         goto success;
     } else if (!strcmp(template->proceduretype, "-execute")) {
         if (!template->executeflags) {
-            con_err("template compile error: %s missing `E:` tag (use `$null` for exclude)\n", file);
-            goto failure;
+            /* default to $null */
+            template->executeflags = util_strdup("$null");
         }
         if (!template->comparematch) {
             con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
@@ -525,16 +552,25 @@ void task_template_destroy(task_template_t **template) {
     if ((*template)->proceduretype)  mem_d((*template)->proceduretype);
     if ((*template)->compileflags)   mem_d((*template)->compileflags);
     if ((*template)->executeflags)   mem_d((*template)->executeflags);
-    if ((*template)->comparematch)   mem_d((*template)->comparematch);
     if ((*template)->sourcefile)     mem_d((*template)->sourcefile);
     
+    /*
+     * Delete all allocated string for task template then destroy the
+     * main vector.
+     */
+    {
+        size_t i = 0;
+        for (; i < vec_size((*template)->comparematch); i++)
+            mem_d((*template)->comparematch[i]);
+            
+        vec_free((*template)->comparematch);
+    }
+    
     /*
      * Nullify all the template members otherwise NULL comparision
      * checks will fail if template pointer is reused.
      */
     mem_d(*template);
-    task_template_nullify(*template);
-    *template = NULL;
 }
 
 /*
@@ -583,9 +619,12 @@ bool task_propogate(const char *curdir) {
          * We made it here, which concludes the file/directory is not
          * actually a directory, so it must be a file :)
          */
-        if (strstr(files->d_name, ".tmpl")) {
-            con_out("compiling task template: %s/%s\n", curdir, files->d_name);
+        if (strstr(files->d_name, ".tmpl") == &files->d_name[strlen(files->d_name) - (sizeof(".tmpl") - 1)]) {
             task_template_t *template = task_template_compile(files->d_name, curdir);
+            char             buf[4096]; /* one page should be enough */
+            task_t           task;
+            
+            util_debug("TEST", "compiling task template: %s/%s\n", curdir, files->d_name);
             if (!template) {
                 con_err("error compiling task template: %s\n", files->d_name);
                 success = false;
@@ -602,7 +641,6 @@ bool task_propogate(const char *curdir) {
              * which will be refered to with a handle in the task for
              * reading the data from the pipe.
              */
-            char     buf[4096]; /* one page should be enough */
             memset  (buf,0,sizeof(buf));
             snprintf(buf,  sizeof(buf), "%s %s/%s %s -o %s",
                 task_bins[TASK_COMPILE],
@@ -616,7 +654,6 @@ bool task_propogate(const char *curdir) {
              * The task template was compiled, now lets create a task from
              * the template data which has now been propogated.
              */
-            task_t task;
             task.template = template;
             if (!(task.runhandles = task_popen(buf, "r"))) {
                 con_err("error opening pipe to process for test: %s\n", template->description);
@@ -624,21 +661,27 @@ bool task_propogate(const char *curdir) {
                 continue;
             }
             
-            con_out("executing test: `%s` [%s]\n", template->description, buf);
+            util_debug("TEST", "executing test: `%s` [%s]\n", template->description, buf);
             
             /*
              * Open up some file desciptors for logging the stdout/stderr
              * to our own.
              */
             memset  (buf,0,sizeof(buf));
-            snprintf(buf,  sizeof(buf), "%s/%s.stdout", curdir, template->sourcefile);
+            snprintf(buf,  sizeof(buf), "%s.stdout", template->tempfilename);
             task.stdoutlogfile = util_strdup(buf);
-            task.stdoutlog     = fopen(buf, "w");
+            if (!(task.stdoutlog     = fopen(buf, "w"))) {
+                con_err("error opening %s for stdout\n", buf);
+                continue;
+            }
             
             memset  (buf,0,sizeof(buf));
-            snprintf(buf,  sizeof(buf), "%s/%s.stderr", curdir, template->sourcefile);
+            snprintf(buf,  sizeof(buf), "%s.stderr", template->tempfilename);
             task.stderrlogfile = util_strdup(buf);
-            task.stderrlog     = fopen(buf, "w");
+            if (!(task.stderrlog     = fopen(buf, "w"))) {
+                con_err("error opening %s for stderr\n", buf);
+                continue;
+            }
             
             vec_push(task_tasks, task);
         }
@@ -666,7 +709,7 @@ void task_cleanup(const char *curdir) {
             if (remove(buffer))
                 con_err("error removing temporary file: %s\n", buffer);
             else
-                con_out("removed temporary file: %s\n", buffer);
+                util_debug("TEST", "removed temporary file: %s\n", buffer);
         }
     }
     
@@ -694,7 +737,7 @@ void task_precleanup(const char *curdir) {
             if (remove(buffer))
                 con_err("error removing temporary file: %s\n", buffer);
             else
-                con_out("removed temporary file: %s\n", buffer);
+                util_debug("TEST", "removed temporary file: %s\n", buffer);
         }
     }
     
@@ -725,12 +768,12 @@ void task_destroy(const char *curdir) {
             if (remove(task_tasks[i].stdoutlogfile))
                 con_err("error removing stdout log file: %s\n", task_tasks[i].stdoutlogfile);
             else
-                con_out("removed stdout log file: %s\n", task_tasks[i].stdoutlogfile);
+                util_debug("TEST", "removed stdout log file: %s\n", task_tasks[i].stdoutlogfile);
             
             if (remove(task_tasks[i].stderrlogfile))
                 con_err("error removing stderr log file: %s\n", task_tasks[i].stderrlogfile);
             else
-                con_out("removed stderr log file: %s\n", task_tasks[i].stderrlogfile);
+                util_debug("TEST", "removed stderr log file: %s\n", task_tasks[i].stderrlogfile);
         }
         
         /* free util_strdup data for log files */
@@ -775,7 +818,7 @@ bool task_execute(task_template_t *template) {
         );
     }
     
-    con_out("executing qcvm: `%s` [%s]\n",
+    util_debug("TEST", "executing qcvm: `%s` [%s]\n",
         template->description,
         buffer
     );
@@ -789,37 +832,36 @@ bool task_execute(task_template_t *template) {
      * and handle accordingly.
      */
     {
-        char  *data  = NULL;
-        size_t size  = 0;
-        while (util_getline(&data, &size, execute) != EOF) {}
-        
-        if (!strcmp(data, "No main function found\n")) {
-            con_err("test failure: `%s` [%s] (No main function found)\n",
-                template->description,
-                (template->failuremessage) ?
-                template->failuremessage : "unknown"
-            );
-            pclose(execute);
-            return false;
+        char  *data    = NULL;
+        size_t size    = 0;
+        size_t compare = 0;
+        while (util_getline(&data, &size, execute) != EOF) {
+            if (!strcmp(data, "No main function found\n")) {
+                con_err("test failure: `%s` [%s] (No main function found)\n",
+                    template->description,
+                    (template->failuremessage) ?
+                    template->failuremessage : "unknown"
+                );
+                pclose(execute);
+                return false;
+            }
+            
+            /* 
+             * Trim newlines from data since they will just break our
+             * ability to properly validate matches.
+             */
+            if  (strrchr(data, '\n'))
+                *strrchr(data, '\n') = '\0';
+            
+            
+            /*
+             * We only care about the last line from the output for now
+             * implementing multi-line match is TODO.
+             */
+            success = !!!(strcmp(data, template->comparematch[compare++]));
         }
-        
-        /* 
-         * Trim newlines from data since they will just break our
-         * ability to properly validate matches.
-         */
-        if  (strrchr(data, '\n'))
-            *strrchr(data, '\n') = '\0';
-        
-        /* null list */
-        if (!strcmp(template->comparematch, "$null"))
-            success = true;
-        
-        /*
-         * We only care about the last line from the output for now
-         * implementing multi-line match is TODO.
-         */
-        if (!strcmp(data, template->comparematch))
-            success = true;
+        mem_d(data);
+        data = NULL;
     }
     pclose(execute);
     return success;
@@ -831,18 +873,17 @@ bool task_execute(task_template_t *template) {
  * execution this takes more work since a task needs to be generated
  * from thin air and executed INLINE.
  */
-void task_schedualize(const char *curdir) {
+void task_schedualize() {
     bool   execute  = false;
-    char  *back     = NULL;
     char  *data     = NULL;
     size_t size     = 0;
     size_t i;
     
     for (i = 0; i < vec_size(task_tasks); i++) {
         /*
-        * Generate a task from thin air if it requires execution in
-        * the QCVM.
-        */
+         * Generate a task from thin air if it requires execution in
+         * the QCVM.
+         */
         if (!strcmp(task_tasks[i].template->proceduretype, "-execute"))
             execute = true;
             
@@ -857,12 +898,10 @@ void task_schedualize(const char *curdir) {
          * then we do the same for stderr.
          */    
         while (util_getline(&data, &size, task_tasks[i].runhandles[1]) != EOF) {
-            back = data;
             fputs(data, task_tasks[i].stdoutlog);
             fflush(task_tasks[i].stdoutlog);
         }
         while (util_getline(&data, &size, task_tasks[i].runhandles[2]) != EOF) {
-            back = data;
             /*
              * If a string contains an error we just dissalow execution
              * of it in the vm.
@@ -880,9 +919,6 @@ void task_schedualize(const char *curdir) {
             fflush(task_tasks[i].stdoutlog);
         }
         
-        if (back)
-            mem_d(back);
-        
         /*
          * If we can execute we do so after all data has been read and
          * this paticular task has coupled execution in its procedure type
@@ -895,10 +931,12 @@ void task_schedualize(const char *curdir) {
          * in the virtual machine.
          */
         if (!task_execute(task_tasks[i].template)) {
-            con_err("test failure: `%s` [%s]\n",
+            con_err("test failure: `%s` [%s] see %s.stdout and %s.stderr\n",
                 task_tasks[i].template->description,
                 (task_tasks[i].template->failuremessage) ?
-                task_tasks[i].template->failuremessage : "unknown"
+                task_tasks[i].template->failuremessage : "unknown",
+                task_tasks[i].template->tempfilename,
+                task_tasks[i].template->tempfilename
             );
             continue;
         }
@@ -909,8 +947,7 @@ void task_schedualize(const char *curdir) {
             task_tasks[i].template->successmessage  : "unknown"
         );
     }
-    if (back)
-        mem_d(back);
+    mem_d(data);
 }
 
 /*
@@ -941,7 +978,7 @@ bool test_perform(const char *curdir) {
      * it's designed to prevent lock contention, and possible syncronization
      * issues.
      */
-    task_schedualize(curdir);
+    task_schedualize();
     task_destroy(curdir);
     
     return true;