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