diff --git a/expat/Makefile.in b/expat/Makefile.in index 31f180a2..22749266 100644 --- a/expat/Makefile.in +++ b/expat/Makefile.in @@ -145,9 +145,10 @@ examples/outline.o: examples/outline.c examples/outline: examples/outline.o lib/$(LIBRARY) $(LINK_EXE) $< lib/$(LIBRARY) -tests/runtests.o: tests/runtests.c -tests/runtests: tests/runtests.o lib/$(LIBRARY) - $(LINK_EXE) $< lib/$(LIBRARY) -lcheck +tests/chardata.o: tests/chardata.c tests/chardata.h +tests/runtests.o: tests/runtests.c tests/chardata.h +tests/runtests: tests/runtests.o tests/chardata.o lib/$(LIBRARY) + $(LINK_EXE) $^ -lcheck .SUFFIXES: .c .lo .o diff --git a/expat/tests/chardata.c b/expat/tests/chardata.c new file mode 100644 index 00000000..640f2c1d --- /dev/null +++ b/expat/tests/chardata.c @@ -0,0 +1,103 @@ +/* chardata.c + * + * + */ + +#include +#include +#include + +#include "chardata.h" + + +static int +xmlstrlen(const XML_Char *s) +{ + int len = 0; + while (s[len] != 0) + ++len; + return len; +} + + +void +CharData_Init(CharData *storage) +{ + storage->count = -1; +} + +void +CharData_AppendString(CharData *storage, const char *s) +{ + int maxchars = sizeof(storage->data) / sizeof(storage->data[0]); + int len = strlen(s); + + if (storage->count < 0) + storage->count = 0; + if ((len + storage->count) > maxchars) { + len = (maxchars - storage->count); + } + if (len + storage->count < sizeof(storage->data)) { + memcpy(storage->data + storage->count, s, len); + storage->count += len; + } +} + +void +CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) +{ + int maxchars = sizeof(storage->data) / sizeof(storage->data[0]); + + if (storage->count < 0) + storage->count = 0; + if (len < 0) + len = xmlstrlen(s); + if ((len + storage->count) > maxchars) { + len = (maxchars - storage->count); + } + if (len + storage->count < sizeof(storage->data)) { + memcpy(storage->data + storage->count, s, + len * sizeof(storage->data[0])); + storage->count += len; + } +} + +bool +CharData_CheckString(CharData *storage, const char *expected) +{ + char buffer[1024]; + int len = strlen(expected); + int count = (storage->count < 0) ? 0 : storage->count; + + if (len != count) { + sprintf(buffer, "wrong number of data characters: got %d, expected %d", + count, len); + fail(buffer); + return false; + } + if (memcmp(expected, storage->data, len) != 0) { + fail("got bad data bytes"); + return false; + } + return true; +} + +bool +CharData_CheckXMLChars(CharData *storage, const XML_Char *expected) +{ + char buffer[1024]; + int len = strlen(expected); + int count = (storage->count < 0) ? 0 : storage->count; + + if (len != count) { + sprintf(buffer, "wrong number of data characters: got %d, expected %d", + count, len); + fail(buffer); + return false; + } + if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) { + fail("got bad data bytes"); + return false; + } + return true; +} diff --git a/expat/tests/chardata.h b/expat/tests/chardata.h new file mode 100644 index 00000000..399aed3c --- /dev/null +++ b/expat/tests/chardata.h @@ -0,0 +1,33 @@ +/* chardata.h + * + * + */ + +#ifndef XML_CHARDATA_H +#define XML_CHARDATA_H 1 + +#ifndef XML_VERSION +#include "expat.h" /* need XML_Char */ +#endif + +#include + + +typedef struct { + int count; /* # of chars, < 0 if not set */ + XML_Char data[1024]; +} CharData; + + +void CharData_Init(CharData *storage); + +void CharData_AppendString(CharData *storage, const char *s); + +void CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len); + +bool CharData_CheckString(CharData *storage, const char *s); + +bool CharData_CheckXMLChars(CharData *storage, const XML_Char *s); + + +#endif /* XML_CHARDATA_H */ diff --git a/expat/tests/runtests.c b/expat/tests/runtests.c index 79e60e54..75204693 100644 --- a/expat/tests/runtests.c +++ b/expat/tests/runtests.c @@ -5,6 +5,7 @@ #include #include "expat.h" +#include "chardata.h" static XML_Parser parser; @@ -102,24 +103,10 @@ START_TEST(test_bom_utf16_le) } END_TEST - -typedef struct -{ - int count; /* # of chars, < 0 if not set */ - XML_Char data[1024]; -} CharData; - static void accumulate_characters(void *userData, const XML_Char *s, int len) { - CharData *storage = (CharData *)userData; - if (storage->count < 0) - storage->count = 0; - if (len + storage->count < sizeof(storage->data)) { - memcpy(storage->data + storage->count, s, - len * sizeof(storage->data[0])); - storage->count += len; - } + CharData_AppendXMLChars((CharData *)userData, s, len); } static void @@ -129,46 +116,22 @@ accumulate_attribute(void *userData, const XML_Char *name, CharData *storage = (CharData *)userData; if (storage->count < 0 && atts != NULL && atts[0] != NULL) { /* "accumulate" the value of the first attribute we see */ - int maxchars = sizeof(storage->data) / sizeof(storage->data[0]); - int i; - for (i = 0; i < maxchars; ++i) { - XML_Char ch = atts[1][i]; - if (ch == 0) - break; - storage->data[i] = ch; - } - storage->count = i; + CharData_AppendXMLChars(storage, atts[1], -1); } } -static void -check_characters(CharData *storage, XML_Char *expected) -{ - char buffer[1024]; - int len = strlen(expected); - if (storage->count < 0) - storage->count = 0; - if (len != storage->count) { - sprintf(buffer, "wrong number of data characters: got %d, expected %d", - storage->count, len); - fail(buffer); - return; - } - if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) - fail("got bad data bytes"); -} static void run_character_check(XML_Char *text, XML_Char *expected) { CharData storage; - storage.count = -1; + CharData_Init(&storage); XML_SetUserData(parser, &storage); XML_SetCharacterDataHandler(parser, accumulate_characters); if (!XML_Parse(parser, text, strlen(text), 1)) xml_failure(); - check_characters(&storage, expected); + CharData_CheckXMLChars(&storage, expected); } static void @@ -176,12 +139,12 @@ run_attribute_check(XML_Char *text, XML_Char *expected) { CharData storage; - storage.count = -1; /* magical "not-set" value */ + CharData_Init(&storage); XML_SetUserData(parser, &storage); XML_SetStartElementHandler(parser, accumulate_attribute); if (!XML_Parse(parser, text, strlen(text), 1)) xml_failure(); - check_characters(&storage, expected); + CharData_CheckXMLChars(&storage, expected); } /* Regression test for SF bug #491986. */