blob: b43cbd0e963e5cf81cf4d1fd03bd80061f27e443 [file] [log] [blame]
William M. Brackc1099be2007-01-31 18:38:56 +00001#include "libxml.h"
2
Daniel Veillardab7488e2001-10-17 11:30:37 +00003#include <stdlib.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00004#include <stdio.h>
Daniel Veillardab7488e2001-10-17 11:30:37 +00005
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00006#if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED) && defined(LIBXML_SAX1_ENABLED)
Daniel Veillardab7488e2001-10-17 11:30:37 +00007#include <libxml/globals.h>
8#include <libxml/threads.h>
9#include <libxml/parser.h>
10#include <libxml/catalog.h>
Daniel Veillard82cb3192003-10-29 13:39:15 +000011#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000012#include <pthread.h>
Daniel Veillard82cb3192003-10-29 13:39:15 +000013#elif defined HAVE_BEOS_THREADS
14#include <OS.h>
15#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000016#include <string.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000017#if !defined(_MSC_VER)
Daniel Veillardab7488e2001-10-17 11:30:37 +000018#include <unistd.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000019#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000020#include <assert.h>
21
22#define MAX_ARGC 20
Daniel Veillard82cb3192003-10-29 13:39:15 +000023#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000024static pthread_t tid[MAX_ARGC];
Daniel Veillard82cb3192003-10-29 13:39:15 +000025#elif defined HAVE_BEOS_THREADS
26static thread_id tid[MAX_ARGC];
27#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000028
29static const char *catalog = "test/threads/complex.xml";
30static const char *testfiles[] = {
31 "test/threads/abc.xml",
32 "test/threads/acb.xml",
33 "test/threads/bac.xml",
34 "test/threads/bca.xml",
35 "test/threads/cab.xml",
36 "test/threads/cba.xml",
37 "test/threads/invalid.xml",
38};
39
Daniel Veillard24505b02005-07-28 23:49:35 +000040static const char *Okay = "OK";
41static const char *Failed = "Failed";
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000042
43#ifndef xmlDoValidityCheckingDefaultValue
44#error xmlDoValidityCheckingDefaultValue is not a macro
45#endif
46#ifndef xmlGenericErrorContext
47#error xmlGenericErrorContext is not a macro
48#endif
49
Daniel Veillardab7488e2001-10-17 11:30:37 +000050static void *
51thread_specific_data(void *private_data)
52{
53 xmlDocPtr myDoc;
54 const char *filename = (const char *) private_data;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000055 int okay = 1;
Daniel Veillardab7488e2001-10-17 11:30:37 +000056
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000057 if (!strcmp(filename, "test/threads/invalid.xml")) {
Daniel Veillardab7488e2001-10-17 11:30:37 +000058 xmlDoValidityCheckingDefaultValue = 0;
59 xmlGenericErrorContext = stdout;
60 } else {
61 xmlDoValidityCheckingDefaultValue = 1;
62 xmlGenericErrorContext = stderr;
63 }
64 myDoc = xmlParseFile(filename);
65 if (myDoc) {
66 xmlFreeDoc(myDoc);
Daniel Veillardab7488e2001-10-17 11:30:37 +000067 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000068 printf("parse failed\n");
69 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000070 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000071 if (!strcmp(filename, "test/threads/invalid.xml")) {
72 if (xmlDoValidityCheckingDefaultValue != 0) {
73 printf("ValidityCheckingDefaultValue override failed\n");
74 okay = 0;
75 }
76 if (xmlGenericErrorContext != stdout) {
77 printf("xmlGenericErrorContext override failed\n");
78 okay = 0;
79 }
80 } else {
81 if (xmlDoValidityCheckingDefaultValue != 1) {
82 printf("ValidityCheckingDefaultValue override failed\n");
83 okay = 0;
84 }
85 if (xmlGenericErrorContext != stderr) {
86 printf("xmlGenericErrorContext override failed\n");
87 okay = 0;
88 }
89 }
90 if (okay == 0)
91 return((void *) Failed);
92 return ((void *) Okay);
Daniel Veillardab7488e2001-10-17 11:30:37 +000093}
94
Daniel Veillard82cb3192003-10-29 13:39:15 +000095#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000096int
William M. Bracka71a8ef2003-08-06 04:43:55 +000097main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +000098{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000099 unsigned int i, repeat;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000100 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000101 void *results[MAX_ARGC];
102 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000103
104 xmlInitParser();
Daniel Veillard89cad532001-10-22 09:46:13 +0000105 for (repeat = 0;repeat < 500;repeat++) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000106 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000107
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000108 for (i = 0; i < num_threads; i++) {
109 results[i] = NULL;
Daniel Veillardd3b08822001-12-05 12:03:33 +0000110 tid[i] = (pthread_t) -1;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000111 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000112
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000113 for (i = 0; i < num_threads; i++) {
Daniel Veillard24505b02005-07-28 23:49:35 +0000114 ret = pthread_create(&tid[i], NULL, thread_specific_data,
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000115 (void *) testfiles[i]);
116 if (ret != 0) {
117 perror("pthread_create");
118 exit(1);
119 }
120 }
121 for (i = 0; i < num_threads; i++) {
122 ret = pthread_join(tid[i], &results[i]);
123 if (ret != 0) {
124 perror("pthread_join");
125 exit(1);
126 }
127 }
128
129 xmlCatalogCleanup();
130 for (i = 0; i < num_threads; i++)
131 if (results[i] != (void *) Okay)
132 printf("Thread %d handling %s failed\n", i, testfiles[i]);
133 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000134 xmlCleanupParser();
135 xmlMemoryDump();
136 return (0);
137}
Daniel Veillard82cb3192003-10-29 13:39:15 +0000138#elif defined HAVE_BEOS_THREADS
139int
140main(void)
141{
142 unsigned int i, repeat;
143 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
144 void *results[MAX_ARGC];
145 status_t ret;
146
147 xmlInitParser();
148 printf("Parser initialized\n");
149 for (repeat = 0;repeat < 500;repeat++) {
150 printf("repeat: %d\n",repeat);
151 xmlLoadCatalog(catalog);
152 printf("loaded catalog: %s\n", catalog);
153 for (i = 0; i < num_threads; i++) {
154 results[i] = NULL;
155 tid[i] = (thread_id) -1;
156 }
157 printf("cleaned threads\n");
158 for (i = 0; i < num_threads; i++) {
159 tid[i] = spawn_thread(thread_specific_data, "xmlTestThread", B_NORMAL_PRIORITY, (void *) testfiles[i]);
160 if (tid[i] < B_OK) {
161 perror("beos_thread_create");
162 exit(1);
163 }
164 printf("beos_thread_create %d -> %d\n", i, tid[i]);
165 }
166 for (i = 0; i < num_threads; i++) {
167 ret = wait_for_thread(tid[i], &results[i]);
168 printf("beos_thread_wait %d -> %d\n", i, ret);
169 if (ret != B_OK) {
170 perror("beos_thread_wait");
171 exit(1);
172 }
173 }
174
175 xmlCatalogCleanup();
176 ret = B_OK;
177 for (i = 0; i < num_threads; i++)
178 if (results[i] != (void *) Okay) {
179 printf("Thread %d handling %s failed\n", i, testfiles[i]);
180 ret = B_ERROR;
181 }
182 }
183 xmlCleanupParser();
184 xmlMemoryDump();
185
186 if (ret == B_OK)
187 printf("testThread : BeOS : SUCCESS!\n");
188 else
189 printf("testThread : BeOS : FAILED!\n");
190
191 return (0);
192}
193#endif /* pthreads or BeOS threads */
Daniel Veillardab7488e2001-10-17 11:30:37 +0000194
195#else /* !LIBXML_THREADS_ENABLED */
196int
Daniel Veillard118aed72002-09-24 14:13:13 +0000197main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000198{
Daniel Veillarda4617b82001-11-04 20:19:12 +0000199 fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
Daniel Veillardab7488e2001-10-17 11:30:37 +0000200 return (0);
201}
202#endif