dsrc isn't necessary for this repo
[client-tools.git] / src / external / 3rd / library / libxml / testThreads.c
blob1479d8d30b46f4fa555d3e317b5e0c75042761b7
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include "libxml.h"
5 #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_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 #if !defined(_MSC_VER)
13 #include <unistd.h>
14 #endif
15 #include <assert.h>
17 #define MAX_ARGC 20
18 static pthread_t tid[MAX_ARGC];
20 static const char *catalog = "test/threads/complex.xml";
21 static 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",
31 const char *Okay = "OK";
32 const char *Failed = "Failed";
34 #ifndef xmlDoValidityCheckingDefaultValue
35 #error xmlDoValidityCheckingDefaultValue is not a macro
36 #endif
37 #ifndef xmlGenericErrorContext
38 #error xmlGenericErrorContext is not a macro
39 #endif
41 static void *
42 thread_specific_data(void *private_data)
44 xmlDocPtr myDoc;
45 const char *filename = (const char *) private_data;
46 int okay = 1;
48 if (!strcmp(filename, "test/threads/invalid.xml")) {
49 xmlDoValidityCheckingDefaultValue = 0;
50 xmlGenericErrorContext = stdout;
51 } else {
52 xmlDoValidityCheckingDefaultValue = 1;
53 xmlGenericErrorContext = stderr;
55 myDoc = xmlParseFile(filename);
56 if (myDoc) {
57 xmlFreeDoc(myDoc);
58 } else {
59 printf("parse failed\n");
60 okay = 0;
62 if (!strcmp(filename, "test/threads/invalid.xml")) {
63 if (xmlDoValidityCheckingDefaultValue != 0) {
64 printf("ValidityCheckingDefaultValue override failed\n");
65 okay = 0;
67 if (xmlGenericErrorContext != stdout) {
68 printf("xmlGenericErrorContext override failed\n");
69 okay = 0;
71 } else {
72 if (xmlDoValidityCheckingDefaultValue != 1) {
73 printf("ValidityCheckingDefaultValue override failed\n");
74 okay = 0;
76 if (xmlGenericErrorContext != stderr) {
77 printf("xmlGenericErrorContext override failed\n");
78 okay = 0;
81 if (okay == 0)
82 return((void *) Failed);
83 return ((void *) Okay);
86 int
87 main()
89 unsigned int i, repeat;
90 unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
91 void *results[MAX_ARGC];
92 int ret;
94 xmlInitParser();
95 for (repeat = 0;repeat < 500;repeat++) {
96 xmlLoadCatalog(catalog);
98 for (i = 0; i < num_threads; i++) {
99 results[i] = NULL;
100 tid[i] = (pthread_t) -1;
103 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);
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);
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]);
124 xmlCleanupParser();
125 xmlMemoryDump();
126 return (0);
129 #else /* !LIBXML_THREADS_ENABLED */
131 main(void)
133 fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
134 return (0);
136 #endif