testThreadsWin32.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "libxml.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #if defined(LIBXML_THREAD_ENABLED) && defined(LIBXML_CATALOG_ENABLED)
  5. #include <libxml/globals.h>
  6. #include <libxml/threads.h>
  7. #include <libxml/parser.h>
  8. #include <libxml/catalog.h>
  9. #include <windows.h>
  10. #include <string.h>
  11. #include <assert.h>
  12. #define MAX_ARGC 20
  13. #define TEST_REPEAT_COUNT 500
  14. static HANDLE tid[MAX_ARGC];
  15. static const char *catalog = "test/threads/complex.xml";
  16. static char *testfiles[] = {
  17. "test/threads/abc.xml",
  18. "test/threads/acb.xml",
  19. "test/threads/bac.xml",
  20. "test/threads/bca.xml",
  21. "test/threads/cab.xml",
  22. "test/threads/cba.xml",
  23. "test/threads/invalid.xml",
  24. };
  25. const char *Okay = "OK";
  26. const char *Failed = "Failed";
  27. #ifndef xmlDoValidityCheckingDefaultValue
  28. #error xmlDoValidityCheckingDefaultValue is not a macro
  29. #endif
  30. #ifndef xmlGenericErrorContext
  31. #error xmlGenericErrorContext is not a macro
  32. #endif
  33. static DWORD WINAPI
  34. thread_specific_data(void *private_data)
  35. {
  36. xmlDocPtr myDoc;
  37. const char *filename = (const char *) private_data;
  38. int okay = 1;
  39. if (!strcmp(filename, "test/threads/invalid.xml")) {
  40. xmlDoValidityCheckingDefaultValue = 0;
  41. xmlGenericErrorContext = stdout;
  42. } else {
  43. xmlDoValidityCheckingDefaultValue = 1;
  44. xmlGenericErrorContext = stderr;
  45. }
  46. myDoc = xmlParseFile(filename);
  47. if (myDoc) {
  48. xmlFreeDoc(myDoc);
  49. } else {
  50. printf("parse failed\n");
  51. okay = 0;
  52. }
  53. if (!strcmp(filename, "test/threads/invalid.xml")) {
  54. if (xmlDoValidityCheckingDefaultValue != 0) {
  55. printf("ValidityCheckingDefaultValue override failed\n");
  56. okay = 0;
  57. }
  58. if (xmlGenericErrorContext != stdout) {
  59. printf("xmlGenericErrorContext override failed\n");
  60. okay = 0;
  61. }
  62. } else {
  63. if (xmlDoValidityCheckingDefaultValue != 1) {
  64. printf("ValidityCheckingDefaultValue override failed\n");
  65. okay = 0;
  66. }
  67. if (xmlGenericErrorContext != stderr) {
  68. printf("xmlGenericErrorContext override failed\n");
  69. okay = 0;
  70. }
  71. }
  72. if (okay == 0)
  73. return ((DWORD) Failed);
  74. return ((DWORD) Okay);
  75. }
  76. int
  77. main()
  78. {
  79. unsigned int i, repeat;
  80. unsigned int num_threads = sizeof(testfiles) / sizeof(testfiles[0]);
  81. DWORD results[MAX_ARGC];
  82. BOOL ret;
  83. xmlInitParser();
  84. for (repeat = 0;repeat < TEST_REPEAT_COUNT;repeat++)
  85. {
  86. xmlLoadCatalog(catalog);
  87. for (i = 0; i < num_threads; i++)
  88. {
  89. results[i] = 0;
  90. tid[i] = (HANDLE) -1;
  91. }
  92. for (i = 0; i < num_threads; i++)
  93. {
  94. DWORD useless;
  95. tid[i] = CreateThread(NULL, 0,
  96. thread_specific_data, testfiles[i], 0, &useless);
  97. if (tid[i] == NULL)
  98. {
  99. perror("CreateThread");
  100. exit(1);
  101. }
  102. }
  103. if (WaitForMultipleObjects (num_threads, tid, TRUE, INFINITE) == WAIT_FAILED)
  104. perror ("WaitForMultipleObjects failed");
  105. for (i = 0; i < num_threads; i++)
  106. {
  107. ret = GetExitCodeThread (tid[i], &results[i]);
  108. if (ret == 0)
  109. {
  110. perror("GetExitCodeThread");
  111. exit(1);
  112. }
  113. CloseHandle (tid[i]);
  114. }
  115. xmlCatalogCleanup();
  116. for (i = 0; i < num_threads; i++) {
  117. if (results[i] != (DWORD) Okay)
  118. printf("Thread %d handling %s failed\n", i, testfiles[i]);
  119. }
  120. }
  121. xmlCleanupParser();
  122. xmlMemoryDump();
  123. return (0);
  124. }
  125. #else /* !LIBXML_THREADS_ENABLED */
  126. int
  127. main()
  128. {
  129. fprintf(stderr, "libxml was not compiled with thread or catalog support\n");
  130. return (0);
  131. }
  132. #endif