Added a test utility marcos TL_ASSERT, TL_ASSERT2
[idzebra-moved-to-github.git] / test / api / testlib.h
1 /* $Id: testlib.h,v 1.17 2005-12-15 13:28:32 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 /** testlib - utilities for the api tests */
24
25 #include <stdlib.h>
26 #include <yaz/yconfig.h>
27 #include <yaz/pquery.h>
28 #include <yaz/log.h>
29 #include <idzebra/api.h>
30
31 /** 
32  * start_up : Does all the usual start functions
33  *    - nmem_init
34  *    - build the name of logfile from argv[0], and open it
35  *      if no argv passed, do not open a log
36  *    - read zebra.cfg from env var srcdir if it exists; otherwise current dir 
37  *      default to zebra.cfg, if no name is given
38  */
39 ZebraService start_up(char *cfgname, int argc, char **argv);
40
41 /**
42  * get_srcdir : returns the source dir. Most often ".", but when
43  * making distcheck, some other dir 
44  */
45 const char *get_srcdir();
46
47 /** 
48  * start_log: open a log file 
49  */
50 /*    FIXME - parse command line arguments to set log levels etc */
51 void start_log(int argc, char **argv);
52
53 /** 
54  * start_service - do a zebra_start with a decent config name 
55  * Takes care of checking the environment for srcdir (as needed by distcheck)
56  * and uses that if need be. 
57  * The name defaults to zebra.cfg, if null or emtpy
58  */
59 ZebraService start_service(const char *cfgname);
60
61
62 /** 
63  * close_down closes it all down
64  * Does a zebra_close on zh, if not null.
65  * Does a zebra_stop on zs, if not null 
66  * Writes a log message, OK if retcode is zero, error if not
67  * closes down nmem and xmalloc
68  * returns the retcode, for use in return or exit in main()
69  */
70 int close_down(ZebraHandle zh, ZebraService zs, int retcode);
71
72 /** inits the database and inserts test data */
73 void init_data(ZebraHandle zh, const char **recs);
74
75
76 /**
77  * do_query does a simple query, and checks that the number of hits matches
78  */
79 int do_query(int lineno, ZebraHandle zh, const char *query, zint exphits);
80
81
82 /**
83  * do_query does a simple query, and checks that error is what is expected
84  */
85 int do_query_x(int lineno, ZebraHandle zh, const char *query, zint exphits,
86                int experror);
87
88 /**
89  * do_scan is a utility for scan testing 
90  */
91 void do_scan(int lineno, ZebraHandle zh, const char *query,
92              int pos, int num,  /* input params */
93              int exp_pos, int exp_num,  int exp_partial, /* expected result */
94              const char **exp_entries  /* expected entries (or NULL) */
95     );
96
97 void do_sort(ZebraHandle zh, const char *query, zint hits, zint *exp);
98
99 /** 
100  * ranking_query makes a query, checks number of hits, and for 
101  * the first hit, that it contains the given string, and that it 
102  * gets the right score
103  */
104 void ranking_query(int lineno, ZebraHandle zh, char *query, 
105           int exphits, char *firstrec, int firstscore );
106
107 /** 
108  * meta_query makes a query, checks number of hits, and for 
109  * checks that the all records in result set has the proper identifiers (ids)
110  */
111 void meta_query(int lineno, ZebraHandle zh, char *query, int exphits,
112                 zint *ids);
113
114 /**
115  * if filter given by name does not exist, exit nicely but warn in log 
116  */
117 void check_filter(ZebraService zs, const char *name);
118
119 #define TL_ASSERT(x) if (!(x)) { yaz_log(YLOG_FATAL, "%s:%d TL_ASSERT(%s) failed",  __FILE__, __LINE__, #x); exit(1); }
120 #define TL_ASSERT2(x,m) if (!(x)) { yaz_log(YLOG_WARN, "%s", m); yaz_log(YLOG_FATAL, "%s:%d TL_ASSERT(%s) failed",  __FILE__, __LINE__, #x); exit(1); }