testThreads.c 3.17 KB
Newer Older
1
#include <stdlib.h>
2
#include <stdio.h>
3
#include "libxml.h"
4

5
#if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
6 7 8 9 10 11
#include <libxml/globals.h>
#include <libxml/threads.h>
#include <libxml/parser.h>
#include <libxml/catalog.h>
#include <pthread.h>
#include <string.h>
12
#if !defined(_MSC_VER)
13
#include <unistd.h>
14
#endif
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <assert.h>

#define	MAX_ARGC	20
static pthread_t tid[MAX_ARGC];

static const char *catalog = "test/threads/complex.xml";
static const char *testfiles[] = {
    "test/threads/abc.xml",
    "test/threads/acb.xml",
    "test/threads/bac.xml",
    "test/threads/bca.xml",
    "test/threads/cab.xml",
    "test/threads/cba.xml",
    "test/threads/invalid.xml",
};

31 32 33 34 35 36 37 38 39 40
const char *Okay = "OK";
const char *Failed = "Failed";

#ifndef xmlDoValidityCheckingDefaultValue
#error xmlDoValidityCheckingDefaultValue is not a macro
#endif
#ifndef xmlGenericErrorContext
#error xmlGenericErrorContext is not a macro
#endif

41 42 43 44 45
static void *
thread_specific_data(void *private_data)
{
    xmlDocPtr myDoc;
    const char *filename = (const char *) private_data;
46
    int okay = 1;
47

48
    if (!strcmp(filename, "test/threads/invalid.xml")) {
49 50 51 52 53 54 55 56 57
        xmlDoValidityCheckingDefaultValue = 0;
        xmlGenericErrorContext = stdout;
    } else {
        xmlDoValidityCheckingDefaultValue = 1;
        xmlGenericErrorContext = stderr;
    }
    myDoc = xmlParseFile(filename);
    if (myDoc) {
        xmlFreeDoc(myDoc);
58
    } else {
59
        printf("parse failed\n");
60 61 62 63
	okay = 0;
    }
    if (!strcmp(filename, "test/threads/invalid.xml")) {
        if (xmlDoValidityCheckingDefaultValue != 0) {
64
	    printf("ValidityCheckingDefaultValue override failed\n");
65 66 67 68 69 70
	    okay = 0;
	}
        if (xmlGenericErrorContext != stdout) {
	    printf("xmlGenericErrorContext override failed\n");
	    okay = 0;
	}
71
    } else {
72
        if (xmlDoValidityCheckingDefaultValue != 1) {
73
	    printf("ValidityCheckingDefaultValue override failed\n");
74 75 76 77 78 79
	    okay = 0;
	}
        if (xmlGenericErrorContext != stderr) {
	    printf("xmlGenericErrorContext override failed\n");
	    okay = 0;
	}
80
    }
81 82 83
    if (okay == 0)
	return((void *) Failed);
    return ((void *) Okay);
84 85 86 87 88
}

int
main()
{
89
    unsigned int i, repeat;
90
    unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
91 92
    void *results[MAX_ARGC];
    int ret;
93 94

    xmlInitParser();
95
    for (repeat = 0;repeat < 500;repeat++) {
96
	xmlLoadCatalog(catalog);
97

98 99
	for (i = 0; i < num_threads; i++) {
	    results[i] = NULL;
100
	    tid[i] = (pthread_t) -1;
101
	}
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	for (i = 0; i < num_threads; i++) {
	    ret = pthread_create(&tid[i], 0, thread_specific_data,
				 (void *) testfiles[i]);
	    if (ret != 0) {
		perror("pthread_create");
		exit(1);
	    }
	}
	for (i = 0; i < num_threads; i++) {
	    ret = pthread_join(tid[i], &results[i]);
	    if (ret != 0) {
		perror("pthread_join");
		exit(1);
	    }
	}

	xmlCatalogCleanup();
	for (i = 0; i < num_threads; i++)
	    if (results[i] != (void *) Okay)
		printf("Thread %d handling %s failed\n", i, testfiles[i]);
    }
124 125 126 127 128 129 130
    xmlCleanupParser();
    xmlMemoryDump();
    return (0);
}

#else /* !LIBXML_THREADS_ENABLED */
int
131
main(void)
132
{
133
    fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
134 135 136
    return (0);
}
#endif