blob: 1479d8d30b46f4fa555d3e317b5e0c75042761b7 [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>
10#include <pthread.h>
11#include <string.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000012#if !defined(_MSC_VER)
Daniel Veillardab7488e2001-10-17 11:30:37 +000013#include <unistd.h>
Igor Zlatkovic082ff502002-10-31 15:59:09 +000014#endif
Daniel Veillardab7488e2001-10-17 11:30:37 +000015#include <assert.h>
16
17#define MAX_ARGC 20
18static pthread_t tid[MAX_ARGC];
19
20static const char *catalog = "test/threads/complex.xml";
21static const char *testfiles[] = {
22 "test/threads/abc.xml",
23 "test/threads/acb.xml",
24 "test/threads/bac.xml",
25 "test/threads/bca.xml",
26 "test/threads/cab.xml",
27 "test/threads/cba.xml",
28 "test/threads/invalid.xml",
29};
30
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000031const char *Okay = "OK";
32const char *Failed = "Failed";
33
34#ifndef xmlDoValidityCheckingDefaultValue
35#error xmlDoValidityCheckingDefaultValue is not a macro
36#endif
37#ifndef xmlGenericErrorContext
38#error xmlGenericErrorContext is not a macro
39#endif
40
Daniel Veillardab7488e2001-10-17 11:30:37 +000041static void *
42thread_specific_data(void *private_data)
43{
44 xmlDocPtr myDoc;
45 const char *filename = (const char *) private_data;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000046 int okay = 1;
Daniel Veillardab7488e2001-10-17 11:30:37 +000047
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000048 if (!strcmp(filename, "test/threads/invalid.xml")) {
Daniel Veillardab7488e2001-10-17 11:30:37 +000049 xmlDoValidityCheckingDefaultValue = 0;
50 xmlGenericErrorContext = stdout;
51 } else {
52 xmlDoValidityCheckingDefaultValue = 1;
53 xmlGenericErrorContext = stderr;
54 }
55 myDoc = xmlParseFile(filename);
56 if (myDoc) {
57 xmlFreeDoc(myDoc);
Daniel Veillardab7488e2001-10-17 11:30:37 +000058 } else {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000059 printf("parse failed\n");
60 okay = 0;
Daniel Veillardab7488e2001-10-17 11:30:37 +000061 }
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000062 if (!strcmp(filename, "test/threads/invalid.xml")) {
63 if (xmlDoValidityCheckingDefaultValue != 0) {
64 printf("ValidityCheckingDefaultValue override failed\n");
65 okay = 0;
66 }
67 if (xmlGenericErrorContext != stdout) {
68 printf("xmlGenericErrorContext override failed\n");
69 okay = 0;
70 }
71 } else {
72 if (xmlDoValidityCheckingDefaultValue != 1) {
73 printf("ValidityCheckingDefaultValue override failed\n");
74 okay = 0;
75 }
76 if (xmlGenericErrorContext != stderr) {
77 printf("xmlGenericErrorContext override failed\n");
78 okay = 0;
79 }
80 }
81 if (okay == 0)
82 return((void *) Failed);
83 return ((void *) Okay);
Daniel Veillardab7488e2001-10-17 11:30:37 +000084}
85
86int
87main()
88{
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000089 unsigned int i, repeat;
Daniel Veillardab7488e2001-10-17 11:30:37 +000090 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000091 void *results[MAX_ARGC];
92 int ret;
Daniel Veillardab7488e2001-10-17 11:30:37 +000093
94 xmlInitParser();
Daniel Veillard89cad532001-10-22 09:46:13 +000095 for (repeat = 0;repeat < 500;repeat++) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000096 xmlLoadCatalog(catalog);
Daniel Veillardab7488e2001-10-17 11:30:37 +000097
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000098 for (i = 0; i < num_threads; i++) {
99 results[i] = NULL;
Daniel Veillardd3b08822001-12-05 12:03:33 +0000100 tid[i] = (pthread_t) -1;
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000101 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000102
Daniel Veillard3c01b1d2001-10-17 15:58:35 +0000103 for (i = 0; i < num_threads; i++) {
104 ret = pthread_create(&tid[i], 0, thread_specific_data,
105 (void *) testfiles[i]);
106 if (ret != 0) {
107 perror("pthread_create");
108 exit(1);
109 }
110 }
111 for (i = 0; i < num_threads; i++) {
112 ret = pthread_join(tid[i], &results[i]);
113 if (ret != 0) {
114 perror("pthread_join");
115 exit(1);
116 }
117 }
118
119 xmlCatalogCleanup();
120 for (i = 0; i < num_threads; i++)
121 if (results[i] != (void *) Okay)
122 printf("Thread %d handling %s failed\n", i, testfiles[i]);
123 }
Daniel Veillardab7488e2001-10-17 11:30:37 +0000124 xmlCleanupParser();
125 xmlMemoryDump();
126 return (0);
127}
128
129#else /* !LIBXML_THREADS_ENABLED */
130int
Daniel Veillard118aed72002-09-24 14:13:13 +0000131main(void)
Daniel Veillardab7488e2001-10-17 11:30:37 +0000132{
Daniel Veillarda4617b82001-11-04 20:19:12 +0000133 fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
Daniel Veillardab7488e2001-10-17 11:30:37 +0000134 return (0);
135}
136#endif