blob: d740a1441d9d42048a215c471d18f544c02f39ae [file] [log] [blame]
Daniel Veillardab7488e2001-10-17 11:30:37 +00001#include <stdlib.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +00002#include <stdio.h>
Daniel Veillard2a906822001-12-06 14:34:08 +00003#include "libxml.h"
Daniel Veillardab7488e2001-10-17 11:30:37 +00004
Daniel Veillarda4617b82001-11-04 20:19:12 +00005#if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
Daniel Veillardab7488e2001-10-17 11:30:37 +00006#include <libxml/globals.h>
7#include <libxml/threads.h>
8#include <libxml/parser.h>
9#include <libxml/catalog.h>
Daniel Veillard82cb3192003-10-29 13:39:15 +000010#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000011#include <pthread.h>
Daniel Veillard82cb3192003-10-29 13:39:15 +000012#elif defined HAVE_BEOS_THREADS
13#include <OS.h>
14#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000015#include <string.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000016#if !defined(_MSC_VER)
Daniel Veillardab7488e2001-10-17 11:30:37 +000017#include <unistd.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000018#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000019#include <assert.h>
20
21#define MAX_ARGC 20
Daniel Veillard82cb3192003-10-29 13:39:15 +000022#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000023static pthread_t tid[MAX_ARGC];
Daniel Veillard82cb3192003-10-29 13:39:15 +000024#elif defined HAVE_BEOS_THREADS
25static thread_id tid[MAX_ARGC];
26#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000027
28static const char *catalog = "test/threads/complex.xml";
29static const char *testfiles[] = {
30 "test/threads/abc.xml",
31 "test/threads/acb.xml",
32 "test/threads/bac.xml",
33 "test/threads/bca.xml",
34 "test/threads/cab.xml",
35 "test/threads/cba.xml",
36 "test/threads/invalid.xml",
37};
38
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000039const char *Okay = "OK";
40const char *Failed = "Failed";
41
42#ifndef xmlDoValidityCheckingDefaultValue
43#error xmlDoValidityCheckingDefaultValue is not a macro
44#endif
45#ifndef xmlGenericErrorContext
46#error xmlGenericErrorContext is not a macro
47#endif
48
Daniel Veillardab7488e2001-10-17 11:30:37 +000049static void *
50thread_specific_data(void *private_data)
51{
52 xmlDocPtr myDoc;
53 const char *filename = (const char *) private_data;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000054 int okay = 1;
Daniel Veillardab7488e2001-10-17 11:30:37 +000055
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000056 if (!strcmp(filename, "test/threads/invalid.xml")) {
Daniel Veillardab7488e2001-10-17 11:30:37 +000057 xmlDoValidityCheckingDefaultValue = 0;
58 xmlGenericErrorContext = stdout;
59 } else {
60 xmlDoValidityCheckingDefaultValue = 1;
61 xmlGenericErrorContext = stderr;
62 }
63 myDoc = xmlParseFile(filename);
64 if (myDoc) {
65 xmlFreeDoc(myDoc);
Daniel Veillardab7488e2001-10-17 11:30:37 +000066 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000067 printf("parse failed\n");
68 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000069 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000070 if (!strcmp(filename, "test/threads/invalid.xml")) {
71 if (xmlDoValidityCheckingDefaultValue != 0) {
72 printf("ValidityCheckingDefaultValue override failed\n");
73 okay = 0;
74 }
75 if (xmlGenericErrorContext != stdout) {
76 printf("xmlGenericErrorContext override failed\n");
77 okay = 0;
78 }
79 } else {
80 if (xmlDoValidityCheckingDefaultValue != 1) {
81 printf("ValidityCheckingDefaultValue override failed\n");
82 okay = 0;
83 }
84 if (xmlGenericErrorContext != stderr) {
85 printf("xmlGenericErrorContext override failed\n");
86 okay = 0;
87 }
88 }
89 if (okay == 0)
90 return((void *) Failed);
91 return ((void *) Okay);
Daniel Veillardab7488e2001-10-17 11:30:37 +000092}
93
Daniel Veillard82cb3192003-10-29 13:39:15 +000094#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +000095int
William M. Bracka71a8ef2003-08-06 04:43:55 +000096main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +000097{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000098 unsigned int i, repeat;
Daniel Veillardab7488e2001-10-17 11:30:37 +000099 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000100 void *results[MAX_ARGC];
101 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000102
103 xmlInitParser();
Daniel Veillard89cad532001-10-22 09:46:13 +0000104 for (repeat = 0;repeat < 500;repeat++) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000105 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000106
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000107 for (i = 0; i < num_threads; i++) {
108 results[i] = NULL;
Daniel Veillardd3b08822001-12-05 12:03:33 +0000109 tid[i] = (pthread_t) -1;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000110 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000111
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000112 for (i = 0; i < num_threads; i++) {
113 ret = pthread_create(&tid[i], 0, thread_specific_data,
114 (void *) testfiles[i]);
115 if (ret != 0) {
116 perror("pthread_create");
117 exit(1);
118 }
119 }
120 for (i = 0; i < num_threads; i++) {
121 ret = pthread_join(tid[i], &results[i]);
122 if (ret != 0) {
123 perror("pthread_join");
124 exit(1);
125 }
126 }
127
128 xmlCatalogCleanup();
129 for (i = 0; i < num_threads; i++)
130 if (results[i] != (void *) Okay)
131 printf("Thread %d handling %s failed\n", i, testfiles[i]);
132 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000133 xmlCleanupParser();
134 xmlMemoryDump();
135 return (0);
136}
Daniel Veillard82cb3192003-10-29 13:39:15 +0000137#elif defined HAVE_BEOS_THREADS
138int
139main(void)
140{
141 unsigned int i, repeat;
142 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
143 void *results[MAX_ARGC];
144 status_t ret;
145
146 xmlInitParser();
147 printf("Parser initialized\n");
148 for (repeat = 0;repeat < 500;repeat++) {
149 printf("repeat: %d\n",repeat);
150 xmlLoadCatalog(catalog);
151 printf("loaded catalog: %s\n", catalog);
152 for (i = 0; i < num_threads; i++) {
153 results[i] = NULL;
154 tid[i] = (thread_id) -1;
155 }
156 printf("cleaned threads\n");
157 for (i = 0; i < num_threads; i++) {
158 tid[i] = spawn_thread(thread_specific_data, "xmlTestThread", B_NORMAL_PRIORITY, (void *) testfiles[i]);
159 if (tid[i] < B_OK) {
160 perror("beos_thread_create");
161 exit(1);
162 }
163 printf("beos_thread_create %d -> %d\n", i, tid[i]);
164 }
165 for (i = 0; i < num_threads; i++) {
166 ret = wait_for_thread(tid[i], &results[i]);
167 printf("beos_thread_wait %d -> %d\n", i, ret);
168 if (ret != B_OK) {
169 perror("beos_thread_wait");
170 exit(1);
171 }
172 }
173
174 xmlCatalogCleanup();
175 ret = B_OK;
176 for (i = 0; i < num_threads; i++)
177 if (results[i] != (void *) Okay) {
178 printf("Thread %d handling %s failed\n", i, testfiles[i]);
179 ret = B_ERROR;
180 }
181 }
182 xmlCleanupParser();
183 xmlMemoryDump();
184
185 if (ret == B_OK)
186 printf("testThread : BeOS : SUCCESS!\n");
187 else
188 printf("testThread : BeOS : FAILED!\n");
189
190 return (0);
191}
192#endif /* pthreads or BeOS threads */
Daniel Veillardab7488e2001-10-17 11:30:37 +0000193
194#else /* !LIBXML_THREADS_ENABLED */
195int
Daniel Veillard118aed72002-09-24 14:13:13 +0000196main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000197{
Daniel Veillarda4617b82001-11-04 20:19:12 +0000198 fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
Daniel Veillardab7488e2001-10-17 11:30:37 +0000199 return (0);
200}
201#endif