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
40 enum Match_Result expected_result;
45 test_automaton (struct GNUNET_REGEX_Automaton *a, struct Regex_String_Pair *rxstr)
57 eval = GNUNET_REGEX_eval (a, rxstr->string);
58 regcomp (&rx, rxstr->regex, REG_EXTENDED);
59 eval_check = regexec (&rx, rxstr->string, 0, NULL, 0);
61 if ((rxstr->expected_result == match
62 && (0 != eval || 0 != eval_check))
64 (rxstr->expected_result == nomatch
65 && (0 == eval || 0 == eval_check)))
69 regerror (eval_check, &rx, error, sizeof error);
70 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
71 "Unexpected result:\nregex: %s\nstring: %s\nexpected result: %i\ngnunet regex: %i\nglibc regex: %i\nglibc error: %s\n\n",
72 rxstr->regex, rxstr->string, rxstr->expected_result, eval, eval_check, error);
81 main (int argc, char *argv[])
83 GNUNET_log_setup ("test-regex",
93 struct Regex_String_Pair rxstr[3];
94 struct GNUNET_REGEX_Automaton *a;
97 rxstr[0].regex = "ab(c|d)+c*(a(b|c)d)+";
98 rxstr[0].string = "abcdcdcdcdddddabd";
99 rxstr[0].expected_result = match;
101 rxstr[1].regex = "a*";
102 rxstr[1].string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
103 rxstr[1].expected_result = match;
105 rxstr[2].regex = "a*b*c*d+";
106 rxstr[2].string = "a";
107 rxstr[2].expected_result = nomatch;
112 a = GNUNET_REGEX_construct_nfa (rxstr[i].regex, strlen (rxstr[i].regex));
113 check_nfa += test_automaton (a, &rxstr[i]);
114 GNUNET_REGEX_automaton_destroy (a);
117 a = GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex));
118 check_dfa += test_automaton (a, &rxstr[i]);
119 GNUNET_REGEX_automaton_destroy (a);
122 return check_nfa + check_dfa;