xref: /freebsd/contrib/expat/tests/structdata.c (revision ce6a89e27cd190313be39bb479880aeda4778436)
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 
37 #include <assert.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 
42 #include "structdata.h"
43 #include "minicheck.h"
44 
45 #define STRUCT_EXTENSION_COUNT 8
46 
47 #ifdef XML_UNICODE_WCHAR_T
48 #  include <wchar.h>
49 #  define XML_FMT_STR "ls"
50 #  define xcstrlen(s) wcslen(s)
51 #  define xcstrcmp(s, t) wcscmp((s), (t))
52 #else
53 #  define XML_FMT_STR "s"
54 #  define xcstrlen(s) strlen(s)
55 #  define xcstrcmp(s, t) strcmp((s), (t))
56 #endif
57 
58 static XML_Char *
59 xmlstrdup(const XML_Char *s) {
60   size_t byte_count = (xcstrlen(s) + 1) * sizeof(XML_Char);
61   XML_Char *dup = malloc(byte_count);
62 
63   assert(dup != NULL);
64   memcpy(dup, s, byte_count);
65   return dup;
66 }
67 
68 void
69 StructData_Init(StructData *storage) {
70   assert(storage != NULL);
71   storage->count = 0;
72   storage->max_count = 0;
73   storage->entries = NULL;
74 }
75 
76 void
77 StructData_AddItem(StructData *storage, const XML_Char *s, int data0, int data1,
78                    int data2) {
79   StructDataEntry *entry;
80 
81   assert(storage != NULL);
82   assert(s != NULL);
83   if (storage->count == storage->max_count) {
84     StructDataEntry *new;
85 
86     storage->max_count += STRUCT_EXTENSION_COUNT;
87     new = realloc(storage->entries,
88                   storage->max_count * sizeof(StructDataEntry));
89     assert(new != NULL);
90     storage->entries = new;
91   }
92 
93   entry = &storage->entries[storage->count];
94   entry->str = xmlstrdup(s);
95   entry->data0 = data0;
96   entry->data1 = data1;
97   entry->data2 = data2;
98   storage->count++;
99 }
100 
101 /* 'fail()' aborts the function via a longjmp, so there is no point
102  * in returning a value from this function.
103  */
104 void
105 StructData_CheckItems(StructData *storage, const StructDataEntry *expected,
106                       int count) {
107   char buffer[1024];
108   int i;
109 
110   assert(storage != NULL);
111   assert(expected != NULL);
112   if (count != storage->count) {
113     sprintf(buffer, "wrong number of entries: got %d, expected %d",
114             storage->count, count);
115     StructData_Dispose(storage);
116     fail(buffer);
117   } else {
118     for (i = 0; i < count; i++) {
119       const StructDataEntry *got = &storage->entries[i];
120       const StructDataEntry *want = &expected[i];
121 
122       assert(got != NULL);
123       assert(want != NULL);
124 
125       if (xcstrcmp(got->str, want->str) != 0) {
126         StructData_Dispose(storage);
127         fail("structure got bad string");
128       } else {
129         if (got->data0 != want->data0 || got->data1 != want->data1
130             || got->data2 != want->data2) {
131           sprintf(buffer,
132                   "struct '%" XML_FMT_STR
133                   "' expected (%d,%d,%d), got (%d,%d,%d)",
134                   got->str, want->data0, want->data1, want->data2, got->data0,
135                   got->data1, got->data2);
136           StructData_Dispose(storage);
137           fail(buffer);
138         }
139       }
140     }
141   }
142 }
143 
144 void
145 StructData_Dispose(StructData *storage) {
146   int i;
147 
148   assert(storage != NULL);
149   for (i = 0; i < storage->count; i++)
150     free((void *)storage->entries[i].str);
151   free(storage->entries);
152 
153   storage->count = 0;
154   storage->entries = NULL;
155 }
156