blob: 044c90e10ffd8a7fdf7dc7549010cae3026cf43a [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 Veillardab7488e2001-10-17 11:30:37 +00003#include <features.h>
4#include <libxml/xmlversion.h>
5
6#ifdef LIBXML_THREAD_ENABLED
7#include <libxml/globals.h>
8#include <libxml/threads.h>
9#include <libxml/parser.h>
10#include <libxml/catalog.h>
11#include <pthread.h>
12#include <string.h>
13#include <unistd.h>
14#include <assert.h>
15
16#define MAX_ARGC 20
17static pthread_t tid[MAX_ARGC];
18
19static const char *catalog = "test/threads/complex.xml";
20static const char *testfiles[] = {
21 "test/threads/abc.xml",
22 "test/threads/acb.xml",
23 "test/threads/bac.xml",
24 "test/threads/bca.xml",
25 "test/threads/cab.xml",
26 "test/threads/cba.xml",
27 "test/threads/invalid.xml",
28};
29
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000030const char *Okay = "OK";
31const char *Failed = "Failed";
32
33#ifndef xmlDoValidityCheckingDefaultValue
34#error xmlDoValidityCheckingDefaultValue is not a macro
35#endif
36#ifndef xmlGenericErrorContext
37#error xmlGenericErrorContext is not a macro
38#endif
39
Daniel Veillardab7488e2001-10-17 11:30:37 +000040static void *
41thread_specific_data(void *private_data)
42{
43 xmlDocPtr myDoc;
44 const char *filename = (const char *) private_data;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000045 int okay = 1;
Daniel Veillardab7488e2001-10-17 11:30:37 +000046
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000047 if (!strcmp(filename, "test/threads/invalid.xml")) {
Daniel Veillardab7488e2001-10-17 11:30:37 +000048 xmlDoValidityCheckingDefaultValue = 0;
49 xmlGenericErrorContext = stdout;
50 } else {
51 xmlDoValidityCheckingDefaultValue = 1;
52 xmlGenericErrorContext = stderr;
53 }
54 myDoc = xmlParseFile(filename);
55 if (myDoc) {
56 xmlFreeDoc(myDoc);
Daniel Veillardab7488e2001-10-17 11:30:37 +000057 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000058 printf("parse failed\n");
59 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000060 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000061 if (!strcmp(filename, "test/threads/invalid.xml")) {
62 if (xmlDoValidityCheckingDefaultValue != 0) {
63 printf("ValidityCheckingDefaultValue override failed\n");
64 okay = 0;
65 }
66 if (xmlGenericErrorContext != stdout) {
67 printf("xmlGenericErrorContext override failed\n");
68 okay = 0;
69 }
70 } else {
71 if (xmlDoValidityCheckingDefaultValue != 1) {
72 printf("ValidityCheckingDefaultValue override failed\n");
73 okay = 0;
74 }
75 if (xmlGenericErrorContext != stderr) {
76 printf("xmlGenericErrorContext override failed\n");
77 okay = 0;
78 }
79 }
80 if (okay == 0)
81 return((void *) Failed);
82 return ((void *) Okay);
Daniel Veillardab7488e2001-10-17 11:30:37 +000083}
84
85int
86main()
87{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000088 unsigned int i, repeat;
Daniel Veillardab7488e2001-10-17 11:30:37 +000089 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000090 void *results[MAX_ARGC];
91 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +000092
93 xmlInitParser();
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000094 for (repeat = 0;repeat < 10000;repeat++) {
95 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +000096
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000097 for (i = 0; i < num_threads; i++) {
98 results[i] = NULL;
99 tid[i] = -1;
100 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000101
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000102 for (i = 0; i < num_threads; i++) {
103 ret = pthread_create(&tid[i], 0, thread_specific_data,
104 (void *) testfiles[i]);
105 if (ret != 0) {
106 perror("pthread_create");
107 exit(1);
108 }
109 }
110 for (i = 0; i < num_threads; i++) {
111 ret = pthread_join(tid[i], &results[i]);
112 if (ret != 0) {
113 perror("pthread_join");
114 exit(1);
115 }
116 }
117
118 xmlCatalogCleanup();
119 for (i = 0; i < num_threads; i++)
120 if (results[i] != (void *) Okay)
121 printf("Thread %d handling %s failed\n", i, testfiles[i]);
122 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000123 xmlCleanupParser();
124 xmlMemoryDump();
125 return (0);
126}
127
128#else /* !LIBXML_THREADS_ENABLED */
129int
130main()
131{
132 fprintf(stderr, "libxml was not compiled with thread support\n");
133 return (0);
134}
135#endif