-reducing CPU usage from nfa_closure_set_create by avoiding double-sorting and quadra...
[oweals/gnunet.git] / src / regex / test_regex_iterate_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_iterate_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 /**
32  * Regex initial padding.
33  */
34 #define INITIAL_PADDING "PADPADPADPADPADP"
35
36 /**
37  * Set to GNUNET_YES to save a debug graph.
38  */
39 #define GNUNET_REGEX_ITERATE_SAVE_DEBUG_GRAPH GNUNET_NO
40
41 static unsigned int transition_counter;
42
43 struct IteratorContext
44 {
45   int error;
46   int should_save_graph;
47   FILE *graph_filep;
48   unsigned int string_count;
49   char *const *strings;
50   unsigned int match_count;
51 };
52
53 struct RegexStringPair
54 {
55   char *regex;
56   unsigned int string_count;
57   char *strings[20];
58 };
59
60 void
61 key_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
62               int accepting, unsigned int num_edges,
63               const struct GNUNET_REGEX_Edge *edges)
64 {
65   unsigned int i;
66   struct IteratorContext *ctx = cls;
67   char *out_str;
68   char *state_id = GNUNET_strdup (GNUNET_h2s (key));
69
70   if (GNUNET_YES == ctx->should_save_graph)
71   {
72     if (GNUNET_YES == accepting)
73       GNUNET_asprintf (&out_str, "\"%s\" [shape=doublecircle]\n", state_id);
74     else
75       GNUNET_asprintf (&out_str, "\"%s\" [shape=circle]\n", state_id);
76     fwrite (out_str, strlen (out_str), 1, ctx->graph_filep);
77     GNUNET_free (out_str);
78
79     for (i = 0; i < num_edges; i++)
80     {
81       transition_counter++;
82       GNUNET_asprintf (&out_str, "\"%s\" -> \"%s\" [label = \"%s (%s)\"]\n",
83                        state_id, GNUNET_h2s (&edges[i].destination),
84                        edges[i].label, proof);
85       fwrite (out_str, strlen (out_str), 1, ctx->graph_filep);
86
87       GNUNET_free (out_str);
88     }
89   }
90   else
91   {
92     for (i = 0; i < num_edges; i++)
93       transition_counter++;
94   }
95
96   for (i = 0; i < ctx->string_count; i++)
97   {
98     if (0 == strcmp (proof, ctx->strings[i]))
99       ctx->match_count++;
100   }
101
102   if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
103   {
104     ctx->error++;
105     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
106                 "Proof check failed: proof: %s key: %s\n", proof, state_id);
107   }
108
109   GNUNET_free (state_id);
110 }
111
112 int
113 main (int argc, char *argv[])
114 {
115   GNUNET_log_setup ("test-regex", "WARNING", NULL);
116
117   int error;
118   struct GNUNET_REGEX_Automaton *dfa;
119   unsigned int i;
120   unsigned int num_transitions;
121   char *filename = NULL;
122   struct IteratorContext ctx = { 0, 0, NULL, 0, NULL, 0 };
123
124   error = 0;
125
126   const struct RegexStringPair rxstr[13] = {
127     {INITIAL_PADDING "ab(c|d)+c*(a(b|c)+d)+(bla)+", 2,
128      {INITIAL_PADDING "abcdcdca", INITIAL_PADDING "abcabdbl"}},
129     {INITIAL_PADDING
130      "abcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd", 1,
131      {INITIAL_PADDING "abcdefgh"}},
132     {INITIAL_PADDING "VPN-4-1(0|1)*", 2,
133      {INITIAL_PADDING "VPN-4-10", INITIAL_PADDING "VPN-4-11"}},
134     {INITIAL_PADDING "(a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*)", 2,
135      {INITIAL_PADDING "aaaaaaaa", INITIAL_PADDING "aaXXyyyc"}},
136     {INITIAL_PADDING "a*", 1, {INITIAL_PADDING "aaaaaaaa"}},
137     {INITIAL_PADDING "xzxzxzxzxz", 1, {INITIAL_PADDING "xzxzxzxz"}},
138     {INITIAL_PADDING "xyz*", 1, {INITIAL_PADDING "xyzzzzzz"}},
139     {INITIAL_PADDING
140      "abcd:(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1):(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)(0|1)",
141      2, {INITIAL_PADDING "abcd:000", INITIAL_PADDING "abcd:101"}},
142     {INITIAL_PADDING "(x*|(0|1|2)(a|b|c|d)+)", 2,
143      {INITIAL_PADDING "xxxxxxxx", INITIAL_PADDING "0abcdbad"}},
144     {INITIAL_PADDING "(0|1)(0|1)23456789ABC", 1, {INITIAL_PADDING "11234567"}},
145     {INITIAL_PADDING "0*123456789ABC*", 3,
146      {INITIAL_PADDING "00123456", INITIAL_PADDING "00000000",
147       INITIAL_PADDING "12345678"}},
148     {INITIAL_PADDING "0123456789A*BC", 1, {INITIAL_PADDING "01234567"}},
149     {"GNUNETVPN000100000IPEX6-fc5a:4e1:c2ba::1", 1, {"GNUNETVPN000100000IPEX6-"}}
150   };
151
152   const char *graph_start_str = "digraph G {\nrankdir=LR\n";
153   const char *graph_end_str = "\n}\n";
154
155   for (i = 0; i < 13; i++)
156   {
157     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating DFA for regex %s\n",
158                 rxstr[i].regex);
159
160
161     /* Create graph */
162     if (GNUNET_YES == GNUNET_REGEX_ITERATE_SAVE_DEBUG_GRAPH)
163     {
164       GNUNET_asprintf (&filename, "iteration_graph_%u.dot", i);
165       ctx.graph_filep = fopen (filename, "w");
166       if (NULL == ctx.graph_filep)
167       {
168         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
169                     "Could not open file %s for saving iteration graph.\n",
170                     filename);
171         ctx.should_save_graph = GNUNET_NO;
172       }
173       else
174       {
175         ctx.should_save_graph = GNUNET_YES;
176         fwrite (graph_start_str, strlen (graph_start_str), 1, ctx.graph_filep);
177       }
178       GNUNET_free (filename);
179     }
180     else
181     {
182       ctx.should_save_graph = GNUNET_NO;
183       ctx.graph_filep = NULL;
184     }
185
186     /* Iterate over DFA edges */
187     transition_counter = 0;
188     ctx.string_count = rxstr[i].string_count;
189     ctx.strings = rxstr[i].strings;
190     ctx.match_count = 0;
191     dfa =
192         GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0);
193     GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, &ctx);
194     num_transitions =
195         GNUNET_REGEX_get_transition_count (dfa) - dfa->start->transition_count;
196
197     if (transition_counter < num_transitions)
198     {
199       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200                   "Automaton has %d transitions, iterated over %d transitions\n",
201                   num_transitions, transition_counter);
202       error += 1;
203     }
204
205     if (ctx.match_count < ctx.string_count)
206     {
207       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
208                   "Missing initial states for regex %s\n", rxstr[i].regex);
209       error += (ctx.string_count - ctx.match_count);
210     }
211     else if (ctx.match_count > ctx.string_count)
212     {
213       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214                   "Duplicate initial transitions for regex %s\n",
215                   rxstr[i].regex);
216       error += (ctx.string_count - ctx.match_count);
217     }
218
219     GNUNET_REGEX_automaton_destroy (dfa);
220
221     /* Finish graph */
222     if (GNUNET_YES == ctx.should_save_graph)
223     {
224       fwrite (graph_end_str, strlen (graph_end_str), 1, ctx.graph_filep);
225       fclose (ctx.graph_filep);
226       ctx.graph_filep = NULL;
227       ctx.should_save_graph = GNUNET_NO;
228     }
229   }
230
231
232   for (i = 0; i < 13; i++)
233   {
234     ctx.string_count = rxstr[i].string_count;
235     ctx.strings = rxstr[i].strings;
236     ctx.match_count = 0;
237
238     dfa =
239         GNUNET_REGEX_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0);
240     GNUNET_REGEX_dfa_add_multi_strides (NULL, dfa, 2);
241     GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, &ctx);
242
243     if (ctx.match_count < ctx.string_count)
244     {
245       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246                   "Missing initial states for regex %s\n", rxstr[i].regex);
247       error += (ctx.string_count - ctx.match_count);
248     }
249
250     GNUNET_REGEX_automaton_destroy (dfa);
251   }
252
253   error += ctx.error;
254
255   return error;
256 }