Use HAVE_UNISTD_H when including unistd.h.
[idzebra-moved-to-github.git] / test / api / testlib.c
index ac8f9cb..3d1f743 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: testlib.c,v 1.14 2005-04-20 10:18:19 adam Exp $
+/* $Id: testlib.c,v 1.23 2005-06-14 20:28:54 adam Exp $
    Copyright (C) 1995-2005
    Index Data ApS
 
@@ -22,6 +22,16 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 /** testlib - utilities for the api tests */
 
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#if HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
 #include <assert.h>
 #include <yaz/log.h>
 #include <yaz/pquery.h>
@@ -34,6 +44,7 @@ int log_level=0; /* not static, t*.c may use it */
 
 void start_log(int argc, char **argv)
 {
+    int cmd_level = 0;
     char logname[2048];
     if (!argv) 
         return;
@@ -41,9 +52,11 @@ void start_log(int argc, char **argv)
         return;
     sprintf(logname, "%s.log", argv[0]);
     yaz_log_init_file(logname);
-    log_level = yaz_log_mask_str_x(argv[0],0);
-    yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level);
-    yaz_log(log_level,"starting %s",argv[0]);
+    log_level = yaz_log_mask_str_x(argv[0], 0);
+    if (argc >= 2)
+       cmd_level |= yaz_log_mask_str_x(argv[1], 0);
+    yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level | cmd_level);
+    yaz_log(log_level, "starting %s", argv[0]);
 }
 
 /** 
@@ -56,6 +69,14 @@ void start_log(int argc, char **argv)
  */
 ZebraService start_up(char *cfgname, int argc, char **argv)
 {
+#if HAVE_SYS_RESOURCE_H
+#if HAVE_SYS_TIME_H
+    struct rlimit rlim;
+    rlim.rlim_cur = 20;
+    rlim.rlim_max = 20;
+    setrlimit(RLIMIT_CPU, &rlim);
+#endif
+#endif
     nmem_init();
     start_log(argc, argv);
     return start_service(cfgname);
@@ -139,8 +160,8 @@ void init_data(ZebraHandle zh, const char **recs)
 
 }
 
-int do_query_x(int lineno, ZebraHandle zh, char *query, int exphits,
-             int experror)
+int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
+              int experror)
 {
     ODR odr;
     YAZ_PQF_Parser parser;
@@ -163,13 +184,14 @@ int do_query_x(int lineno, ZebraHandle zh, char *query, int exphits,
     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);
        }
-       int code = zebra_errCode(zh);
+       code = zebra_errCode(zh);
        if (code != experror)
        {
            printf("Error: search returned error code %d, but error %d was "
@@ -181,12 +203,14 @@ int do_query_x(int lineno, ZebraHandle zh, char *query, int exphits,
     else
     {
        if (rc == ZEBRA_FAIL) {
-           printf("Error: search returned %d\n%s\n", rc, query);
+           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 %d\n%s\n",
+                  " hits instead of " ZINT_FORMAT "\n%s\n",
                   hits, exphits, query);
            exit (1);
        }
@@ -196,11 +220,80 @@ int do_query_x(int lineno, ZebraHandle zh, char *query, int exphits,
 }
 
 
-int do_query(int lineno, ZebraHandle zh, char *query, int exphits)
+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 
@@ -233,7 +326,7 @@ 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("Expected '%s' but got\n", firstrec);
         printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
         exit(1);
     }
@@ -274,7 +367,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);
        }