Merge icu_chain into zebramaps.
[idzebra-moved-to-github.git] / util / tst_index_types.c
1 /* $Id: tst_index_types.c,v 1.3 2007-10-25 19:25:00 adam Exp $
2    Copyright (C) 1995-2007
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <yaz/test.h>
24 #include <index_types.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 const char *xml_str = 
29 "    <indextypes>"
30 "      <indextype id=\"*:w:el\" position=\"1\" alwaysmatches=\"1\" firstinfield=\"1\"\n"
31 "       locale=\"el\">\n"
32 "        <simple/>\n"
33 "      </indextype>\n"
34 "      <indextype id=\"*:w\" position=\"1\" alwaysmatches=\"1\" firstinfield=\"1\"\n"
35 "       locale=\"en\">\n"
36 "        <simple/>\n"
37 "      </indextype>\n"
38 "      <indextype id=\"*:p\" position=\"0\" alwaysmatches=\"0\" firstinfield=\"0\"\n"
39 "        locale=\"en\">\n"
40 "        <simple/>\n"
41 "      </indextype>\n"
42 "      <indextype id=\"*:s\" sort=\"1\" \n"
43 "        locale=\"en\">\n"
44 "        <simple/>\n"
45 "      </indextype>\n"
46 "    </indextypes>\n"
47 ;
48
49 int compare_lookup(zebra_index_types_t r, const char *id,
50                    const char *expected_id)
51 {
52     const char *got_id = zebra_index_type_lookup_str(r, id);
53     if (!got_id && !expected_id)
54         return 1;  /* none expected */
55
56     if (got_id && expected_id && !strcmp(got_id, expected_id))
57         return 1;
58     return 0;
59 }
60
61 void tst1(void)
62 {
63 #if YAZ_HAVE_XML2
64     xmlDocPtr doc = xmlParseMemory(xml_str, strlen(xml_str));
65
66     YAZ_CHECK(doc);
67     if (doc)
68     {
69         zebra_index_types_t rules = zebra_index_types_create_doc(doc);
70         zebra_index_type_t type;
71         YAZ_CHECK(rules);
72
73         if (!rules)
74             return ;
75         
76         YAZ_CHECK(compare_lookup(rules, "title:s", "*:s"));
77         YAZ_CHECK(compare_lookup(rules, "title:sx", 0));
78         YAZ_CHECK(compare_lookup(rules, "title:Sx", 0));
79         YAZ_CHECK(compare_lookup(rules, "any:w", "*:w"));
80         YAZ_CHECK(compare_lookup(rules, "any:w:en", 0));
81         YAZ_CHECK(compare_lookup(rules, "any:w:el", "*:w:el"));
82         
83         {
84             int i, iter = 3333;
85             for (i = 0; i < iter; i++)
86             {
87                 compare_lookup(rules, "title:s", "*:s");
88                 compare_lookup(rules, "title:sx", 0);
89                 compare_lookup(rules, "title:Sx", 0);
90             }
91         }
92
93         type = zebra_index_type_get(rules, "any:w");
94         YAZ_CHECK(type);
95         if (type)
96         {
97             const char *buf = " How are you?";
98             size_t len = strlen(buf);
99             int r = 1;
100
101             if (r)
102             {
103                 const char *result_buf = 0;
104                 size_t result_len = 0;
105                 r = zebra_index_type_tokenize(type, buf, len,
106                                           &result_buf, &result_len);
107                 YAZ_CHECK_EQ(r, 1);
108                 YAZ_CHECK(result_len == 3 &&
109                           !memcmp(result_buf, "how", result_len));
110             }
111
112             if (r)
113             {
114                 const char *result_buf = 0;
115                 size_t result_len = 0;
116                 r = zebra_index_type_tokenize(type, 0,  0,
117                                               &result_buf, &result_len);
118                 YAZ_CHECK_EQ(r, 1);
119                 YAZ_CHECK(result_len == 3 && 
120                           !memcmp(result_buf, "are", result_len));
121             }            
122
123             if (r)
124             {
125                 const char *result_buf = 0;
126                 size_t result_len = 0;
127                 r = zebra_index_type_tokenize(type, 0,  0,
128                                               &result_buf, &result_len);
129                 YAZ_CHECK_EQ(r, 1);
130                 YAZ_CHECK(result_len == 3 && 
131                           !memcmp(result_buf, "you", result_len));
132             }            
133
134             if (r)
135             {
136                 const char *result_buf = 0;
137                 size_t result_len = 0;
138                 r = zebra_index_type_tokenize(type, 0,  0,
139                                               &result_buf, &result_len);
140                 YAZ_CHECK_EQ(r, 0);
141             }            
142         }
143         zebra_index_types_destroy(rules);
144     }
145 #else
146     zebra_index_types_t rules = zebra_index_types_create_doc(doc);
147     YAZ_CHECK(!rules);
148 #endif
149 }
150
151 int main(int argc, char **argv)
152 {
153     YAZ_CHECK_INIT(argc, argv);
154     YAZ_CHECK_LOG();
155
156     tst1();
157
158     YAZ_CHECK_TERM;
159 }
160
161 /*
162  * Local variables:
163  * c-basic-offset: 4
164  * indent-tabs-mode: nil
165  * End:
166  * vim: shiftwidth=4 tabstop=8 expandtab
167  */
168