Cleaned the test programs a bit, used routines in testlib
authorHeikki Levanto <heikki@indexdata.dk>
Thu, 28 Oct 2004 15:24:36 +0000 (15:24 +0000)
committerHeikki Levanto <heikki@indexdata.dk>
Thu, 28 Oct 2004 15:24:36 +0000 (15:24 +0000)
13 files changed:
test/api/Makefile.am
test/api/t1.c
test/api/t10.c
test/api/t2.c
test/api/t3.c
test/api/t4.c
test/api/t5.c
test/api/t6.c
test/api/t7.c
test/api/t8.c
test/api/t9.c
test/api/testlib.c
test/api/testlib.h

index 0bd1cac..29e3fe6 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: Makefile.am,v 1.20 2004-10-28 10:37:15 heikki Exp $
+# $Id: Makefile.am,v 1.21 2004-10-28 15:24:36 heikki Exp $
 
 noinst_PROGRAMS = testclient
 testclient_SOURCES = testclient.c
@@ -8,14 +8,14 @@ TESTS = $(check_PROGRAMS)
 
 EXTRA_DIST=zebra.cfg zebra6.cfg zebra8.cfg
 
-t1_SOURCES = t1.c
-t2_SOURCES = t2.c
-t3_SOURCES = t3.c
-t4_SOURCES = t4.c
-t5_SOURCES = t5.c
-t6_SOURCES = t6.c
-t7_SOURCES = t7.c
-t8_SOURCES = t8.c
+t1_SOURCES = t1.c testlib.c
+t2_SOURCES = t2.c testlib.c
+t3_SOURCES = t3.c testlib.c
+t4_SOURCES = t4.c testlib.c
+t5_SOURCES = t5.c testlib.c
+t6_SOURCES = t6.c testlib.c
+t7_SOURCES = t7.c testlib.c
+t8_SOURCES = t8.c testlib.c
 t9_SOURCES = t9.c testlib.c
 t10_SOURCES = t10.c testlib.c
 
index ab5a7c7..b6e20c6 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t1.c,v 1.8 2004-08-25 09:23:39 adam Exp $
+/* $Id: t1.c,v 1.9 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
    Index Data Aps
 
@@ -20,18 +20,13 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 02111-1307, USA.
 */
 
+/** t1 - just start and stop */
+
 #include <stdlib.h>
 #include <yaz/log.h>
 #include <idzebra/api.h>
+#include "testlib.h"
 
-/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-static ZebraService start_service()
-{
-    char cfg[256];
-    char *srcdir = getenv("srcdir");
-    sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");
-    return zebra_start(cfg);
-}
        
 int main(int argc, char **argv)
 {
@@ -40,12 +35,13 @@ int main(int argc, char **argv)
 
     yaz_log_init_file("t1.log");
     nmem_init();
-    zs = start_service();
+    zs = start_service(0);
     zh = zebra_open (zs);
     
     zebra_close (zh);
     zebra_stop (zs);
     nmem_exit ();
     xmalloc_trav ("x");
+    logf(LOG_LOG,"================ All tests OK ");
     exit (0);
 }
index f333448..636d539 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t10.c,v 1.1 2004-10-28 10:37:15 heikki Exp $
+/* $Id: t10.c,v 1.2 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -25,6 +25,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/log.h>
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
+#include <assert.h>
 #include "testlib.h"
 #include "rankingrecords.h"
 
@@ -78,7 +79,6 @@ struct tst tests[] = {
 int main(int argc, char **argv)
 {
     int i;
-    char *addinfo;
     ZebraService zs;
     ZebraHandle zh;
 
@@ -88,25 +88,10 @@ int main(int argc, char **argv)
     nmem_init ();
     
     zs = start_service("zebrazv.cfg"); 
+    assert(zs);
     zh = zebra_open (zs);
-    zebra_select_database(zh, "Default");
-    logf(LOG_LOG,"going to call init");
-    i=zebra_init(zh);
-    logf(LOG_LOG,"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);
-        exit(1);
-    }
-
-    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);
   
-    // yaz_log_init_level(LOG_ALL); 
+    init_data(zh, recs);
 
     zebra_close(zh);
 
@@ -134,5 +119,5 @@ int main(int argc, char **argv)
     nmem_exit ();
     xmalloc_trav ("x");
     logf(LOG_LOG,"============ ALL TESTS PASSED OK ============");
-    exit (0);
+    exit(0);
 }
index 7beea78..51f479f 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t2.c,v 1.12 2004-08-25 09:23:39 adam Exp $
+/* $Id: t2.c,v 1.13 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -23,45 +23,30 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <stdlib.h>
 #include <yaz/log.h>
 #include <idzebra/api.h>
+#include "testlib.h"
 
 /* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-static ZebraService start_service()
-{
-    char cfg[256];
-    char *srcdir = getenv("srcdir");
-    sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");
-    return zebra_start(cfg);
-}
 
 int main(int argc, char **argv)
 {
-    int exit_code = 0;
-    int hits;
     ZebraService zs;
     ZebraHandle zh;
-    const char *myrec =
+    const char *myrec[] = {
         "<gils>\n"
         "  <title>My title</title>\n"
-        "</gils>\n";
+        "</gils>\n",
+        0};
+
 
     yaz_log_init_file("t2.log");
 
     nmem_init ();
 
-    zs = start_service();
+    zs = start_service(0);
     zh = zebra_open (zs);
-    zebra_select_database(zh, "Default");
-    zebra_init(zh);
-    zebra_begin_trans (zh, 1);
-
-    zebra_add_record (zh, myrec, strlen(myrec));
+    init_data(zh,myrec);
 
-    zebra_search_PQF (zh, "@attr 1=4 my", "set1", &hits);
-    if (hits != 1)
-    {
-        yaz_log(LOG_FATAL, "Expected 1 hit. Got %d", hits);
-        exit_code = 1;
-    }
+    Query(__LINE__,zh, "@attr 1=4 my", 1);
 
     zebra_end_trans (zh);
     zebra_commit (zh);
@@ -70,5 +55,6 @@ int main(int argc, char **argv)
 
     nmem_exit ();
     xmalloc_trav ("x");
-    exit (exit_code);
+    logf(LOG_LOG,"================ All tests OK ");
+    exit (0);
 }
index 864cad3..adf8be1 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t3.c,v 1.10 2004-10-15 10:07:34 heikki Exp $
+/* $Id: t3.c,v 1.11 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -23,15 +23,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/log.h>
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
+#include "testlib.h"
 
-/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-static ZebraService start_service()
-{
-    char cfg[256];
-    char *srcdir = getenv("srcdir");
-    sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");
-    return zebra_start(cfg);
-}
        
 int main(int argc, char **argv)
 {
@@ -48,7 +41,7 @@ int main(int argc, char **argv)
 
     nmem_init ();
     
-    zs = start_service();
+    zs = start_service(0);
     zh = zebra_open (zs);
     zebra_select_database(zh, "Default");
     zebra_init(zh);
@@ -81,6 +74,7 @@ int main(int argc, char **argv)
         zebra_end_trans (zh);
         yaz_pqf_destroy(parser);
 #if 0
+        /*FIXME Why is this disabled ??? */
         zebra_records_retrieve (zh, odr_output, setname, 0,
                                 VAL_TEXT_XML, 1, &retrievalRecord);
 #endif
@@ -97,5 +91,6 @@ int main(int argc, char **argv)
 
     nmem_exit ();
     xmalloc_trav ("x");
+    logf(LOG_LOG,"================ All tests OK ");
     exit (0);
 }
index cb56b2a..4ee95db 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t4.c,v 1.9 2004-08-25 09:23:39 adam Exp $
+/* $Id: t4.c,v 1.10 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -23,43 +23,36 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/log.h>
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
+#include "testlib.h"
 
-/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-static ZebraService start_service()
-{
-    char cfg[256];
-    char *srcdir = getenv("srcdir");
-    sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");
-    return zebra_start(cfg);
-}
        
 int main(int argc, char **argv)
 {
     int i;
     ZebraService zs;
     ZebraHandle zh;
-    const char *myrec =
+    const char *myrec[] = {
         "<gils>\n"
         "  <title>My title</title>\n"
-        "</gils>\n";
+        "</gils>\n",
+        0};
 
     yaz_log_init_file("t4.log");
 
     nmem_init ();
     
-    zs = start_service();
+    zs = start_service(0);
     zh = zebra_open (zs);
-    zebra_init(zh);
-    zebra_select_database(zh, "Default");
+    init_data(zh,myrec);
 
     zebra_begin_trans (zh, 1);
     for (i = 0; i<1200; i++)
-       zebra_add_record (zh, myrec, strlen(myrec));
+       zebra_add_record (zh, myrec[0], strlen(myrec[0]));
     zebra_end_trans (zh);
     zebra_close(zh);
     zebra_stop(zs);
 
-    zs = start_service();
+    zs = start_service("");
     zh = zebra_open (zs);
     zebra_select_database(zh, "Default");
 
@@ -102,5 +95,6 @@ int main(int argc, char **argv)
 
     nmem_exit ();
     xmalloc_trav ("x");
+    logf(LOG_LOG,"================ All tests OK ");
     exit (0);
 }
index e5f4334..6a483d5 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t5.c,v 1.7 2004-10-20 14:32:29 heikki Exp $
+/* $Id: t5.c,v 1.8 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -24,36 +24,12 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
 
-/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-static ZebraService start_service()
-{
-    char cfg[256];
-    char *srcdir = getenv("srcdir");
-    sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");
-    return zebra_start(cfg);
-}
+#include "testlib.h"
+
        
-static void expect(ZebraHandle zh, const char *pqf, int hits_expect,
-                  int *exit_code)
-{
-    int hits;
-    if (zebra_search_PQF (zh, pqf, "set1", &hits) != 0)
-    {
-        yaz_log(LOG_FATAL, "Search %s: failed", pqf);
-        *exit_code = 1;
-    }
-    else if (hits != hits_expect)
-    {
-        yaz_log(LOG_FATAL, "Search %s: Expected %d, got %d", pqf,
-               hits_expect, hits);
-        *exit_code = 2;
-    }
-}
 
 int main(int argc, char **argv)
 {
-    int i;
-    int exit_code = 0;
     ZebraService zs;
     ZebraHandle zh;
     const char *myrec[] = {
@@ -68,43 +44,38 @@ int main(int argc, char **argv)
 
     nmem_init ();
     
-    zs = start_service();
+    zs = start_service(0);
     zh = zebra_open (zs);
-    zebra_select_database(zh, "Default");
-    zebra_init(zh);
-
-    zebra_begin_trans (zh, 1);
-    for (i = 0; myrec[i]; i++)
-       zebra_add_record (zh, myrec[i], strlen(myrec[i]));
-    zebra_end_trans (zh);
-
-    expect(zh, "@attr 1=4 my", 3, &exit_code);
-    expect(zh, "@attr 1=4 {my x}", 1, &exit_code);
-    expect(zh, "@attr 1=4 {my x}", 1, &exit_code);
-    expect(zh, "@attr 1=4 {x my}", 0, &exit_code);
-    expect(zh, "@attr 1=4 {my x title}", 1, &exit_code);
-    expect(zh, "@attr 1=4 {my title}", 2, &exit_code);
-    expect(zh, "@attr 1=4 @and x title", 2, &exit_code);
+    init_data(zh,myrec);
+
+    Query(__LINE__,zh, "@attr 1=4 my", 3);
+    Query(__LINE__,zh, "@attr 1=4 {my x}", 1);
+    Query(__LINE__,zh, "@attr 1=4 {my x}", 1);
+    Query(__LINE__,zh, "@attr 1=4 {x my}", 0);
+    Query(__LINE__,zh, "@attr 1=4 {my x title}", 1);
+    Query(__LINE__,zh, "@attr 1=4 {my title}", 2);
+    Query(__LINE__,zh, "@attr 1=4 @and x title", 2);
 
     /* exl=0 distance=2 order=1 relation=2 (<=), known, unit=word */
-    expect(zh, "@prox 0 2 1 2 k 2 my x", 2, &exit_code);
+    Query(__LINE__,zh, "@prox 0 2 1 2 k 2 my x", 2);
 
     /* exl=0 distance=2 order=1 relation=2 (<=), known, unit=word */
-    expect(zh, "@prox 0 2 1 2 k 2 x my", 0, &exit_code);
+    Query(__LINE__,zh, "@prox 0 2 1 2 k 2 x my", 0);
 
     /* exl=0 distance=2 order=0 relation=2 (<=), known, unit=word */
-    expect(zh, "@prox 0 2 0 2 k 2 x my", 2, &exit_code);
+    Query(__LINE__,zh, "@prox 0 2 0 2 k 2 x my", 2);
 
     /* exl=0 distance=2 order=0 relation=3 (=), known, unit=word */
-    expect(zh, "@prox 0 2 1 3 k 2 my x", 1, &exit_code);
+    Query(__LINE__,zh, "@prox 0 2 1 3 k 2 my x", 1);
 
     /* exl=1 distance=2 order=0 relation=3 (=), known, unit=word */
-    expect(zh, "@prox 1 2 1 3 k 2 my x", 1, &exit_code);
+    Query(__LINE__,zh, "@prox 1 2 1 3 k 2 my x", 1);
 
     zebra_close (zh);
     zebra_stop (zs);
 
     nmem_exit ();
     xmalloc_trav ("x");
-    exit (exit_code);
+    logf(LOG_LOG,"======== All tests OK");
+    exit (0);
 }
index 35a2cde..6052f7d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t6.c,v 1.3 2004-08-25 09:23:39 adam Exp $
+/* $Id: t6.c,v 1.4 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -26,14 +26,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
 
-/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-static ZebraService start_service()
-{
-    char cfg[256];
-    char *srcdir = getenv("srcdir");
-    sprintf(cfg, "%.200s%szebra6.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");
-    return zebra_start(cfg);
-}
+#include "testlib.h"
+
        
 int main(int argc, char **argv)
 {
@@ -47,7 +41,7 @@ int main(int argc, char **argv)
     
     srand(17);
     
-    zs = start_service();
+    zs = start_service("");
     zh = zebra_open(zs);
     zebra_select_database(zh, "Default");
     zebra_init(zh);
@@ -84,5 +78,6 @@ int main(int argc, char **argv)
 
     nmem_exit ();
     xmalloc_trav ("x");
+    logf(LOG_LOG,"========= all tests OK");
     exit (0);
 }
index 8d94065..2378a6a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t7.c,v 1.2 2004-10-01 09:13:06 heikki Exp $
+/* $Id: t7.c,v 1.3 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 2004
    Index Data Aps
 
@@ -25,23 +25,18 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/sortspec.h>
 #include <idzebra/api.h>
 
-/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-static ZebraService start_service()
-{
-    char cfg[256];
-    char *srcdir = getenv("srcdir");
-    sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");
-    return zebra_start(cfg);
-}
+#include "testlib.h"
+
        
 int main(int argc, char **argv)
 {
     ZebraService zs;
     ZebraHandle zh;
-    const char *myrec =
+    const char *recs[] = {
         "<gils>\n"
         "  <title>My title</title>\n"
-        "</gils>\n";
+        "</gils>\n",
+        0};
     const char *setname1="set1";
     const char *setname2="set2";
     const char *setname3="set3";
@@ -60,23 +55,29 @@ int main(int argc, char **argv)
 
     nmem_init ();
     
-    zs = start_service();
+    zs = start_service(""); /* default to zebra.cfg */
     zh = zebra_open (zs);
-    zebra_select_database(zh, "Default");
-    zebra_init(zh);
 
-    zebra_begin_trans (zh, 1);
-    zebra_add_record (zh, myrec, strlen(myrec));
-    zebra_end_trans (zh);
+    init_data(zh,recs);
+
 
     zebra_begin_trans (zh, 0);
         
     zebra_search_RPN (zh, odr_input, query, setname1, &hits);
 
     rc=zebra_sort(zh, odr_output, 1, &setname1, setname2, spec, &status);
-    if (rc) { printf("sort A returned %d %d \n",rc,status); exit(1);}
+    if (rc) 
+    { 
+        printf("sort A returned %d %d \n",rc,status); 
+        exit(1);
+    }
+    
     rc=zebra_sort(zh, odr_output, 1, &setname2, setname3, spec, &status);
-    if (rc) { printf("sort B returned %d %d \n",rc,status); exit(1);}
+    if (rc) 
+    { 
+        printf("sort B returned %d %d \n",rc,status); 
+        exit(1);
+    }
 
     zebra_end_trans (zh);
     yaz_pqf_destroy(parser);
@@ -84,7 +85,7 @@ int main(int argc, char **argv)
     /*
      zebra_deleleResultSet(zh, Z_DeleteRequest_list,
                           1, &setnamep, &status);
-    */
+    */  
     odr_destroy (odr_input);
     odr_destroy (odr_output);
 
@@ -94,5 +95,6 @@ int main(int argc, char **argv)
 
     nmem_exit ();
     xmalloc_trav ("x");
+    logf(LOG_LOG,"========= all tests OK");
     exit (0);
 }
index 67e996b..aabf47a 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t8.c,v 1.3 2004-10-24 13:34:45 adam Exp $
+/* $Id: t8.c,v 1.4 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -26,23 +26,12 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/log.h>
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
+#include "testlib.h"
 
 #define LOGLEVEL LOG_ALL 
-static int testno=1;
 
-/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-static ZebraService start_service()
-{
-    char cfg[256];
-    char *srcdir = getenv("srcdir");
-    sprintf(cfg, "%.200s%szebra8.cfg", 
-            srcdir ? srcdir : "", srcdir ? "/" : "");
-    return zebra_start(cfg);
-}
        
-static void insertdata(ZebraHandle zh)
-{
-    const char *rec1 =
+const char *recs[] = {
         "<gils>\n"
         "  <title>My title</title>\n"
         "  <abstract>test record with single coordset, negatives</abstract>\n"
@@ -52,8 +41,8 @@ static void insertdata(ZebraHandle zh)
         "    <North-Bounding-Coordinate>  49 </North-Bounding-Coordinate>\n"
         "    <South-Bounding-Coordinate>  31 </South-Bounding-Coordinate>\n"
         "  </Bounding-Coordinates></Spatial-Domain>"
-        "</gils>\n";
-    const char *rec2 =
+        "</gils>\n",
+
         "<gils>\n"
         "  <title>Another title</title>\n"
         "  <abstract>second test with two coord sets</abstract>\n"
@@ -69,57 +58,10 @@ static void insertdata(ZebraHandle zh)
         "    <North-Bounding-Coordinate>  41 </North-Bounding-Coordinate>\n"
         "    <South-Bounding-Coordinate>  25 </South-Bounding-Coordinate>\n"
         "  </Bounding-Coordinates></Spatial-Domain>"
-        "</gils>\n";
-
-    zebra_select_database(zh, "Default");
-    zebra_init(zh);
-    zebra_begin_trans (zh, 1);
-    zebra_add_record (zh, rec1, strlen(rec1));
-    zebra_add_record (zh, rec2, strlen(rec2));
-    zebra_end_trans (zh);
-
-}
-
-static void query( ZebraHandle zh, int lineno, char *qry, int expectedhits)
-{
-    ODR odr_input = odr_createmem (ODR_DECODE);    
-    YAZ_PQF_Parser parser = yaz_pqf_create();
-    Z_RPNQuery *query;
-                                      
-    int hits;
-    int thistest=testno++;
-    int rc;
-
-    logf(LOG_LOG,"Test %d (line %d): expecting  %d", 
-            thistest, lineno,expectedhits);
-    logf(LOG_LOG,"%s", qry);
-
-    query = yaz_pqf_parse(parser, odr_input, qry);
-    assert(query);
-    zebra_begin_trans (zh, 0);
+        "</gils>\n",
+        0};
         
 
-    logf(LOG_DEBUG,"calling search");
-    rc=zebra_search_RPN (zh, odr_input, query, "nameless", &hits);
-    logf(LOG_DEBUG,"search returned %d",rc);
-    if (rc)
-        printf("search returned %d",rc);
-    if (hits != expectedhits) 
-    {
-        printf( "FAIL: Test %d (line %d):\n"
-                "got %d hits, expected %d\n"
-                "in '%s'\n", 
-                thistest, lineno,hits, expectedhits, qry);
-        logf( LOG_FATAL, "FAIL: Test %d (line %d): got %d hits, expected %d\n",
-                thistest, lineno, hits,expectedhits);
-        exit(1);
-    }
-
-    zebra_end_trans (zh);
-    yaz_pqf_destroy(parser);
-    odr_destroy (odr_input);
-    logf(LOG_LOG,"Test %d ok",thistest);
-}
 
 
 int main(int argc, char **argv)
@@ -133,12 +75,12 @@ int main(int argc, char **argv)
 
     nmem_init ();
     
-    zs = start_service();
+    zs = start_service("zebra8.cfg");
     zh = zebra_open (zs);
 
-    insertdata(zh);
+    init_data(zh, recs);
 
-#define Q(q,n) query(zh,__LINE__,q,n)
+#define Q(q,n) Query(__LINE__,zh,q,n)
     /* couple of simple queries just to see that we have indexed the stuff */
     Q( "@attr 1=4 title",2 );
     Q( "title",2 );
index f22cfdb..07d1f45 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: t9.c,v 1.1 2004-10-28 10:37:15 heikki Exp $
+/* $Id: t9.c,v 1.2 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -25,7 +25,9 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <yaz/log.h>
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
+
 #include "testlib.h"
+
 #include "rankingrecords.h"
 
 #define qry(zh,query,hits,string,score) \
@@ -33,8 +35,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 int main(int argc, char **argv)
 {
-    int i;
-    char *addinfo;
     ZebraService zs;
     ZebraHandle zh;
 
@@ -45,21 +45,7 @@ int main(int argc, char **argv)
     
     zs = start_service(""); /* default to zebra.cfg */
     zh = zebra_open (zs);
-    zebra_select_database(zh, "Default");
-    logf(LOG_LOG,"going to call init");
-    i=zebra_init(zh);
-    logf(LOG_LOG,"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);
-        exit(1);
-    }
-
-    zebra_begin_trans (zh, 1);
-    for (i = 0; recs[i]; i++)
-       zebra_add_record (zh, recs[i], strlen(recs[i]));
-    zebra_end_trans (zh);
+    init_data(zh, recs);
 
     zebra_select_database(zh, "Default");
 
@@ -75,11 +61,11 @@ int main(int argc, char **argv)
             3, "third title", 895 );
 
     
-    zebra_commit (zh);
     zebra_close (zh);
     zebra_stop (zs);
 
     nmem_exit ();
     xmalloc_trav ("x");
-    exit (0);
+    logf(LOG_LOG,"============ ALL TESTS PASSED OK ============");
+    exit(0);
 }
index 6513bbf..e66ba92 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: testlib.c,v 1.1 2004-10-28 10:37:15 heikki Exp $
+/* $Id: testlib.c,v 1.2 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -22,6 +22,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 /** testlib - utilities for the api tests */
 
+#include <assert.h>
 #include <yaz/log.h>
 #include <yaz/pquery.h>
 #include <idzebra/api.h>
@@ -32,6 +33,7 @@ ZebraService start_service(char *cfgname)
 {
     char cfg[256];
     char *srcdir = getenv("srcdir");
+    ZebraService zs;
     if (!srcdir || ! *srcdir)
         srcdir=".";
     if (!cfgname || ! *cfgname )
@@ -39,34 +41,65 @@ ZebraService start_service(char *cfgname)
     /*sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : "");     */
 
     sprintf(cfg, "%.200s/%s",srcdir, cfgname);
-    return zebra_start(cfg);
+    zs=zebra_start(cfg);
+    if (!zs)
+    {
+        printf("zebra_start failed, probably because missing config file \n"
+               "check %s\n", cfg);
+        exit(9);
+    }
+    return zs;
 }
 
-/** 
- * 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 RankingQuery(int lineno, ZebraHandle zh, char *query, 
-          int exphits, char *firstrec, int firstscore )
+/** inits the database and inserts test data */
+
+void init_data( ZebraHandle zh, const char **recs)
 {
-    ZebraRetrievalRecord retrievalRecord[10];
-    ODR odr_output = odr_createmem (ODR_DECODE);    
-    ODR odr_input = odr_createmem (ODR_DECODE);    
-    YAZ_PQF_Parser parser = yaz_pqf_create();
-    Z_RPNQuery *rpn = yaz_pqf_parse(parser, odr_input, query);
+    int i;
+    char *addinfo;
+    assert(zh);
+    zebra_select_database(zh, "Default");
+    logf(LOG_LOG,"going to call init");
+    i=zebra_init(zh);
+    logf(LOG_LOG,"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);
+        exit(1);
+    }
+    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);
+
+}
+
+
+
+int Query(int lineno, ZebraHandle zh, char *query, int exphits)
+{
+    ODR odr;
+    YAZ_PQF_Parser parser;
+    Z_RPNQuery *rpn;
     const char *setname="rsetname";
     int hits;
     int rc;
-    int i;
         
+
     logf(LOG_LOG,"======================================");
     logf(LOG_LOG,"qry[%d]: %s", lineno, query);
+    odr=odr_createmem (ODR_DECODE);    
 
+    parser = yaz_pqf_create();
+    rpn = yaz_pqf_parse(parser, odr, query);
     if (!rpn) {
         printf("Error: Parse failed \n%s\n",query);
         exit(1);
     }
-    rc=zebra_search_RPN (zh, odr_input, rpn, setname, &hits);
+    rc=zebra_search_RPN (zh, odr, rpn, setname, &hits);
     if (rc) {
         printf("Error: search returned %d \n%s\n",rc,query);
         exit (1);
@@ -78,12 +111,29 @@ void RankingQuery(int lineno, ZebraHandle zh, char *query,
         exit (1);
     }
     yaz_pqf_destroy(parser);
+    odr_destroy (odr);
+    return hits;
+}
+
+
+/** 
+ * 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 RankingQuery(int lineno, ZebraHandle zh, char *query, 
+          int exphits, char *firstrec, int firstscore )
+{
+    ZebraRetrievalRecord retrievalRecord[10];
+    ODR odr_output = odr_createmem (ODR_ENCODE);    
+    const char *setname="rsetname";
+    int hits;
+    int rc;
+    int i;
+        
+    hits=Query(lineno, zh, query, exphits);
 
     for (i = 0; i<10; i++)
-    {
         retrievalRecord[i].position = i+1;
-        retrievalRecord[i].score = i+20000;
-    }
 
     rc=zebra_records_retrieve (zh, odr_output, setname, 0,
                      VAL_TEXT_XML, hits, retrievalRecord);
@@ -108,6 +158,5 @@ void RankingQuery(int lineno, ZebraHandle zh, char *query,
         exit(1);
     }
     odr_destroy (odr_output);
-    odr_destroy (odr_input);
 }
 
index cc60d2f..c8b928d 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: testlib.h,v 1.1 2004-10-28 10:43:38 heikki Exp $
+/* $Id: testlib.h,v 1.2 2004-10-28 15:24:36 heikki Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
    Index Data Aps
 
@@ -28,7 +28,15 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 
 /** read zebra.cfg from env var srcdir if it exists; otherwise current dir */
-ZebraService start_service();
+ZebraService start_service(char *cfgfile);
+
+/** initialises the zebra base and inserts some test data in it */
+void init_data( ZebraHandle zh, const char **recs);
+
+
+/** makes a query, and compares the number of hits to the expected */
+void Query(int lineno, ZebraHandle zh, char *query, int exphits);
+
 
 /** 
  * makes a query, checks number of hits, and for the first hit, that 
@@ -37,4 +45,3 @@ ZebraService start_service();
 void RankingQuery(int lineno, ZebraHandle zh, char *query, 
            int exphits, char *firstrec, int firstscore );
 
-