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
|
/*
* Copyright (c) 2008
* Jonathan Schleifer <js@webkeks.org>
*
* All rights reserved.
*
* This file is part of libobjfw. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE included in
* the packaging of this file.
*/
#import "config.h"
#define _ISOC99_SOURCE
#import <stdlib.h>
#import <string.h>
#import <wchar.h>
#import "OFXMLFactory.h"
#define NUM_TESTS 10
static int i;
inline void
check_result(char *result, const char *should)
{
/* Use wprintf here so we don't mix printf and wprintf! */
i++;
if (!strcmp(result, should)) {
wprintf(L"\r\033[1;%dmchar* tests successful: %2d/%d\033[0m",
(i == NUM_TESTS ? 32 : 33), i, NUM_TESTS);
fflush(stdout);
} else {
wprintf(L"\r\033[K\033[1;31mchar* test %d/%d failed!\033[0m\n",
i, NUM_TESTS);
wprintf(L"%s is NOT expected result!\n", result);
exit(1);
}
free(result);
}
inline void
check_result_wide(wchar_t *result, const wchar_t *should)
{
i++;
if (!wcscmp(result, should)) {
wprintf(L"\r\033[1;%dmwchar_t* tests successful: %2d/%d\033[0m",
(i == NUM_TESTS ? 32 : 33), i, NUM_TESTS);
fflush(stdout);
} else {
wprintf(L"\r\033[K\033[1;31mwchar_t* test %d/%d failed!\033[0m"
"\n", i, NUM_TESTS);
wprintf(L"%s is NOT expected result!\n", result);
exit(1);
}
free(result);
}
inline void
|
<
<
<
<
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
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
|
/*
* Copyright (c) 2008
* Jonathan Schleifer <js@webkeks.org>
*
* All rights reserved.
*
* This file is part of libobjfw. It may be distributed under the terms of the
* Q Public License 1.0, which can be found in the file LICENSE included in
* the packaging of this file.
*/
#import "config.h"
#import <stdlib.h>
#import <string.h>
#import "OFXMLFactory.h"
#define NUM_TESTS 10
static int i;
inline void
check_result(char *result, const char *should)
{
i++;
if (!strcmp(result, should)) {
printf("\r\033[1;%dmTests successful: %2d/%d\033[0m",
(i == NUM_TESTS ? 32 : 33), i, NUM_TESTS);
fflush(stdout);
} else {
printf("\r\033[K\033[1;31mTest %d/%d failed!\033[0m\n",
i, NUM_TESTS);
printf("%s is NOT expected result!\n", result);
exit(1);
}
free(result);
}
inline void
|
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
|
strs[2] = s3;
strs[3] = NULL;
check_result([OFXMLFactory concatAndFreeCStrings: strs],
"<foo>bar<test/>");
}
inline void
test_concat_wide()
{
const wchar_t *c1 = L"<foo>", *c2 = L"bar", *c3 = L"<test/>";
wchar_t *s1, *s2, *s3;
wchar_t *strs[4];
if ((s1 = malloc((wcslen(c1) + 1) * sizeof(wchar_t))) == NULL ||
(s2 = malloc((wcslen(c2) + 1) * sizeof(wchar_t))) == NULL ||
(s3 = malloc((wcslen(c3) + 1) * sizeof(wchar_t))) == NULL)
exit(1);
wcsncpy(s1, c1, wcslen(c1) + 1);
wcsncpy(s2, c2, wcslen(c2) + 1);
wcsncpy(s3, c3, wcslen(c3) + 1);
strs[0] = s1;
strs[1] = s2;
strs[2] = s3;
strs[3] = NULL;
check_result_wide([OFXMLFactory concatAndFreeWideCStrings: strs],
L"<foo>bar<test/>");
}
inline void
test_create_stanza()
{
check_result([OFXMLFactory createStanza: "foo"
withCloseTag: NO
andData: NULL,
NULL],
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
strs[2] = s3;
strs[3] = NULL;
check_result([OFXMLFactory concatAndFreeCStrings: strs],
"<foo>bar<test/>");
}
inline void
test_create_stanza()
{
check_result([OFXMLFactory createStanza: "foo"
withCloseTag: NO
andData: NULL,
NULL],
|
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
|
"bar", "b'az",
"x", "y",
"a", "b",
NULL],
"<foo bar='b'az' x='y' a='b'>bar</foo>");
}
inline void
test_create_stanza_wide()
{
check_result_wide([OFXMLFactory createWideStanza: L"foo"
withCloseTag: NO
andData: NULL,
NULL],
L"<foo>");
check_result_wide([OFXMLFactory createWideStanza: L"foo"
withCloseTag: NO
andData: NULL,
L"bar", L"baz",
L"blub", L"asd",
NULL],
L"<foo bar='baz' blub='asd'>");
check_result_wide([OFXMLFactory createWideStanza: L"foo"
withCloseTag: YES
andData: NULL,
NULL],
L"<foo/>");
check_result_wide([OFXMLFactory createWideStanza: L"foo"
withCloseTag: YES
andData: L"bar",
NULL],
L"<foo>bar</foo>");
check_result_wide([OFXMLFactory createWideStanza: L"foo"
withCloseTag: YES
andData: NULL,
L"bar", L"b&az",
NULL],
L"<foo bar='b&az'/>");
check_result_wide([OFXMLFactory createWideStanza: L"foo"
withCloseTag: YES
andData: L"bar",
L"bar", L"b'az",
NULL],
L"<foo bar='b'az'>bar</foo>");
check_result_wide([OFXMLFactory createWideStanza: L"foo"
withCloseTag: YES
andData: NULL,
L"bar", L"b&az",
L"x", L"asd\"",
NULL],
L"<foo bar='b&az' x='asd"'/>");
check_result_wide([OFXMLFactory createWideStanza: L"foo"
withCloseTag: YES
andData: L"bar",
L"bar", L"b'az",
L"x", L"y",
L"a", L"b",
NULL],
L"<foo bar='b'az' x='y' a='b'>bar</foo>");
}
inline void
test_escape()
{
check_result([OFXMLFactory escapeCString: "<hallo> &welt'\"!&"],
"<hallo> &welt'"!&");
}
inline void
test_escape_wide()
{
check_result_wide(
[OFXMLFactory escapeWideCString: L"<hallo> &welt'\"!&"],
L"<hallo> &welt'"!&");
}
int main()
{
i = 0;
test_escape();
test_create_stanza();
test_concat();
wprintf(L"\n");
i = 0;
test_escape_wide();
test_create_stanza_wide();
test_concat_wide();
wprintf(L"\n");
return 0;
}
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
|
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
"bar", "b'az",
"x", "y",
"a", "b",
NULL],
"<foo bar='b'az' x='y' a='b'>bar</foo>");
}
inline void
test_escape()
{
check_result([OFXMLFactory escapeCString: "<hallo> &welt'\"!&"],
"<hallo> &welt'"!&");
}
int main()
{
i = 0;
test_escape();
test_create_stanza();
test_concat();
puts("");
return 0;
}
|