regex: iterating over the initial states
[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 #include "regex_internal.h"
30
31 enum Match_Result
32 {
33   match = 0,
34   nomatch = 1
35 };
36
37 struct Regex_String_Pair
38 {
39   char *regex;
40   int string_count;
41   char *strings[20];
42   enum Match_Result expected_results[20];
43 };
44
45
46 /**
47  * Random regex test. Generate a random regex as well as 'str_count' strings to
48  * match it against. Will match using GNUNET_REGEX implementation and compare
49  * the result to glibc regex result. 'rx_length' has to be smaller then 'max_str_len'.
50  *
51  * @param rx_length length of the regular expression.
52  * @param max_str_len maximum length of the random strings.
53  * @param str_count number of generated random strings.
54  *
55  * @return 0 on success, non 0 otherwise.
56  */
57 int
58 test_random (unsigned int rx_length, unsigned int max_str_len,
59              unsigned int str_count)
60 {
61   unsigned int i;
62   char *rand_rx;
63   char *matching_str;
64   int eval;
65   int eval_check;
66   int eval_canonical;
67   struct GNUNET_REGEX_Automaton *dfa;
68   regex_t rx;
69   regmatch_t matchptr[1];
70   char error[200];
71   int result;
72   size_t str_len;
73   char *canonical_regex;
74
75   // At least one string is needed for matching
76   GNUNET_assert (str_count > 0);
77   // The string should be at least as long as the regex itself
78   GNUNET_assert (max_str_len >= rx_length);
79
80   // Generate random regex and a string that matches the regex
81   matching_str = GNUNET_malloc (rx_length + 1);
82   rand_rx = GNUNET_REGEX_generate_random_regex (rx_length, matching_str);
83
84   // Now match
85   result = 0;
86   for (i = 0; i < str_count; i++)
87   {
88     if (0 < i)
89     {
90       matching_str = GNUNET_REGEX_generate_random_string (max_str_len);
91       str_len = strlen (matching_str);
92     }
93
94     // Match string using DFA
95     dfa = GNUNET_REGEX_construct_dfa (rand_rx, strlen (rand_rx));
96     if (NULL == dfa)
97     {
98       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n");
99       return -1;
100     }
101
102     eval = GNUNET_REGEX_eval (dfa, matching_str);
103     canonical_regex = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (dfa));
104     GNUNET_REGEX_automaton_destroy (dfa);
105
106     // Match string using glibc regex
107     if (0 != regcomp (&rx, rand_rx, REG_EXTENDED))
108     {
109       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110                   "Could not compile regex using regcomp\n");
111       return -1;
112     }
113
114     eval_check = regexec (&rx, matching_str, 1, matchptr, 0);
115     regfree (&rx);
116
117     // We only want to match the whole string, because that's what our DFA does, too.
118     if (eval_check == 0 &&
119         (matchptr[0].rm_so != 0 || matchptr[0].rm_eo != strlen (matching_str)))
120       eval_check = 1;
121
122     // Match canonical regex
123     if (0 != regcomp (&rx, canonical_regex, REG_EXTENDED))
124     {
125       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
126                   "Could not compile regex using regcomp: %s\n",
127                   canonical_regex);
128       return -1;
129     }
130
131     eval_canonical = regexec (&rx, matching_str, 1, matchptr, 0);
132     regfree (&rx);
133     GNUNET_free (canonical_regex);
134
135     // compare result
136     if (eval_check != eval)
137     {
138       regerror (eval_check, &rx, error, sizeof error);
139       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
140                   "Unexpected result:\nregex: %s\nstring: %s\ngnunet regex: %i\nglibc regex: %i\nglibc error: %s\n\n",
141                   rand_rx, matching_str, eval, eval_check, error);
142       result += 1;
143     }
144
145     GNUNET_free (matching_str);
146   }
147
148   GNUNET_free (rand_rx);
149
150   return result;
151 }
152
153 /**
154  * Automaton test that compares the result of matching regular expression 'rx'
155  * with the strings and expected results in 'rxstr' with the result of matching
156  * the same strings with glibc regex.
157  *
158  * @param a automaton.
159  * @param rx compiled glibc regex.
160  * @param rxstr regular expression and strings with expected results to match against.
161  *
162  * @return 0 on successfull, non 0 otherwise
163  */
164 int
165 test_automaton (struct GNUNET_REGEX_Automaton *a, regex_t * rx,
166                 struct Regex_String_Pair *rxstr)
167 {
168   int result;
169   int eval;
170   int eval_check;
171   char error[200];
172   regmatch_t matchptr[1];
173   int i;
174
175   if (NULL == a)
176   {
177     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Automaton was NULL\n");
178     return 1;
179   }
180
181   result = 0;
182
183   for (i = 0; i < rxstr->string_count; i++)
184   {
185     eval = GNUNET_REGEX_eval (a, rxstr->strings[i]);
186     eval_check = regexec (rx, rxstr->strings[i], 1, matchptr, 0);
187
188     // We only want to match the whole string, because that's what our DFA does, too.
189     if (eval_check == 0 &&
190         (matchptr[0].rm_so != 0 ||
191          matchptr[0].rm_eo != strlen (rxstr->strings[i])))
192       eval_check = 1;
193
194     if ((rxstr->expected_results[i] == match && (0 != eval || 0 != eval_check))
195         || (rxstr->expected_results[i] == nomatch &&
196             (0 == eval || 0 == eval_check)))
197     {
198       result = 1;
199       regerror (eval_check, rx, error, sizeof error);
200       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
201                   "Unexpected result:\nregex: %s\ncanonical_regex: %s\nstring: %s\nexpected result: %i\n"
202                   "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\nrm_so: %i\nrm_eo: %i\n\n",
203                   rxstr->regex, GNUNET_REGEX_get_canonical_regex (a),
204                   rxstr->strings[i], rxstr->expected_results[i], eval,
205                   eval_check, error, matchptr[0].rm_so, matchptr[0].rm_eo);
206     }
207   }
208   return result;
209 }
210
211 int
212 main (int argc, char *argv[])
213 {
214   GNUNET_log_setup ("test-regex",
215 #if VERBOSE
216                     "DEBUG",
217 #else
218                     "WARNING",
219 #endif
220                     NULL);
221
222   struct GNUNET_REGEX_Automaton *a;
223   regex_t rx;
224   int i;
225   int check_nfa;
226   int check_dfa;
227   int check_rand;
228   char *check_proof;
229
230   struct Regex_String_Pair rxstr[16] = {
231     {"ab?(abcd)?", 5,
232      {"ababcd", "abab", "aabcd", "a", "abb"},
233      {match, nomatch, match, match, nomatch}},
234     {"ab(c|d)+c*(a(b|c)d)+", 5,
235      {"abcdcdcdcdddddabd", "abcd", "abcddddddccccccccccccccccccccccccabdacdabd",
236       "abccccca", "abcdcdcdccdabdabd"},
237      {match, nomatch, match, nomatch, match}},
238     {"ab+c*(a(bx|c)d)+", 5,
239      {"abcdcdcdcdddddabd", "abcd", "abcddddddccccccccccccccccccccccccabdacdabd",
240       "abccccca", "abcdcdcdccdabdabd"},
241      {nomatch, nomatch, nomatch, nomatch, nomatch}},
242     {"a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", 1,
243      {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"},
244      {nomatch}},
245     {"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,
246      {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"},
247      {nomatch}},
248     {"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,
249      {"osfjsodfonONONOnosndfsdnfsd"},
250      {nomatch}},
251     {"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,
252      {"VMoxpdhbEmhYEOWWPoZHMIqCa559bzGykRpu8hBlHeLO1Fv05C"},
253      {nomatch}},
254     {"(bla)*", 8,
255      {"", "bla", "blabla", "bl", "la", "b", "l", "a"},
256      {match, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
257     {"ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", 8,
258      {"ab", "abcabdbla", "abdcccccccccccabcbccdblablabla", "bl", "la", "b", "l",
259       "a"},
260      {nomatch, match, match, nomatch, nomatch, nomatch, nomatch, nomatch}},
261     {"a|aa*a", 6,
262      {"", "a", "aa", "aaa", "aaaa", "aaaaa"},
263      {nomatch, match, match, match, match, match}},
264     {"ab(c|d)+c*(a(b|c)+d)+(bla)+", 1,
265      {"abcabdblaacdbla"},
266      {nomatch}},
267     {"(ac|b)+", 8,
268      {"b", "bb", "ac", "", "acb", "bacbacac", "acacac", "abc"},
269      {match, match, match, nomatch, match, match, match, nomatch}},
270     {"(ab|c)+", 7,
271      {"", "ab", "c", "abc", "ababcc", "acc", "abac"},
272      {nomatch, match, match, match, match, nomatch, nomatch}},
273     {"((j|2j)K|(j|2j)AK|(j|2j)(D|e|(j|2j)A(D|e))D*K)", 1,
274      {"", "2j2jADK", "j2jADK"},
275      {nomatch, match, match}},
276     {"((j|2j)K|(j|2j)(D|e|((j|2j)j|(j|2j)2j)A(D|e))D*K|(j|2j)AK)", 2,
277      {"", "2j2jjADK", "j2jADK"},
278      {nomatch, match, match}},
279     {"ab(c|d)+c*(a(b|c)d)+", 1,
280      {"abacd"},
281      {nomatch}}
282   };
283
284   check_nfa = 0;
285   check_dfa = 0;
286   check_rand = 0;
287
288   for (i = 0; i < 16; i++)
289   {
290     if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED))
291     {
292       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
293                   "Could not compile regex using regcomp()\n");
294       return 1;
295     }
296
297     // NFA test
298     a = GNUNET_REGEX_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex));
299     check_nfa += test_automaton (a, &rx, &rxstr[i]);
300     GNUNET_REGEX_automaton_destroy (a);
301
302     // DFA test
303     a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex));
304     check_dfa += test_automaton (a, &rx, &rxstr[i]);
305     check_proof = GNUNET_strdup (GNUNET_REGEX_get_canonical_regex (a));
306     GNUNET_REGEX_automaton_destroy (a);
307
308     a = GNUNET_REGEX_construct_dfa (check_proof, strlen (check_proof));
309     check_dfa += test_automaton (a, &rx, &rxstr[i]);
310     GNUNET_REGEX_automaton_destroy (a);
311     if (0 != check_dfa)
312       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "check_proof: %s\n", check_proof);
313     GNUNET_free_non_null (check_proof);
314
315     regfree (&rx);
316   }
317
318   srand (time (NULL));
319   for (i = 0; i < 50; i++)
320     check_rand += test_random (100, 120, 20);
321
322   return check_nfa + check_dfa + check_rand;
323 }