X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=test%2Fnfaxmltest1.c;h=754be3b209e0ccdef1a4e037a7ee13fad4bfbe4e;hb=32687c1504015e0675d49983707576f8db9b7bfc;hp=296dd97496a0f3a95177c53d38829b4102c21bfb;hpb=f83e5541f06bc5587efb2231006841137ba9f6d4;p=yaz-moved-to-github.git
diff --git a/test/nfaxmltest1.c b/test/nfaxmltest1.c
index 296dd97..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.5 2006-07-07 07:14:30 adam Exp $
+ * $Id: nfaxmltest1.c,v 1.7 2006-07-14 13:06:38 heikki Exp $
*
*/
@@ -19,35 +19,286 @@
/** \brief Test parsing of a minimal, valid xml string */
void test1() {
- char *xmlstr=" "
- " "
- " foo "
- " bar "
- ""
- "";
- yaz_nfa *nfa=yaz_nfa_parse_xml_memory(xmlstr);
-#if 0
-/* doesn't parse */
+ char *xmlstr = " "
+ " "
+ " foo "
+ " bar "
+ ""
+ "";
+ yaz_nfa *nfa = yaz_nfa_parse_xml_memory(xmlstr,"test1");
YAZ_CHECK(nfa);
-#endif
+ yaz_nfa_destroy(nfa);
}
-
/** \brief Test parsing of a minimal, invalid xml string */
void test2() {
yaz_nfa *nfa;
- char *xmlstr=" "
- " "
- " foo "
- " bar "
- "";
+ char *xmlstr = " "
+ " "
+ " foo "
+ " bar "
+ "";
/* missing "" */
yaz_log(YLOG_LOG,"Parsing bad xml, expecting errors:");
- nfa = yaz_nfa_parse_xml_memory(xmlstr);
+ 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)
{
@@ -57,6 +308,9 @@ int main(int argc, char **argv)
test1();
test2();
+ test3();
+ test4();
+ test5();
nmem_exit ();
YAZ_CHECK_TERM;