]> xenbits.xensource.com Git - libvirt.git/commitdiff
Test script helper for printing string differences
authorDaniel P. Berrange <berrange@redhat.com>
Fri, 18 Apr 2008 15:05:29 +0000 (15:05 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Fri, 18 Apr 2008 15:05:29 +0000 (15:05 +0000)
ChangeLog
tests/testutils.c
tests/testutils.h

index 6ea2f90f0ce16d4a90157bc083c46a931c874a22..f1821ad3fb89db2730e41429798f6c3dbf95529f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Apr 18 11:04:24 EST 2008 Daniel P. Berrange <berrange@redhat.com>
+
+       * tests/testutils.h, tests/testutils.c: Add virtTestDifference
+       for printing out trimmed string differences
+
 Fri Apr 18 11:24:24 CEST 2008 Jim Meyering <meyering@redhat.com>
 
        avoid compile error when <pthread.h> is absent
index 238c229a865f22bed3986a1c1b626a258fc8b91d..56f45fbc9717b92b3ffa133f609d8cb049b6a004 100644 (file)
@@ -19,6 +19,7 @@
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <unistd.h>
+#include <string.h>
 #include <fcntl.h>
 #include <limits.h>
 #include "testutils.h"
@@ -58,6 +59,12 @@ virtTestRun(const char *title, int nloops, int (*body)(const void *data), const
 {
     int i, ret = 0;
     double *ts = NULL;
+    static int counter = 0;
+
+    counter++;
+
+    fprintf(stderr, "%2d) %-65s ... ", counter, title);
+    fflush(stderr);
 
     if (nloops > 1 && (ts = calloc(nloops,
                                    sizeof(double)))==NULL)
@@ -76,12 +83,12 @@ virtTestRun(const char *title, int nloops, int (*body)(const void *data), const
         }
     }
     if (ret == 0 && ts)
-        fprintf(stderr, "%-50s ... OK     [%.5f ms]\n", title,
+        fprintf(stderr, "OK     [%.5f ms]\n",
                 virtTestCountAverage(ts, nloops));
     else if (ret == 0)
-        fprintf(stderr, "%-50s ... OK\n", title);
+        fprintf(stderr, "OK\n");
     else
-        fprintf(stderr, "%-50s ... FAILED\n", title);
+        fprintf(stderr, "FAILED\n");
 
     free(ts);
     return ret;
@@ -206,3 +213,57 @@ int virtTestCaptureProgramOutput(const char *const argv[],
         }
     }
 }
+
+
+/**
+ * @param stream: output stream write to differences to
+ * @param expect: expected output text
+ * @param actual: actual output text
+ *
+ * Display expected and actual output text, trimmed to
+ * first and last characters at which differences occur
+ */
+int virtTestDifference(FILE *stream,
+                       const char *expect,
+                       const char *actual)
+{
+    const char *expectStart = expect;
+    const char *expectEnd = expect + (strlen(expect)-1);
+    const char *actualStart = actual;
+    const char *actualEnd = actual + (strlen(actual)-1);
+
+    if (getenv("DEBUG_TESTS") == NULL)
+        return 0;
+
+    /* Skip to first character where they differ */
+    while (*expectStart && *actualStart &&
+           *actualStart == *expectStart) {
+        actualStart++;
+        expectStart++;
+    }
+
+    /* Work backwards to last character where they differ */
+    while (actualEnd > actualStart &&
+           expectEnd > expectStart &&
+           *actualEnd == *expectEnd) {
+        actualEnd--;
+        expectEnd--;
+    }
+
+    /* Show the trimmed differences */
+    fprintf(stream, "\nExpect [");
+    if ((expectEnd - expectStart + 1) &&
+        fwrite(expectStart, (expectEnd-expectStart+1), 1, stream) != 1)
+        return -1;
+    fprintf(stream, "]\n");
+    fprintf(stream, "Actual [");
+    if ((actualEnd - actualStart + 1) &&
+        fwrite(actualStart, (actualEnd-actualStart+1), 1, stream) != 1)
+        return -1;
+    fprintf(stream, "]\n");
+
+    /* Pad to line up with test name ... in virTestRun */
+    fprintf(stream, "                                                                      ... ");
+
+    return 0;
+}
index 44bbb4cab2c2541cbfffe5b418ce77d832c9dff1..b1cd22e13b93bd1d4ff2584db4d60055d3b22879 100644 (file)
@@ -32,6 +32,11 @@ extern "C" {
                                      char **buf,
                                      int buflen);
 
+
+    int virtTestDifference(FILE *stream,
+                           const char *expect,
+                           const char *actual);
+
 #ifdef __cplusplus
 }
 #endif