blob: 2ef70a74c907da41573952c7172050e57831ee17 [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
Nick Wellnhofer01a4b812017-06-16 21:27:47 +02006#if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_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 }
Nick Wellnhofer01a4b812017-06-16 21:27:47 +020064#ifdef LIBXML_SAX1_ENABLED
Daniel Veillardab7488e2001-10-17 11:30:37 +000065 myDoc = xmlParseFile(filename);
Nick Wellnhofer01a4b812017-06-16 21:27:47 +020066#else
67 myDoc = xmlReadFile(filename, NULL, XML_WITH_CATALOG);
68#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000069 if (myDoc) {
70 xmlFreeDoc(myDoc);
Daniel Veillardab7488e2001-10-17 11:30:37 +000071 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000072 printf("parse failed\n");
73 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000074 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000075 if (!strcmp(filename, "test/threads/invalid.xml")) {
76 if (xmlDoValidityCheckingDefaultValue != 0) {
77 printf("ValidityCheckingDefaultValue override failed\n");
78 okay = 0;
79 }
80 if (xmlGenericErrorContext != stdout) {
81 printf("xmlGenericErrorContext override failed\n");
82 okay = 0;
83 }
84 } else {
85 if (xmlDoValidityCheckingDefaultValue != 1) {
86 printf("ValidityCheckingDefaultValue override failed\n");
87 okay = 0;
88 }
89 if (xmlGenericErrorContext != stderr) {
90 printf("xmlGenericErrorContext override failed\n");
91 okay = 0;
92 }
93 }
94 if (okay == 0)
95 return((void *) Failed);
96 return ((void *) Okay);
Daniel Veillardab7488e2001-10-17 11:30:37 +000097}
98
Daniel Veillard82cb3192003-10-29 13:39:15 +000099#ifdef HAVE_PTHREAD_H
Daniel Veillardab7488e2001-10-17 11:30:37 +0000100int
William M. Bracka71a8ef2003-08-06 04:43:55 +0000101main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000102{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000103 unsigned int i, repeat;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000104 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000105 void *results[MAX_ARGC];
106 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +0000107
108 xmlInitParser();
Daniel Veillard89cad532001-10-22 09:46:13 +0000109 for (repeat = 0;repeat < 500;repeat++) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000110 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000111
Andrew W. Nosenkod794a842010-11-15 13:00:29 +0100112 memset(results, 0, sizeof(*results)*num_threads);
113 memset(tid, 0xff, sizeof(*tid)*num_threads);
Daniel Veillardab7488e2001-10-17 11:30:37 +0000114
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000115 for (i = 0; i < num_threads; i++) {
Daniel Veillard24505b02005-07-28 23:49:35 +0000116 ret = pthread_create(&tid[i], NULL, thread_specific_data,
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000117 (void *) testfiles[i]);
118 if (ret != 0) {
119 perror("pthread_create");
120 exit(1);
121 }
122 }
123 for (i = 0; i < num_threads; i++) {
124 ret = pthread_join(tid[i], &results[i]);
125 if (ret != 0) {
126 perror("pthread_join");
127 exit(1);
128 }
129 }
130
131 xmlCatalogCleanup();
132 for (i = 0; i < num_threads; i++)
133 if (results[i] != (void *) Okay)
134 printf("Thread %d handling %s failed\n", i, testfiles[i]);
135 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000136 xmlCleanupParser();
137 xmlMemoryDump();
138 return (0);
139}
Daniel Veillard82cb3192003-10-29 13:39:15 +0000140#elif defined HAVE_BEOS_THREADS
141int
142main(void)
143{
144 unsigned int i, repeat;
145 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
146 void *results[MAX_ARGC];
147 status_t ret;
148
149 xmlInitParser();
150 printf("Parser initialized\n");
151 for (repeat = 0;repeat < 500;repeat++) {
152 printf("repeat: %d\n",repeat);
153 xmlLoadCatalog(catalog);
154 printf("loaded catalog: %s\n", catalog);
155 for (i = 0; i < num_threads; i++) {
156 results[i] = NULL;
157 tid[i] = (thread_id) -1;
158 }
159 printf("cleaned threads\n");
160 for (i = 0; i < num_threads; i++) {
161 tid[i] = spawn_thread(thread_specific_data, "xmlTestThread", B_NORMAL_PRIORITY, (void *) testfiles[i]);
162 if (tid[i] < B_OK) {
163 perror("beos_thread_create");
164 exit(1);
165 }
166 printf("beos_thread_create %d -> %d\n", i, tid[i]);
167 }
168 for (i = 0; i < num_threads; i++) {
169 ret = wait_for_thread(tid[i], &results[i]);
170 printf("beos_thread_wait %d -> %d\n", i, ret);
171 if (ret != B_OK) {
172 perror("beos_thread_wait");
173 exit(1);
174 }
175 }
176
177 xmlCatalogCleanup();
178 ret = B_OK;
179 for (i = 0; i < num_threads; i++)
180 if (results[i] != (void *) Okay) {
181 printf("Thread %d handling %s failed\n", i, testfiles[i]);
182 ret = B_ERROR;
183 }
184 }
185 xmlCleanupParser();
186 xmlMemoryDump();
187
188 if (ret == B_OK)
189 printf("testThread : BeOS : SUCCESS!\n");
190 else
191 printf("testThread : BeOS : FAILED!\n");
192
193 return (0);
194}
195#endif /* pthreads or BeOS threads */
Daniel Veillardab7488e2001-10-17 11:30:37 +0000196
197#else /* !LIBXML_THREADS_ENABLED */
198int
Daniel Veillard118aed72002-09-24 14:13:13 +0000199main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000200{
Daniel Veillarda4617b82001-11-04 20:19:12 +0000201 fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
Daniel Veillardab7488e2001-10-17 11:30:37 +0000202 return (0);
203}
204#endif