📄 runtest.c
字号:
static void
htmlstartElementDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, const xmlChar **atts)
{
int i;
fprintf(SAXdebug, "SAX.startElement(%s", (char *) name);
if (atts != NULL) {
for (i = 0;(atts[i] != NULL);i++) {
fprintf(SAXdebug, ", %s", atts[i++]);
if (atts[i] != NULL) {
unsigned char output[40];
const unsigned char *att = atts[i];
int outlen, attlen;
fprintf(SAXdebug, "='");
while ((attlen = strlen((char*)att)) > 0) {
outlen = sizeof output - 1;
htmlEncodeEntities(output, &outlen, att, &attlen, '\'');
output[outlen] = 0;
fprintf(SAXdebug, "%s", (char *) output);
att += attlen;
}
fprintf(SAXdebug, "'");
}
}
}
fprintf(SAXdebug, ")\n");
}
/**
* htmlcharactersDebug:
* @ctxt: An XML parser context
* @ch: a xmlChar string
* @len: the number of xmlChar
*
* receiving some chars from the parser.
* Question: how much at a time ???
*/
static void
htmlcharactersDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
{
unsigned char output[40];
int inlen = len, outlen = 30;
htmlEncodeEntities(output, &outlen, ch, &inlen, 0);
output[outlen] = 0;
fprintf(SAXdebug, "SAX.characters(%s, %d)\n", output, len);
}
/**
* htmlcdataDebug:
* @ctxt: An XML parser context
* @ch: a xmlChar string
* @len: the number of xmlChar
*
* receiving some cdata chars from the parser.
* Question: how much at a time ???
*/
static void
htmlcdataDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *ch, int len)
{
unsigned char output[40];
int inlen = len, outlen = 30;
htmlEncodeEntities(output, &outlen, ch, &inlen, 0);
output[outlen] = 0;
fprintf(SAXdebug, "SAX.cdata(%s, %d)\n", output, len);
}
xmlSAXHandler debugHTMLSAXHandlerStruct = {
internalSubsetDebug,
isStandaloneDebug,
hasInternalSubsetDebug,
hasExternalSubsetDebug,
resolveEntityDebug,
getEntityDebug,
entityDeclDebug,
notationDeclDebug,
attributeDeclDebug,
elementDeclDebug,
unparsedEntityDeclDebug,
setDocumentLocatorDebug,
startDocumentDebug,
endDocumentDebug,
htmlstartElementDebug,
endElementDebug,
referenceDebug,
htmlcharactersDebug,
ignorableWhitespaceDebug,
processingInstructionDebug,
commentDebug,
warningDebug,
errorDebug,
fatalErrorDebug,
getParameterEntityDebug,
htmlcdataDebug,
externalSubsetDebug,
1,
NULL,
NULL,
NULL,
NULL
};
xmlSAXHandlerPtr debugHTMLSAXHandler = &debugHTMLSAXHandlerStruct;
#endif /* LIBXML_HTML_ENABLED */
#ifdef LIBXML_SAX1_ENABLED
/**
* saxParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file using the SAX API and check for errors.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
saxParseTest(const char *filename, const char *result,
const char *err ATTRIBUTE_UNUSED,
int options) {
int ret;
char *temp;
nb_tests++;
temp = resultFilename(filename, "", ".res");
if (temp == NULL) {
fprintf(stderr, "out of memory\n");
fatalError();
}
SAXdebug = fopen(temp, "wb");
if (SAXdebug == NULL) {
fprintf(stderr, "Failed to write to %s\n", temp);
free(temp);
return(-1);
}
/* for SAX we really want the callbacks though the context handlers */
xmlSetStructuredErrorFunc(NULL, NULL);
xmlSetGenericErrorFunc(NULL, testErrorHandler);
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML) {
htmlSAXParseFile(filename, NULL, emptySAXHandler, NULL);
ret = 0;
} else
#endif
ret = xmlSAXUserParseFile(emptySAXHandler, NULL, filename);
if (ret == XML_WAR_UNDECLARED_ENTITY) {
fprintf(SAXdebug, "xmlSAXUserParseFile returned error %d\n", ret);
ret = 0;
}
if (ret != 0) {
fprintf(stderr, "Failed to parse %s\n", filename);
return(1);
}
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML) {
htmlSAXParseFile(filename, NULL, debugHTMLSAXHandler, NULL);
ret = 0;
} else
#endif
if (options & XML_PARSE_SAX1) {
ret = xmlSAXUserParseFile(debugSAXHandler, NULL, filename);
} else {
ret = xmlSAXUserParseFile(debugSAX2Handler, NULL, filename);
}
if (ret == XML_WAR_UNDECLARED_ENTITY) {
fprintf(SAXdebug, "xmlSAXUserParseFile returned error %d\n", ret);
ret = 0;
}
fclose(SAXdebug);
if (compareFiles(temp, result)) {
fprintf(stderr, "Got a difference for %s\n", filename);
ret = 1;
} else
unlink(temp);
free(temp);
/* switch back to structured error handling */
xmlSetGenericErrorFunc(NULL, NULL);
xmlSetStructuredErrorFunc(NULL, testStructuredErrorHandler);
return(ret);
}
#endif
/************************************************************************
* *
* Parse to tree based tests *
* *
************************************************************************/
/**
* oldParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages: unused
*
* Parse a file using the old xmlParseFile API, then serialize back
* reparse the result and serialize again, then check for deviation
* in serialization.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
oldParseTest(const char *filename, const char *result,
const char *err ATTRIBUTE_UNUSED,
int options ATTRIBUTE_UNUSED) {
xmlDocPtr doc;
char *temp;
int res = 0;
nb_tests++;
/*
* base of the test, parse with the old API
*/
#ifdef LIBXML_SAX1_ENABLED
doc = xmlParseFile(filename);
#else
doc = xmlReadFile(filename, NULL, 0);
#endif
if (doc == NULL)
return(1);
temp = resultFilename(filename, "", ".res");
if (temp == NULL) {
fprintf(stderr, "out of memory\n");
fatalError();
}
xmlSaveFile(temp, doc);
if (compareFiles(temp, result)) {
res = 1;
}
xmlFreeDoc(doc);
/*
* Parse the saved result to make sure the round trip is okay
*/
#ifdef LIBXML_SAX1_ENABLED
doc = xmlParseFile(temp);
#else
doc = xmlReadFile(temp, NULL, 0);
#endif
if (doc == NULL)
return(1);
xmlSaveFile(temp, doc);
if (compareFiles(temp, result)) {
res = 1;
}
xmlFreeDoc(doc);
unlink(temp);
free(temp);
return(res);
}
#ifdef LIBXML_PUSH_ENABLED
/**
* pushParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages: unused
*
* Parse a file using the Push API, then serialize back
* to check for content.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
pushParseTest(const char *filename, const char *result,
const char *err ATTRIBUTE_UNUSED,
int options) {
xmlParserCtxtPtr ctxt;
xmlDocPtr doc;
const char *base;
int size, res;
int cur = 0;
nb_tests++;
/*
* load the document in memory and work from there.
*/
if (loadMem(filename, &base, &size) != 0) {
fprintf(stderr, "Failed to load %s\n", filename);
return(-1);
}
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML)
ctxt = htmlCreatePushParserCtxt(NULL, NULL, base + cur, 4, filename,
XML_CHAR_ENCODING_NONE);
else
#endif
ctxt = xmlCreatePushParserCtxt(NULL, NULL, base + cur, 4, filename);
xmlCtxtUseOptions(ctxt, options);
cur += 4;
while (cur < size) {
if (cur + 1024 >= size) {
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML)
htmlParseChunk(ctxt, base + cur, size - cur, 1);
else
#endif
xmlParseChunk(ctxt, base + cur, size - cur, 1);
break;
} else {
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML)
htmlParseChunk(ctxt, base + cur, 1024, 0);
else
#endif
xmlParseChunk(ctxt, base + cur, 1024, 0);
cur += 1024;
}
}
doc = ctxt->myDoc;
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML)
res = 1;
else
#endif
res = ctxt->wellFormed;
xmlFreeParserCtxt(ctxt);
free((char *)base);
if (!res) {
xmlFreeDoc(doc);
fprintf(stderr, "Failed to parse %s\n", filename);
return(-1);
}
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML)
htmlDocDumpMemory(doc, (xmlChar **) &base, &size);
else
#endif
xmlDocDumpMemory(doc, (xmlChar **) &base, &size);
xmlFreeDoc(doc);
res = compareFileMem(result, base, size);
if ((base == NULL) || (res != 0)) {
if (base != NULL)
xmlFree((char *)base);
fprintf(stderr, "Result for %s failed\n", filename);
return(-1);
}
xmlFree((char *)base);
if (err != NULL) {
res = compareFileMem(err, testErrors, testErrorsSize);
if (res != 0) {
fprintf(stderr, "Error for %s failed\n", filename);
return(-1);
}
}
return(0);
}
#endif
/**
* memParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages: unused
*
* Parse a file using the old xmlReadMemory API, then serialize back
* reparse the result and serialize again, then check for deviation
* in serialization.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
memParseTest(const char *filename, const char *result,
const char *err ATTRIBUTE_UNUSED,
int options ATTRIBUTE_UNUSED) {
xmlDocPtr doc;
const char *base;
int size, res;
nb_tests++;
/*
* load and parse the memory
*/
if (loadMem(filename, &base, &size) != 0) {
fprintf(stderr, "Failed to load %s\n", filename);
return(-1);
}
doc = xmlReadMemory(base, size, filename, NULL, 0);
unloadMem(base);
if (doc == NULL) {
return(1);
}
xmlDocDumpMemory(doc, (xmlChar **) &base, &size);
xmlFreeDoc(doc);
res = compareFileMem(result, base, size);
if ((base == NULL) || (res != 0)) {
if (base != NULL)
xmlFree((char *)base);
fprintf(stderr, "Result for %s failed\n", filename);
return(-1);
}
xmlFree((char *)base);
return(0);
}
/**
* noentParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages: unused
*
* Parse a file with entity resolution, then serialize back
* reparse the result and serialize again, then check for deviation
* in serialization.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
noentParseTest(const char *filename, const char *result,
const char *err ATTRIBUTE_UNUSED,
int options) {
xmlDocPtr doc;
char *temp;
int res = 0;
nb_tests++;
/*
* base of the test, parse with the old API
*/
doc = xmlReadFile(filename, NULL, options);
if (doc == NULL)
return(1);
temp = resultFilename(filename, "", ".res");
if (temp == NULL) {
fprintf(stderr, "Out of memory\n");
fatalError();
}
xmlSaveFile(temp, doc);
if (compareFiles(temp, result)) {
res = 1;
}
xmlFreeDoc(doc);
/*
* Parse the saved result to make sure the round trip is okay
*/
doc = xmlReadFile(filename, NULL, options);
if (doc == NULL)
return(1);
xmlSaveFile(temp, doc);
if (compareFiles(temp, result)) {
res = 1;
}
xmlFreeDoc(doc);
unlink(temp);
free(temp);
return(res);
}
/**
* errParseTest:
* @filename: the file to parse
* @result: the file with expected result
* @err: the file with error messages
*
* Parse a file using the xmlReadFile API and check for errors.
*
* Returns 0 in case of success, an error code otherwise
*/
static int
errParseTest(const char *filename, const char *result, const char *err,
int options) {
xmlDocPtr doc;
const char *base = NULL;
int size, res = 0;
nb_tests++;
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML) {
doc = htmlReadFile(filename, NULL, options);
} else
#endif
#ifdef LIBXML_XINCLUDE_ENABLED
if (options & XML_PARSE_XINCLUDE) {
doc = xmlReadFile(filename, NULL, options);
xmlXIncludeProcessFlags(doc, options);
} else
#endif
{
xmlGetWarningsDefaultValue = 1;
doc = xmlReadFile(filename, NULL, options);
}
xmlGetWarningsDefaultValue = 0;
if (result) {
if (doc == NULL) {
base = "";
size = 0;
} else {
#ifdef LIBXML_HTML_ENABLED
if (options & XML_PARSE_HTML) {
htmlDocDumpMemory(doc, (xmlChar **) &base, &size);
} else
#endif
xmlDocDumpMemory(doc, (xmlChar **) &base, &size);
}
res = compareFileMem(result, base, size);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -