Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
Daniel Veillard | 2a90682 | 2001-12-06 14:34:08 +0000 | [diff] [blame] | 3 | #include "libxml.h" |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 4 | |
Daniel Veillard | a4617b8 | 2001-11-04 20:19:12 +0000 | [diff] [blame] | 5 | #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED) |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 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 |
| 16 | static pthread_t tid[MAX_ARGC]; |
| 17 | |
| 18 | static const char *catalog = "test/threads/complex.xml"; |
| 19 | static 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 Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 29 | const char *Okay = "OK"; |
| 30 | const 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 Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 39 | static void * |
| 40 | thread_specific_data(void *private_data) |
| 41 | { |
| 42 | xmlDocPtr myDoc; |
| 43 | const char *filename = (const char *) private_data; |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 44 | int okay = 1; |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 45 | |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 46 | if (!strcmp(filename, "test/threads/invalid.xml")) { |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 47 | 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 Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 56 | } else { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 57 | printf("parse failed\n"); |
| 58 | okay = 0; |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 59 | } |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 60 | 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 Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | int |
| 85 | main() |
| 86 | { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 87 | unsigned int i, repeat; |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 88 | unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]); |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 89 | void *results[MAX_ARGC]; |
| 90 | int ret; |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 91 | |
| 92 | xmlInitParser(); |
Daniel Veillard | 89cad53 | 2001-10-22 09:46:13 +0000 | [diff] [blame] | 93 | for (repeat = 0;repeat < 500;repeat++) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 94 | xmlLoadCatalog(catalog); |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 95 | |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 96 | for (i = 0; i < num_threads; i++) { |
| 97 | results[i] = NULL; |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 98 | tid[i] = (pthread_t) -1; |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 99 | } |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 100 | |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 101 | 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 Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 122 | xmlCleanupParser(); |
| 123 | xmlMemoryDump(); |
| 124 | return (0); |
| 125 | } |
| 126 | |
| 127 | #else /* !LIBXML_THREADS_ENABLED */ |
| 128 | int |
| 129 | main() |
| 130 | { |
Daniel Veillard | a4617b8 | 2001-11-04 20:19:12 +0000 | [diff] [blame] | 131 | fprintf(stderr, "libxml was not compiled with thread or catalog support\n"); |
Daniel Veillard | ab7488e | 2001-10-17 11:30:37 +0000 | [diff] [blame] | 132 | return (0); |
| 133 | } |
| 134 | #endif |