+ sprintf(cfg, "%.200s/%.50s", srcdir, cfgname);
+ return zebra_start(cfg);
+}
+
+/** tl_close_down closes down the zebra, logfile, nmem, xmalloc etc. logs an OK */
+int tl_close_down(ZebraHandle zh, ZebraService zs)
+{
+ if (zh)
+ zebra_close(zh);
+ if (zs)
+ zebra_stop(zs);
+
+ nmem_exit();
+ xmalloc_trav("x");
+ return 1;
+}
+
+/** inits the database and inserts test data */
+
+int tl_init_data(ZebraHandle zh, const char **recs)
+{
+ ZEBRA_RES res;
+
+ if (!zh)
+ return 0;
+
+ if (zebra_select_database(zh, "Default") != ZEBRA_OK)
+ return 0;
+
+ yaz_log(log_level, "going to call init");
+ res = zebra_init(zh);
+ if (res == ZEBRA_FAIL)
+ {
+ yaz_log(log_level, "init_data: zebra_init failed with %d", res);
+ printf("init_data failed with %d\n", res);
+ return 0;
+ }
+ if (recs)
+ {
+ int i;
+ if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
+ return 0;
+ for (i = 0; recs[i]; i++)
+ zebra_add_record(zh, recs[i], strlen(recs[i]));
+ if (zebra_end_trans(zh) != ZEBRA_OK)
+ return 0;
+ zebra_commit(zh);
+ }
+ return 1;
+}
+
+int tl_query_x(ZebraHandle zh, const char *query, zint exphits, int experror)
+{
+ ODR odr;
+ YAZ_PQF_Parser parser;
+ Z_RPNQuery *rpn;
+ const char *setname="rsetname";
+ zint hits;
+ ZEBRA_RES rc;
+
+ yaz_log(log_level, "======================================");
+ yaz_log(log_level, "query: %s", query);
+ odr = odr_createmem (ODR_DECODE);
+ if (!odr)
+ return 0;
+
+ parser = yaz_pqf_create();
+ rpn = yaz_pqf_parse(parser, odr, query);
+ yaz_pqf_destroy(parser);
+ if (!rpn)
+ {
+ yaz_log(log_level, "could not parse pqf query %s\n", query);
+ printf("could not parse pqf query %s\n", query);
+ odr_destroy(odr);
+ return 0;
+ }
+
+ rc = zebra_search_RPN(zh, odr, rpn, setname, &hits);
+ if (experror)
+ {
+ int code;
+ if (rc != ZEBRA_FAIL)
+ {
+ yaz_log(log_level, "search returned %d (OK), but error was "
+ "expected", rc);
+ printf("Error: search returned %d (OK), but error was expected\n"
+ "%s\n", rc, query);
+ return 0;
+ }
+ code = zebra_errCode(zh);
+ if (code != experror)
+ {
+ yaz_log(log_level, "search returned error code %d, but error %d "
+ "was expected", code, experror);
+ printf("Error: search returned error code %d, but error %d was "
+ "expected\n%s\n",
+ code, experror, query);
+ return 0;
+ }
+ }
+ else
+ {
+ if (rc == ZEBRA_FAIL) {
+ int code = zebra_errCode(zh);
+ yaz_log(log_level, "search returned %d. Code %d", rc, code);
+
+ printf("Error: search returned %d. Code %d\n%s\n", rc,
+ code, query);
+ return 0;
+ }
+ if (exphits != -1 && hits != exphits)
+ {
+ yaz_log(log_level, "search returned " ZINT_FORMAT
+ " hits instead of " ZINT_FORMAT, hits, exphits);
+ printf("Error: search returned " ZINT_FORMAT
+ " hits instead of " ZINT_FORMAT "\n%s\n",
+ hits, exphits, query);
+ return 0;
+ }
+ }
+ odr_destroy(odr);
+ return 1;
+}
+
+
+int tl_query(ZebraHandle zh, const char *query, zint exphits)
+{
+ return tl_query_x(zh, query, exphits, 0);
+}
+
+int tl_scan(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: pos=%d num=%d %s", pos, num, query);
+
+ res = zebra_scan_PQF(zh, odr, query, &pos, &num, &entries, &partial,
+ 0 /* setname */);
+ if (res != ZEBRA_OK)
+ {
+ printf("Error: scan returned %d (FAIL), but no error was expected\n"
+ "%s\n", res, query);
+ return 0;
+ }
+ 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)
+ return 0;
+ 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)
+ return 0;
+ }
+ odr_destroy(odr);
+ return 1;