⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 runtest.c

📁 xml开源解析代码.版本为libxml2-2.6.29,可支持GB3212.网络消息发送XML时很有用.
💻 C
📖 第 1 页 / 共 5 页
字号:
    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 intstreamParseTest(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 intwalkerParseTest(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 intstreamMemParseTest(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				* *									* ************************************************************************/static FILE *xpathOutput;static xmlDocPtr xpathDocument;static voidtestXPath(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 intxpathCommonTest(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 intxpathExprTest(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 intxpathDocTest(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 intxptrDocTest(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 intxmlidDocTest(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 for %s failed\n", filename);	    res = 1;	}    }    return(res);}#endif /* LIBXML_DEBUG_ENABLED */#endif /* XPATH *//************************************************************************ *									* *			URI based tests					* *									* ************************************************************************/static voidhandleURI(const char *str, const char *base, FILE *o) {    int ret;    xmlURIPtr uri;    xmlChar *res = NULL;    uri = xmlCreateURI();    if (base == NULL) {	ret = xmlParseURIReference(uri, str);	if (ret != 0)	    fprintf(o, "%s : error %d\n", str, ret);	else {	    xmlNormalizeURIPath(uri->path);	    xmlPrintURI(o, uri);	    fprintf(o, "\n");	}    } else {	res = xmlBuildURI((xmlChar *)str, (xmlChar *) base);	if (res != NULL) {	    fprintf(o, "%s\n", (char *) res);	}	else	    fprintf(o, "::ERROR::\n");    }    if (res != NULL)	xmlFree(res);    xmlFreeURI(uri);}/** * uriCommonTest: * @filename: the file to parse * @result: the file with expected result * @err: the file with error messages * * Parse a file containing URI and check for errors * * Returns 0 in case of success, an error code otherwise */static inturiCommonTest(const char *filename,             const char *result,             const char *err,             const char *base) {    char *temp;    FILE *o, *f;    char str[1024];    int res = 0, i, ret;    temp = resultFilename(filename, "", ".res");    if (temp == NULL) {        fprintf(stderr, "Out of memory\n");        fatalError();    }    o = fopen(temp, "wb");    if (o == NULL) {	fprintf(stderr, "failed to open output file %s\n", temp);        free(temp);	return(-1);    }    f = fopen(filename, "rb");    if (f == NULL) {	fprintf(stderr, "failed to open input file %s\n", filename);	fclose(o);	unlink(temp);        free(temp);	return(-1);    }    while (1) {	/*	 * read one line in string buffer.	 */	if (fgets (&str[0], sizeof (str) - 1, f) == NULL)	   break;	/*	 * remove the ending spaces	 */	i = strlen(str);	while ((i > 0) &&	       ((str[i - 1] == '\n') || (str[i - 1] == '\r') ||		(str[i - 1] == ' ') || (str[i - 1] == '\t'))) {	    i--;	    str[i] = 0;	}	nb_tests++;	handleURI(str, base, o); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -