mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-21 20:15:17 +00:00
Test script helper for printing string differences
This commit is contained in:
parent
2d206f1d7a
commit
484559148d
@ -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>
|
Fri Apr 18 11:24:24 CEST 2008 Jim Meyering <meyering@redhat.com>
|
||||||
|
|
||||||
avoid compile error when <pthread.h> is absent
|
avoid compile error when <pthread.h> is absent
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include "testutils.h"
|
#include "testutils.h"
|
||||||
@ -58,6 +59,12 @@ virtTestRun(const char *title, int nloops, int (*body)(const void *data), const
|
|||||||
{
|
{
|
||||||
int i, ret = 0;
|
int i, ret = 0;
|
||||||
double *ts = NULL;
|
double *ts = NULL;
|
||||||
|
static int counter = 0;
|
||||||
|
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
fprintf(stderr, "%2d) %-65s ... ", counter, title);
|
||||||
|
fflush(stderr);
|
||||||
|
|
||||||
if (nloops > 1 && (ts = calloc(nloops,
|
if (nloops > 1 && (ts = calloc(nloops,
|
||||||
sizeof(double)))==NULL)
|
sizeof(double)))==NULL)
|
||||||
@ -76,12 +83,12 @@ virtTestRun(const char *title, int nloops, int (*body)(const void *data), const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ret == 0 && ts)
|
if (ret == 0 && ts)
|
||||||
fprintf(stderr, "%-50s ... OK [%.5f ms]\n", title,
|
fprintf(stderr, "OK [%.5f ms]\n",
|
||||||
virtTestCountAverage(ts, nloops));
|
virtTestCountAverage(ts, nloops));
|
||||||
else if (ret == 0)
|
else if (ret == 0)
|
||||||
fprintf(stderr, "%-50s ... OK\n", title);
|
fprintf(stderr, "OK\n");
|
||||||
else
|
else
|
||||||
fprintf(stderr, "%-50s ... FAILED\n", title);
|
fprintf(stderr, "FAILED\n");
|
||||||
|
|
||||||
free(ts);
|
free(ts);
|
||||||
return ret;
|
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;
|
||||||
|
}
|
||||||
|
@ -32,6 +32,11 @@ extern "C" {
|
|||||||
char **buf,
|
char **buf,
|
||||||
int buflen);
|
int buflen);
|
||||||
|
|
||||||
|
|
||||||
|
int virtTestDifference(FILE *stream,
|
||||||
|
const char *expect,
|
||||||
|
const char *actual);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user