[Expat-checkins] CVS: expat/tests chardata.c,NONE,1.1 chardata.h,NONE,1.1 runtests.c,1.16,1.17

Fred L. Drake fdrake@users.sourceforge.net
Wed Apr 24 21:05:02 2002


Update of /cvsroot/expat/expat/tests
In directory usw-pr-cvs1:/tmp/cvs-serv25688/tests

Modified Files:
	runtests.c 
Added Files:
	chardata.c chardata.h 
Log Message:
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.


--- NEW FILE: chardata.c ---
/*  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;
}

--- NEW FILE: chardata.h ---
/*  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 */

Index: runtests.c
===================================================================
RCS file: /cvsroot/expat/expat/tests/runtests.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** runtests.c	25 Apr 2002 01:42:34 -0000	1.16
--- runtests.c	25 Apr 2002 04:04:42 -0000	1.17
***************
*** 6,9 ****
--- 6,10 ----
  
  #include "expat.h"
+ #include "chardata.h"
  
  
***************
*** 103,124 ****
  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;
!     }
  }
  
--- 104,111 ----
  END_TEST
  
  static void
  accumulate_characters(void *userData, const XML_Char *s, int len)
  {
!     CharData_AppendXMLChars((CharData *)userData, s, len);
  }
  
***************
*** 130,161 ****
      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;
      }
  }
  
- 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
--- 117,124 ----
      if (storage->count < 0 && atts != NULL && atts[0] != NULL) {
          /* "accumulate" the value of the first attribute we see */
!         CharData_AppendXMLChars(storage, atts[1], -1);
      }
  }
  
  
  static void
***************
*** 164,173 ****
      CharData storage;
  
!     storage.count = -1;
      XML_SetUserData(parser, &storage);
      XML_SetCharacterDataHandler(parser, accumulate_characters);
      if (!XML_Parse(parser, text, strlen(text), 1))
          xml_failure();
!     check_characters(&storage, expected);
  }
  
--- 127,136 ----
      CharData storage;
  
!     CharData_Init(&storage);
      XML_SetUserData(parser, &storage);
      XML_SetCharacterDataHandler(parser, accumulate_characters);
      if (!XML_Parse(parser, text, strlen(text), 1))
          xml_failure();
!     CharData_CheckXMLChars(&storage, expected);
  }
  
***************
*** 177,186 ****
      CharData storage;
  
!     storage.count = -1; /* magical "not-set" value */
      XML_SetUserData(parser, &storage);
      XML_SetStartElementHandler(parser, accumulate_attribute);
      if (!XML_Parse(parser, text, strlen(text), 1))
          xml_failure();
!     check_characters(&storage, expected);
  }
  
--- 140,149 ----
      CharData storage;
  
!     CharData_Init(&storage);
      XML_SetUserData(parser, &storage);
      XML_SetStartElementHandler(parser, accumulate_attribute);
      if (!XML_Parse(parser, text, strlen(text), 1))
          xml_failure();
!     CharData_CheckXMLChars(&storage, expected);
  }