X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=test%2Fnfaxmltest1.c;h=754be3b209e0ccdef1a4e037a7ee13fad4bfbe4e;hp=106c78d7dfdb7c55f2ee94f8a9b8b9a543d00e36;hb=fd3a0b7302ceed6425c05b6537cb654201cb1494;hpb=fc6d778b923000b5c6ad8e108b0b184178a9d33f diff --git a/test/nfaxmltest1.c b/test/nfaxmltest1.c index 106c78d..754be3b 100644 --- a/test/nfaxmltest1.c +++ b/test/nfaxmltest1.c @@ -1,7 +1,7 @@ /* Copyright (C) 2006, Index Data ApS * See the file LICENSE for details. * - * $Id: nfaxmltest1.c,v 1.2 2006-07-06 10:17:55 adam Exp $ + * $Id: nfaxmltest1.c,v 1.7 2006-07-14 13:06:38 heikki Exp $ * */ @@ -17,25 +17,300 @@ #include +/** \brief Test parsing of a minimal, valid xml string */ void test1() { - char *xmlstr=" " - " " - " foo " - " bar " - "" - ""; - xmlDocPtr doc = xmlParseMemory(xmlstr, strlen(xmlstr)); - YAZ_CHECK(doc); - if (!doc) - return; + char *xmlstr = " " + " " + " foo " + " bar " + "" + ""; + yaz_nfa *nfa = yaz_nfa_parse_xml_memory(xmlstr,"test1"); + YAZ_CHECK(nfa); + yaz_nfa_destroy(nfa); } + +/** \brief Test parsing of a minimal, invalid xml string */ +void test2() { + yaz_nfa *nfa; + char *xmlstr = " " + " " + " foo " + " bar " + ""; + /* missing "" */ + yaz_log(YLOG_LOG,"Parsing bad xml, expecting errors:"); + nfa = yaz_nfa_parse_xml_memory(xmlstr,"test2"); + YAZ_CHECK(!nfa); + nfa = yaz_nfa_parse_xml_memory(0,"test2-null"); + YAZ_CHECK(!nfa); +} + +/** \brief Test parsing a few minimal xml files */ +void test3() { + char *goodfilenames[] = { + "nfaxml-simple.xml", + "nfaxml-main.xml", /* x-includes nfaxml-include */ + 0}; + char *badfilenames[] = { + "nfaxml-missing.xml", /* file not there at all */ + "nfaxml-badinclude.xml", /* bad xinclude in it */ + 0}; + yaz_nfa *nfa; + char **f = goodfilenames; + do { + yaz_log(YLOG_LOG,"Parsing (good) xml file '%s'", *f); + nfa=yaz_nfa_parse_xml_file(*f); + YAZ_CHECK_TODO(nfa); /* fails on make distcheck, can't find the files*/ + } while (*++f); + + f = badfilenames; + do { + yaz_log(YLOG_LOG,"Parsing bad xml file '%s'. Expecting errors", *f); + nfa = yaz_nfa_parse_xml_file(*f); + YAZ_CHECK(!nfa); + } while (*++f); +} + +/** \brief Test parsing of a few minimal xml strings, with logical errors */ +void test4() { + yaz_nfa *nfa; + char *xmls[] = { + /*a*/" ", + /*b*/" ", + /*c*/" ", + /*d*/"" + "MissingTo" + "", + /*e*/"" + "DuplicateFrom" + "Another Fromstring" + "", + /*f*/"" + "MissingFrom" + "", + /*g*/"" + "DuplicateTo" + "AnotherTo" + "", + /*h*/"" + "GoodUTF:æøå" + "", + /*i*/"" + "BadUtf8:Ø" + "", + /*j*/"" + "" + "ConflictingRules" + "IdenticalStrings" + "" + "" + "ConflictingRules" + "IdenticalStrings" + "" + "", + /*k*/"", /* empty string! */ + /*l*/"" + "" + "A-Z" + "a-x" + "" + "", + 0 }; + char **xmlp=xmls; + char label[]= { 'a', 0 }; + while ( *xmlp ) { + yaz_log(YLOG_LOG,"test4-%s: Parsing bad xml, expecting errors:", + label); + nfa = yaz_nfa_parse_xml_memory(*xmlp,label); + YAZ_CHECK(!nfa); + xmlp++; + label[0]++; + } +} /* test4 */ + +static void test5() { + struct conv_test { + unsigned char *name; + int expresult; + unsigned char *xml; + unsigned char *from; + unsigned char *to; + }; + struct conv_test tests[]= { + { "test5-1", YAZ_NFA_SUCCESS, + "" + "" + "foo" + "bar" + "" + "", + "this is a foo test ofoofo fo foofoo fofoofooofoooo ", + "this is a bar test obarfo fo barbar fobarbarobaroo " + }, + { "test5-2", YAZ_NFA_SUCCESS, + "" + "" + "ooooo" + "five " + "" + "" + "oooo" + "four " + "" + "" + "ooo" + "three " + "" + "" + "oo" + "two " + "" + "", + "oo-oooo-", + "two -four -" + }, + { "test5-4", YAZ_NFA_SUCCESS, 0, /* same xml */ + "oo-oooo-ooooooo-", + "two -four -five two -" + }, + { "test5-3", YAZ_NFA_OVERRUN, 0, /* could match further oo's */ + "oo-oooo-ooooooo", + "two -four -five " + }, + { "test5-4 (lowercase)", YAZ_NFA_SUCCESS, + "" + "" + "A-Z" + "a-z" + "" + "", + "LowerCase TEST with A-Z and a-z", + "lowercase test with a-z and a-z" + }, + { "test5-5 (lowercase entities)", YAZ_NFA_SUCCESS, + "" + "" + "A-Z" + "a-z" + "" + "", + "LowerCase TEST with A-Z and a-z (and ) A; )", + "lowercase test with a-z and a-z (and ) a; )" + }, + { "test5-6 (danish lowercase)", YAZ_NFA_SUCCESS, + "" + "" + "A-Z" + "a-z" + "" + "" + "À-Ö" + "à-ö" + "" + "" + "Ø-ß" + "ø-ÿ" + "" + "" + "Å" + "å" + "" + "" + "Dänish" + "DÄNISH" + "" + "", + "LowerCase TEST with Dänish Å !? åæø ÅÆØ XYZ", + "lowercase test with DÄNISH å !? åæø åæø xyz" + }, + {0,0,0,0} + }; + char *xml=0; +#define MAXBUF 2048 + yaz_nfa *nfa; + yaz_nfa_char frombuf[MAXBUF]; + yaz_nfa_char tobuf[MAXBUF]; + unsigned char charbuf[MAXBUF]; + struct conv_test *thistest=tests; + unsigned char *cp; + yaz_nfa_char *ycp; + size_t incharsleft; + size_t outcharsleft; + size_t prev_incharsleft; + int rc; + yaz_nfa_char *fromp; + yaz_nfa_char *top; + while (thistest->name) { + yaz_log(YLOG_DEBUG,"Starting test %s",thistest->name); + if (thistest->xml) + xml=thistest->xml; + nfa = yaz_nfa_parse_xml_memory(xml, thistest->name); + YAZ_CHECK(nfa); + if (nfa) { + if ( yaz_test_get_verbosity() > 3) { + yaz_nfa_dump(0,nfa,yaz_nfa_dump_converter); + } + ycp=frombuf; + cp=thistest->from; + while ( (*ycp++ = *cp++) ) + ; /* strcpy, but expand to yaz_nfa_chars */ + incharsleft = strlen(thistest->from); + prev_incharsleft = 0; + outcharsleft = MAXBUF-1; + fromp = frombuf; + top = tobuf; + rc = YAZ_NFA_SUCCESS; + while ( (rc == YAZ_NFA_SUCCESS) && (incharsleft>0) && + (prev_incharsleft != incharsleft ) ) /* prevent loops */ + { + prev_incharsleft=incharsleft; + rc=yaz_nfa_convert_slice(nfa, &fromp, &incharsleft, + &top, &outcharsleft); + } + YAZ_CHECK_EQ(rc, thistest->expresult); + if ( (rc == thistest->expresult) && + (rc == YAZ_NFA_SUCCESS)) { + YAZ_CHECK_EQ(incharsleft, 0); + YAZ_CHECK( prev_incharsleft != incharsleft ); + } + ycp=tobuf; + cp=charbuf; + while (ycp != top ) + *cp++ = *ycp++; + *cp=0; + if ( yaz_test_get_verbosity() > 2) { + printf("%s from: '%s' \n",thistest->name, thistest->from); + printf("%s result: '%s' \n",thistest->name, charbuf); + printf("%s expect: '%s' \n",thistest->name, thistest->to); + } + YAZ_CHECK( 0==strcmp(thistest->to,charbuf) ); + yaz_nfa_destroy(nfa); + } + thistest++; + } + +} /* test5 */ + + +/* More things to test: + * + * - Empty strings in to/from + * - ranges, length mismatches, etc + */ + int main(int argc, char **argv) { YAZ_CHECK_INIT(argc, argv); + YAZ_CHECK_LOG(); nmem_init (); test1(); + test2(); + test3(); + test4(); + test5(); nmem_exit (); YAZ_CHECK_TERM;