Home | History | Annotate | Download | only in tests
      1 /* Copyright (c) 1998-2003 Thai Open Source Software Center Ltd
      2    See the file COPYING for copying permission.
      3 
      4    chardata.c
      5 */
      6 
      7 #ifdef HAVE_EXPAT_CONFIG_H
      8 #include <expat_config.h>
      9 #endif
     10 #include "minicheck.h"
     11 
     12 #include <assert.h>
     13 #include <stdio.h>
     14 #include <string.h>
     15 
     16 #include "chardata.h"
     17 
     18 
     19 static int
     20 xmlstrlen(const XML_Char *s)
     21 {
     22     int len = 0;
     23     assert(s != NULL);
     24     while (s[len] != 0)
     25         ++len;
     26     return len;
     27 }
     28 
     29 
     30 void
     31 CharData_Init(CharData *storage)
     32 {
     33     assert(storage != NULL);
     34     storage->count = -1;
     35 }
     36 
     37 void
     38 CharData_AppendString(CharData *storage, const char *s)
     39 {
     40     int maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
     41     int len;
     42 
     43     assert(s != NULL);
     44     len = strlen(s);
     45     if (storage->count < 0)
     46         storage->count = 0;
     47     if ((len + storage->count) > maxchars) {
     48         len = (maxchars - storage->count);
     49     }
     50     if (len + storage->count < sizeof(storage->data)) {
     51         memcpy(storage->data + storage->count, s, len);
     52         storage->count += len;
     53     }
     54 }
     55 
     56 void
     57 CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len)
     58 {
     59     int maxchars;
     60 
     61     assert(storage != NULL);
     62     assert(s != NULL);
     63     maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
     64     if (storage->count < 0)
     65         storage->count = 0;
     66     if (len < 0)
     67         len = xmlstrlen(s);
     68     if ((len + storage->count) > maxchars) {
     69         len = (maxchars - storage->count);
     70     }
     71     if (len + storage->count < sizeof(storage->data)) {
     72         memcpy(storage->data + storage->count, s,
     73                len * sizeof(storage->data[0]));
     74         storage->count += len;
     75     }
     76 }
     77 
     78 int
     79 CharData_CheckString(CharData *storage, const char *expected)
     80 {
     81     char buffer[1280];
     82     int len;
     83     int count;
     84 
     85     assert(storage != NULL);
     86     assert(expected != NULL);
     87     count = (storage->count < 0) ? 0 : storage->count;
     88     len = strlen(expected);
     89     if (len != count) {
     90         if (sizeof(XML_Char) == 1)
     91             sprintf(buffer, "wrong number of data characters:"
     92                     " got %d, expected %d:\n%s", count, len, storage->data);
     93         else
     94             sprintf(buffer,
     95                     "wrong number of data characters: got %d, expected %d",
     96                     count, len);
     97         fail(buffer);
     98         return 0;
     99     }
    100     if (memcmp(expected, storage->data, len) != 0) {
    101         fail("got bad data bytes");
    102         return 0;
    103     }
    104     return 1;
    105 }
    106 
    107 int
    108 CharData_CheckXMLChars(CharData *storage, const XML_Char *expected)
    109 {
    110     char buffer[1024];
    111     int len = xmlstrlen(expected);
    112     int count;
    113 
    114     assert(storage != NULL);
    115     count = (storage->count < 0) ? 0 : storage->count;
    116     if (len != count) {
    117         sprintf(buffer, "wrong number of data characters: got %d, expected %d",
    118                 count, len);
    119         fail(buffer);
    120         return 0;
    121     }
    122     if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
    123         fail("got bad data bytes");
    124         return 0;
    125     }
    126     return 1;
    127 }
    128