X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=test%2Ftstxmlquery.c;h=8164d80f08df677e5e5f795d4cd30b3730bf4efa;hp=c72343e66527077654140eca18b56969b2d5fcb7;hb=69c2dee55aa204cab368989b8385eda8b83bf2e2;hpb=bedf53c5f2d3e240608d27a71037777fada559d3 diff --git a/test/tstxmlquery.c b/test/tstxmlquery.c index c72343e..8164d80 100644 --- a/test/tstxmlquery.c +++ b/test/tstxmlquery.c @@ -1,24 +1,37 @@ -/* - * Copyright (C) 1995-2005, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2009 Index Data * See the file LICENSE for details. - * - * $Id: tstxmlquery.c,v 1.1 2006-01-27 17:33:15 adam Exp $ */ #include #include #include +#include #include #include #include -#if HAVE_XML2 -static void pqf2xml_text(const char *pqf) +#if YAZ_HAVE_XML2 +#include +#include +#endif + +enum pqf2xml_status { + PQF_FAILED, + QUERY2XML_FAILED, + XML_NO_MATCH, + XML_MATCH, + XML_NO_ERROR +}; + +enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml, + const char *expect_pqf) { YAZ_PQF_Parser parser = yaz_pqf_create(); ODR odr = odr_createmem(ODR_ENCODE); Z_RPNQuery *rpn; + enum pqf2xml_status status = XML_NO_ERROR; YAZ_CHECK(parser); @@ -26,29 +39,209 @@ static void pqf2xml_text(const char *pqf) rpn = yaz_pqf_parse(parser, odr, pqf); - YAZ_CHECK(rpn); - yaz_pqf_destroy(parser); - Z_Query *query = odr_malloc(odr, sizeof(*query)); - query->which = Z_Query_type_1; - query->u.type_1 = rpn; + if (!rpn) + status = PQF_FAILED; + else + { +#if YAZ_HAVE_XML2 + xmlDocPtr doc = 0; + + yaz_rpnquery2xml(rpn, &doc); + + if (!doc) + status = QUERY2XML_FAILED; + else + { + char *buf_out; + int len_out; + xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out); + + if (len_out == strlen(expect_xml) + && memcmp(buf_out, expect_xml, len_out) == 0) + { + Z_Query *query2 = 0; + int error_code = 0; + const char *addinfo = 0; + const xmlNode *root_element = xmlDocGetRootElement(doc); + ODR odr2 = odr_createmem(ODR_ENCODE); + + yaz_xml2query(root_element, &query2, odr2, + &error_code, &addinfo); + if (error_code || !query2) + status = XML_NO_MATCH; + else + { + WRBUF w = wrbuf_alloc(); + yaz_query_to_wrbuf(w, query2); + if (!expect_pqf || strcmp(expect_pqf, wrbuf_cstr(w)) == 0) + status = XML_MATCH; + else + { + status = XML_NO_MATCH; + printf("Result: %s\n", wrbuf_cstr(w)); + } + wrbuf_destroy(w); + } + odr_destroy(odr2); + } + else + { + printf("%.*s\n", len_out, buf_out); + status = XML_NO_MATCH; + } + xmlFreeDoc(doc); + xmlFree(buf_out); + } +#else + status = QUERY2XML_FAILED; +#endif + } odr_destroy(odr); + return status; } + +static void tst(void) +{ + YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", "", 0), PQF_FAILED); +#if YAZ_HAVE_XML2 + YAZ_CHECK_EQ(pqf2xml_text( + "@attr 1=4 computer", + "\n" + "" + "" + "computer" + "\n", + "RPN @attrset Bib-1 @attr 1=4 computer" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@attr 2=1 @attr 1=title computer", + "\n" + "" + "" + "" + "computer" + "\n", + "RPN @attrset Bib-1 @attr \"1=title\" @attr 2=1 computer" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@attr 2=1 @attr exp1 1=1 computer", + "\n" + "" + "" + "" + "computer" + "\n", + "RPN @attrset Bib-1 @attr Exp-1 1=1 @attr 2=1 computer" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@and a b", + "\n" + "" + "" + "a" + "b" + "\n", + "RPN @attrset Bib-1 @and a b" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@or @and a b c", + "\n" + "" + "" + "" + "a" + "b" + "c" + "\n", + "RPN @attrset Bib-1 @or @and a b c" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@set abe", + "\n" + "" + "abe\n", + "RPN @attrset Bib-1 @set abe" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + /* exclusion, distance, ordered, relationtype, + knownunit, proxunit */ + "@prox 0 3 1 2 k 2 a b", + "\n" + "" + "" + "a" + "b" + "\n", + "RPN @attrset Bib-1 @prox 0 3 1 2 k 2 a b" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@term numeric 32", + "\n" + "" + "" + "32" + "\n", + "RPN @attrset Bib-1 @term numeric 32" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@term string computer", + "\n" + "" + "" + "computer" + "\n", + "RPN @attrset Bib-1 @term string computer" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@term null void", + "\n" + "" + "" + "" + "\n", + "RPN @attrset Bib-1 @term null x" + ), XML_MATCH); + + YAZ_CHECK_EQ(pqf2xml_text( + "@attrset gils @attr 4=2 x", + "\n" + "" + "" + "" + "x" + "\n", + "RPN @attrset GILS @attr 4=2 x" + ), XML_MATCH); #endif +} int main (int argc, char **argv) { - pqf2xml_text("@attr 1=4 computer"); - - exit(0); - return 0; + YAZ_CHECK_INIT(argc, argv); + tst(); + YAZ_CHECK_TERM; } /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab