X-Git-Url: http://git.indexdata.com/?p=idzebra-moved-to-github.git;a=blobdiff_plain;f=dict%2Fscantest.c;h=5da634b2a84cc4d2fffead6b7bf762dc95ea9098;hp=732aab53e1bedba7780d9f15a05bbbe153f060da;hb=5d536c8cf5400b1e4da91061cf736a9ab53e5bd1;hpb=ecb3935e78cd9bcfdebafdee0834cfb1060d7b5e diff --git a/dict/scantest.c b/dict/scantest.c index 732aab5..5da634b 100644 --- a/dict/scantest.c +++ b/dict/scantest.c @@ -1,8 +1,5 @@ -/* $Id: scantest.c,v 1.5 2006-05-10 08:13:18 adam Exp $ - Copyright (C) 1995-2005 - Index Data ApS - -This file is part of the Zebra server. +/* This file is part of the Zebra server. + Copyright (C) 1994-2011 Index Data Zebra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -15,22 +12,27 @@ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License -along with Zebra; see the file LICENSE.zebra. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#if HAVE_CONFIG_H +#include +#endif #include #include #include #include - +#include #include #include struct handle_info { int b; int a; + int start_cut; + int end_cut; char **ar; }; @@ -43,48 +45,90 @@ static int handler(char *name, const char *info, int pos, void *client) else idx = -pos - 1; -#if 0 - printf ("pos=%d idx=%d name=%s\n", pos, idx, name); -#endif + yaz_log(YLOG_DEBUG, "pos=%d idx=%d name=%s", pos, idx, name); if (idx < 0) return 0; + if (idx < hi->start_cut || idx >= hi->end_cut) + { + return 1; + } hi->ar[idx] = malloc(strlen(name)+1); strcpy(hi->ar[idx], name); + return 0; } -int tst(Dict dict, int before, int after, char *scan_term, char **cmp_strs, - int verbose) +int do_scan(Dict dict, int before, int after, const char *sterm, + char **cmp_strs, + int verbose, int start_cut, int end_cut) { struct handle_info hi; + char scan_term[1024]; + int i; int errors = 0; + + strcpy(scan_term, sterm); + hi.start_cut = start_cut; + hi.end_cut = end_cut; hi.a = after; hi.b = before; hi.ar = malloc(sizeof(char*) * (after+before+1)); for (i = 0; i= start_cut && i < end_cut) + { + if (!hi.ar[i]) + { + printf ("--> FAIL i=%d hi.ar[i] == NULL\n", i); + errors++; + } + } + else + { + if (hi.ar[i]) + { + printf ("--> FAIL i=%d hi.ar[i] = %s\n", i, hi.ar[i]); + errors++; + } + } + } + else { - if (!cmp_strs[i]) - { - printf ("--> FAIL cmp_strs == NULL\n"); - errors++; - } - else if (!hi.ar[i]) - { - printf ("--> FAIL strs == NULL\n"); - errors++; - } - else if (strcmp(cmp_strs[i], hi.ar[i])) - { - printf ("--> FAIL expected %s\n", cmp_strs[i]); - errors++; - } + if (i >= start_cut && i < end_cut) + { + if (!hi.ar[i]) + { + printf ("--> FAIL i=%d strs == NULL\n", i); + errors++; + } + else if (!cmp_strs[i]) + { + printf ("--> FAIL i=%d cmp_strs == NULL\n", i); + errors++; + } + else if (strcmp(cmp_strs[i], hi.ar[i])) + { + printf ("--> FAIL i=%d expected %s\n", i, cmp_strs[i]); + errors++; + } + } + else + { + if (hi.ar[i]) + { + printf ("--> FAIL i=%d hi.ar[i] != NULL\n", i); + errors++; + } + } } if (verbose || errors) { @@ -105,17 +149,68 @@ int tst(Dict dict, int before, int after, char *scan_term, char **cmp_strs, return errors; } +static void tst(Dict dict, int start, int number) +{ + int i; + + /* insert again with original value again */ + for (i = start; i < number; i += 100) + { + int v = i; + char w[32]; + sprintf(w, "%d", i); + YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 2); + } + /* insert again with different value */ + for (i = start; i < number; i += 100) + { + int v = i-1; + char w[32]; + sprintf(w, "%d", i); + YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1); + } + /* insert again with original value again */ + for (i = start; i < number; i += 100) + { + int v = i; + char w[32]; + sprintf(w, "%d", i); + YAZ_CHECK_EQ(dict_insert(dict, w, sizeof(v), &v), 1); + } + + { + char *cs[] = { + "4497", + "4498", + "4499", + "45"}; + YAZ_CHECK_EQ(do_scan(dict, 2, 2, "4499", cs, 0, 0, 3), 0); + } + { + char *cs[] = { + "4498", + "4499", + "45", + "450"}; + YAZ_CHECK_EQ(do_scan(dict, 2, 2, "45", cs, 0, 0, 3), 0); + } + + for (i = 0; i < 20; i++) + YAZ_CHECK_EQ(do_scan(dict, 20, 20, "45", 0, 0, 20-i, 20+i), 0); +} + int main(int argc, char **argv) { - BFiles bfs; - Dict dict; + BFiles bfs = 0; + Dict dict = 0; int i; - int errors = 0; int ret; int before = 0, after = 0, number = 10000; char scan_term[1024]; char *arg; + YAZ_CHECK_INIT(argc, argv); + strcpy(scan_term, "1004"); while ((ret = options("b:a:t:n:v:", argv, argc, &arg)) != -2) { @@ -130,7 +225,12 @@ int main(int argc, char **argv) after = atoi(arg); break; case 't': - strcpy(scan_term, arg); + if (strlen(arg) >= sizeof(scan_term)-1) + { + fprintf(stderr, "scan term too long\n"); + exit(1); + } + strcpy(scan_term, arg); break; case 'n': number = atoi(arg); @@ -141,54 +241,39 @@ int main(int argc, char **argv) } bfs = bfs_create(".:100M", 0); - if (!bfs) + YAZ_CHECK(bfs); + if (bfs) { - fprintf(stderr, "bfs_create failed\n"); - exit(1); + bf_reset(bfs); + dict = dict_open(bfs, "dict", 10, 1, 0, 0); + YAZ_CHECK(dict); } - dict = dict_open(bfs, "dict", 10, 1, 0, 0); - for (i = 10; i 0 || before > 0) - tst(dict, before, after, scan_term, 0, 1); - else - { - if (argc <= 1) - { - char *cs[] = { - "4497", - "4498", - "4499", - "45"}; - strcpy(scan_term, "4499"); - errors += tst(dict, 2, 2, scan_term, cs, 0); - } - if (argc <= 1) - { - char *cs[] = { - "4498", - "4499", - "45", - "450"}; - strcpy(scan_term, "45"); - errors += tst(dict, 2, 2, scan_term, cs, 0); - } + if (after > 0 || before > 0) + do_scan(dict, before, after, scan_term, 0, 1, 0, after+1); + else + tst(dict, start, number); + + dict_close(dict); } - dict_close(dict); - bfs_destroy(bfs); - if (errors) - exit(1); - exit(0); + if (bfs) + bfs_destroy(bfs); + YAZ_CHECK_TERM; } /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil * End: * vim: shiftwidth=4 tabstop=8 expandtab