| 1 | #include <string.h> |
| 2 | #include <libxml/parser.h> |
| 3 | #include <libxml/dict.h> |
| 4 | |
| 5 | |
| 6 | |
| 7 | static const char *seeds1[] = { |
| 8 | "a", "b", "c", |
| 9 | "d", "e", "f", |
| 10 | "g", "h", "i", |
| 11 | "j", "k", "l", |
| 12 | |
| 13 | NULL((void*)0) |
| 14 | }; |
| 15 | |
| 16 | static const char *seeds2[] = { |
| 17 | "m", "n", "o", |
| 18 | "p", "q", "r", |
| 19 | "s", "t", "u", |
| 20 | "v", "w", "x", |
| 21 | |
| 22 | NULL((void*)0) |
| 23 | }; |
| 24 | |
| 25 | #define NB_STRINGS_NS100 100 |
| 26 | #define NB_STRINGS_MAX10000 10000 |
| 27 | #define NB_STRINGS_MIN10 10 |
| 28 | |
| 29 | static xmlChar *strings1[NB_STRINGS_MAX10000]; |
| 30 | static xmlChar *strings2[NB_STRINGS_MAX10000]; |
| 31 | static const xmlChar *test1[NB_STRINGS_MAX10000]; |
| 32 | static const xmlChar *test2[NB_STRINGS_MAX10000]; |
| 33 | static int nbErrors = 0; |
| 34 | |
| 35 | static void fill_strings(void) { |
| 36 | int i, j, k; |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | for (i = 0; seeds1[i] != NULL((void*)0); i++) { |
| 44 | strings1[i] = xmlStrdup((const xmlChar *) seeds1[i]); |
| 45 | if (strings1[i] == NULL((void*)0)) { |
| 46 | fprintf(stderrstderr, "Out of memory while generating strings1\n"); |
| 47 | exit(1); |
| 48 | } |
| 49 | } |
| 50 | for (j = 0, k = 0;i < NB_STRINGS_MAX10000 - NB_STRINGS_NS100;i++,j++) { |
| 51 | strings1[i] = xmlStrncatNew(strings1[j], strings1[k], -1); |
| 52 | if (strings1[i] == NULL((void*)0)) { |
| 53 | fprintf(stderrstderr, "Out of memory while generating strings1\n"); |
| 54 | exit(1); |
| 55 | } |
| 56 | if (j >= 50) { |
| 57 | j = 0; |
| 58 | k++; |
| 59 | } |
| 60 | } |
| 61 | for (j = 0; (j < 50) && (i < NB_STRINGS_MAX10000); i++, j+=2) { |
| 62 | strings1[i] = xmlStrncatNew(strings1[j], (const xmlChar *) ":", -1); |
| 63 | if (strings1[i] == NULL((void*)0)) { |
| 64 | fprintf(stderrstderr, "Out of memory while generating strings1\n"); |
| 65 | exit(1); |
| 66 | } |
| 67 | } |
| 68 | for (j = NB_STRINGS_MAX10000 - NB_STRINGS_NS100, k = 0; |
| 69 | i < NB_STRINGS_MAX10000;i++,j++) { |
| 70 | strings1[i] = xmlStrncatNew(strings1[j], strings1[k], -1); |
| 71 | if (strings1[i] == NULL((void*)0)) { |
| 72 | fprintf(stderrstderr, "Out of memory while generating strings1\n"); |
| 73 | exit(1); |
| 74 | } |
| 75 | k += 3; |
| 76 | if (k >= 50) k = 0; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | for (i = 0; seeds2[i] != NULL((void*)0); i++) { |
| 83 | strings2[i] = xmlStrdup((const xmlChar *) seeds2[i]); |
| 84 | if (strings2[i] == NULL((void*)0)) { |
| 85 | fprintf(stderrstderr, "Out of memory while generating strings2\n"); |
| 86 | exit(1); |
| 87 | } |
| 88 | } |
| 89 | for (j = 0, k = 0;i < NB_STRINGS_MAX10000 - NB_STRINGS_NS100;i++,j++) { |
| 90 | strings2[i] = xmlStrncatNew(strings2[j], strings2[k], -1); |
| 91 | if (strings2[i] == NULL((void*)0)) { |
| 92 | fprintf(stderrstderr, "Out of memory while generating strings2\n"); |
| 93 | exit(1); |
| 94 | } |
| 95 | if (j >= 50) { |
| 96 | j = 0; |
| 97 | k++; |
| 98 | } |
| 99 | } |
| 100 | for (j = 0; (j < 50) && (i < NB_STRINGS_MAX10000); i++, j+=2) { |
| 101 | strings2[i] = xmlStrncatNew(strings2[j], (const xmlChar *) ":", -1); |
| 102 | if (strings2[i] == NULL((void*)0)) { |
| 103 | fprintf(stderrstderr, "Out of memory while generating strings2\n"); |
| 104 | exit(1); |
| 105 | } |
| 106 | } |
| 107 | for (j = NB_STRINGS_MAX10000 - NB_STRINGS_NS100, k = 0; |
| 108 | i < NB_STRINGS_MAX10000;i++,j++) { |
| 109 | strings2[i] = xmlStrncatNew(strings2[j], strings2[k], -1); |
| 110 | if (strings2[i] == NULL((void*)0)) { |
| 111 | fprintf(stderrstderr, "Out of memory while generating strings2\n"); |
| 112 | exit(1); |
| 113 | } |
| 114 | k += 3; |
| 115 | if (k >= 50) k = 0; |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | |
| 120 | #ifdef WITH_PRINT |
| 121 | static void print_strings(void) { |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; i < NB_STRINGS_MAX10000;i++) { |
| 125 | printf("%s\n", strings1[i]); |
| 126 | } |
| 127 | for (i = 0; i < NB_STRINGS_MAX10000;i++) { |
| 128 | printf("%s\n", strings2[i]); |
| 129 | } |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | static void clean_strings(void) { |
| 134 | int i; |
| 135 | |
| 136 | for (i = 0; i < NB_STRINGS_MAX10000; i++) { |
| 137 | if (strings1[i] != NULL((void*)0)) |
| 138 | xmlFree(strings1[i]); |
| 139 | } |
| 140 | for (i = 0; i < NB_STRINGS_MAX10000; i++) { |
| 141 | if (strings2[i] != NULL((void*)0)) |
| 142 | xmlFree(strings2[i]); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | static int run_test2(xmlDictPtr parent) { |
| 150 | int i, j; |
| 151 | xmlDictPtr dict; |
| 152 | int ret = 0; |
| 153 | xmlChar prefix[40]; |
| 154 | xmlChar *cur, *pref; |
| 155 | const xmlChar *tmp; |
| 156 | |
| 157 | dict = xmlDictCreateSub(parent); |
| 158 | if (dict == NULL((void*)0)) { |
| 159 | fprintf(stderrstderr, "Out of memory while creating sub-dictionary\n"); |
| 160 | exit(1); |
| 161 | } |
| 162 | memset(test2, 0, sizeof(test2)); |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | for (i = 0;i < NB_STRINGS_MIN10;i++) { |
| 171 | test2[i] = xmlDictLookup(dict, strings2[i], -1); |
| 172 | if (test2[i] == NULL((void*)0)) { |
| 173 | fprintf(stderrstderr, "Failed lookup for '%s'\n", strings2[i]); |
| 174 | ret = 1; |
| 175 | nbErrors++; |
| 176 | } |
| 177 | } |
| 178 | j = NB_STRINGS_MAX10000 - NB_STRINGS_NS100; |
| 179 | |
| 180 | for (i = 0;i < NB_STRINGS_MIN10;i++, j++) { |
| 181 | test2[j] = xmlDictLookup(dict, strings2[j], xmlStrlen(strings2[j])); |
| 182 | if (test2[j] == NULL((void*)0)) { |
| 183 | fprintf(stderrstderr, "Failed lookup for '%s'\n", strings2[j]); |
| 184 | ret = 1; |
| 185 | nbErrors++; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | j = NB_STRINGS_MAX10000 - NB_STRINGS_MIN10; |
| 190 | for (i = 0;i < NB_STRINGS_MIN10;i++, j++) { |
| 191 | test2[j] = xmlDictLookup(dict, strings2[j], xmlStrlen(strings2[j])); |
| 192 | if (test2[j] == NULL((void*)0)) { |
| 193 | fprintf(stderrstderr, "Failed lookup for '%s'\n", strings2[j]); |
| 194 | ret = 1; |
| 195 | nbErrors++; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | for (i = 0;i < NB_STRINGS_MAX10000;i++) { |
| 205 | if (test2[i] != NULL((void*)0)) |
| 206 | continue; |
| 207 | test2[i] = xmlDictLookup(dict, strings2[i], -1); |
| 208 | if (test2[i] == NULL((void*)0)) { |
| 209 | fprintf(stderrstderr, "Failed lookup for '%s'\n", strings2[i]); |
| 210 | ret = 1; |
| 211 | nbErrors++; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | for (i = 0;i < NB_STRINGS_MAX10000;i++) { |
| 220 | if (!xmlDictOwns(dict, test2[i])) { |
| 221 | fprintf(stderrstderr, "Failed ownership failure for '%s'\n", |
| 222 | strings2[i]); |
| 223 | ret = 1; |
| 224 | nbErrors++; |
| 225 | } |
| 226 | if (xmlDictOwns(parent, test2[i])) { |
| 227 | fprintf(stderrstderr, "Failed parent ownership failure for '%s'\n", |
| 228 | strings2[i]); |
| 229 | ret = 1; |
| 230 | nbErrors++; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | for (i = 0;i < NB_STRINGS_MAX10000;i++) { |
| 238 | if (!xmlDictOwns(dict, test1[i])) { |
| 239 | fprintf(stderrstderr, "Failed sub-ownership failure for '%s'\n", |
| 240 | strings1[i]); |
| 241 | ret = 1; |
| 242 | nbErrors++; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | for (i = 0;i < NB_STRINGS_MAX10000;i++) { |
| 250 | if (xmlDictLookup(dict, strings2[i], -1) != test2[i]) { |
| 251 | fprintf(stderrstderr, "Failed re-lookup check for %d, '%s'\n", |
| 252 | i, strings2[i]); |
| 253 | ret = 1; |
| 254 | nbErrors++; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | for (i = 0;i < NB_STRINGS_MAX10000;i++) { |
| 262 | if (xmlDictLookup(dict, strings1[i], -1) != test1[i]) { |
| 263 | fprintf(stderrstderr, "Failed parent string lookup check for %d, '%s'\n", |
| 264 | i, strings1[i]); |
| 265 | ret = 1; |
| 266 | nbErrors++; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | for (i = NB_STRINGS_MAX10000 - NB_STRINGS_NS100;i < NB_STRINGS_MAX10000;i++) { |
| 274 | cur = strings2[i]; |
| 275 | pref = &prefix[0]; |
| 276 | while (*cur != ':') *pref++ = *cur++; |
| 277 | cur++; |
| 278 | *pref = 0; |
| 279 | tmp = xmlDictQLookup(dict, &prefix[0], cur); |
| 280 | if (xmlDictQLookup(dict, &prefix[0], cur) != test2[i]) { |
| 281 | fprintf(stderrstderr, "Failed lookup check for '%s':'%s'\n", |
| 282 | &prefix[0], cur); |
| 283 | ret = 1; |
| 284 | nbErrors++; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | |
| 289 | |
| 290 | for (i = NB_STRINGS_MAX10000 - NB_STRINGS_NS100;i < NB_STRINGS_MAX10000;i++) { |
| 291 | cur = strings1[i]; |
| 292 | pref = &prefix[0]; |
| 293 | while (*cur != ':') *pref++ = *cur++; |
| 294 | cur++; |
| 295 | *pref = 0; |
| 296 | tmp = xmlDictQLookup(dict, &prefix[0], cur); |
| Value stored to 'tmp' is never read |
| 297 | if (xmlDictQLookup(dict, &prefix[0], cur) != test1[i]) { |
| 298 | fprintf(stderrstderr, "Failed parent lookup check for '%s':'%s'\n", |
| 299 | &prefix[0], cur); |
| 300 | ret = 1; |
| 301 | nbErrors++; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | xmlDictFree(dict); |
| 306 | return(ret); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | static int run_test1(void) { |
| 313 | int i, j; |
| 314 | xmlDictPtr dict; |
| 315 | int ret = 0; |
| 316 | xmlChar prefix[40]; |
| 317 | xmlChar *cur, *pref; |
| 318 | const xmlChar *tmp; |
| 319 | |
| 320 | dict = xmlDictCreate(); |
| 321 | if (dict == NULL((void*)0)) { |
| 322 | fprintf(stderrstderr, "Out of memory while creating dictionary\n"); |
| 323 | exit(1); |
| 324 | } |
| 325 | memset(test1, 0, sizeof(test1)); |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | for (i = 0;i < NB_STRINGS_MIN10;i++) { |
| 332 | test1[i] = xmlDictLookup(dict, strings1[i], -1); |
| 333 | if (test1[i] == NULL((void*)0)) { |
| 334 | fprintf(stderrstderr, "Failed lookup for '%s'\n", strings1[i]); |
| 335 | ret = 1; |
| 336 | nbErrors++; |
| 337 | } |
| 338 | } |
| 339 | j = NB_STRINGS_MAX10000 - NB_STRINGS_NS100; |
| 340 | |
| 341 | for (i = 0;i < NB_STRINGS_MIN10;i++, j++) { |
| 342 | test1[j] = xmlDictLookup(dict, strings1[j], xmlStrlen(strings1[j])); |
| 343 | if (test1[j] == NULL((void*)0)) { |
| 344 | fprintf(stderrstderr, "Failed lookup for '%s'\n", strings1[j]); |
| 345 | ret = 1; |
| 346 | nbErrors++; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | j = NB_STRINGS_MAX10000 - NB_STRINGS_MIN10; |
| 351 | for (i = 0;i < NB_STRINGS_MIN10;i++, j++) { |
| 352 | test1[j] = xmlDictLookup(dict, strings1[j], xmlStrlen(strings1[j])); |
| 353 | if (test1[j] == NULL((void*)0)) { |
| 354 | fprintf(stderrstderr, "Failed lookup for '%s'\n", strings1[j]); |
| 355 | ret = 1; |
| 356 | nbErrors++; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | for (i = 0;i < NB_STRINGS_MAX10000;i++) { |
| 366 | if (test1[i] != NULL((void*)0)) |
| 367 | continue; |
| 368 | test1[i] = xmlDictLookup(dict, strings1[i], -1); |
| 369 | if (test1[i] == NULL((void*)0)) { |
| 370 | fprintf(stderrstderr, "Failed lookup for '%s'\n", strings1[i]); |
| 371 | ret = 1; |
| 372 | nbErrors++; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | for (i = 0;i < NB_STRINGS_MAX10000;i++) { |
| 381 | if (!xmlDictOwns(dict, test1[i])) { |
| 382 | fprintf(stderrstderr, "Failed ownership failure for '%s'\n", |
| 383 | strings1[i]); |
| 384 | ret = 1; |
| 385 | nbErrors++; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | for (i = 0;i < NB_STRINGS_MAX10000;i++) { |
| 393 | if (xmlDictLookup(dict, strings1[i], -1) != test1[i]) { |
| 394 | fprintf(stderrstderr, "Failed re-lookup check for %d, '%s'\n", |
| 395 | i, strings1[i]); |
| 396 | ret = 1; |
| 397 | nbErrors++; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | for (i = NB_STRINGS_MAX10000 - NB_STRINGS_NS100;i < NB_STRINGS_MAX10000;i++) { |
| 405 | cur = strings1[i]; |
| 406 | pref = &prefix[0]; |
| 407 | while (*cur != ':') *pref++ = *cur++; |
| 408 | cur++; |
| 409 | *pref = 0; |
| 410 | tmp = xmlDictQLookup(dict, &prefix[0], cur); |
| 411 | if (xmlDictQLookup(dict, &prefix[0], cur) != test1[i]) { |
| 412 | fprintf(stderrstderr, "Failed lookup check for '%s':'%s'\n", |
| 413 | &prefix[0], cur); |
| 414 | ret = 1; |
| 415 | nbErrors++; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | run_test2(dict); |
| 420 | |
| 421 | xmlDictFree(dict); |
| 422 | return(ret); |
| 423 | } |
| 424 | |
| 425 | int main(void) |
| 426 | { |
| 427 | int ret; |
| 428 | |
| 429 | LIBXML_TEST_VERSIONxmlCheckVersion(20703); |
| 430 | fill_strings(); |
| 431 | #ifdef WITH_PRINT |
| 432 | print_strings(); |
| 433 | #endif |
| 434 | ret = run_test1(); |
| 435 | if (ret == 0) { |
| 436 | printf("dictionary tests succeeded %d strings\n", 2 * NB_STRINGS_MAX10000); |
| 437 | } else { |
| 438 | printf("dictionary tests failed with %d errors\n", nbErrors); |
| 439 | } |
| 440 | clean_strings(); |
| 441 | xmlCleanupParser(); |
| 442 | xmlMemoryDump(); |
| 443 | return(ret); |
| 444 | } |