Use GCC attribute warn_unsed_result for a few of the most important
[idzebra-moved-to-github.git] / test / api / testlib.c
1 /* $Id: testlib.c,v 1.30 2006-03-31 15:58:05 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 #if HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28 #if HAVE_SYS_RESOURCE_H
29 #include <sys/resource.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include <assert.h>
36 #include <yaz/log.h>
37 #include <yaz/pquery.h>
38 #include <idzebra/api.h>
39 #include "testlib.h"
40
41 /** start_log: open a log file */
42 /*    FIXME - parse command line arguments to set log levels etc */
43 int log_level=0; /* not static, t*.c may use it */
44
45 void tl_start_log(int argc, char **argv)
46 {
47     int cmd_level = 0;
48     char logname[2048];
49     if (!argv) 
50         return;
51     if (!argv[0])
52         return;
53     sprintf(logname, "%s.log", argv[0]);
54     yaz_log_init_file(logname);
55     log_level = yaz_log_mask_str_x(argv[0], 0);
56     if (argc >= 2)
57         cmd_level |= yaz_log_mask_str_x(argv[1], 0);
58     yaz_log_init_level(YLOG_DEFAULT_LEVEL | log_level | cmd_level);
59     yaz_log(log_level, "starting %s", argv[0]);
60 }
61
62 /** 
63  * tl_start_up : do common start things, and a zebra_start
64  *    - nmem_init
65  *    - build the name of logfile from argv[0], and open it
66  *      if no argv passed, do not open a log
67  *    - read zebra.cfg from env var srcdir if it exists; otherwise current dir 
68  *      default to zebra.cfg, if no name is given
69  */
70 ZebraService tl_start_up(char *cfgname, int argc, char **argv)
71 {
72 #if HAVE_SYS_RESOURCE_H
73 #if HAVE_SYS_TIME_H
74     struct rlimit rlim;
75     rlim.rlim_cur = 20;
76     rlim.rlim_max = 20;
77     setrlimit(RLIMIT_CPU, &rlim);
78 #endif
79 #endif
80     nmem_init();
81     tl_start_log(argc, argv);
82     return tl_zebra_start(cfgname);
83 }
84
85 /**
86  * get_srcdir: return env srcdir or . (if does does not exist)
87  */
88 const char *tl_get_srcdir()
89 {
90     const char *srcdir = getenv("srcdir");
91     if (!srcdir || ! *srcdir)
92         srcdir = ".";
93     return srcdir;
94
95 }
96 /** tl_zebra_start - do a zebra_start with a decent config name */
97 ZebraService tl_zebra_start(const char *cfgname)
98 {
99     char cfg[256];
100     const char *srcdir = tl_get_srcdir();
101     if (!cfgname || ! *cfgname )
102         cfgname="zebra.cfg";
103
104     sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
105     return  zebra_start(cfg);
106 }
107
108 /** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
109 int tl_close_down(ZebraHandle zh, ZebraService zs)
110 {
111     if (zh)
112         zebra_close(zh);
113     if (zs)
114         zebra_stop(zs);
115
116     nmem_exit();
117     xmalloc_trav("x");
118     return 1;
119 }
120
121 /** inits the database and inserts test data */
122
123 int tl_init_data(ZebraHandle zh, const char **recs)
124 {
125     ZEBRA_RES res;
126
127     if (!zh)
128         return 0;
129
130     if (zebra_select_database(zh, "Default") != ZEBRA_OK)
131         return 0;
132
133     yaz_log(log_level, "going to call init");
134     res = zebra_init(zh);
135     if (res == ZEBRA_FAIL) 
136     {
137         yaz_log(log_level, "init_data: zebra_init failed with %d", res);
138         printf("init_data failed with %d\n", res);
139         return 0;
140     }
141     if (recs)
142     {
143         int i;
144         if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
145             return 0;
146         for (i = 0; recs[i]; i++)
147             zebra_add_record(zh, recs[i], strlen(recs[i]));
148         if (zebra_end_trans(zh) != ZEBRA_OK)
149             return 0;
150         zebra_commit(zh);
151     }
152     return 1;
153 }
154
155 int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
156 {
157     ODR odr;
158     YAZ_PQF_Parser parser;
159     Z_RPNQuery *rpn;
160     const char *setname="rsetname";
161     zint hits;
162     ZEBRA_RES rc;
163
164     yaz_log(log_level, "======================================");
165     yaz_log(log_level, "query: %s", query);
166     odr = odr_createmem (ODR_DECODE);
167     if (!odr)
168         return 0;
169
170     parser = yaz_pqf_create();
171     rpn = yaz_pqf_parse(parser, odr, query);
172     yaz_pqf_destroy(parser);
173     if (!rpn)
174     {
175         odr_destroy(odr);
176         return 0;
177     }
178
179     rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
180     if (experror)
181     {
182         int code;
183         if (rc != ZEBRA_FAIL)
184         {
185             yaz_log(log_level, "search returned %d (OK), but error was "
186                     "expected", rc);
187             printf("Error: search returned %d (OK), but error was expected\n"
188                    "%s\n",  rc, query);
189             return 0;
190         }
191         code = zebra_errCode(zh);
192         if (code != experror)
193         {
194             yaz_log(log_level, "search returned error code %d, but error %d "
195                     "was expected", code, experror);
196             printf("Error: search returned error code %d, but error %d was "
197                    "expected\n%s\n",
198                    code, experror, query);
199             return 0;
200         }
201     }
202     else
203     {
204         if (rc == ZEBRA_FAIL) {
205             int code = zebra_errCode(zh);
206             yaz_log(log_level, "search returned %d. Code %d", rc, code);
207             
208             printf("Error: search returned %d. Code %d\n%s\n", rc, 
209                    code, query);
210             return 0;
211         }
212         if (exphits != -1 && hits != exphits)
213         {
214             yaz_log(log_level, "search returned " ZINT_FORMAT 
215                    " hits instead of " ZINT_FORMAT, hits, exphits);
216             printf("Error: search returned " ZINT_FORMAT 
217                    " hits instead of " ZINT_FORMAT "\n%s\n",
218                    hits, exphits, query);
219             return 0;
220         }
221     }
222     odr_destroy(odr);
223     return 1;
224 }
225
226
227 int tl_query(ZebraHandle zh, const char *query, zint exphits)
228 {
229     return tl_query_x(zh, query, exphits, 0);
230 }
231
232 int tl_scan(ZebraHandle zh, const char *query,
233             int pos, int num,
234             int exp_pos, int exp_num, int exp_partial,
235             const char **exp_entries)
236 {
237     ODR odr = odr_createmem(ODR_ENCODE);
238     ZebraScanEntry *entries = 0;
239     int partial = -123;
240     ZEBRA_RES res;
241
242     yaz_log(log_level, "======================================");
243     yaz_log(log_level, "scan: pos=%d num=%d %s", pos, num, query);
244
245     res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial, 
246                          0 /* setname */);
247     if (res != ZEBRA_OK)
248     {
249         printf("Error: scan returned %d (FAIL), but no error was expected\n"
250                "%s\n",  res, query);
251         return 0;
252     }
253     else
254     {
255         int fails = 0;
256         if (partial == -123)
257         {
258             printf("Error: scan returned OK, but partial was not set\n"
259                    "%s\n", query);
260             fails++;
261         }
262         if (partial != exp_partial)
263         {
264             printf("Error: scan returned OK, with partial/expected %d/%d\n"
265                    "%s\n", partial, exp_partial, query);
266             fails++;
267         }
268         if (num != exp_num)
269         {
270             printf("Error: scan returned OK, with num/expected %d/%d\n"
271                    "%s\n", num, exp_num, query);
272             fails++;
273         }
274         if (pos != exp_pos)
275         {
276             printf("Error: scan returned OK, with pos/expected %d/%d\n"
277                    "%s\n", pos, exp_pos, query);
278             fails++;
279         }
280         if (fails)
281             return 0;
282         fails = 0;
283         if (exp_entries)
284         {
285             int i;
286             for (i = 0; i<num; i++)
287             {
288                 if (strcmp(exp_entries[i], entries[i].term))
289                 {
290                     printf("Error: scan OK, but entry %d term/exp %s/%s\n"
291                            "%s\n",
292                            i, entries[i].term, exp_entries[i], query);
293                     fails++;
294                 }
295             }
296         }
297         if (fails)
298             return 0;
299     }
300     odr_destroy(odr);
301     return 1;
302 }
303
304 /** 
305  * makes a query, checks number of hits, and for the first hit, that 
306  * it contains the given string, and that it gets the right score
307  */
308 int tl_ranking_query(ZebraHandle zh, char *query, 
309                      int exphits, char *firstrec, int firstscore)
310 {
311     ZebraRetrievalRecord retrievalRecord[10];
312     ODR odr_output = odr_createmem (ODR_ENCODE);    
313     const char *setname="rsetname";
314     int rc;
315     int i;
316         
317     if (!tl_query(zh, query, exphits))
318         return 0;
319
320     for (i = 0; i<10; i++)
321         retrievalRecord[i].position = i+1;
322
323     rc = zebra_records_retrieve (zh, odr_output, setname, 0,
324                                  VAL_TEXT_XML, exphits, retrievalRecord);
325     if (rc != ZEBRA_OK)
326         return 0;
327
328     if (!strstr(retrievalRecord[0].buf, firstrec))
329     {
330         printf("Error: Got the wrong record first\n");
331         printf("Expected '%s' but got\n", firstrec);
332         printf("%.*s\n", retrievalRecord[0].len, retrievalRecord[0].buf);
333         return 0;
334     }
335     
336     if (retrievalRecord[0].score != firstscore)
337     {
338         printf("Error: first rec got score %d instead of %d\n",
339                retrievalRecord[0].score, firstscore);
340         return 0;
341     }
342     odr_destroy (odr_output);
343     return 1;
344 }
345
346 int tl_meta_query(ZebraHandle zh, char *query, int exphits,
347                   zint *ids)
348 {
349     ZebraMetaRecord *meta;
350     ODR odr_output = odr_createmem (ODR_ENCODE);    
351     const char *setname="rsetname";
352     zint *positions = (zint *) malloc(1 + (exphits * sizeof(zint)));
353     int i;
354         
355     if (!tl_query(zh, query, exphits))
356         return 0;
357     
358     for (i = 0; i<exphits; i++)
359         positions[i] = i+1;
360
361     meta = zebra_meta_records_create (zh, setname,  exphits, positions);
362     
363     if (!meta)
364     {
365         printf("Error: retrieve returned error\n%s\n", query);
366         return 0;
367     }
368
369     for (i = 0; i<exphits; i++)
370     {
371         if (meta[i].sysno != ids[i])
372         {
373             printf("Expected id=" ZINT_FORMAT " but got id=" ZINT_FORMAT "\n",
374                    ids[i], meta[i].sysno);
375             return 0;
376         }
377     }
378     zebra_meta_records_destroy(zh, meta, exphits);
379     odr_destroy (odr_output);
380     free(positions);
381     return 1;
382 }
383
384 int tl_sort(ZebraHandle zh, const char *query, zint hits, zint *exp)
385 {
386     ZebraMetaRecord *recs;
387     zint i;
388     int errs = 0;
389     zint min_val_recs = 0;
390     zint min_val_exp = 0;
391
392     assert(query);
393     if (!tl_query(zh, query, hits))
394         return 0;
395
396     recs = zebra_meta_records_create_range (zh, "rsetname", 1, 4);
397     if (!recs)
398         return 0;
399
400     /* find min for each sequence to get proper base offset */
401     for (i = 0; i<hits; i++)
402     {
403         if (min_val_recs == 0 || recs[i].sysno < min_val_recs)
404             min_val_recs = recs[i].sysno;
405         if (min_val_exp == 0 || exp[i] < min_val_exp)
406             min_val_exp = exp[i];
407     }
408             
409     /* compare sequences using base offset */
410     for (i = 0; i<hits; i++)
411         if ((recs[i].sysno-min_val_recs) != (exp[i]-min_val_exp))
412             errs++;
413     if (errs)
414     {
415         printf("Sequence not in right order for query\n%s\ngot exp\n",
416                query);
417         for (i = 0; i<hits; i++)
418             printf(" " ZINT_FORMAT "   " ZINT_FORMAT "\n",
419                    recs[i].sysno, exp[i]);
420     }
421     zebra_meta_records_destroy (zh, recs, 4);
422
423     if (errs)
424         return 0;
425     return 1;
426 }
427
428
429 struct finfo {
430     const char *name;
431     int occurred;
432 };
433
434 static void filter_cb(void *cd, const char *name)
435 {
436     struct finfo *f = (struct finfo*) cd;
437     if (!strcmp(f->name, name))
438         f->occurred = 1;
439 }
440
441 void tl_check_filter(ZebraService zs, const char *name)
442 {
443     struct finfo f;
444
445     f.name = name;
446     f.occurred = 0;
447     zebra_filter_info(zs, &f, filter_cb);
448     if (!f.occurred)
449     {
450         yaz_log(YLOG_WARN, "Filter %s does not exist.", name);
451         exit(0);
452     }
453 }
454
455
456
457