ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

testdict.c
Go to the documentation of this file.
00001 #include <string.h>
00002 #include <libxml/parser.h>
00003 #include <libxml/dict.h>
00004 
00005 /* #define WITH_PRINT */
00006 
00007 static const char *seeds1[] = {
00008    "a", "b", "c",
00009    "d", "e", "f",
00010    "g", "h", "i",
00011    "j", "k", "l",
00012 
00013    NULL
00014 };
00015 
00016 static const char *seeds2[] = {
00017    "m", "n", "o",
00018    "p", "q", "r",
00019    "s", "t", "u",
00020    "v", "w", "x",
00021 
00022    NULL
00023 };
00024 
00025 #define NB_STRINGS_NS 100
00026 #define NB_STRINGS_MAX 10000
00027 #define NB_STRINGS_MIN 10
00028 
00029 static xmlChar *strings1[NB_STRINGS_MAX];
00030 static xmlChar *strings2[NB_STRINGS_MAX];
00031 static const xmlChar *test1[NB_STRINGS_MAX];
00032 static const xmlChar *test2[NB_STRINGS_MAX];
00033 static int nbErrors = 0;
00034 
00035 static void fill_strings(void) {
00036     int i, j, k;
00037 
00038     /*
00039      * That's a bit nasty but the output is fine and it doesn't take hours
00040      * there is a small but sufficient number of duplicates, and we have
00041      * ":xxx" and full QNames in the last NB_STRINGS_NS values
00042      */
00043     for (i = 0; seeds1[i] != NULL; i++) {
00044         strings1[i] = xmlStrdup((const xmlChar *) seeds1[i]);
00045     if (strings1[i] == NULL) {
00046         fprintf(stderr, "Out of memory while generating strings1\n");
00047         exit(1);
00048     }
00049     }
00050     for (j = 0, k = 0;i < NB_STRINGS_MAX - NB_STRINGS_NS;i++,j++) {
00051         strings1[i] = xmlStrncatNew(strings1[j], strings1[k], -1);
00052     if (strings1[i] == NULL) {
00053         fprintf(stderr, "Out of memory while generating strings1\n");
00054         exit(1);
00055     }
00056     if (j >= 50) {
00057         j = 0;
00058         k++;
00059     }
00060     }
00061     for (j = 0; (j < 50) && (i < NB_STRINGS_MAX); i++, j+=2) {
00062         strings1[i] = xmlStrncatNew(strings1[j], (const xmlChar *) ":", -1);
00063     if (strings1[i] == NULL) {
00064         fprintf(stderr, "Out of memory while generating strings1\n");
00065         exit(1);
00066     }
00067     }
00068     for (j = NB_STRINGS_MAX - NB_STRINGS_NS, k = 0;
00069          i < NB_STRINGS_MAX;i++,j++) {
00070         strings1[i] = xmlStrncatNew(strings1[j], strings1[k], -1);
00071     if (strings1[i] == NULL) {
00072         fprintf(stderr, "Out of memory while generating strings1\n");
00073         exit(1);
00074     }
00075     k += 3;
00076     if (k >= 50) k = 0;
00077     }
00078 
00079     /*
00080      * Now do the same with the second pool of strings
00081      */
00082     for (i = 0; seeds2[i] != NULL; i++) {
00083         strings2[i] = xmlStrdup((const xmlChar *) seeds2[i]);
00084     if (strings2[i] == NULL) {
00085         fprintf(stderr, "Out of memory while generating strings2\n");
00086         exit(1);
00087     }
00088     }
00089     for (j = 0, k = 0;i < NB_STRINGS_MAX - NB_STRINGS_NS;i++,j++) {
00090         strings2[i] = xmlStrncatNew(strings2[j], strings2[k], -1);
00091     if (strings2[i] == NULL) {
00092         fprintf(stderr, "Out of memory while generating strings2\n");
00093         exit(1);
00094     }
00095     if (j >= 50) {
00096         j = 0;
00097         k++;
00098     }
00099     }
00100     for (j = 0; (j < 50) && (i < NB_STRINGS_MAX); i++, j+=2) {
00101         strings2[i] = xmlStrncatNew(strings2[j], (const xmlChar *) ":", -1);
00102     if (strings2[i] == NULL) {
00103         fprintf(stderr, "Out of memory while generating strings2\n");
00104         exit(1);
00105     }
00106     }
00107     for (j = NB_STRINGS_MAX - NB_STRINGS_NS, k = 0;
00108          i < NB_STRINGS_MAX;i++,j++) {
00109         strings2[i] = xmlStrncatNew(strings2[j], strings2[k], -1);
00110     if (strings2[i] == NULL) {
00111         fprintf(stderr, "Out of memory while generating strings2\n");
00112         exit(1);
00113     }
00114     k += 3;
00115     if (k >= 50) k = 0;
00116     }
00117 
00118 }
00119 
00120 #ifdef WITH_PRINT
00121 static void print_strings(void) {
00122     int i;
00123 
00124     for (i = 0; i < NB_STRINGS_MAX;i++) {
00125         printf("%s\n", strings1[i]);
00126     }
00127     for (i = 0; i < NB_STRINGS_MAX;i++) {
00128         printf("%s\n", strings2[i]);
00129     }
00130 }
00131 #endif
00132 
00133 static void clean_strings(void) {
00134     int i;
00135 
00136     for (i = 0; i < NB_STRINGS_MAX; i++) {
00137         if (strings1[i] != NULL) /* really should not happen */
00138         xmlFree(strings1[i]);
00139     }
00140     for (i = 0; i < NB_STRINGS_MAX; i++) {
00141         if (strings2[i] != NULL) /* really should not happen */
00142         xmlFree(strings2[i]);
00143     }
00144 }
00145 
00146 /*
00147  * This tests the sub-dictionary support
00148  */
00149 static int run_test2(xmlDictPtr parent) {
00150     int i, j;
00151     xmlDictPtr dict;
00152     int ret = 0;
00153     xmlChar prefix[40];
00154     xmlChar *cur, *pref;
00155     const xmlChar *tmp;
00156 
00157     dict = xmlDictCreateSub(parent);
00158     if (dict == NULL) {
00159     fprintf(stderr, "Out of memory while creating sub-dictionary\n");
00160     exit(1);
00161     }
00162     memset(test2, 0, sizeof(test2));
00163 
00164     /*
00165      * Fill in NB_STRINGS_MIN, at this point the dictionary should not grow
00166      * and we allocate all those doing the fast key computations
00167      * All the strings are based on a different seeds subset so we know
00168      * they are allocated in the main dictionary, not coming from the parent
00169      */
00170     for (i = 0;i < NB_STRINGS_MIN;i++) {
00171         test2[i] = xmlDictLookup(dict, strings2[i], -1);
00172     if (test2[i] == NULL) {
00173         fprintf(stderr, "Failed lookup for '%s'\n", strings2[i]);
00174         ret = 1;
00175         nbErrors++;
00176     }
00177     }
00178     j = NB_STRINGS_MAX - NB_STRINGS_NS;
00179     /* ":foo" like strings2 */
00180     for (i = 0;i < NB_STRINGS_MIN;i++, j++) {
00181         test2[j] = xmlDictLookup(dict, strings2[j], xmlStrlen(strings2[j]));
00182     if (test2[j] == NULL) {
00183         fprintf(stderr, "Failed lookup for '%s'\n", strings2[j]);
00184         ret = 1;
00185         nbErrors++;
00186     }
00187     }
00188     /* "a:foo" like strings2 */
00189     j = NB_STRINGS_MAX - NB_STRINGS_MIN;
00190     for (i = 0;i < NB_STRINGS_MIN;i++, j++) {
00191         test2[j] = xmlDictLookup(dict, strings2[j], xmlStrlen(strings2[j]));
00192     if (test2[j] == NULL) {
00193         fprintf(stderr, "Failed lookup for '%s'\n", strings2[j]);
00194         ret = 1;
00195         nbErrors++;
00196     }
00197     }
00198 
00199     /*
00200      * At this point allocate all the strings
00201      * the dictionary will grow in the process, reallocate more string tables
00202      * and switch to the better key generator
00203      */
00204     for (i = 0;i < NB_STRINGS_MAX;i++) {
00205         if (test2[i] != NULL)
00206         continue;
00207     test2[i] = xmlDictLookup(dict, strings2[i], -1);
00208     if (test2[i] == NULL) {
00209         fprintf(stderr, "Failed lookup for '%s'\n", strings2[i]);
00210         ret = 1;
00211         nbErrors++;
00212     }
00213     }
00214 
00215     /*
00216      * Now we can start to test things, first that all strings2 belongs to
00217      * the dict, and that none of them was actually allocated in the parent
00218      */
00219     for (i = 0;i < NB_STRINGS_MAX;i++) {
00220         if (!xmlDictOwns(dict, test2[i])) {
00221         fprintf(stderr, "Failed ownership failure for '%s'\n",
00222                 strings2[i]);
00223         ret = 1;
00224         nbErrors++;
00225     }
00226         if (xmlDictOwns(parent, test2[i])) {
00227         fprintf(stderr, "Failed parent ownership failure for '%s'\n",
00228                 strings2[i]);
00229         ret = 1;
00230         nbErrors++;
00231     }
00232     }
00233 
00234     /*
00235      * Also verify that all strings from the parent are seen from the subdict
00236      */
00237     for (i = 0;i < NB_STRINGS_MAX;i++) {
00238         if (!xmlDictOwns(dict, test1[i])) {
00239         fprintf(stderr, "Failed sub-ownership failure for '%s'\n",
00240                 strings1[i]);
00241         ret = 1;
00242         nbErrors++;
00243     }
00244     }
00245 
00246     /*
00247      * Then that another lookup to the string in sub will return the same
00248      */
00249     for (i = 0;i < NB_STRINGS_MAX;i++) {
00250         if (xmlDictLookup(dict, strings2[i], -1) != test2[i]) {
00251         fprintf(stderr, "Failed re-lookup check for %d, '%s'\n",
00252                 i, strings2[i]);
00253         ret = 1;
00254         nbErrors++;
00255     }
00256     }
00257     /*
00258      * But also that any lookup for a string in the parent will be provided
00259      * as in the parent
00260      */
00261     for (i = 0;i < NB_STRINGS_MAX;i++) {
00262         if (xmlDictLookup(dict, strings1[i], -1) != test1[i]) {
00263         fprintf(stderr, "Failed parent string lookup check for %d, '%s'\n",
00264                 i, strings1[i]);
00265         ret = 1;
00266         nbErrors++;
00267     }
00268     }
00269 
00270     /*
00271      * check the QName lookups
00272      */
00273     for (i = NB_STRINGS_MAX - NB_STRINGS_NS;i < NB_STRINGS_MAX;i++) {
00274         cur = strings2[i];
00275     pref = &prefix[0];
00276     while (*cur != ':') *pref++ = *cur++;
00277     cur++;
00278     *pref = 0;
00279     tmp = xmlDictQLookup(dict, &prefix[0], cur);
00280     if (xmlDictQLookup(dict, &prefix[0], cur) != test2[i]) {
00281         fprintf(stderr, "Failed lookup check for '%s':'%s'\n",
00282                 &prefix[0], cur);
00283             ret = 1;
00284         nbErrors++;
00285     }
00286     }
00287     /*
00288      * check the QName lookups for strings from the parent
00289      */
00290     for (i = NB_STRINGS_MAX - NB_STRINGS_NS;i < NB_STRINGS_MAX;i++) {
00291         cur = strings1[i];
00292     pref = &prefix[0];
00293     while (*cur != ':') *pref++ = *cur++;
00294     cur++;
00295     *pref = 0;
00296     tmp = xmlDictQLookup(dict, &prefix[0], cur);
00297     if (xmlDictQLookup(dict, &prefix[0], cur) != test1[i]) {
00298         fprintf(stderr, "Failed parent lookup check for '%s':'%s'\n",
00299                 &prefix[0], cur);
00300             ret = 1;
00301         nbErrors++;
00302     }
00303     }
00304 
00305     xmlDictFree(dict);
00306     return(ret);
00307 }
00308 
00309 /*
00310  * Test a single dictionary
00311  */
00312 static int run_test1(void) {
00313     int i, j;
00314     xmlDictPtr dict;
00315     int ret = 0;
00316     xmlChar prefix[40];
00317     xmlChar *cur, *pref;
00318     const xmlChar *tmp;
00319 
00320     dict = xmlDictCreate();
00321     if (dict == NULL) {
00322     fprintf(stderr, "Out of memory while creating dictionary\n");
00323     exit(1);
00324     }
00325     memset(test1, 0, sizeof(test1));
00326 
00327     /*
00328      * Fill in NB_STRINGS_MIN, at this point the dictionary should not grow
00329      * and we allocate all those doing the fast key computations
00330      */
00331     for (i = 0;i < NB_STRINGS_MIN;i++) {
00332         test1[i] = xmlDictLookup(dict, strings1[i], -1);
00333     if (test1[i] == NULL) {
00334         fprintf(stderr, "Failed lookup for '%s'\n", strings1[i]);
00335         ret = 1;
00336         nbErrors++;
00337     }
00338     }
00339     j = NB_STRINGS_MAX - NB_STRINGS_NS;
00340     /* ":foo" like strings1 */
00341     for (i = 0;i < NB_STRINGS_MIN;i++, j++) {
00342         test1[j] = xmlDictLookup(dict, strings1[j], xmlStrlen(strings1[j]));
00343     if (test1[j] == NULL) {
00344         fprintf(stderr, "Failed lookup for '%s'\n", strings1[j]);
00345         ret = 1;
00346         nbErrors++;
00347     }
00348     }
00349     /* "a:foo" like strings1 */
00350     j = NB_STRINGS_MAX - NB_STRINGS_MIN;
00351     for (i = 0;i < NB_STRINGS_MIN;i++, j++) {
00352         test1[j] = xmlDictLookup(dict, strings1[j], xmlStrlen(strings1[j]));
00353     if (test1[j] == NULL) {
00354         fprintf(stderr, "Failed lookup for '%s'\n", strings1[j]);
00355         ret = 1;
00356         nbErrors++;
00357     }
00358     }
00359 
00360     /*
00361      * At this point allocate all the strings
00362      * the dictionary will grow in the process, reallocate more string tables
00363      * and switch to the better key generator
00364      */
00365     for (i = 0;i < NB_STRINGS_MAX;i++) {
00366         if (test1[i] != NULL)
00367         continue;
00368     test1[i] = xmlDictLookup(dict, strings1[i], -1);
00369     if (test1[i] == NULL) {
00370         fprintf(stderr, "Failed lookup for '%s'\n", strings1[i]);
00371         ret = 1;
00372         nbErrors++;
00373     }
00374     }
00375 
00376     /*
00377      * Now we can start to test things, first that all strings1 belongs to
00378      * the dict
00379      */
00380     for (i = 0;i < NB_STRINGS_MAX;i++) {
00381         if (!xmlDictOwns(dict, test1[i])) {
00382         fprintf(stderr, "Failed ownership failure for '%s'\n",
00383                 strings1[i]);
00384         ret = 1;
00385         nbErrors++;
00386     }
00387     }
00388 
00389     /*
00390      * Then that another lookup to the string will return the same
00391      */
00392     for (i = 0;i < NB_STRINGS_MAX;i++) {
00393         if (xmlDictLookup(dict, strings1[i], -1) != test1[i]) {
00394         fprintf(stderr, "Failed re-lookup check for %d, '%s'\n",
00395                 i, strings1[i]);
00396         ret = 1;
00397         nbErrors++;
00398     }
00399     }
00400 
00401     /*
00402      * More complex, check the QName lookups
00403      */
00404     for (i = NB_STRINGS_MAX - NB_STRINGS_NS;i < NB_STRINGS_MAX;i++) {
00405         cur = strings1[i];
00406     pref = &prefix[0];
00407     while (*cur != ':') *pref++ = *cur++;
00408     cur++;
00409     *pref = 0;
00410     tmp = xmlDictQLookup(dict, &prefix[0], cur);
00411     if (xmlDictQLookup(dict, &prefix[0], cur) != test1[i]) {
00412         fprintf(stderr, "Failed lookup check for '%s':'%s'\n",
00413                 &prefix[0], cur);
00414             ret = 1;
00415         nbErrors++;
00416     }
00417     }
00418 
00419     run_test2(dict);
00420 
00421     xmlDictFree(dict);
00422     return(ret);
00423 }
00424 
00425 int main(void)
00426 {
00427     int ret;
00428 
00429     LIBXML_TEST_VERSION
00430     fill_strings();
00431 #ifdef WITH_PRINT
00432     print_strings();
00433 #endif
00434     ret = run_test1();
00435     if (ret == 0) {
00436         printf("dictionary tests succeeded %d strings\n", 2 * NB_STRINGS_MAX);
00437     } else {
00438         printf("dictionary tests failed with %d errors\n", nbErrors);
00439     }
00440     clean_strings();
00441     xmlCleanupParser();
00442     xmlMemoryDump();
00443     return(ret);
00444 }

Generated on Sat May 26 2012 04:33:32 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.