1 /* Copyright (C) 2006, Index Data ApS
2 * See the file LICENSE for details.
4 * $Id: nfaxmltest1.c,v 1.7 2006-07-14 13:06:38 heikki Exp $
14 #include <yaz/nfaxml.h>
17 #include <libxml/parser.h>
20 /** \brief Test parsing of a minimal, valid xml string */
22 char *xmlstr = "<ruleset> "
24 " <fromstring>foo</fromstring> "
25 " <tostring>bar</tostring> "
28 yaz_nfa *nfa = yaz_nfa_parse_xml_memory(xmlstr,"test1");
34 /** \brief Test parsing of a minimal, invalid xml string */
37 char *xmlstr = "<ruleset> "
39 " <fromstring>foo</fromstring> "
40 " <tostring>bar</tostring> "
42 /* missing "</ruleset>" */
43 yaz_log(YLOG_LOG,"Parsing bad xml, expecting errors:");
44 nfa = yaz_nfa_parse_xml_memory(xmlstr,"test2");
46 nfa = yaz_nfa_parse_xml_memory(0,"test2-null");
50 /** \brief Test parsing a few minimal xml files */
52 char *goodfilenames[] = {
54 "nfaxml-main.xml", /* x-includes nfaxml-include */
56 char *badfilenames[] = {
57 "nfaxml-missing.xml", /* file not there at all */
58 "nfaxml-badinclude.xml", /* bad xinclude in it */
61 char **f = goodfilenames;
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*/
70 yaz_log(YLOG_LOG,"Parsing bad xml file '%s'. Expecting errors", *f);
71 nfa = yaz_nfa_parse_xml_file(*f);
76 /** \brief Test parsing of a few minimal xml strings, with logical errors */
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>"
86 /*e*/"<ruleset><rule>"
87 "<fromstring>DuplicateFrom</fromstring>"
88 "<fromstring>Another Fromstring</fromstring>"
90 /*f*/"<ruleset><rule>"
91 "<tostring>MissingFrom</tostring>"
93 /*g*/"<ruleset><rule>"
94 "<tostring>DuplicateTo</tostring>"
95 "<tostring>AnotherTo</tostring>"
97 /*h*/"<ruleset><rule>"
98 "<fromstring>GoodUTF:æøå</fromstring>"
100 /*i*/"<ruleset><rule>"
101 "<tostring>BadUtf8:Ø</tostring>"
105 "<fromstring>ConflictingRules</fromstring>"
106 "<tostring>IdenticalStrings</tostring>"
109 "<fromstring>ConflictingRules</fromstring>"
110 "<tostring>IdenticalStrings</tostring>"
113 /*k*/"", /* empty string! */
116 "<fromrange>A-Z</fromrange>"
117 "<torange>a-x</torange>"
122 char label[]= { 'a', 0 };
124 yaz_log(YLOG_LOG,"test4-%s: Parsing bad xml, expecting errors:",
126 nfa = yaz_nfa_parse_xml_memory(*xmlp,label);
133 static void test5() {
141 struct conv_test tests[]= {
142 { "test5-1", YAZ_NFA_SUCCESS,
145 "<fromstring>foo</fromstring>"
146 "<tostring>bar</tostring>"
149 "this is a foo test ofoofo fo foofoo fofoofooofoooo ",
150 "this is a bar test obarfo fo barbar fobarbarobaroo "
152 { "test5-2", YAZ_NFA_SUCCESS,
155 "<fromstring>ooooo</fromstring>"
156 "<tostring>five </tostring>"
159 "<fromstring>oooo</fromstring>"
160 "<tostring>four </tostring>"
163 "<fromstring>ooo</fromstring>"
164 "<tostring>three </tostring>"
167 "<fromstring>oo</fromstring>"
168 "<tostring>two </tostring>"
174 { "test5-4", YAZ_NFA_SUCCESS, 0, /* same xml */
176 "two -four -five two -"
178 { "test5-3", YAZ_NFA_OVERRUN, 0, /* could match further oo's */
182 { "test5-4 (lowercase)", YAZ_NFA_SUCCESS,
185 "<fromrange>A-Z</fromrange>"
186 "<torange>a-z</torange>"
189 "LowerCase TEST with A-Z and a-z",
190 "lowercase test with a-z and a-z"
192 { "test5-5 (lowercase entities)", YAZ_NFA_SUCCESS,
195 "<fromrange>A-Z</fromrange>"
196 "<torange>a-z</torange>"
199 "LowerCase TEST with A-Z and a-z (and ) A; )",
200 "lowercase test with a-z and a-z (and ) a; )"
202 { "test5-6 (danish lowercase)", YAZ_NFA_SUCCESS,
205 "<fromrange>A-Z</fromrange>"
206 "<torange>a-z</torange>"
209 "<fromrange>À-Ö</fromrange>"
210 "<torange>à-ö</torange>"
213 "<fromrange>Ø-ß</fromrange>"
214 "<torange>ø-ÿ</torange>"
217 "<fromstring>Å</fromstring>"
218 "<tostring>å</tostring>"
221 "<fromstring>Dänish</fromstring>"
222 "<tostring>DÄNISH</tostring>"
225 "LowerCase TEST with Dänish Å !? åæø ÅÆØ XYZ",
226 "lowercase test with DÄNISH å !? åæø åæø xyz"
233 yaz_nfa_char frombuf[MAXBUF];
234 yaz_nfa_char tobuf[MAXBUF];
235 unsigned char charbuf[MAXBUF];
236 struct conv_test *thistest=tests;
241 size_t prev_incharsleft;
245 while (thistest->name) {
246 yaz_log(YLOG_DEBUG,"Starting test %s",thistest->name);
249 nfa = yaz_nfa_parse_xml_memory(xml, thistest->name);
252 if ( yaz_test_get_verbosity() > 3) {
253 yaz_nfa_dump(0,nfa,yaz_nfa_dump_converter);
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;
264 rc = YAZ_NFA_SUCCESS;
265 while ( (rc == YAZ_NFA_SUCCESS) && (incharsleft>0) &&
266 (prev_incharsleft != incharsleft ) ) /* prevent loops */
268 prev_incharsleft=incharsleft;
269 rc=yaz_nfa_convert_slice(nfa, &fromp, &incharsleft,
270 &top, &outcharsleft);
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 );
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);
288 YAZ_CHECK( 0==strcmp(thistest->to,charbuf) );
289 yaz_nfa_destroy(nfa);
297 /* More things to test:
299 * - Empty strings in to/from
300 * - ranges, length mismatches, etc
303 int main(int argc, char **argv)
305 YAZ_CHECK_INIT(argc, argv);
320 int main(int argc, char **argv) {
321 YAZ_CHECK_INIT(argc, argv);
330 * indent-tabs-mode: nil
332 * vim: shiftwidth=4 tabstop=8 expandtab