updated libxml2 to 2.5.10
[TestXSLT.git] / libxml2 / testThreads.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include "libxml.h"
4
5 #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
6 #include <libxml/globals.h>
7 #include <libxml/threads.h>
8 #include <libxml/parser.h>
9 #include <libxml/catalog.h>
10 #include <pthread.h>
11 #include <string.h>
12 #if !defined(_MSC_VER)
13 #include <unistd.h>
14 #endif
15 #include <assert.h>
16
17 #define MAX_ARGC        20
18 static pthread_t tid[MAX_ARGC];
19
20 static const char *catalog = "test/threads/complex.xml";
21 static const char *testfiles[] = {
22     "test/threads/abc.xml",
23     "test/threads/acb.xml",
24     "test/threads/bac.xml",
25     "test/threads/bca.xml",
26     "test/threads/cab.xml",
27     "test/threads/cba.xml",
28     "test/threads/invalid.xml",
29 };
30
31 const char *Okay = "OK";
32 const char *Failed = "Failed";
33
34 #ifndef xmlDoValidityCheckingDefaultValue
35 #error xmlDoValidityCheckingDefaultValue is not a macro
36 #endif
37 #ifndef xmlGenericErrorContext
38 #error xmlGenericErrorContext is not a macro
39 #endif
40
41 static void *
42 thread_specific_data(void *private_data)
43 {
44     xmlDocPtr myDoc;
45     const char *filename = (const char *) private_data;
46     int okay = 1;
47
48     if (!strcmp(filename, "test/threads/invalid.xml")) {
49         xmlDoValidityCheckingDefaultValue = 0;
50         xmlGenericErrorContext = stdout;
51     } else {
52         xmlDoValidityCheckingDefaultValue = 1;
53         xmlGenericErrorContext = stderr;
54     }
55     myDoc = xmlParseFile(filename);
56     if (myDoc) {
57         xmlFreeDoc(myDoc);
58     } else {
59         printf("parse failed\n");
60         okay = 0;
61     }
62     if (!strcmp(filename, "test/threads/invalid.xml")) {
63         if (xmlDoValidityCheckingDefaultValue != 0) {
64             printf("ValidityCheckingDefaultValue override failed\n");
65             okay = 0;
66         }
67         if (xmlGenericErrorContext != stdout) {
68             printf("xmlGenericErrorContext override failed\n");
69             okay = 0;
70         }
71     } else {
72         if (xmlDoValidityCheckingDefaultValue != 1) {
73             printf("ValidityCheckingDefaultValue override failed\n");
74             okay = 0;
75         }
76         if (xmlGenericErrorContext != stderr) {
77             printf("xmlGenericErrorContext override failed\n");
78             okay = 0;
79         }
80     }
81     if (okay == 0)
82         return((void *) Failed);
83     return ((void *) Okay);
84 }
85
86 int
87 main(void)
88 {
89     unsigned int i, repeat;
90     unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
91     void *results[MAX_ARGC];
92     int ret;
93
94     xmlInitParser();
95     for (repeat = 0;repeat < 500;repeat++) {
96         xmlLoadCatalog(catalog);
97
98         for (i = 0; i < num_threads; i++) {
99             results[i] = NULL;
100             tid[i] = (pthread_t) -1;
101         }
102
103         for (i = 0; i < num_threads; i++) {
104             ret = pthread_create(&tid[i], 0, thread_specific_data,
105                                  (void *) testfiles[i]);
106             if (ret != 0) {
107                 perror("pthread_create");
108                 exit(1);
109             }
110         }
111         for (i = 0; i < num_threads; i++) {
112             ret = pthread_join(tid[i], &results[i]);
113             if (ret != 0) {
114                 perror("pthread_join");
115                 exit(1);
116             }
117         }
118
119         xmlCatalogCleanup();
120         for (i = 0; i < num_threads; i++)
121             if (results[i] != (void *) Okay)
122                 printf("Thread %d handling %s failed\n", i, testfiles[i]);
123     }
124     xmlCleanupParser();
125     xmlMemoryDump();
126     return (0);
127 }
128
129 #else /* !LIBXML_THREADS_ENABLED */
130 int
131 main(void)
132 {
133     fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
134     return (0);
135 }
136 #endif