Add casts for signed/unsigned ints
[yaz-moved-to-github.git] / test / tstxmlquery.c
index 625d5e2..c1e9034 100644 (file)
@@ -1,19 +1,18 @@
-/*
- * Copyright (C) 1995-2005, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2010 Index Data
  * See the file LICENSE for details.
- *
- * $Id: tstxmlquery.c,v 1.6 2006-01-30 14:02:07 adam Exp $
  */
 
 #include <stdlib.h>
 #include <stdio.h>
 
 #include <yaz/wrbuf.h>
+#include <yaz/querytowrbuf.h>
 #include <yaz/xmlquery.h>
 #include <yaz/pquery.h>
 #include <yaz/test.h>
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 #endif
@@ -26,7 +25,8 @@ enum pqf2xml_status {
     XML_NO_ERROR
 };
 
-enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml)
+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);
@@ -45,9 +45,9 @@ enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml)
         status = PQF_FAILED;
     else
     {
-        status = QUERY2XML_FAILED;
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
         xmlDocPtr doc = 0;
+
         yaz_rpnquery2xml(rpn, &doc);
         
         if (!doc)
@@ -59,10 +59,33 @@ enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml)
 
             xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);
             
-            if (len_out == strlen(expect_xml)
+            if (len_out == (int) strlen(expect_xml)
                 && memcmp(buf_out, expect_xml, len_out) == 0)
             {
-                status = XML_MATCH;
+                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
             {
@@ -70,81 +93,141 @@ enum pqf2xml_status pqf2xml_text(const char *pqf, const char *expect_xml)
                 status = XML_NO_MATCH;
             }
             xmlFreeDoc(doc);
+            xmlFree(buf_out);
         }
+#else
+        status = QUERY2XML_FAILED;
 #endif
     }
     odr_destroy(odr);
     return status;
 }
 
-void tst()
+static void tst(void)
 {
-    YAZ_CHECK_EQ(pqf2xml_text("@attr 1=4 bad query", ""), PQF_FAILED);
-#if HAVE_XML2
+    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", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt><attr type=\"1\" value=\"4\"/>"
-                     "<term>computer</term></apt>"
-                     "</query>\n"), XML_MATCH);
+                     "<term type=\"general\">computer</term></apt>"
+                     "</rpn></query>\n",
+                     "RPN @attrset Bib-1 @attr 1=4 computer"
+                     ), XML_MATCH);
     
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@attr 2=1 @attr 1=title computer",
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt><attr type=\"1\" value=\"title\"/>"
                      "<attr type=\"2\" value=\"1\"/>"
-                     "<term>computer</term></apt>"
-                     "</query>\n"), XML_MATCH);
+                     "<term type=\"general\">computer</term></apt>"
+                     "</rpn></query>\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",
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
+                     "<query><rpn set=\"Bib-1\">"
                      "<apt><attr set=\"Exp-1\" type=\"1\" value=\"1\"/>"
                      "<attr type=\"2\" value=\"1\"/>"
-                     "<term>computer</term></apt>"
-                     "</query>\n"), XML_MATCH);
+                     "<term type=\"general\">computer</term></apt>"
+                     "</rpn></query>\n",
+                     "RPN @attrset Bib-1 @attr Exp-1 1=1 @attr 2=1 computer"
+                     ), XML_MATCH);
     
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@and a b", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
-                     "<binary type=\"and\">"
-                     "<apt><term>a</term></apt><apt><term>b</term></apt>"
-                     "</binary></query>\n"), XML_MATCH);
+                     "<query><rpn set=\"Bib-1\">"
+                     "<operator type=\"and\">"
+                     "<apt><term type=\"general\">a</term></apt>"
+                     "<apt><term type=\"general\">b</term></apt>"
+                     "</operator></rpn></query>\n",
+                     "RPN @attrset Bib-1 @and a b"
+                     ), XML_MATCH);
     
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@or @and a b c", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
-                     "<binary type=\"or\">"
-                     "<binary type=\"and\"><apt><term>a</term></apt>"
-                     "<apt><term>b</term></apt></binary>"
-                     "<apt><term>c</term></apt>"
-                     "</binary></query>\n"), XML_MATCH);
+                     "<query><rpn set=\"Bib-1\">"
+                     "<operator type=\"or\">"
+                     "<operator type=\"and\">"
+                     "<apt><term type=\"general\">a</term></apt>"
+                     "<apt><term type=\"general\">b</term></apt></operator>"
+                     "<apt><term type=\"general\">c</term></apt>"
+                     "</operator></rpn></query>\n",
+                     "RPN @attrset Bib-1 @or @and a b c"
+                     ), XML_MATCH);
 
     YAZ_CHECK_EQ(pqf2xml_text(
                      "@set abe", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
-                     "<rset>abe</rset></query>\n"), XML_MATCH);
+                     "<query><rpn set=\"Bib-1\">"
+                     "<rset>abe</rset></rpn></query>\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", 
                      "<?xml version=\"1.0\"?>\n"
-                     "<query set=\"Bib-1\" type=\"rpn\">"
-                     "<binary type=\"prox\" exclusion=\"false\" "
+                     "<query><rpn set=\"Bib-1\">"
+                     "<operator type=\"prox\" exclusion=\"false\" "
                      "distance=\"3\" "
                      "ordered=\"true\" "
                      "relationType=\"2\" "
                      "knownProximityUnit=\"2\">"
-                     "<apt><term>a</term></apt><apt><term>b</term></apt>"
-                     "</binary></query>\n"), XML_MATCH);
+                     "<apt><term type=\"general\">a</term></apt>"
+                     "<apt><term type=\"general\">b</term></apt>"
+                     "</operator></rpn></query>\n",
+                     "RPN @attrset Bib-1 @prox 0 3 1 2 k 2 a b"
+                     ), XML_MATCH);
 
+    YAZ_CHECK_EQ(pqf2xml_text(
+                     "@term numeric 32", 
+                     "<?xml version=\"1.0\"?>\n"
+                     "<query><rpn set=\"Bib-1\">"
+                     "<apt>"
+                     "<term type=\"numeric\">32</term></apt>"
+                     "</rpn></query>\n",
+                     "RPN @attrset Bib-1 @term numeric 32"
+                     ), XML_MATCH);
+    
+    YAZ_CHECK_EQ(pqf2xml_text(
+                     "@term string computer", 
+                     "<?xml version=\"1.0\"?>\n"
+                     "<query><rpn set=\"Bib-1\">"
+                     "<apt>"
+                     "<term type=\"string\">computer</term></apt>"
+                     "</rpn></query>\n",
+                     "RPN @attrset Bib-1 @term string computer"
+                     ), XML_MATCH);
+    
+    YAZ_CHECK_EQ(pqf2xml_text(
+                     "@term null void", 
+                     "<?xml version=\"1.0\"?>\n"
+                     "<query><rpn set=\"Bib-1\">"
+                     "<apt>"
+                     "<term type=\"null\"/></apt>"
+                     "</rpn></query>\n",
+                     "RPN @attrset Bib-1 @term null x"
+                     ), XML_MATCH);
+
+    YAZ_CHECK_EQ(pqf2xml_text(
+                     "@attrset gils @attr 4=2 x", 
+                     "<?xml version=\"1.0\"?>\n"
+                     "<query><rpn set=\"GILS\">"
+                     "<apt>"
+                     "<attr type=\"4\" value=\"2\"/>"
+                     "<term type=\"general\">x</term></apt>"
+                     "</rpn></query>\n",
+                     "RPN @attrset GILS @attr 4=2 x"
+                     ), XML_MATCH);
 #endif
 }
 
@@ -158,6 +241,7 @@ int main (int argc, char **argv)
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab