summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile.in7
-rw-r--r--tests/chardata.c103
-rw-r--r--tests/chardata.h33
-rw-r--r--tests/runtests.c51
4 files changed, 147 insertions, 47 deletions
diff --git a/Makefile.in b/Makefile.in
index 31f180a..2274926 100644
--- a/Makefile.in
+++ b/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/tests/chardata.c b/tests/chardata.c
new file mode 100644
index 0000000..640f2c1
--- /dev/null
+++ b/tests/chardata.c
@@ -0,0 +1,103 @@
+/* chardata.c
+ *
+ *
+ */
+
+#include <check.h>
+#include <stdio.h>
+#include <string.h>
+
+#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/tests/chardata.h b/tests/chardata.h
new file mode 100644
index 0000000..399aed3
--- /dev/null
+++ b/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 <stdbool.h>
+
+
+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/tests/runtests.c b/tests/runtests.c
index 79e60e5..7520469 100644
--- a/tests/runtests.c
+++ b/tests/runtests.c
@@ -5,6 +5,7 @@
#include <string.h>
#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. */