~noskcaj/ubuntu/trusty/libextractor/merge

« back to all changes in this revision

Viewing changes to src/test/mt_extracttest1.c

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-11-08 02:05:01 UTC
  • mfrom: (1.10.2 upstream) (5.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20081108020501-ky32z9x7hk26po32
Tags: 0.5.20c-1ubuntu1
* Merge from Debian unstable, remaining Ubuntu changes:
  - (Build-)depend on libltdl7-dev (Ubuntu-specific change).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file test/mt_extracttest1.c
 
3
 * @brief test keyword extraction from multiple threads simultaneously
 
4
 * @author Heikki Lindholm
 
5
 */
 
6
#include "platform.h"
 
7
#include "extractor.h"
 
8
#include <pthread.h>
 
9
 
 
10
struct TaskData
 
11
{
 
12
  int id;
 
13
  const char *filename;
 
14
};
 
15
 
 
16
static volatile int done = 0;
 
17
static volatile int failed = 0;
 
18
 
 
19
pthread_mutex_t reference_lock = PTHREAD_MUTEX_INITIALIZER;
 
20
static EXTRACTOR_KeywordList *reference_list;
 
21
 
 
22
static int
 
23
compare_keywords_to_ref (EXTRACTOR_KeywordList * list)
 
24
{
 
25
  EXTRACTOR_KeywordList *ptr1, *ptr2;
 
26
  unsigned int cnt;
 
27
  int *match;
 
28
  int i;
 
29
 
 
30
  cnt = EXTRACTOR_countKeywords (list);
 
31
 
 
32
  pthread_mutex_lock (&reference_lock);
 
33
 
 
34
  if (cnt != EXTRACTOR_countKeywords (reference_list))
 
35
    {
 
36
      pthread_mutex_unlock (&reference_lock);
 
37
      return -1;
 
38
    }
 
39
 
 
40
  match = alloca (cnt * sizeof (int));
 
41
  memset (match, 0x00, cnt * sizeof (int));
 
42
  ptr1 = list;
 
43
  while (ptr1 != NULL)
 
44
    {
 
45
      int found;
 
46
      found = 0;
 
47
      ptr2 = reference_list;
 
48
      i = 0;
 
49
      while (ptr2 != NULL)
 
50
        {
 
51
          if (ptr2->keywordType == ptr1->keywordType &&
 
52
              strcmp (ptr2->keyword, ptr1->keyword) == 0 && match[i] == 0)
 
53
            {
 
54
              found = 1;
 
55
              match[i] = 1;
 
56
              break;
 
57
            }
 
58
          i++;
 
59
          ptr2 = ptr2->next;
 
60
        }
 
61
      if (found == 0)
 
62
        break;
 
63
      ptr1 = ptr1->next;
 
64
    }
 
65
 
 
66
  pthread_mutex_unlock (&reference_lock);
 
67
  for (i = 0; i < cnt; i++)
 
68
    if (match[i] == 0)
 
69
      return -1;
 
70
 
 
71
  return 0;
 
72
}
 
73
 
 
74
static EXTRACTOR_KeywordList *
 
75
get_keywords_for_file (const char *filename)
 
76
{
 
77
  EXTRACTOR_ExtractorList *el;
 
78
  EXTRACTOR_KeywordList *list;
 
79
 
 
80
  el = EXTRACTOR_loadDefaultLibraries ();
 
81
  if (el == NULL)
 
82
    {
 
83
      printf ("ERROR: failed to load plugins!\n");
 
84
      return NULL;
 
85
    }
 
86
  errno = 0;
 
87
  list = EXTRACTOR_getKeywords (el, filename);
 
88
  if (errno != 0) {
 
89
    printf("ERROR: EXTRACTOR_getKeywords: %s\n", strerror(errno));
 
90
  }
 
91
  /*EXTRACTOR_printKeywords (stderr, list); */
 
92
  EXTRACTOR_removeAll (el);
 
93
 
 
94
  return list;
 
95
}
 
96
 
 
97
static void *
 
98
test_plugins (void *arg)
 
99
{
 
100
  struct TaskData *td = (struct TaskData *)arg;
 
101
  while (!done)
 
102
    {
 
103
      EXTRACTOR_KeywordList *list;
 
104
 
 
105
      list = get_keywords_for_file (td->filename);
 
106
 
 
107
      if ((list == NULL) || (compare_keywords_to_ref (list) != 0))
 
108
        {
 
109
          printf ("ERROR: thread id %d failed keyword comparison!\n", td->id);
 
110
          failed = 1;
 
111
        }
 
112
      if (list != NULL)
 
113
        EXTRACTOR_freeKeywords (list);
 
114
    }
 
115
  return 0;
 
116
}
 
117
 
 
118
static const char *filename = TESTDATADIR "/text2.sxw";
 
119
 
 
120
#define TEST_SECS 10
 
121
 
 
122
int
 
123
main (int argc, char *argv[])
 
124
{
 
125
  int num_tasks = 10;
 
126
  pthread_t task_list[num_tasks];
 
127
  struct TaskData td[num_tasks];
 
128
  int ret = 0;
 
129
  int i;
 
130
 
 
131
  printf("testing with '%s' for %d seconds\n", filename, TEST_SECS);
 
132
  reference_list = get_keywords_for_file (filename);
 
133
 
 
134
    for (i = 0; i < num_tasks; i++)
 
135
      {
 
136
        td[i].id = i;
 
137
        td[i].filename = filename;
 
138
        ret = pthread_create (&task_list[i], NULL, test_plugins, &td[i]);
 
139
        if (ret != 0)
 
140
          {
 
141
            printf ("ERROR: pthread_create failed for thread %d\n", i);
 
142
            num_tasks = i;
 
143
            done = 1;
 
144
            break;
 
145
          }
 
146
      }
 
147
    if (!done)
 
148
      sleep (TEST_SECS);
 
149
    done = 1;
 
150
    for (i = 0; i < num_tasks; i++)
 
151
      {
 
152
        if (pthread_join (task_list[i], NULL) != 0)
 
153
          printf ("WARNING: pthread_join failed for thread %d\n", i);
 
154
      }
 
155
 
 
156
    if (reference_list != NULL)
 
157
      EXTRACTOR_freeKeywords (reference_list);
 
158
 
 
159
  return failed;
 
160
}