Revise the data accumulation to be at least a little separated from the

actual test code.  Not pretty & lacking in internal documentation, but
allows the test code to be a bit more readable.
This commit is contained in:
Fred L. Drake, Jr. 2002-04-25 04:04:42 +00:00
parent d701da0bd5
commit 198ab270fe
4 changed files with 147 additions and 47 deletions

View File

@ -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

103
expat/tests/chardata.c Normal file
View File

@ -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;
}

33
expat/tests/chardata.h Normal file
View File

@ -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 */

View File

@ -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. */