Avoid mix vardecl/stmt
[idzebra-moved-to-github.git] / test / api / testlib.c
index c32fb16..e960d0d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: testlib.c,v 1.11 2005-01-15 19:38:35 adam Exp $
+/* $Id: testlib.c,v 1.19 2005-05-09 12:03:59 adam Exp $
    Copyright (C) 1995-2005
    Index Data ApS
 
@@ -120,68 +120,166 @@ void init_data(ZebraHandle zh, const char **recs)
     zebra_select_database(zh, "Default");
     yaz_log(log_level, "going to call init");
     i = zebra_init(zh);
-    yaz_log(log_level, "init returned %d",i);
+    yaz_log(log_level, "init returned %d", i);
     if (i) 
     {
         printf("init failed with %d\n",i);
         zebra_result(zh, &i, &addinfo);
-        printf("  Error %d   %s\n",i,addinfo);
+        printf("  Error %d   %s\n", i, addinfo);
         exit(1);
     }
     if (recs)
     {
         zebra_begin_trans (zh, 1);
         for (i = 0; recs[i]; i++)
-            zebra_add_record (zh, recs[i], strlen(recs[i]));
-        zebra_end_trans (zh);
-        zebra_commit (zh);
+            zebra_add_record(zh, recs[i], strlen(recs[i]));
+        zebra_end_trans(zh);
+        zebra_commit(zh);
     }
 
 }
 
-int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
+int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
+              int experror)
 {
     ODR odr;
     YAZ_PQF_Parser parser;
     Z_RPNQuery *rpn;
     const char *setname="rsetname";
-    int hits;
-    int rc;
-        
+    zint hits;
+    ZEBRA_RES rc;
 
-    yaz_log(log_level,"======================================");
-    yaz_log(log_level,"qry[%d]: %s", lineno, query);
-    odr=odr_createmem (ODR_DECODE);    
+    yaz_log(log_level, "======================================");
+    yaz_log(log_level, "qry[%d]: %s", lineno, query);
+    odr = odr_createmem (ODR_DECODE);    
 
     parser = yaz_pqf_create();
     rpn = yaz_pqf_parse(parser, odr, query);
+    yaz_pqf_destroy(parser);
     if (!rpn) {
-        printf("Error: Parse failed \n%s\n",query);
+        printf("Error: Parse failed \n%s\n", query);
         exit(1);
     }
-    rc = zebra_search_RPN (zh, odr, rpn, setname, &hits);
-    if (rc) {
-        printf("Error: search returned %d \n%s\n",rc,query);
-        exit (1);
+    rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
+    if (experror)
+    {
+       int code;
+       if (rc != ZEBRA_FAIL)
+       {
+           printf("Error: search returned %d (OK), but error was expected\n"
+                  "%s\n",  rc, query);
+           exit(1);
+       }
+       code = zebra_errCode(zh);
+       if (code != experror)
+       {
+           printf("Error: search returned error code %d, but error %d was "
+                  "expected\n%s\n",
+                  code, experror, query);
+           exit(1);
+       }
     }
-
-    if (hits != exphits) {
-        printf("Error: search returned %d hits instead of %d\n",
-                hits, exphits);
-        exit (1);
+    else
+    {
+       if (rc == ZEBRA_FAIL) {
+           int code = zebra_errCode(zh);
+           printf("Error: search returned %d. Code %d\n%s\n", rc, 
+                  code, query);
+           exit (1);
+       }
+       if (exphits != -1 && hits != exphits) {
+           printf("Error: search returned " ZINT_FORMAT 
+                  " hits instead of " ZINT_FORMAT "\n%s\n",
+                  hits, exphits, query);
+           exit (1);
+       }
     }
-    yaz_pqf_destroy(parser);
     odr_destroy (odr);
     return hits;
 }
 
 
+int do_query(int lineno, ZebraHandle zh, const char *query, zint exphits)
+{
+    return do_query_x(lineno, zh, query, exphits, 0);
+}
+
+void do_scan(int lineno, ZebraHandle zh, const char *query,
+            int pos, int num,
+            int exp_pos, int exp_num, int exp_partial,
+            const char **exp_entries)
+{
+    ODR odr = odr_createmem(ODR_ENCODE);
+    ZebraScanEntry *entries = 0;
+    int partial = -123;
+    ZEBRA_RES res;
+
+    yaz_log(log_level, "======================================");
+    yaz_log(log_level, "scan[%d]: pos=%d num=%d %s", lineno, pos, num, query);
+
+    res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial);
+    if (res != ZEBRA_OK)
+    {
+       printf("Error: scan returned %d (FAIL), but no error was expected\n"
+              "%s\n",  res, query);
+       exit(1);
+    }
+    else
+    {
+       int fails = 0;
+       if (partial == -123)
+       {
+           printf("Error: scan returned OK, but partial was not set\n"
+                  "%s\n", query);
+           fails++;
+       }
+       if (partial != exp_partial)
+       {
+           printf("Error: scan returned OK, with partial/expected %d/%d\n"
+                  "%s\n", partial, exp_partial, query);
+           fails++;
+       }
+       if (num != exp_num)
+       {
+           printf("Error: scan returned OK, with num/expected %d/%d\n"
+                  "%s\n", num, exp_num, query);
+           fails++;
+       }
+       if (pos != exp_pos)
+       {
+           printf("Error: scan returned OK, with pos/expected %d/%d\n"
+                  "%s\n", pos, exp_pos, query);
+           fails++;
+       }
+       if (fails)
+           exit(1);
+       fails = 0;
+       if (exp_entries)
+       {
+           int i;
+           for (i = 0; i<num; i++)
+           {
+               if (strcmp(exp_entries[i], entries[i].term))
+               {
+                   printf("Error: scan OK, but entry %d term/exp %s/%s\n"
+                          "%s\n",
+                          i, entries[i].term, exp_entries[i], query);
+                   fails++;
+               }
+           }
+       }
+       if (fails)
+           exit(0);
+    }
+    odr_destroy(odr);
+}
+
 /** 
  * makes a query, checks number of hits, and for the first hit, that 
  * it contains the given string, and that it gets the right score
  */
 void ranking_query(int lineno, ZebraHandle zh, char *query, 
-          int exphits, char *firstrec, int firstscore )
+                  int exphits, char *firstrec, int firstscore)
 {
     ZebraRetrievalRecord retrievalRecord[10];
     ODR odr_output = odr_createmem (ODR_ENCODE);    
@@ -207,8 +305,8 @@ void ranking_query(int lineno, ZebraHandle zh, char *query,
     if (!strstr(retrievalRecord[0].buf, firstrec))
     {
         printf("Error: Got the wrong record first\n");
-        printf("Expected '%s' but got \n",firstrec);
-        printf("%.*s\n",retrievalRecord[0].len,retrievalRecord[0].buf);
+        printf("Expected '%s' but got\n", firstrec);
+        printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
         exit(1);
     }
     
@@ -248,7 +346,7 @@ void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
     {
        if (meta[i].sysno != ids[i])
        {
-           printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT,
+           printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
                   ids[i], meta[i].sysno);
            exit(1);
        }