blob: 8424d31d46e725ba52cb5f15f559da1dafa8e2ee [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 <libxml/xmlversion.h>
4
5#ifdef LIBXML_THREAD_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#include <unistd.h>
13#include <assert.h>
14
15#define MAX_ARGC 20
16static pthread_t tid[MAX_ARGC];
17
18static const char *catalog = "test/threads/complex.xml";
19static const char *testfiles[] = {
20 "test/threads/abc.xml",
21 "test/threads/acb.xml",
22 "test/threads/bac.xml",
23 "test/threads/bca.xml",
24 "test/threads/cab.xml",
25 "test/threads/cba.xml",
26 "test/threads/invalid.xml",
27};
28
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000029const char *Okay = "OK";
30const char *Failed = "Failed";
31
32#ifndef xmlDoValidityCheckingDefaultValue
33#error xmlDoValidityCheckingDefaultValue is not a macro
34#endif
35#ifndef xmlGenericErrorContext
36#error xmlGenericErrorContext is not a macro
37#endif
38
Daniel Veillardab7488e2001-10-17 11:30:37 +000039static void *
40thread_specific_data(void *private_data)
41{
42 xmlDocPtr myDoc;
43 const char *filename = (const char *) private_data;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000044 int okay = 1;
Daniel Veillardab7488e2001-10-17 11:30:37 +000045
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000046 if (!strcmp(filename, "test/threads/invalid.xml")) {
Daniel Veillardab7488e2001-10-17 11:30:37 +000047 xmlDoValidityCheckingDefaultValue = 0;
48 xmlGenericErrorContext = stdout;
49 } else {
50 xmlDoValidityCheckingDefaultValue = 1;
51 xmlGenericErrorContext = stderr;
52 }
53 myDoc = xmlParseFile(filename);
54 if (myDoc) {
55 xmlFreeDoc(myDoc);
Daniel Veillardab7488e2001-10-17 11:30:37 +000056 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000057 printf("parse failed\n");
58 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000059 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000060 if (!strcmp(filename, "test/threads/invalid.xml")) {
61 if (xmlDoValidityCheckingDefaultValue != 0) {
62 printf("ValidityCheckingDefaultValue override failed\n");
63 okay = 0;
64 }
65 if (xmlGenericErrorContext != stdout) {
66 printf("xmlGenericErrorContext override failed\n");
67 okay = 0;
68 }
69 } else {
70 if (xmlDoValidityCheckingDefaultValue != 1) {
71 printf("ValidityCheckingDefaultValue override failed\n");
72 okay = 0;
73 }
74 if (xmlGenericErrorContext != stderr) {
75 printf("xmlGenericErrorContext override failed\n");
76 okay = 0;
77 }
78 }
79 if (okay == 0)
80 return((void *) Failed);
81 return ((void *) Okay);
Daniel Veillardab7488e2001-10-17 11:30:37 +000082}
83
84int
85main()
86{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000087 unsigned int i, repeat;
Daniel Veillardab7488e2001-10-17 11:30:37 +000088 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000089 void *results[MAX_ARGC];
90 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +000091
92 xmlInitParser();
Daniel Veillard89cad532001-10-22 09:46:13 +000093 for (repeat = 0;repeat < 500;repeat++) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000094 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +000095
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000096 for (i = 0; i < num_threads; i++) {
97 results[i] = NULL;
98 tid[i] = -1;
99 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000100
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000101 for (i = 0; i < num_threads; i++) {
102 ret = pthread_create(&tid[i], 0, thread_specific_data,
103 (void *) testfiles[i]);
104 if (ret != 0) {
105 perror("pthread_create");
106 exit(1);
107 }
108 }
109 for (i = 0; i < num_threads; i++) {
110 ret = pthread_join(tid[i], &results[i]);
111 if (ret != 0) {
112 perror("pthread_join");
113 exit(1);
114 }
115 }
116
117 xmlCatalogCleanup();
118 for (i = 0; i < num_threads; i++)
119 if (results[i] != (void *) Okay)
120 printf("Thread %d handling %s failed\n", i, testfiles[i]);
121 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000122 xmlCleanupParser();
123 xmlMemoryDump();
124 return (0);
125}
126
127#else /* !LIBXML_THREADS_ENABLED */
128int
129main()
130{
131 fprintf(stderr, "libxml was not compiled with thread support\n");
132 return (0);
133}
134#endif