2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file regex/test_regex.c
22 * @brief test for regex.c
23 * @author Maximilian Szengel
28 #include "gnunet_regex_lib.h"
36 struct Regex_String_Pair
41 enum Match_Result expected_results[20];
44 static const char allowed_literals[] =
45 "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz";
48 test_random (unsigned int rx_length, unsigned int max_str_len,
49 unsigned int str_count)
54 char rand_rx[rx_length + 1];
55 char matching_str[str_count][max_str_len + 1];
63 struct GNUNET_REGEX_Automaton *dfa;
65 regmatch_t matchptr[1];
70 // At least one string is needed for matching
71 GNUNET_assert (str_count > 0);
72 // The string should be at least as long as the regex itself
73 GNUNET_assert (max_str_len >= rx_length);
76 matching_strp = matching_str[0];
80 // Generate random regex and a string that matches the regex
81 for (i = 0; i < rx_length; i++)
83 char_op_switch = 0 + (int) (1.0 * rand () / (RAND_MAX + 1.0));
85 if (0 == char_op_switch && !last_was_op)
99 if (i < rx_length - 1) // '|' cannot be at the end
103 allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
110 allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
114 if (current_char != '+' && current_char != '*' && current_char != '|')
116 *matching_strp = current_char;
120 *rand_rxp = current_char;
124 *matching_strp = '\0';
126 // Generate some random strings for matching...
127 // Start at 1, because the first string is generated above during regex generation
128 for (i = 1; i < str_count; i++)
130 str_len = rand () % max_str_len;
131 for (j = 0; j < str_len; j++)
133 allowed_literals[rand () % (sizeof (allowed_literals) - 1)];
134 matching_str[i][str_len] = '\0';
139 for (i = 0; i < str_count; i++)
141 // Match string using DFA
142 dfa = GNUNET_REGEX_construct_dfa (rand_rx, strlen (rand_rx));
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Constructing DFA failed\n");
149 eval = GNUNET_REGEX_eval (dfa, matching_str[i]);
150 GNUNET_REGEX_automaton_destroy (dfa);
152 // Match string using glibc regex
153 if (0 != regcomp (&rx, rand_rx, REG_EXTENDED))
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156 "Could not compile regex using regcomp\n");
160 eval_check = regexec (&rx, matching_str[i], 1, matchptr, 0);
163 // We only want to match the whole string, because that's what our DFA does, too.
164 if (eval_check == 0 &&
165 (matchptr[0].rm_so != 0 ||
166 matchptr[0].rm_eo != strlen (matching_str[i])))
170 if (eval_check != eval)
172 regerror (eval_check, &rx, error, sizeof error);
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 "Unexpected result:\nregex: %s\nstring: %s\ngnunet regex: %i\nglibc regex: %i\nglibc error: %s\n\n",
175 rand_rx, matching_str, eval, eval_check, error);
183 test_automaton (struct GNUNET_REGEX_Automaton *a, regex_t * rx,
184 struct Regex_String_Pair *rxstr)
190 regmatch_t matchptr[1];
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Automaton was NULL\n");
201 for (i = 0; i < rxstr->string_count; i++)
203 eval = GNUNET_REGEX_eval (a, rxstr->strings[i]);
204 eval_check = regexec (rx, rxstr->strings[i], 1, matchptr, 0);
206 // We only want to match the whole string, because that's what our DFA does, too.
207 if (eval_check == 0 &&
208 (matchptr[0].rm_so != 0 ||
209 matchptr[0].rm_eo != strlen (rxstr->strings[i])))
212 if ((rxstr->expected_results[i] == match && (0 != eval || 0 != eval_check))
213 || (rxstr->expected_results[i] == nomatch &&
214 (0 == eval || 0 == eval_check)))
217 regerror (eval_check, rx, error, sizeof error);
218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219 "Unexpected result:\nregex: %s\nstring: %s\nexpected result: %i\n"
220 "gnunet regex: %i\nglibc regex: %i\nglibc error: %s\nrm_so: %i\nrm_eo: %i\n\n",
221 rxstr->regex, rxstr->strings[i], rxstr->expected_results[i],
222 eval, eval_check, error, matchptr[0].rm_so,
230 main (int argc, char *argv[])
232 GNUNET_log_setup ("test-regex",
240 struct GNUNET_REGEX_Automaton *a;
247 struct Regex_String_Pair rxstr[4] = {
248 {"ab(c|d)+c*(a(b|c)d)+", 5,
249 {"abcdcdcdcdddddabd", "abcd", "abcddddddccccccccccccccccccccccccabdacdabd",
250 "abccccca", "abcdcdcdccdabdabd"},
251 {match, nomatch, match, nomatch, match}},
252 {"ab+c*(a(bx|c)d)+", 5,
253 {"abcdcdcdcdddddabd", "abcd", "abcddddddccccccccccccccccccccccccabdacdabd",
254 "abccccca", "abcdcdcdccdabdabd"},
255 {nomatch, nomatch, nomatch, nomatch, nomatch}},
256 {"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,
257 {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"},
259 {"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,
260 {"kaXycQepRZKyRwY6nhkwVFWBegNVtLPj39XhJJ6bEifRSZRYZg"},
268 for (i = 0; i < 4; i++)
270 if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED))
272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273 "Could not compile regex using regcomp()\n");
278 a = GNUNET_REGEX_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex));
279 check_nfa += test_automaton (a, &rx, &rxstr[i]);
280 GNUNET_REGEX_automaton_destroy (a);
283 a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex));
284 check_dfa += test_automaton (a, &rx, &rxstr[i]);
285 GNUNET_REGEX_automaton_destroy (a);
291 for (i = 0; i < 100; i++)
292 check_rand += test_random (100, 150, 10);
294 return check_nfa + check_dfa + check_rand;