Fixed bug #687: Missing log lines. Immediate logging (i.e. flush) is no
[yaz-moved-to-github.git] / test / nfaxmltest1.c
1 /*  Copyright (C) 2006, Index Data ApS
2  *  See the file LICENSE for details.
3  *
4  *  $Id: nfaxmltest1.c,v 1.8 2006-10-04 16:59:34 mike Exp $
5  *
6  */
7
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <yaz/nfa.h>
12 #include <yaz/nmem.h>
13 #include <yaz/test.h>
14 #include <yaz/nfaxml.h>
15
16 #if YAZ_HAVE_XML2
17 #include <libxml/parser.h>
18
19
20 /** \brief  Test parsing of a minimal, valid xml string */
21 void test1(void) {
22     char *xmlstr = "<ruleset> "
23                    "<rule> "
24                    "  <fromstring>foo</fromstring> "
25                    "  <tostring>bar</tostring> "
26                    "</rule>"
27                    "</ruleset>";
28     yaz_nfa *nfa = yaz_nfa_parse_xml_memory(xmlstr,"test1");
29     YAZ_CHECK(nfa);
30     yaz_nfa_destroy(nfa);
31 }
32
33
34 /** \brief  Test parsing of a minimal, invalid xml string */
35 void test2(void) {
36     yaz_nfa *nfa;
37     char *xmlstr = "<ruleset> "
38                    "<rule> "
39                    "  <fromstring>foo</fromstring> "
40                    "  <tostring>bar</tostring> "
41                    "</rule>";
42                  /* missing "</ruleset>" */
43     yaz_log(YLOG_LOG,"Parsing bad xml, expecting errors:");
44     nfa = yaz_nfa_parse_xml_memory(xmlstr,"test2");
45     YAZ_CHECK(!nfa);
46     nfa = yaz_nfa_parse_xml_memory(0,"test2-null");
47     YAZ_CHECK(!nfa);
48 }
49
50 /** \brief  Test parsing a few minimal xml files */
51 void test3(void) {
52     char *goodfilenames[] = {
53              "nfaxml-simple.xml",
54              "nfaxml-main.xml", /* x-includes nfaxml-include */
55               0};
56     char *badfilenames[] = {
57              "nfaxml-missing.xml",  /* file not there at all */
58              "nfaxml-badinclude.xml",  /* bad xinclude in it */
59               0};
60     yaz_nfa *nfa;
61     char **f = goodfilenames;
62     do {
63         yaz_log(YLOG_LOG,"Parsing (good) xml file '%s'", *f);
64         nfa=yaz_nfa_parse_xml_file(*f);
65         YAZ_CHECK_TODO(nfa);  /* fails on make distcheck, can't find the files*/
66     } while (*++f);
67
68     f = badfilenames;
69     do {
70         yaz_log(YLOG_LOG,"Parsing bad xml file '%s'. Expecting errors", *f);
71         nfa = yaz_nfa_parse_xml_file(*f);
72         YAZ_CHECK(!nfa);
73     } while (*++f);
74 }
75
76 /** \brief  Test parsing of a few minimal xml strings, with logical errors */
77 void test4(void) {
78     yaz_nfa *nfa;
79     char *xmls[] = { 
80       /*a*/"<missingruleset>   <foo/>   </missingruleset>",
81       /*b*/"<ruleset> <missingrule/> </ruleset>",
82       /*c*/"<ruleset> <rule> <garbagerule/> </rule> </ruleset>",
83       /*d*/"<ruleset><rule>"
84               "<fromstring>MissingTo</fromstring>"
85            "</rule></ruleset>",
86       /*e*/"<ruleset><rule>"
87               "<fromstring>DuplicateFrom</fromstring>"
88               "<fromstring>Another Fromstring</fromstring>"
89            "</rule></ruleset>",
90       /*f*/"<ruleset><rule>"
91               "<tostring>MissingFrom</tostring>"
92            "</rule></ruleset>",
93       /*g*/"<ruleset><rule>"
94               "<tostring>DuplicateTo</tostring>"
95               "<tostring>AnotherTo</tostring>"
96            "</rule></ruleset>",
97       /*h*/"<ruleset><rule>"
98               "<fromstring>GoodUTF:æøå</fromstring>"
99            "</rule></ruleset>",
100       /*i*/"<ruleset><rule>"
101               "<tostring>BadUtf8:Ø</tostring>"
102            "</rule></ruleset>",
103       /*j*/"<ruleset>"
104              "<rule>"
105                "<fromstring>ConflictingRules</fromstring>"
106                "<tostring>IdenticalStrings</tostring>"
107              "</rule>"
108              "<rule>"
109                "<fromstring>ConflictingRules</fromstring>"
110                "<tostring>IdenticalStrings</tostring>"
111              "</rule>"
112            "</ruleset>",
113       /*k*/"", /* empty string! */
114       /*l*/"<ruleset>"
115              "<rule>"
116                "<fromrange>A-Z</fromrange>"
117                "<torange>a-x</torange>"
118              "</rule>"
119            "</ruleset>",
120               0 };
121     char **xmlp=xmls;    
122     char label[]= { 'a', 0 };
123     while ( *xmlp ) {
124         yaz_log(YLOG_LOG,"test4-%s: Parsing bad xml, expecting errors:",
125                 label);
126         nfa = yaz_nfa_parse_xml_memory(*xmlp,label);
127         YAZ_CHECK(!nfa);
128         xmlp++;
129         label[0]++; 
130     }
131 } /* test4 */
132
133 static void test5(void) {
134     struct conv_test {
135         unsigned char *name;
136         int expresult;
137         unsigned char *xml;
138         unsigned char *from;
139         unsigned char *to;
140     };
141     struct conv_test tests[]= {
142         { "test5-1",  YAZ_NFA_SUCCESS,
143              "<ruleset>"
144                "<rule>"
145                  "<fromstring>foo</fromstring>"
146                  "<tostring>bar</tostring>"
147                "</rule>"
148              "</ruleset>",
149              "this is a foo test ofoofo fo foofoo fofoofooofoooo ",
150              "this is a bar test obarfo fo barbar fobarbarobaroo "
151         },
152         { "test5-2",  YAZ_NFA_SUCCESS,
153              "<ruleset>"
154                "<rule>"
155                  "<fromstring>ooooo</fromstring>"
156                  "<tostring>five </tostring>"
157                "</rule>"
158                "<rule>"
159                  "<fromstring>oooo</fromstring>"
160                  "<tostring>four </tostring>"
161                "</rule>"
162                "<rule>"
163                  "<fromstring>ooo</fromstring>"
164                  "<tostring>three </tostring>"
165                "</rule>"
166                "<rule>"
167                  "<fromstring>oo</fromstring>"
168                  "<tostring>two </tostring>"
169                "</rule>"
170              "</ruleset>",
171              "oo-oooo-",
172              "two -four -"
173         },
174         { "test5-4",  YAZ_NFA_SUCCESS, 0, /* same xml */
175              "oo-oooo-ooooooo-",
176              "two -four -five two -"
177         },
178         { "test5-3",  YAZ_NFA_OVERRUN, 0, /* could match further oo's */
179              "oo-oooo-ooooooo",
180              "two -four -five "  
181         },
182         { "test5-4 (lowercase)",  YAZ_NFA_SUCCESS,
183              "<ruleset>"
184                "<rule>"
185                  "<fromrange>A-Z</fromrange>"
186                  "<torange>a-z</torange>"
187                "</rule>"
188              "</ruleset>",
189              "LowerCase TEST with A-Z and a-z",
190              "lowercase test with a-z and a-z"
191         },
192         { "test5-5 (lowercase entities)",  YAZ_NFA_SUCCESS,
193              "<ruleset>"
194                "<rule>"
195                  "<fromrange>&#x41;-Z</fromrange>"
196                  "<torange>&#97;-&#x7A;</torange>"
197                "</rule>"
198              "</ruleset>",
199              "LowerCase TEST with A-Z and a-z (and &#41; &#5A; )",
200              "lowercase test with a-z and a-z (and &#41; &#5a; )"
201         },
202         { "test5-6 (danish lowercase)",  YAZ_NFA_SUCCESS,
203              "<ruleset>"
204                "<rule>"
205                  "<fromrange>A-Z</fromrange>"
206                  "<torange>a-z</torange>"
207                "</rule>"
208                "<rule>"
209                  "<fromrange>&#xC0;-&#xD6;</fromrange>"
210                  "<torange>&#xE0;-&#xF6;</torange>"
211                "</rule>"
212                "<rule>"
213                  "<fromrange>&#xD8;-&#xDF;</fromrange>"
214                  "<torange>&#xF8;-&#xFF;</torange>"
215                "</rule>"
216                "<rule>"
217                  "<fromstring>&#xC5;</fromstring>"
218                  "<tostring>&#xE5;</tostring>"
219                "</rule>"
220                "<rule>"
221                  "<fromstring>D&#xe4;nish</fromstring>"
222                  "<tostring>D&#xc4;NISH</tostring>"
223                "</rule>"
224              "</ruleset>",
225              "LowerCase TEST with Dänish Å !? åæø ÅÆØ XYZ",
226              "lowercase test with DÄNISH å !? åæø åæø xyz"
227         },
228         {0,0,0,0}
229     };
230     char *xml=0;
231 #define MAXBUF 2048    
232     yaz_nfa *nfa;
233     yaz_nfa_char frombuf[MAXBUF];
234     yaz_nfa_char tobuf[MAXBUF];
235     unsigned char charbuf[MAXBUF];
236     struct conv_test *thistest=tests;
237     unsigned char *cp;
238     yaz_nfa_char *ycp;
239     size_t incharsleft;
240     size_t outcharsleft;
241     size_t prev_incharsleft;
242     int rc;
243     yaz_nfa_char *fromp;
244     yaz_nfa_char *top;
245     while (thistest->name) {
246         yaz_log(YLOG_DEBUG,"Starting test %s",thistest->name);
247         if (thistest->xml)
248             xml=thistest->xml;
249         nfa = yaz_nfa_parse_xml_memory(xml, thistest->name);
250         YAZ_CHECK(nfa);
251         if (nfa) {
252             if ( yaz_test_get_verbosity() > 3) {
253                 yaz_nfa_dump(0,nfa,yaz_nfa_dump_converter);
254             }
255             ycp=frombuf;
256             cp=thistest->from;
257             while ( (*ycp++ = *cp++) )
258                 ; /* strcpy, but expand to yaz_nfa_chars */
259             incharsleft = strlen(thistest->from);
260             prev_incharsleft = 0;
261             outcharsleft = MAXBUF-1;
262             fromp = frombuf;
263             top = tobuf;
264             rc = YAZ_NFA_SUCCESS;
265             while ( (rc == YAZ_NFA_SUCCESS) && (incharsleft>0) && 
266                     (prev_incharsleft != incharsleft ) )  /* prevent loops */
267             {
268                 prev_incharsleft=incharsleft;
269                 rc=yaz_nfa_convert_slice(nfa, &fromp, &incharsleft,
270                         &top, &outcharsleft);
271             }
272             YAZ_CHECK_EQ(rc, thistest->expresult);
273             if ( (rc == thistest->expresult) &&
274                  (rc == YAZ_NFA_SUCCESS)) {
275                 YAZ_CHECK_EQ(incharsleft, 0);
276                 YAZ_CHECK( prev_incharsleft != incharsleft ); 
277             }
278             ycp=tobuf;
279             cp=charbuf;
280             while (ycp != top )
281                 *cp++ = *ycp++;
282             *cp=0;
283             if ( yaz_test_get_verbosity() > 2) {
284                 printf("%s from:   '%s' \n",thistest->name, thistest->from);
285                 printf("%s result: '%s' \n",thistest->name, charbuf);
286                 printf("%s expect: '%s' \n",thistest->name, thistest->to);
287             }
288             YAZ_CHECK( 0==strcmp(thistest->to,charbuf) );
289             yaz_nfa_destroy(nfa);
290         }
291         thistest++;
292     }
293     
294 } /* test5 */
295
296
297 /* More things to test:
298  *
299  *   - Empty strings in to/from
300  *   - ranges, length mismatches, etc
301  */
302
303 int main(int argc, char **argv)
304 {
305     YAZ_CHECK_INIT(argc, argv);
306     YAZ_CHECK_LOG();
307     nmem_init ();
308
309     test1();
310     test2();
311     test3();
312     test4();
313     test5();
314
315     nmem_exit ();
316     YAZ_CHECK_TERM;
317 }
318
319 #else
320 int main(int argc, char **argv) {
321     YAZ_CHECK_INIT(argc, argv);
322     YAZ_CHECK_TERM;
323 }
324
325 #endif
326
327 /* 
328  * Local variables:
329  * c-basic-offset: 4
330  * indent-tabs-mode: nil
331  * End:
332  * vim: shiftwidth=4 tabstop=8 expandtab
333  */