fix
[oweals/gnunet.git] / src / regex / test_regex.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.c
22  * @brief test for regex.c
23  * @author Maximilian Szengel
24  */
25 #include <regex.h>
26
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   char *string;
40   enum Match_Result expected_result;
41 };
42
43
44 int
45 test_automaton (struct GNUNET_REGEX_Automaton *a, struct Regex_String_Pair *rxstr)
46 {
47   regex_t rx;
48   int result;
49   int eval;
50   int eval_check;
51
52   if (NULL == a)
53     return 1;
54
55   result = 0;
56
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);
60
61   if ((rxstr->expected_result == match
62        && (0 != eval || 0 != eval_check))
63       ||
64       (rxstr->expected_result == nomatch
65        && (0 == eval || 0 == eval_check)))
66   {
67       result = 1;
68       char error[200];
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);
73   }
74
75   regfree (&rx);
76
77   return result;
78 }
79
80 int
81 main (int argc, char *argv[])
82 {
83   GNUNET_log_setup ("test-regex",
84 #if VERBOSE
85                     "DEBUG",
86 #else
87                     "WARNING",
88 #endif
89                     NULL);
90
91   int check_nfa;
92   int check_dfa;
93   struct Regex_String_Pair rxstr[3];
94   struct GNUNET_REGEX_Automaton *a;
95   int i;
96
97   rxstr[0].regex = "ab(c|d)+c*(a(b|c)d)+";
98   rxstr[0].string = "abcdcdcdcdddddabd";
99   rxstr[0].expected_result = match;
100
101   rxstr[1].regex = "a*";
102   rxstr[1].string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
103   rxstr[1].expected_result = match;
104
105   rxstr[2].regex = "a*b*c*d+";
106   rxstr[2].string = "a";
107   rxstr[2].expected_result = nomatch;
108
109   for (i=0; i<3; i++)
110   {
111     // NFA test
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);
115
116     // DFA test
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);
120   }
121
122   return check_nfa + check_dfa;
123 }