From: Daniel P. Berrange Date: Fri, 18 Apr 2008 15:05:29 +0000 (+0000) Subject: Test script helper for printing string differences X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=484559148d64724a1caa0ed49446333f94626e1d;p=libvirt.git Test script helper for printing string differences --- diff --git a/ChangeLog b/ChangeLog index 6ea2f90f0c..f1821ad3fb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Apr 18 11:04:24 EST 2008 Daniel P. Berrange + + * tests/testutils.h, tests/testutils.c: Add virtTestDifference + for printing out trimmed string differences + Fri Apr 18 11:24:24 CEST 2008 Jim Meyering avoid compile error when is absent diff --git a/tests/testutils.c b/tests/testutils.c index 238c229a86..56f45fbc97 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #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; +} diff --git a/tests/testutils.h b/tests/testutils.h index 44bbb4cab2..b1cd22e13b 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -32,6 +32,11 @@ extern "C" { char **buf, int buflen); + + int virtTestDifference(FILE *stream, + const char *expect, + const char *actual); + #ifdef __cplusplus } #endif