New tests for numeric searches
[idzebra-moved-to-github.git] / test / api / t8.c
1 /* $Id: t8.c,v 1.1 2004-10-01 15:36:41 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 /* t8: test numeric attributes */
24
25 #include <assert.h>
26 #include <yaz/log.h>
27 #include <yaz/pquery.h>
28 #include <idzebra/api.h>
29
30 static int testno=1;
31
32 /* read zebra.cfg from env var srcdir if it exists; otherwise current dir */
33 static ZebraService start_service()
34 {
35     char cfg[256];
36     char *srcdir = getenv("srcdir");
37     sprintf(cfg, "%.200s%szebragils.cfg", 
38             srcdir ? srcdir : "", srcdir ? "/" : "");
39     return zebra_start(cfg);
40 }
41         
42 static void insertdata(ZebraHandle zh)
43 {
44     const char *rec1 =
45         "<gils>\n"
46         "  <title>My title</title>\n"
47         "  <abstract>test record with single coordset, negatives</abstract>\n"
48         "  <Spatial-Domain><Bounding-Coordinates>\n"
49         "    <West-Bounding-Coordinate> -120 </West-Bounding-Coordinate>\n"
50         "    <East-Bounding-Coordinate> -102 </East-Bounding-Coordinate>\n"
51         "    <North-Bounding-Coordinate>  49 </North-Bounding-Coordinate>\n"
52         "    <South-Bounding-Coordinate>  31 </South-Bounding-Coordinate>\n"
53         "  </Bounding-Coordinates></Spatial-Domain>"
54         "</gils>\n";
55     const char *rec2 =
56         "<gils>\n"
57         "  <title>Another title</title>\n"
58         "  <abstract>second test with two coord sets</abstract>\n"
59         "  <Spatial-Domain><Bounding-Coordinates>\n"
60         "    <West-Bounding-Coordinate> -120 </West-Bounding-Coordinate>\n"
61         "    <East-Bounding-Coordinate> -102 </East-Bounding-Coordinate>\n"
62         "    <North-Bounding-Coordinate>  49 </North-Bounding-Coordinate>\n"
63         "    <South-Bounding-Coordinate>  31 </South-Bounding-Coordinate>\n"
64         "  </Bounding-Coordinates>"
65         "  <Bounding-Coordinates>\n"
66         "    <West-Bounding-Coordinate> -125 </West-Bounding-Coordinate>\n"
67         "    <East-Bounding-Coordinate> -108 </East-Bounding-Coordinate>\n"
68         "    <North-Bounding-Coordinate>  41 </North-Bounding-Coordinate>\n"
69         "    <South-Bounding-Coordinate>  25 </South-Bounding-Coordinate>\n"
70         "  </Bounding-Coordinates></Spatial-Domain>"
71         "</gils>\n";
72
73     zebra_select_database(zh, "Default");
74     zebra_init(zh);
75     zebra_begin_trans (zh, 1);
76     zebra_add_record (zh, rec1, strlen(rec1));
77     zebra_add_record (zh, rec2, strlen(rec2));
78     zebra_end_trans (zh);
79
80 }
81
82 static void query( ZebraHandle zh, int lineno, char *qry, int expectedhits)
83 {
84     ODR odr_input = odr_createmem (ODR_DECODE);    
85     YAZ_PQF_Parser parser = yaz_pqf_create();
86     Z_RPNQuery *query;
87                                       
88     int hits;
89     int thistest=testno++;
90     int rc;
91
92     logf(LOG_LOG,"Test %d (line %d): expecting  %d", 
93             thistest, lineno,expectedhits);
94     logf(LOG_LOG,"%s", qry);
95
96     query = yaz_pqf_parse(parser, odr_input, qry);
97     assert(query);
98     zebra_begin_trans (zh, 0);
99         
100
101     logf(LOG_DEBUG,"calling search");
102     rc=zebra_search_RPN (zh, odr_input, query, "nameless", &hits);
103     logf(LOG_DEBUG,"search returned %d",rc);
104     if (rc)
105         printf("search returned %d",rc);
106     if (hits != expectedhits) 
107     {
108         printf( "FAIL: Test %d (line %d):\n"
109                 "got %d hits, expected %d\n"
110                 "in '%s'\n", 
111                 thistest, lineno,hits, expectedhits, qry);
112         logf( LOG_FATAL, "FAIL: Test %d (line %d): got %d hits, expected %d\n",
113                 thistest, lineno, hits,expectedhits);
114         exit(1);
115     }
116
117     zebra_end_trans (zh);
118     yaz_pqf_destroy(parser);
119     odr_destroy (odr_input);
120     logf(LOG_LOG,"Test %d ok",thistest);
121 }
122
123
124 int main(int argc, char **argv)
125 {
126     ZebraService zs;
127     ZebraHandle zh;
128     yaz_log_init_file("t8.log");
129     /* yaz_log_init_level(LOG_ALL); */
130
131     nmem_init ();
132     
133     zs = start_service();
134     zh = zebra_open (zs);
135
136     insertdata(zh);
137
138 #define Q(q,n) query(zh,__LINE__,q,n)
139     /* couple of simple queries just to see that we have indexed the stuff */
140     Q( "@attr 1=4 title",2 );
141     Q( "title",2 );
142     
143     /* 1=2038: West-Bounding-Coordinate 2039: East: 2040: North: 2041 South*/
144     /* 4=109: numeric string */
145     /* 2=3: equal  2=1: less, 2=4: greater or equal 2=5 greater */
146
147     /* N>25, search attributes work */
148     Q( "@attr 2=4 @attr gils 1=2040 @attr 4=109 25",2);
149
150     /* N=41, get rec1 only */
151     Q( "@attr 2=3 @attr gils 1=2040 @attr 4=109 41",1);
152
153     /* N=49, get both records */
154     Q( "@attr 2=3 @attr gils 1=2040 @attr 4=109 49",2);
155
156     /* W=-120 get both records */
157     Q( "@attr 2=3 @attr gils 1=2038 @attr 4=109 -120",2);
158
159     /* W<-122 get only rec1 */
160     Q( "@attr 2=1 @attr gils 1=2038 @attr 4=109 '-120' ",1);
161
162     /* N=41 and N=49 get only rec2 */
163     Q( "@attr 2=3 @attr gils 1=2040 @attr 4=109 \"41 49\" ",1);
164
165     zebra_commit (zh);
166     zebra_close (zh);
167     zebra_stop (zs);
168
169     nmem_exit ();
170     xmalloc_trav ("x");
171     logf(LOG_LOG,"All tests OK");
172     exit (0);
173 }