X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;ds=sidebyside;f=test%2Fapi%2Ftestlib.c;h=52173d3ad8e69e94f06cee7f33df8c1ff6122895;hb=a47789869bfde93b798a773df1762fb7c76fe665;hp=e66ba92d59c939c33210ee5ae8cc60e1e436b2f1;hpb=4989a5fc444f3bca0f094f1ac2bfe4f04af5e666;p=idzebra-moved-to-github.git diff --git a/test/api/testlib.c b/test/api/testlib.c index e66ba92..52173d3 100644 --- a/test/api/testlib.c +++ b/test/api/testlib.c @@ -1,4 +1,4 @@ -/* $Id: testlib.c,v 1.2 2004-10-28 15:24:36 heikki Exp $ +/* $Id: testlib.c,v 1.4 2004-11-04 13:10:07 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004 Index Data Aps @@ -26,9 +26,41 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include #include +#include "testlib.h" +/** start_log: open a log file */ +/* FIXME - parse command line arguments to set log levels etc */ +static int loglevel=0; +void start_log(int argc, char **argv) +{ + char logname[2048]; + if (!argv) + return; + if (!argv[0]) + return; + sprintf(logname, "%s.log", argv[0]); + yaz_log_init_file(logname); + loglevel = yaz_log_mask_str_x(argv[0],0); + yaz_log_init_level(LOG_DEFAULT_LEVEL | loglevel); + logf(loglevel,"starting %s",argv[0]); +} + +/** + * start_up : do common start things, and a zebra_start + * - nmem_init + * - build the name of logfile from argv[0], and open it + * if no argv passed, do not open a log + * - read zebra.cfg from env var srcdir if it exists; otherwise current dir + * default to zebra.cfg, if no name is given + */ +ZebraService start_up(char *cfgname, int argc, char **argv) +{ + nmem_init(); + start_log(argc, argv); + return start_service(cfgname); +} -/* read zebra.cfg from env var srcdir if it exists; otherwise current dir */ +/** start_service - do a zebra_start with a decent config name */ ZebraService start_service(char *cfgname) { char cfg[256]; @@ -38,7 +70,6 @@ ZebraService start_service(char *cfgname) srcdir="."; if (!cfgname || ! *cfgname ) cfgname="zebra.cfg"; - /*sprintf(cfg, "%.200s%szebra.cfg", srcdir ? srcdir : "", srcdir ? "/" : ""); */ sprintf(cfg, "%.200s/%s",srcdir, cfgname); zs=zebra_start(cfg); @@ -51,6 +82,24 @@ ZebraService start_service(char *cfgname) return zs; } + +/** close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */ +int close_down(ZebraHandle zh, ZebraService zs, int retcode) +{ + if (zh) + zebra_close(zh); + if (zs) + zebra_stop(zs); + + if (retcode) + logf(loglevel,"========= Exiting with return code %d", retcode); + else + logf(loglevel,"========= All tests OK"); + nmem_exit(); + xmalloc_trav("x"); + return retcode; +} + /** inits the database and inserts test data */ void init_data( ZebraHandle zh, const char **recs) @@ -59,9 +108,9 @@ void init_data( ZebraHandle zh, const char **recs) char *addinfo; assert(zh); zebra_select_database(zh, "Default"); - logf(LOG_LOG,"going to call init"); + logf(LOG_DEBUG,"going to call init"); i=zebra_init(zh); - logf(LOG_LOG,"init returned %d",i); + logf(LOG_DEBUG,"init returned %d",i); if (i) { printf("init failed with %d\n",i); @@ -69,17 +118,20 @@ void init_data( ZebraHandle zh, const char **recs) 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); + 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); + } } -int Query(int lineno, ZebraHandle zh, char *query, int exphits) +int do_query(int lineno, ZebraHandle zh, char *query, int exphits) { ODR odr; YAZ_PQF_Parser parser; @@ -89,8 +141,8 @@ int Query(int lineno, ZebraHandle zh, char *query, int exphits) int rc; - logf(LOG_LOG,"======================================"); - logf(LOG_LOG,"qry[%d]: %s", lineno, query); + logf(loglevel,"======================================"); + logf(loglevel,"qry[%d]: %s", lineno, query); odr=odr_createmem (ODR_DECODE); parser = yaz_pqf_create(); @@ -120,7 +172,7 @@ int Query(int lineno, ZebraHandle zh, char *query, int exphits) * 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, +void ranking_query(int lineno, ZebraHandle zh, char *query, int exphits, char *firstrec, int firstscore ) { ZebraRetrievalRecord retrievalRecord[10]; @@ -130,7 +182,7 @@ void RankingQuery(int lineno, ZebraHandle zh, char *query, int rc; int i; - hits=Query(lineno, zh, query, exphits); + hits=do_query(lineno, zh, query, exphits); for (i = 0; i<10; i++) retrievalRecord[i].position = i+1;