testThreads.c 3.14 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 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include <libxml/globals.h>
#include <libxml/threads.h>
#include <libxml/parser.h>
#include <libxml/catalog.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#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",
};

29 30 31 32 33 34 35 36 37 38
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

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

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

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

    xmlInitParser();
93
    for (repeat = 0;repeat < 500;repeat++) {
94
	xmlLoadCatalog(catalog);
95

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

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	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]);
    }
122 123 124 125 126 127 128
    xmlCleanupParser();
    xmlMemoryDump();
    return (0);
}

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