-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
258 lines (234 loc) · 7.26 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>
#include <stdlib.h>
static int num_allocs = 0;
void* TEST_ALLOC( size_t sz )
{
num_allocs++;
return malloc( sz );
}
void TEST_FREE( void* p )
{
num_allocs--;
free( p );
}
#define xt_alloc TEST_ALLOC
#define xt_free TEST_FREE
#include "xmltractor.c"
static xt_Node* root;
#define NODE_STACK_SIZE 256
static xt_Node* chk_node_stack[ NODE_STACK_SIZE ];
int chk_node_stack_pos = 0;
void check_error( const char* code, int line )
{
fprintf( stderr, "Check on line %d failed:\n\n\t%s\n", line, code );
exit( 1 );
}
#define CHECK_( x, line ) if( (x) == 0 ) check_error( #x, line )
#define CHECK( x ) CHECK_( x, __LINE__ )
#define PARSE_FAIL( str ) PARSE_FAIL_( str, __LINE__ )
void PARSE_FAIL_( const char* str, int line )
{
printf( "running test [parse bad input] on line %d\n", line );
CHECK_( num_allocs == 0 && "something wasn't checked before", line );
CHECK_( root == NULL, line );
root = xt_parse( str );
CHECK_( root == NULL, line );
CHECK_( num_allocs == 0 && "unfreed memory", line );
}
#define PARSE( str ) PARSE_( str, __LINE__ )
void PARSE_( const char* str, int line )
{
printf( "running test [parse valid input] on line %d\n", line );
CHECK_( num_allocs == 0 && "something wasn't checked before", line );
CHECK_( root == NULL, line );
root = xt_parse( str );
CHECK_( root != NULL, line );
chk_node_stack[ 0 ] = root;
chk_node_stack_pos = 0;
}
void FREE()
{
CHECK( chk_node_stack_pos == 0 && "error in test" );
CHECK( root != NULL );
xt_destroy_node( root );
root = NULL;
CHECK( num_allocs == 0 && "unfreed memory" );
}
#define EL( str ) EL_( str, __LINE__ )
void EL_( const char* str, int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
int len = (int) strlen( str );
if( n->szname != len || memcmp( n->name, str, len ) != 0 )
{
fprintf( stderr, "Check on line %d failed: EL(\"%s\") [length=%d]\n", line, str, len );
fprintf( stderr, "- node name actually was \"%.*s\" [length=%d]\n", n->szname, n->name, n->szname );
exit( 1 );
}
}
#define CONTENT( str ) CONTENT_( str, __LINE__ )
void CONTENT_( const char* str, int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
int len = (int) strlen( str );
if( n->szcontent != len || memcmp( n->content, str, len ) != 0 )
{
fprintf( stderr, "Check on line %d failed: CONTENT(\"%s\") [length=%d]\n", line, str, len );
fprintf( stderr, "- node content actually was \"%.*s\" [length=%d]\n", n->szcontent, n->content, n->szcontent );
exit( 1 );
}
}
#define HEADER( str ) HEADER_( str, __LINE__ )
void HEADER_( const char* str, int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
int len = (int) strlen( str );
if( n->szheader != len || memcmp( n->header, str, len ) != 0 )
{
fprintf( stderr, "Check on line %d failed: HEADER(\"%s\") [length=%d]\n", line, str, len );
fprintf( stderr, "- node header actually was \"%.*s\" [length=%d]\n", n->szheader, n->header, n->szheader );
exit( 1 );
}
}
#define ATTRNUM( num ) ATTRNUM_( num, __LINE__ )
void ATTRNUM_( int num, int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
if( n->numattribs != num )
{
fprintf( stderr, "Check on line %d failed: ATTRNUM(%d)\n", line, num );
fprintf( stderr, "- node attribute count actually was %d\n", n->numattribs );
exit( 1 );
}
}
#define CHNUM( num ) CHNUM_( num, __LINE__ )
void CHNUM_( int num, int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
if( n->numchildren != num )
{
fprintf( stderr, "Check on line %d failed: CHNUM(%d)\n", line, num );
fprintf( stderr, "- node child count actually was %d\n", n->numchildren );
exit( 1 );
}
}
#define PUSH PUSH_( __LINE__ );
void PUSH_( int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
CHECK_( n->firstchild, line );
CHECK_( n->firstchild->parent == n, line );
chk_node_stack_pos++;
CHECK_( chk_node_stack_pos < NODE_STACK_SIZE, line );
chk_node_stack[ chk_node_stack_pos ] = n->firstchild;
}
#define POP POP_( __LINE__ );
void POP_( int line )
{
CHECK_( chk_node_stack_pos > 0, line );
chk_node_stack_pos--;
}
#define NOCH NOCH_( __LINE__ );
void NOCH_( int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
CHECK_( n->firstchild == NULL, line );
}
#define NEXT NEXT_( __LINE__ );
void NEXT_( int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
CHECK_( n->sibling, line );
chk_node_stack[ chk_node_stack_pos ] = n->sibling;
}
#define END END_( __LINE__ );
void END_( int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
CHECK_( n->sibling == NULL, line );
}
#define NOATTR NOATTR_( __LINE__ );
void NOATTR_( int line )
{
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
CHECK_( n->attribs == NULL, line );
}
#define ATTR( i, nm, val ) ATTR_( i, nm, val, __LINE__ )
void ATTR_( int i, const char* nm, const char* val, int line )
{
xt_Attrib* attr;
xt_Node* n = chk_node_stack[ chk_node_stack_pos ];
int szname = (int) strlen( nm );
int szval = (int) strlen( val );
CHECK_( i >= 0 && i < n->numattribs, line );
attr = &n->attribs[ i ];
if( attr->szname != szname || memcmp( attr->name, nm, szname ) != 0 ||
attr->szvalue != szval || memcmp( attr->value, val, szval ) != 0 )
{
fprintf( stderr, "Check on line %d failed: ATTR(%d,\"%s\",\"%s\")\n", line, i, nm, val );
fprintf( stderr, "- node attribute actually was \"%.*s\"[%d]=\"%.*s\"[%d]\n",
attr->szname, attr->name, attr->szname, attr->szvalue, attr->value, attr->szvalue );
exit( 1 );
}
}
int main()
{
puts( "-- XML Tractor unit tests --" );
PARSE( "<a></a>" );
FREE();
PARSE_FAIL( "<" );
PARSE_FAIL( "a" );
PARSE_FAIL( "<a" );
PARSE_FAIL( ">" );
PARSE_FAIL( "/>" );
PARSE_FAIL( "<a></b>" );
PARSE_FAIL( "<a x=\"y'></a>" );
PARSE_FAIL( "<a x='y\"></a>" );
PARSE( "<a></a>" );
EL( "a" ); CONTENT( "" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
FREE();
PARSE( "<a>\n</a>" );
EL( "a" ); CONTENT( "\n" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
FREE();
PARSE( "<a><b></b></a>" );
EL( "a" ); CONTENT( "<b></b>" ); ATTRNUM( 0 ); CHNUM( 1 );
PUSH
EL( "b" ); CONTENT( "" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
END
POP
FREE();
/* skip declaration */
PARSE( "<?xml version='1.0' encoding='UTF-8' standalone='no' ?><a></a>" );
EL( "a" ); CONTENT( "" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
FREE();
PARSE( "<wa attrname=\"attrval\"></wa>" );
EL( "wa" ); CONTENT( "" ); ATTRNUM( 1 ); CHNUM( 0 ); NOCH ATTR( 0, "attrname", "attrval" );
FREE();
PARSE( "<a>b<c>d</c>e<c>f</c>g</a>" );
EL( "a" ); CONTENT( "b<c>d</c>e<c>f</c>g" ); HEADER( "<a>" ); ATTRNUM( 0 ); CHNUM( 2 ); NOATTR
PUSH
EL( "c" ); CONTENT( "d" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
NEXT
EL( "c" ); CONTENT( "f" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
END
POP
FREE();
PARSE( "<a />" );
EL( "a" ); CONTENT( "" ); HEADER( "<a />" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
FREE();
PARSE( "<wa2 abc='def' ghi=\"jklmn\" />" );
EL( "wa2" ); CONTENT( "" ); ATTRNUM( 2 ); CHNUM( 0 ); NOCH ATTR( 0, "abc", "def" ); ATTR( 1, "ghi", "jklmn" );
FREE();
PARSE( "<a/>" );
EL( "a" ); CONTENT( "" ); HEADER( "<a/>" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
FREE();
PARSE( "<a><!-- comment --><b></b></a>" );
EL( "a" ); CONTENT( "<!-- comment --><b></b>" ); ATTRNUM( 0 ); CHNUM( 1 ); NOATTR
PUSH
EL( "b" ); CONTENT( "" ); ATTRNUM( 0 ); CHNUM( 0 ); NOCH NOATTR
END
POP
FREE();
puts( "-- success --" );
return 0;
}