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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
#include <string.h>
#include <libxml/parser.h>
#include <libxml/dict.h>
/* #define WITH_PRINT */
static const char *seeds1[] = {
"a", "b", "c",
"d", "e", "f",
"g", "h", "i",
"j", "k", "l",
NULL
};
static const char *seeds2[] = {
"m", "n", "o",
"p", "q", "r",
"s", "t", "u",
"v", "w", "x",
NULL
};
#define NB_STRINGS_NS 100
#define NB_STRINGS_MAX 10000
#define NB_STRINGS_MIN 10
static xmlChar *strings1[NB_STRINGS_MAX];
static xmlChar *strings2[NB_STRINGS_MAX];
static const xmlChar *test1[NB_STRINGS_MAX];
static const xmlChar *test2[NB_STRINGS_MAX];
static int nbErrors = 0;
static void fill_strings(void) {
int i, j, k;
/*
* That's a bit nasty but the output is fine and it doesn't take hours
* there is a small but sufficient number of duplicates, and we have
* ":xxx" and full QNames in the last NB_STRINGS_NS values
*/
for (i = 0; seeds1[i] != NULL; i++) {
strings1[i] = xmlStrdup((const xmlChar *) seeds1[i]);
if (strings1[i] == NULL) {
fprintf(stderr, "Out of memory while generating strings1\n");
exit(1);
}
}
for (j = 0, k = 0;i < NB_STRINGS_MAX - NB_STRINGS_NS;i++,j++) {
strings1[i] = xmlStrncatNew(strings1[j], strings1[k], -1);
if (strings1[i] == NULL) {
fprintf(stderr, "Out of memory while generating strings1\n");
exit(1);
}
if (j >= 50) {
j = 0;
k++;
}
}
for (j = 0; (j < 50) && (i < NB_STRINGS_MAX); i++, j+=2) {
strings1[i] = xmlStrncatNew(strings1[j], (const xmlChar *) ":", -1);
if (strings1[i] == NULL) {
fprintf(stderr, "Out of memory while generating strings1\n");
exit(1);
}
}
for (j = NB_STRINGS_MAX - NB_STRINGS_NS, k = 0;
i < NB_STRINGS_MAX;i++,j++) {
strings1[i] = xmlStrncatNew(strings1[j], strings1[k], -1);
if (strings1[i] == NULL) {
fprintf(stderr, "Out of memory while generating strings1\n");
exit(1);
}
k += 3;
if (k >= 50) k = 0;
}
/*
* Now do the same with the second pool of strings
*/
for (i = 0; seeds2[i] != NULL; i++) {
strings2[i] = xmlStrdup((const xmlChar *) seeds2[i]);
if (strings2[i] == NULL) {
fprintf(stderr, "Out of memory while generating strings2\n");
exit(1);
}
}
for (j = 0, k = 0;i < NB_STRINGS_MAX - NB_STRINGS_NS;i++,j++) {
strings2[i] = xmlStrncatNew(strings2[j], strings2[k], -1);
if (strings2[i] == NULL) {
fprintf(stderr, "Out of memory while generating strings2\n");
exit(1);
}
if (j >= 50) {
j = 0;
k++;
}
}
for (j = 0; (j < 50) && (i < NB_STRINGS_MAX); i++, j+=2) {
strings2[i] = xmlStrncatNew(strings2[j], (const xmlChar *) ":", -1);
if (strings2[i] == NULL) {
fprintf(stderr, "Out of memory while generating strings2\n");
exit(1);
}
}
for (j = NB_STRINGS_MAX - NB_STRINGS_NS, k = 0;
i < NB_STRINGS_MAX;i++,j++) {
strings2[i] = xmlStrncatNew(strings2[j], strings2[k], -1);
if (strings2[i] == NULL) {
fprintf(stderr, "Out of memory while generating strings2\n");
exit(1);
}
k += 3;
if (k >= 50) k = 0;
}
}
#ifdef WITH_PRINT
static void print_strings(void) {
int i;
for (i = 0; i < NB_STRINGS_MAX;i++) {
printf("%s\n", strings1[i]);
}
for (i = 0; i < NB_STRINGS_MAX;i++) {
printf("%s\n", strings2[i]);
}
}
#endif
static void clean_strings(void) {
int i;
for (i = 0; i < NB_STRINGS_MAX; i++) {
if (strings1[i] != NULL) /* really should not happen */
xmlFree(strings1[i]);
}
for (i = 0; i < NB_STRINGS_MAX; i++) {
if (strings2[i] != NULL) /* really should not happen */
xmlFree(strings2[i]);
}
}
/*
* This tests the sub-dictionary support
*/
static int run_test2(xmlDictPtr parent) {
int i, j;
xmlDictPtr dict;
int ret = 0;
xmlChar prefix[40];
xmlChar *cur, *pref;
const xmlChar *tmp;
dict = xmlDictCreateSub(parent);
if (dict == NULL) {
fprintf(stderr, "Out of memory while creating sub-dictionary\n");
exit(1);
}
/* Cast to avoid buggy warning on MSVC. */
memset((void *) test2, 0, sizeof(test2));
/*
* Fill in NB_STRINGS_MIN, at this point the dictionary should not grow
* and we allocate all those doing the fast key computations
* All the strings are based on a different seeds subset so we know
* they are allocated in the main dictionary, not coming from the parent
*/
for (i = 0;i < NB_STRINGS_MIN;i++) {
test2[i] = xmlDictLookup(dict, strings2[i], -1);
if (test2[i] == NULL) {
fprintf(stderr, "Failed lookup for '%s'\n", strings2[i]);
ret = 1;
nbErrors++;
}
}
j = NB_STRINGS_MAX - NB_STRINGS_NS;
/* ":foo" like strings2 */
for (i = 0;i < NB_STRINGS_MIN;i++, j++) {
test2[j] = xmlDictLookup(dict, strings2[j], xmlStrlen(strings2[j]));
if (test2[j] == NULL) {
fprintf(stderr, "Failed lookup for '%s'\n", strings2[j]);
ret = 1;
nbErrors++;
}
}
/* "a:foo" like strings2 */
j = NB_STRINGS_MAX - NB_STRINGS_MIN;
for (i = 0;i < NB_STRINGS_MIN;i++, j++) {
test2[j] = xmlDictLookup(dict, strings2[j], xmlStrlen(strings2[j]));
if (test2[j] == NULL) {
fprintf(stderr, "Failed lookup for '%s'\n", strings2[j]);
ret = 1;
nbErrors++;
}
}
/*
* At this point allocate all the strings
* the dictionary will grow in the process, reallocate more string tables
* and switch to the better key generator
*/
for (i = 0;i < NB_STRINGS_MAX;i++) {
if (test2[i] != NULL)
continue;
test2[i] = xmlDictLookup(dict, strings2[i], -1);
if (test2[i] == NULL) {
fprintf(stderr, "Failed lookup for '%s'\n", strings2[i]);
ret = 1;
nbErrors++;
}
}
/*
* Now we can start to test things, first that all strings2 belongs to
* the dict, and that none of them was actually allocated in the parent
*/
for (i = 0;i < NB_STRINGS_MAX;i++) {
if (!xmlDictOwns(dict, test2[i])) {
fprintf(stderr, "Failed ownership failure for '%s'\n",
strings2[i]);
ret = 1;
nbErrors++;
}
if (xmlDictOwns(parent, test2[i])) {
fprintf(stderr, "Failed parent ownership failure for '%s'\n",
strings2[i]);
ret = 1;
nbErrors++;
}
}
/*
* Also verify that all strings from the parent are seen from the subdict
*/
for (i = 0;i < NB_STRINGS_MAX;i++) {
if (!xmlDictOwns(dict, test1[i])) {
fprintf(stderr, "Failed sub-ownership failure for '%s'\n",
strings1[i]);
ret = 1;
nbErrors++;
}
}
/*
* Then that another lookup to the string in sub will return the same
*/
for (i = 0;i < NB_STRINGS_MAX;i++) {
if (xmlDictLookup(dict, strings2[i], -1) != test2[i]) {
fprintf(stderr, "Failed re-lookup check for %d, '%s'\n",
i, strings2[i]);
ret = 1;
nbErrors++;
}
}
/*
* But also that any lookup for a string in the parent will be provided
* as in the parent
*/
for (i = 0;i < NB_STRINGS_MAX;i++) {
if (xmlDictLookup(dict, strings1[i], -1) != test1[i]) {
fprintf(stderr, "Failed parent string lookup check for %d, '%s'\n",
i, strings1[i]);
ret = 1;
nbErrors++;
}
}
/*
* check the QName lookups
*/
for (i = NB_STRINGS_MAX - NB_STRINGS_NS;i < NB_STRINGS_MAX;i++) {
cur = strings2[i];
pref = &prefix[0];
while (*cur != ':') *pref++ = *cur++;
cur++;
*pref = 0;
tmp = xmlDictQLookup(dict, &prefix[0], cur);
if (tmp != test2[i]) {
fprintf(stderr, "Failed lookup check for '%s':'%s'\n",
&prefix[0], cur);
ret = 1;
nbErrors++;
}
}
/*
* check the QName lookups for strings from the parent
*/
for (i = NB_STRINGS_MAX - NB_STRINGS_NS;i < NB_STRINGS_MAX;i++) {
cur = strings1[i];
pref = &prefix[0];
while (*cur != ':') *pref++ = *cur++;
cur++;
*pref = 0;
tmp = xmlDictQLookup(dict, &prefix[0], cur);
if (xmlDictQLookup(dict, &prefix[0], cur) != test1[i]) {
fprintf(stderr, "Failed parent lookup check for '%s':'%s'\n",
&prefix[0], cur);
ret = 1;
nbErrors++;
}
}
xmlDictFree(dict);
return(ret);
}
/*
* Test a single dictionary
*/
static int run_test1(void) {
int i, j;
xmlDictPtr dict;
int ret = 0;
xmlChar prefix[40];
xmlChar *cur, *pref;
const xmlChar *tmp;
dict = xmlDictCreate();
if (dict == NULL) {
fprintf(stderr, "Out of memory while creating dictionary\n");
exit(1);
}
/* Cast to avoid buggy warning on MSVC. */
memset((void *) test1, 0, sizeof(test1));
/*
* Fill in NB_STRINGS_MIN, at this point the dictionary should not grow
* and we allocate all those doing the fast key computations
*/
for (i = 0;i < NB_STRINGS_MIN;i++) {
test1[i] = xmlDictLookup(dict, strings1[i], -1);
if (test1[i] == NULL) {
fprintf(stderr, "Failed lookup for '%s'\n", strings1[i]);
ret = 1;
nbErrors++;
}
}
j = NB_STRINGS_MAX - NB_STRINGS_NS;
/* ":foo" like strings1 */
for (i = 0;i < NB_STRINGS_MIN;i++, j++) {
test1[j] = xmlDictLookup(dict, strings1[j], xmlStrlen(strings1[j]));
if (test1[j] == NULL) {
fprintf(stderr, "Failed lookup for '%s'\n", strings1[j]);
ret = 1;
nbErrors++;
}
}
/* "a:foo" like strings1 */
j = NB_STRINGS_MAX - NB_STRINGS_MIN;
for (i = 0;i < NB_STRINGS_MIN;i++, j++) {
test1[j] = xmlDictLookup(dict, strings1[j], xmlStrlen(strings1[j]));
if (test1[j] == NULL) {
fprintf(stderr, "Failed lookup for '%s'\n", strings1[j]);
ret = 1;
nbErrors++;
}
}
/*
* At this point allocate all the strings
* the dictionary will grow in the process, reallocate more string tables
* and switch to the better key generator
*/
for (i = 0;i < NB_STRINGS_MAX;i++) {
if (test1[i] != NULL)
continue;
test1[i] = xmlDictLookup(dict, strings1[i], -1);
if (test1[i] == NULL) {
fprintf(stderr, "Failed lookup for '%s'\n", strings1[i]);
ret = 1;
nbErrors++;
}
}
/*
* Now we can start to test things, first that all strings1 belongs to
* the dict
*/
for (i = 0;i < NB_STRINGS_MAX;i++) {
if (!xmlDictOwns(dict, test1[i])) {
fprintf(stderr, "Failed ownership failure for '%s'\n",
strings1[i]);
ret = 1;
nbErrors++;
}
}
/*
* Then that another lookup to the string will return the same
*/
for (i = 0;i < NB_STRINGS_MAX;i++) {
if (xmlDictLookup(dict, strings1[i], -1) != test1[i]) {
fprintf(stderr, "Failed re-lookup check for %d, '%s'\n",
i, strings1[i]);
ret = 1;
nbErrors++;
}
}
/*
* More complex, check the QName lookups
*/
for (i = NB_STRINGS_MAX - NB_STRINGS_NS;i < NB_STRINGS_MAX;i++) {
cur = strings1[i];
pref = &prefix[0];
while (*cur != ':') *pref++ = *cur++;
cur++;
*pref = 0;
tmp = xmlDictQLookup(dict, &prefix[0], cur);
if (tmp != test1[i]) {
fprintf(stderr, "Failed lookup check for '%s':'%s'\n",
&prefix[0], cur);
ret = 1;
nbErrors++;
}
}
run_test2(dict);
xmlDictFree(dict);
return(ret);
}
int main(void)
{
int ret;
LIBXML_TEST_VERSION
fill_strings();
#ifdef WITH_PRINT
print_strings();
#endif
ret = run_test1();
if (ret == 0) {
printf("dictionary tests succeeded %d strings\n", 2 * NB_STRINGS_MAX);
} else {
printf("dictionary tests failed with %d errors\n", nbErrors);
}
clean_strings();
xmlCleanupParser();
xmlMemoryDump();
return(ret);
}
|