89a7578060e815edee4d5e4496868157f95d7db9
[oweals/gnunet.git] / src / regex / test_regex_eval_api.c
1 /*
2      This file is part of GNUnet
3      (C) 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file regex/test_regex_eval_api.c
22  * @brief test for regex.c
23  * @author Maximilian Szengel
24  */
25 #include <regex.h>
26 #include <time.h>
27 #include "platform.h"
28 #include "gnunet_regex_lib.h"
29
30 enum Match_Result
31 {
32   match = 0,
33   nomatch = 1
34 };
35
36 struct Regex_String_Pair
37 {
38   char *regex;
39   int string_count;
40   char *strings[20];
41   enum Match_Result expected_results[20];
42 };
43
44 static const char allowed_literals[] =
45     "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
46
47 int
48 test_random (unsigned int rx_length, unsigned int max_str_len,
49              unsigned int str_count)
50 {
51   int i;
52   int j;
53   int rx_exp;
54   char rand_rx[rx_length + 1];
55   char matching_str[str_count][max_str_len + 1];
56   char *rand_rxp;
57   char *matching_strp;
58   int char_op_switch;
59   int last_was_op;
60   char current_char;
61   int eval;
62   int eval_check;
63   int eval_computed;
64   struct GNUNET_REGEX_Automaton *dfa;
65   regex_t rx;
66   regmatch_t matchptr[1];
67   char error[200];
68   int result;
69   unsigned int str_len;
70   char *computed_regex;
71
72   // At least one string is needed for matching
73   GNUNET_assert (str_count > 0);
74   // The string should be at least as long as the regex itself
75   GNUNET_assert (max_str_len >= rx_length);
76
77   rand_rxp = rand_rx;
78   matching_strp = matching_str[0];
79   current_char = 0;
80   last_was_op = 1;
81
82   // Generate random regex and a string that matches the regex
83   for (i = 0; i < rx_length; i++)
84   {
85     char_op_switch = 0 + (int) (1.0 * rand () / (RAND_MAX + 1.0));
86
87     if (0 == char_op_switch && !last_was_op)
88     {
89       last_was_op = 1;
90       rx_exp = rand () % 4;
91
92       switch (rx_exp)
93       {
94       case 0:
95         current_char = '+';
96         break;
97       case 1:
98         current_char = '*';
99         break;
100       case 2:
101         current_char = '?';
102         break;
103       case 3:
104         if (i < rx_length - 1)  // '|' cannot be at the end
105           current_char = '|';
106         else
107           current_char =
108               allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
109         break;
110       }
111     }
112     else
113     {
114       current_char =
115           allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
116       last_was_op = 0;
117     }
118
119     if (current_char != '+' && current_char != '*' && current_char != '?' &&
120         current_char != '|')
121     {
122       *matching_strp = current_char;
123       matching_strp++;
124     }
125
126     *rand_rxp = current_char;
127     rand_rxp++;
128   }
129   *rand_rxp = '\0';
130   *matching_strp = '\0';
131
132   // Generate some random strings for matching...
133   // Start at 1, because the first string is generated above during regex generation
134   for (i = 1; i < str_count; i++)
135   {
136     str_len = rand () % max_str_len;
137     for (j = 0; j < str_len; j++)
138       matching_str[i][j] =
139           allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
140     matching_str[i][str_len] = '\0';
141   }
142
143   // Now match
144   result = 0;
145   for (i = 0; i < str_count; i++)
146   {
147     // Match string using DFA
148     dfa = GNUNET_REGEX_construct_dfa (rand_rx, strlen (rand_rx));
149     if (NULL == dfa)
150     {
151       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n");
152       return -1;
153     }
154
155     eval = GNUNET_REGEX_eval (dfa, matching_str[i]);
156     computed_regex = GNUNET_strdup (GNUNET_REGEX_get_computed_regex (dfa));
157     GNUNET_REGEX_automaton_destroy (dfa);
158
159     // Match string using glibc regex
160     if (0 != regcomp (&rx, rand_rx, REG_EXTENDED))
161     {
162       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163                   "Could not compile regex using regcomp\n");
164       return -1;
165     }
166
167     eval_check = regexec (&rx, matching_str[i], 1, matchptr, 0);
168     regfree (&rx);
169
170     // Match computed regex
171     if (0 != regcomp (&rx, computed_regex, REG_EXTENDED))
172     {
173       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174                   "Could not compile regex using regcomp: %s\n",
175                   computed_regex);
176       return -1;
177     }
178
179     eval_computed = regexec (&rx, matching_str[i], 1, matchptr, 0);
180     regfree (&rx);
181     GNUNET_free (computed_regex);
182
183     // We only want to match the whole string, because that's what our DFA does, too.
184     if (eval_check == 0 &&
185         (matchptr[0].rm_so != 0 ||
186          matchptr[0].rm_eo != strlen (matching_str[i])))
187       eval_check = 1;
188
189     // compare result
190     if (eval_check != eval)
191     {
192       regerror (eval_check, &rx, error, sizeof error);
193       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194                   "Unexpected result:\nregex: %s\nstring: %s\ngnunet regex: %i\nglibc regex: %i\nglibc error: %s\n\n",
195                   rand_rx, matching_str, eval, eval_check, error);
196       result += 1;
197     }
198   }
199   return result;
200 }
201
202 int
203 test_automaton (struct GNUNET_REGEX_Automaton *a, regex_t * rx,
204                 struct Regex_String_Pair *rxstr)
205 {
206   int result;
207   int eval;
208   int eval_check;
209   char error[200];
210   regmatch_t matchptr[1];
211   int i;
212
213   if (NULL == a)
214   {
215     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Automaton was NULL\n");
216     return 1;
217   }
218
219   result = 0;
220
221   for (i = 0; i < rxstr->string_count; i++)
222   {
223     eval = GNUNET_REGEX_eval (a, rxstr->strings[i]);
224     eval_check = regexec (rx, rxstr->strings[i], 1, matchptr, 0);
225
226     // We only want to match the whole string, because that's what our DFA does, too.
227     if (eval_check == 0 &&
228         (matchptr[0].rm_so != 0 ||
229          matchptr[0].rm_eo != strlen (rxstr->strings[i])))
230       eval_check = 1;
231
232     if ((rxstr->expected_results[i] == match && (0 != eval || 0 != eval_check))
233         || (rxstr->expected_results[i] == nomatch &&
234             (0 == eval || 0 == eval_check)))
235     {
236       result = 1;
237       regerror (eval_check, rx, error, sizeof error);
238       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239                   "Unexpected result:\nregex: %s\nstring: %s\nexpected result: %i\n"
240                   "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\nrm_so: %i\nrm_eo: %i\n\n",
241                   rxstr->regex, rxstr->strings[i], rxstr->expected_results[i],
242                   eval, eval_check, error, matchptr[0].rm_so,
243                   matchptr[0].rm_eo);
244     }
245   }
246   return result;
247 }
248
249 int
250 main (int argc, char *argv[])
251 {
252   GNUNET_log_setup ("test-regex",
253 #if VERBOSE
254                     "DEBUG",
255 #else
256                     "WARNING",
257 #endif
258                     NULL);
259
260   struct GNUNET_REGEX_Automaton *a;
261   regex_t rx;
262   int i;
263   int check_nfa;
264   int check_dfa;
265   int check_rand;
266   char *check_proof;
267
268   struct Regex_String_Pair rxstr[12] = {
269     {"ab?(abcd)?", 5,
270      {"ababcd", "abab", "aabcd", "a", "abb"},
271      {match, nomatch, match, match, nomatch}},
272     {"ab(c|d)+c*(a(b|c)d)+", 5,
273      {"abcdcdcdcdddddabd", "abcd", "abcddddddccccccccccccccccccccccccabdacdabd",
274       "abccccca", "abcdcdcdccdabdabd"},
275      {match, nomatch, match, nomatch, match}},
276     {"ab+c*(a(bx|c)d)+", 5,
277      {"abcdcdcdcdddddabd", "abcd", "abcddddddccccccccccccccccccccccccabdacdabd",
278       "abccccca", "abcdcdcdccdabdabd"},
279      {nomatch, nomatch, nomatch, nomatch, nomatch}},
280     {"a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", 1,
281      {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"},
282      {nomatch}},
283     {"k|a+X*y+c|Q*e|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*g|N+V|t+L|P*j*3*9+X*h*J|J*6|b|E*i*f*R+S|Z|R|Y*Z|g*", 1,
284      {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"},
285      {nomatch}},
286     {"F?W+m+2*6*c*s|P?U?a|B|y*i+t+A|V|6*C*7*e?Z*n*i|J?5+g?W*V?7*j?p?1|r?B?C+E+3+6*i+W*P?K?0|D+7?y*m+3?g?K?", 1,
287      {"osfjsodfonONONOnosndfsdnfsd"},
288      {nomatch}},
289     {"V|M*o?x*p*d+h+b|E*m?h?Y*E*O?W*W*P+o?Z+H*M|I*q+C*a+5?5*9|b?z|G*y*k?R|p+u|8*h?B+l*H|e|L*O|1|F?v*0?5|C+", 1,
290      {"VMoxpdhbEmhYEOWWPoZHMIqCa559bzGykRpu8hBlHeLO1Fv05C"},
291      {nomatch}},
292     {"(bla)*", 8,
293      {"", "bla", "blabla", "bl", "la", "b", "l", "a"},
294      {match, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
295     {"ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", 8,
296      {"ab", "abcabdbla", "abdcccccccccccabcbccdblablabla", "bl", "la", "b", "l",
297       "a"},
298      {nomatch, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
299     {"a|aa*a", 6,
300      {"", "a", "aa", "aaa", "aaaa", "aaaaa"},
301      {nomatch, match, match, match, match, match}},
302     {"ab(c|d)+c*(a(b|c)+d)+(bla)+", 1,
303      {"abcabdblaacdbla"},
304      {nomatch}},
305     {"ab(c|d)+c*(a(b|c)d)+", 1,
306      {"abacd"},
307      {nomatch}}
308   };
309
310   check_nfa = 0;
311   check_dfa = 0;
312   check_rand = 0;
313
314   for (i = 0; i < 12; i++)
315   {
316     if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED))
317     {
318       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
319                   "Could not compile regex using regcomp()\n");
320       return 1;
321     }
322
323     // NFA test
324     a = GNUNET_REGEX_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex));
325     check_nfa += test_automaton (a, &rx, &rxstr[i]);
326     GNUNET_REGEX_automaton_destroy (a);
327
328     // DFA test
329     a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex));
330     check_dfa += test_automaton (a, &rx, &rxstr[i]);
331     check_proof = GNUNET_strdup (GNUNET_REGEX_get_computed_regex (a));
332     GNUNET_REGEX_automaton_destroy (a);
333     a = GNUNET_REGEX_construct_dfa (check_proof, strlen (check_proof));
334     check_dfa += test_automaton (a, &rx, &rxstr[i]);
335     GNUNET_REGEX_automaton_destroy (a);
336     if (0 != check_dfa)
337       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "check_proof: %s\n", check_proof);
338     GNUNET_free_non_null (check_proof);
339
340     regfree (&rx);
341   }
342
343   srand (time (NULL));
344   for (i = 0; i < 150; i++)
345     check_rand += test_random (150, 200, 25);
346
347   return check_nfa + check_dfa + check_rand;
348 }