Imported Upstream version 2.2.4
[platform/upstream/expat.git] / tests / chardata.c
1 /*
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8
9    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10    Copyright (c) 2000-2017 Expat development team
11    Licensed under the MIT license:
12
13    Permission is  hereby granted,  free of charge,  to any  person obtaining
14    a  copy  of  this  software   and  associated  documentation  files  (the
15    "Software"),  to  deal in  the  Software  without restriction,  including
16    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17    distribute, sublicense, and/or sell copies of the Software, and to permit
18    persons  to whom  the Software  is  furnished to  do so,  subject to  the
19    following conditions:
20
21    The above copyright  notice and this permission notice  shall be included
22    in all copies or substantial portions of the Software.
23
24    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30    USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33 #ifdef HAVE_EXPAT_CONFIG_H
34 #include <expat_config.h>
35 #endif
36 #include "minicheck.h"
37
38 #include <assert.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #include "chardata.h"
43
44
45 static int
46 xmlstrlen(const XML_Char *s)
47 {
48     int len = 0;
49     assert(s != NULL);
50     while (s[len] != 0)
51         ++len;
52     return len;
53 }
54
55
56 void
57 CharData_Init(CharData *storage)
58 {
59     assert(storage != NULL);
60     storage->count = -1;
61 }
62
63 void
64 CharData_AppendString(CharData *storage, const char *s)
65 {
66     int maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
67     int len;
68
69     assert(s != NULL);
70     len = strlen(s);
71     if (storage->count < 0)
72         storage->count = 0;
73     if ((len + storage->count) > maxchars) {
74         len = (maxchars - storage->count);
75     }
76     if (len + storage->count < (int)sizeof(storage->data)) {
77         memcpy(storage->data + storage->count, s, len);
78         storage->count += len;
79     }
80 }
81
82 void
83 CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len)
84 {
85     int maxchars;
86
87     assert(storage != NULL);
88     assert(s != NULL);
89     maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
90     if (storage->count < 0)
91         storage->count = 0;
92     if (len < 0)
93         len = xmlstrlen(s);
94     if ((len + storage->count) > maxchars) {
95         len = (maxchars - storage->count);
96     }
97     if (len + storage->count < (int)sizeof(storage->data)) {
98         memcpy(storage->data + storage->count, s,
99                len * sizeof(storage->data[0]));
100         storage->count += len;
101     }
102 }
103
104 int
105 CharData_CheckString(CharData *storage, const char *expected)
106 {
107     char buffer[4096];
108     int len;
109     int count;
110
111     assert(storage != NULL);
112     assert(expected != NULL);
113     count = (storage->count < 0) ? 0 : storage->count;
114     len = strlen(expected);
115     if (len != count) {
116         if (sizeof(XML_Char) == 1)
117             sprintf(buffer, "wrong number of data characters:"
118                     " got %d, expected %d:\n%s", count, len, storage->data);
119         else
120             sprintf(buffer,
121                     "wrong number of data characters: got %d, expected %d",
122                     count, len);
123         fail(buffer);
124         return 0;
125     }
126     if (memcmp(expected, storage->data, len) != 0) {
127         fail("got bad data bytes");
128         return 0;
129     }
130     return 1;
131 }
132
133 int
134 CharData_CheckXMLChars(CharData *storage, const XML_Char *expected)
135 {
136     char buffer[1024];
137     int len = xmlstrlen(expected);
138     int count;
139
140     assert(storage != NULL);
141     count = (storage->count < 0) ? 0 : storage->count;
142     if (len != count) {
143         sprintf(buffer, "wrong number of data characters: got %d, expected %d",
144                 count, len);
145         fail(buffer);
146         return 0;
147     }
148     if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
149         fail("got bad data bytes");
150         return 0;
151     }
152     return 1;
153 }