Blame testThreads.c

Packit 423ecb
#include "libxml.h"
Packit 423ecb
Packit 423ecb
#include <stdlib.h>
Packit 423ecb
#include <stdio.h>
Packit 423ecb
Packit 423ecb
#if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
Packit 423ecb
#include <libxml/globals.h>
Packit 423ecb
#include <libxml/threads.h>
Packit 423ecb
#include <libxml/parser.h>
Packit 423ecb
#include <libxml/catalog.h>
Packit 423ecb
#ifdef HAVE_PTHREAD_H
Packit 423ecb
#include <pthread.h>
Packit 423ecb
#elif defined HAVE_BEOS_THREADS
Packit 423ecb
#include <OS.h>
Packit 423ecb
#endif
Packit 423ecb
#include <string.h>
Packit 423ecb
#if !defined(_MSC_VER)
Packit 423ecb
#include <unistd.h>
Packit 423ecb
#endif
Packit 423ecb
#include <assert.h>
Packit 423ecb
Packit 423ecb
#define	MAX_ARGC	20
Packit 423ecb
#ifdef HAVE_PTHREAD_H
Packit 423ecb
static pthread_t tid[MAX_ARGC];
Packit 423ecb
#elif defined HAVE_BEOS_THREADS
Packit 423ecb
static thread_id tid[MAX_ARGC];
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
static const char *catalog = "test/threads/complex.xml";
Packit 423ecb
static const char *testfiles[] = {
Packit 423ecb
    "test/threads/abc.xml",
Packit 423ecb
    "test/threads/acb.xml",
Packit 423ecb
    "test/threads/bac.xml",
Packit 423ecb
    "test/threads/bca.xml",
Packit 423ecb
    "test/threads/cab.xml",
Packit 423ecb
    "test/threads/cba.xml",
Packit 423ecb
    "test/threads/invalid.xml",
Packit 423ecb
};
Packit 423ecb
Packit 423ecb
static const char *Okay = "OK";
Packit 423ecb
static const char *Failed = "Failed";
Packit 423ecb
Packit 423ecb
#ifndef xmlDoValidityCheckingDefaultValue
Packit 423ecb
#error xmlDoValidityCheckingDefaultValue is not a macro
Packit 423ecb
#endif
Packit 423ecb
#ifndef xmlGenericErrorContext
Packit 423ecb
#error xmlGenericErrorContext is not a macro
Packit 423ecb
#endif
Packit 423ecb
Packit 423ecb
static void *
Packit 423ecb
thread_specific_data(void *private_data)
Packit 423ecb
{
Packit 423ecb
    xmlDocPtr myDoc;
Packit 423ecb
    const char *filename = (const char *) private_data;
Packit 423ecb
    int okay = 1;
Packit 423ecb
Packit 423ecb
    if (!strcmp(filename, "test/threads/invalid.xml")) {
Packit 423ecb
        xmlDoValidityCheckingDefaultValue = 0;
Packit 423ecb
        xmlGenericErrorContext = stdout;
Packit 423ecb
    } else {
Packit 423ecb
        xmlDoValidityCheckingDefaultValue = 1;
Packit 423ecb
        xmlGenericErrorContext = stderr;
Packit 423ecb
    }
Packit 423ecb
#ifdef LIBXML_SAX1_ENABLED
Packit 423ecb
    myDoc = xmlParseFile(filename);
Packit 423ecb
#else
Packit 423ecb
    myDoc = xmlReadFile(filename, NULL, XML_WITH_CATALOG);
Packit 423ecb
#endif
Packit 423ecb
    if (myDoc) {
Packit 423ecb
        xmlFreeDoc(myDoc);
Packit 423ecb
    } else {
Packit 423ecb
        printf("parse failed\n");
Packit 423ecb
	okay = 0;
Packit 423ecb
    }
Packit 423ecb
    if (!strcmp(filename, "test/threads/invalid.xml")) {
Packit 423ecb
        if (xmlDoValidityCheckingDefaultValue != 0) {
Packit 423ecb
	    printf("ValidityCheckingDefaultValue override failed\n");
Packit 423ecb
	    okay = 0;
Packit 423ecb
	}
Packit 423ecb
        if (xmlGenericErrorContext != stdout) {
Packit 423ecb
	    printf("xmlGenericErrorContext override failed\n");
Packit 423ecb
	    okay = 0;
Packit 423ecb
	}
Packit 423ecb
    } else {
Packit 423ecb
        if (xmlDoValidityCheckingDefaultValue != 1) {
Packit 423ecb
	    printf("ValidityCheckingDefaultValue override failed\n");
Packit 423ecb
	    okay = 0;
Packit 423ecb
	}
Packit 423ecb
        if (xmlGenericErrorContext != stderr) {
Packit 423ecb
	    printf("xmlGenericErrorContext override failed\n");
Packit 423ecb
	    okay = 0;
Packit 423ecb
	}
Packit 423ecb
    }
Packit 423ecb
    if (okay == 0)
Packit 423ecb
	return((void *) Failed);
Packit 423ecb
    return ((void *) Okay);
Packit 423ecb
}
Packit 423ecb
Packit 423ecb
#ifdef HAVE_PTHREAD_H
Packit 423ecb
int
Packit 423ecb
main(void)
Packit 423ecb
{
Packit 423ecb
    unsigned int i, repeat;
Packit 423ecb
    unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Packit 423ecb
    void *results[MAX_ARGC];
Packit 423ecb
    int ret;
Packit 423ecb
Packit 423ecb
    xmlInitParser();
Packit 423ecb
    for (repeat = 0;repeat < 500;repeat++) {
Packit 423ecb
	xmlLoadCatalog(catalog);
Packit 423ecb
Packit 423ecb
        memset(results, 0, sizeof(*results)*num_threads);
Packit 423ecb
        memset(tid, 0xff, sizeof(*tid)*num_threads);
Packit 423ecb
Packit 423ecb
	for (i = 0; i < num_threads; i++) {
Packit 423ecb
	    ret = pthread_create(&tid[i], NULL, thread_specific_data,
Packit 423ecb
				 (void *) testfiles[i]);
Packit 423ecb
	    if (ret != 0) {
Packit 423ecb
		perror("pthread_create");
Packit 423ecb
		exit(1);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
	for (i = 0; i < num_threads; i++) {
Packit 423ecb
	    ret = pthread_join(tid[i], &results[i]);
Packit 423ecb
	    if (ret != 0) {
Packit 423ecb
		perror("pthread_join");
Packit 423ecb
		exit(1);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
	xmlCatalogCleanup();
Packit 423ecb
	for (i = 0; i < num_threads; i++)
Packit 423ecb
	    if (results[i] != (void *) Okay)
Packit 423ecb
		printf("Thread %d handling %s failed\n", i, testfiles[i]);
Packit 423ecb
    }
Packit 423ecb
    xmlCleanupParser();
Packit 423ecb
    xmlMemoryDump();
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
#elif defined HAVE_BEOS_THREADS
Packit 423ecb
int
Packit 423ecb
main(void)
Packit 423ecb
{
Packit 423ecb
    unsigned int i, repeat;
Packit 423ecb
    unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
Packit 423ecb
    void *results[MAX_ARGC];
Packit 423ecb
    status_t ret;
Packit 423ecb
Packit 423ecb
    xmlInitParser();
Packit 423ecb
    printf("Parser initialized\n");
Packit 423ecb
    for (repeat = 0;repeat < 500;repeat++) {
Packit 423ecb
    printf("repeat: %d\n",repeat);
Packit 423ecb
	xmlLoadCatalog(catalog);
Packit 423ecb
	printf("loaded catalog: %s\n", catalog);
Packit 423ecb
	for (i = 0; i < num_threads; i++) {
Packit 423ecb
	    results[i] = NULL;
Packit 423ecb
	    tid[i] = (thread_id) -1;
Packit 423ecb
	}
Packit 423ecb
	printf("cleaned threads\n");
Packit 423ecb
	for (i = 0; i < num_threads; i++) {
Packit 423ecb
		tid[i] = spawn_thread(thread_specific_data, "xmlTestThread", B_NORMAL_PRIORITY, (void *) testfiles[i]);
Packit 423ecb
		if (tid[i] < B_OK) {
Packit 423ecb
			perror("beos_thread_create");
Packit 423ecb
			exit(1);
Packit 423ecb
		}
Packit 423ecb
		printf("beos_thread_create %d -> %d\n", i, tid[i]);
Packit 423ecb
	}
Packit 423ecb
	for (i = 0; i < num_threads; i++) {
Packit 423ecb
	    ret = wait_for_thread(tid[i], &results[i]);
Packit 423ecb
	    printf("beos_thread_wait %d -> %d\n", i, ret);
Packit 423ecb
	    if (ret != B_OK) {
Packit 423ecb
			perror("beos_thread_wait");
Packit 423ecb
			exit(1);
Packit 423ecb
	    }
Packit 423ecb
	}
Packit 423ecb
Packit 423ecb
	xmlCatalogCleanup();
Packit 423ecb
	ret = B_OK;
Packit 423ecb
	for (i = 0; i < num_threads; i++)
Packit 423ecb
	    if (results[i] != (void *) Okay) {
Packit 423ecb
			printf("Thread %d handling %s failed\n", i, testfiles[i]);
Packit 423ecb
			ret = B_ERROR;
Packit 423ecb
		}
Packit 423ecb
    }
Packit 423ecb
    xmlCleanupParser();
Packit 423ecb
    xmlMemoryDump();
Packit 423ecb
Packit 423ecb
	if (ret == B_OK)
Packit 423ecb
		printf("testThread : BeOS : SUCCESS!\n");
Packit 423ecb
	else
Packit 423ecb
		printf("testThread : BeOS : FAILED!\n");
Packit 423ecb
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
#endif /* pthreads or BeOS threads */
Packit 423ecb
Packit 423ecb
#else /* !LIBXML_THREADS_ENABLED */
Packit 423ecb
int
Packit 423ecb
main(void)
Packit 423ecb
{
Packit 423ecb
    fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
Packit 423ecb
    return (0);
Packit 423ecb
}
Packit 423ecb
#endif