📄 runtest.c
字号:
if (doc != NULL) {
if (base != NULL)
xmlFree((char *)base);
xmlFreeDoc(doc);
}
if (res != 0) {
fprintf(stderr, "Result for %s failed\n", filename);
return(-1);
}
if (err != NULL) {
res = compareFileMem(err, testErrors, testErrorsSize);
if (res != 0) {
fprintf(stderr, "Error for %s failed\n", filename);
return(-1);
}
} else if (options & XML_PARSE_DTDVALID) {
if (testErrorsSize != 0)
fprintf(stderr, "Validation for %s failed\n", filename);
}
return(0);
}
#ifdef LIBXML_READER_ENABLED
/************************************************************************
* *
* Reader based tests *
* *
************************************************************************/
static void processNode(FILE *out, xmlTextReaderPtr reader) {
const xmlChar *name, *value;
int type, empty;
type = xmlTextReaderNodeType(reader);
empty = xmlTextReaderIsEmptyElement(reader);
name = xmlTextReaderConstName(reader);
if (name == NULL)
name = BAD_CAST "--";
value = xmlTextReaderConstValue(reader);
fprintf(out, "%d %d %s %d %d",
xmlTextReaderDepth(reader),
type,
name,
empty,
xmlTextReaderHasValue(reader));
if (value == NULL)
fprintf(out, "\n");
else {
fprintf(out, " %s\n", value);
}
}
static int
streamProcessTest(const char *filename, const char *result, const char *err,
xmlTextReaderPtr reader, const char *rng) {
int ret;
char *temp = NULL;
FILE *t = NULL;
if (reader == NULL)
return(-1);
nb_tests++;
if (result != NULL) {
temp = resultFilename(filename, "", ".res");
if (temp == NULL) {
fprintf(stderr, "Out of memory\n");
fatalError();
}
t = fopen(temp, "wb");
if (t == NULL) {
fprintf(stderr, "Can't open temp file %s\n", temp);
free(temp);
return(-1);
}
}
#ifdef LIBXML_SCHEMAS_ENABLED
if (rng != NULL) {
ret = xmlTextReaderRelaxNGValidate(reader, rng);
if (ret < 0) {
testErrorHandler(NULL, "Relax-NG schema %s failed to compile\n",
rng);
fclose(t);
unlink(temp);
free(temp);
return(0);
}
}
#endif
xmlGetWarningsDefaultValue = 1;
ret = xmlTextReaderRead(reader);
while (ret == 1) {
if ((t != NULL) && (rng == NULL))
processNode(t, reader);
ret = xmlTextReaderRead(reader);
}
if (ret != 0) {
testErrorHandler(NULL, "%s : failed to parse\n", filename);
}
if (rng != NULL) {
if (xmlTextReaderIsValid(reader) != 1) {
testErrorHandler(NULL, "%s fails to validate\n", filename);
} else {
testErrorHandler(NULL, "%s validates\n", filename);
}
}
xmlGetWarningsDefaultValue = 0;
if (t != NULL) {
fclose(t);
ret = compareFiles(temp, result);
unlink(temp);
free(temp);
if (ret) {
fprintf(stderr, "Result for %s failed\n", filename);
return(-1);
}
}
if (err != NULL) {
ret = compareFileMem(err, testErrors, testErrorsSize);
if (ret != 0) {
fprintf(stderr, "Error for %s failed\n", filename);
printf("%s", testErrors);
return(-1);
}
}
return(0);
}
/**
* streamParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file using the reader API and check for errors.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
streamParseTest(const char *filename, const char *result, const char *err,
int options) {
xmlTextReaderPtr reader;
int ret;
reader = xmlReaderForFile(filename, NULL, options);
ret = streamProcessTest(filename, result, err, reader, NULL);
xmlFreeTextReader(reader);
return(ret);
}
/**
* walkerParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file using the walker, i.e. a reader built from a atree.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
walkerParseTest(const char *filename, const char *result, const char *err,
int options) {
xmlDocPtr doc;
xmlTextReaderPtr reader;
int ret;
doc = xmlReadFile(filename, NULL, options);
if (doc == NULL) {
fprintf(stderr, "Failed to parse %s\n", filename);
return(-1);
}
reader = xmlReaderWalker(doc);
ret = streamProcessTest(filename, result, err, reader, NULL);
xmlFreeTextReader(reader);
xmlFreeDoc(doc);
return(ret);
}
/**
* streamMemParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file using the reader API from memory and check for errors.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
streamMemParseTest(const char *filename, const char *result, const char *err,
int options) {
xmlTextReaderPtr reader;
int ret;
const char *base;
int size;
/*
* load and parse the memory
*/
if (loadMem(filename, &base, &size) != 0) {
fprintf(stderr, "Failed to load %s\n", filename);
return(-1);
}
reader = xmlReaderForMemory(base, size, filename, NULL, options);
ret = streamProcessTest(filename, result, err, reader, NULL);
free((char *)base);
xmlFreeTextReader(reader);
return(ret);
}
#endif
#ifdef LIBXML_XPATH_ENABLED
#ifdef LIBXML_DEBUG_ENABLED
/************************************************************************
* *
* XPath and XPointer based tests *
* *
************************************************************************/
FILE *xpathOutput;
xmlDocPtr xpathDocument;
static void
testXPath(const char *str, int xptr, int expr) {
xmlXPathObjectPtr res;
xmlXPathContextPtr ctxt;
nb_tests++;
#if defined(LIBXML_XPTR_ENABLED)
if (xptr) {
ctxt = xmlXPtrNewContext(xpathDocument, NULL, NULL);
res = xmlXPtrEval(BAD_CAST str, ctxt);
} else {
#endif
ctxt = xmlXPathNewContext(xpathDocument);
ctxt->node = xmlDocGetRootElement(xpathDocument);
if (expr)
res = xmlXPathEvalExpression(BAD_CAST str, ctxt);
else {
/* res = xmlXPathEval(BAD_CAST str, ctxt); */
xmlXPathCompExprPtr comp;
comp = xmlXPathCompile(BAD_CAST str);
if (comp != NULL) {
res = xmlXPathCompiledEval(comp, ctxt);
xmlXPathFreeCompExpr(comp);
} else
res = NULL;
}
#if defined(LIBXML_XPTR_ENABLED)
}
#endif
xmlXPathDebugDumpObject(xpathOutput, res, 0);
xmlXPathFreeObject(res);
xmlXPathFreeContext(ctxt);
}
/**
* xpathExprTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file containing XPath standalone expressions and evaluate them
*
* Returns 0 in case of success, an error code otherwise
*/
static int
xpathCommonTest(const char *filename, const char *result,
int xptr, int expr) {
FILE *input;
char expression[5000];
int len, ret = 0;
char *temp;
temp = resultFilename(filename, "", ".res");
if (temp == NULL) {
fprintf(stderr, "Out of memory\n");
fatalError();
}
xpathOutput = fopen(temp, "wb");
if (xpathOutput == NULL) {
fprintf(stderr, "failed to open output file %s\n", temp);
free(temp);
return(-1);
}
input = fopen(filename, "rb");
if (input == NULL) {
xmlGenericError(xmlGenericErrorContext,
"Cannot open %s for reading\n", filename);
free(temp);
return(-1);
}
while (fgets(expression, 4500, input) != NULL) {
len = strlen(expression);
len--;
while ((len >= 0) &&
((expression[len] == '\n') || (expression[len] == '\t') ||
(expression[len] == '\r') || (expression[len] == ' '))) len--;
expression[len + 1] = 0;
if (len >= 0) {
fprintf(xpathOutput,
"\n========================\nExpression: %s\n",
expression) ;
testXPath(expression, xptr, expr);
}
}
fclose(input);
fclose(xpathOutput);
if (result != NULL) {
ret = compareFiles(temp, result);
if (ret) {
fprintf(stderr, "Result for %s failed\n", filename);
}
}
unlink(temp);
free(temp);
return(ret);
}
/**
* xpathExprTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file containing XPath standalone expressions and evaluate them
*
* Returns 0 in case of success, an error code otherwise
*/
static int
xpathExprTest(const char *filename, const char *result,
const char *err ATTRIBUTE_UNUSED,
int options ATTRIBUTE_UNUSED) {
return(xpathCommonTest(filename, result, 0, 1));
}
/**
* xpathDocTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file containing XPath expressions and evaluate them against
* a set of corresponding documents.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
xpathDocTest(const char *filename,
const char *resul ATTRIBUTE_UNUSED,
const char *err ATTRIBUTE_UNUSED,
int options) {
char pattern[500];
char result[500];
glob_t globbuf;
size_t i;
int ret = 0, res;
xpathDocument = xmlReadFile(filename, NULL,
options | XML_PARSE_DTDATTR | XML_PARSE_NOENT);
if (xpathDocument == NULL) {
fprintf(stderr, "Failed to load %s\n", filename);
return(-1);
}
snprintf(pattern, 499, "./test/XPath/tests/%s*", baseFilename(filename));
pattern[499] = 0;
globbuf.gl_offs = 0;
glob(pattern, GLOB_DOOFFS, NULL, &globbuf);
for (i = 0;i < globbuf.gl_pathc;i++) {
snprintf(result, 499, "result/XPath/tests/%s",
baseFilename(globbuf.gl_pathv[i]));
res = xpathCommonTest(globbuf.gl_pathv[i], &result[0], 0, 0);
if (res != 0)
ret = res;
}
globfree(&globbuf);
xmlFreeDoc(xpathDocument);
return(ret);
}
#ifdef LIBXML_XPTR_ENABLED
/**
* xptrDocTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file containing XPath expressions and evaluate them against
* a set of corresponding documents.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
xptrDocTest(const char *filename,
const char *resul ATTRIBUTE_UNUSED,
const char *err ATTRIBUTE_UNUSED,
int options) {
char pattern[500];
char result[500];
glob_t globbuf;
size_t i;
int ret = 0, res;
xpathDocument = xmlReadFile(filename, NULL,
options | XML_PARSE_DTDATTR | XML_PARSE_NOENT);
if (xpathDocument == NULL) {
fprintf(stderr, "Failed to load %s\n", filename);
return(-1);
}
snprintf(pattern, 499, "./test/XPath/xptr/%s*", baseFilename(filename));
pattern[499] = 0;
globbuf.gl_offs = 0;
glob(pattern, GLOB_DOOFFS, NULL, &globbuf);
for (i = 0;i < globbuf.gl_pathc;i++) {
snprintf(result, 499, "result/XPath/xptr/%s",
baseFilename(globbuf.gl_pathv[i]));
res = xpathCommonTest(globbuf.gl_pathv[i], &result[0], 1, 0);
if (res != 0)
ret = res;
}
globfree(&globbuf);
xmlFreeDoc(xpathDocument);
return(ret);
}
#endif /* LIBXML_XPTR_ENABLED */
/**
* xmlidDocTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file containing xml:id and check for errors and verify
* that XPath queries will work on them as expected.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
xmlidDocTest(const char *filename,
const char *result,
const char *err,
int options) {
int res = 0;
int ret = 0;
char *temp;
xpathDocument = xmlReadFile(filename, NULL,
options | XML_PARSE_DTDATTR | XML_PARSE_NOENT);
if (xpathDocument == NULL) {
fprintf(stderr, "Failed to load %s\n", filename);
return(-1);
}
temp = resultFilename(filename, "", ".res");
if (temp == NULL) {
fprintf(stderr, "Out of memory\n");
fatalError();
}
xpathOutput = fopen(temp, "wb");
if (xpathOutput == NULL) {
fprintf(stderr, "failed to open output file %s\n", temp);
xmlFreeDoc(xpathDocument);
free(temp);
return(-1);
}
testXPath("id('bar')", 0, 0);
fclose(xpathOutput);
if (result != NULL) {
ret = compareFiles(temp, result);
if (ret) {
fprintf(stderr, "Result for %s failed\n", filename);
res = 1;
}
}
unlink(temp);
free(temp);
xmlFreeDoc(xpathDocument);
if (err != NULL) {
ret = compareFileMem(err, testErrors, testErrorsSize);
if (ret != 0) {
fprintf(stderr, "Error fo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -