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_iterate_api.c
22 * @brief test for regex.c
23 * @author Maximilian Szengel
28 #include "regex_internal_lib.h"
29 #include "regex_block_lib.h"
30 #include "regex_internal.h"
33 * Regex initial padding.
35 #define INITIAL_PADDING "PADPADPADPADPADP"
38 * Set to GNUNET_YES to save a debug graph.
40 #define REGEX_INTERNAL_ITERATE_SAVE_DEBUG_GRAPH GNUNET_NO
42 static unsigned int transition_counter;
44 struct IteratorContext
47 int should_save_graph;
49 unsigned int string_count;
51 unsigned int match_count;
54 struct RegexStringPair
57 unsigned int string_count;
63 key_iterator (void *cls, const struct GNUNET_HashCode *key,
65 int accepting, unsigned int num_edges,
66 const struct REGEX_BLOCK_Edge *edges)
69 struct IteratorContext *ctx = cls;
71 char *state_id = GNUNET_strdup (GNUNET_h2s (key));
73 GNUNET_assert (NULL != proof);
74 if (GNUNET_YES == ctx->should_save_graph)
76 if (GNUNET_YES == accepting)
77 GNUNET_asprintf (&out_str, "\"%s\" [shape=doublecircle]\n", state_id);
79 GNUNET_asprintf (&out_str, "\"%s\" [shape=circle]\n", state_id);
80 fwrite (out_str, strlen (out_str), 1, ctx->graph_filep);
81 GNUNET_free (out_str);
83 for (i = 0; i < num_edges; i++)
86 GNUNET_asprintf (&out_str, "\"%s\" -> \"%s\" [label = \"%s (%s)\"]\n",
87 state_id, GNUNET_h2s (&edges[i].destination),
88 edges[i].label, proof);
89 fwrite (out_str, strlen (out_str), 1, ctx->graph_filep);
91 GNUNET_free (out_str);
96 for (i = 0; i < num_edges; i++)
100 for (i = 0; i < ctx->string_count; i++)
102 if (0 == strcmp (proof, ctx->strings[i]))
106 if (GNUNET_OK != REGEX_BLOCK_check_proof (proof, strlen (proof), key))
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 "Proof check failed: proof: %s key: %s\n", proof, state_id);
112 GNUNET_free (state_id);
117 main (int argc, char *argv[])
119 GNUNET_log_setup ("test-regex", "WARNING", NULL);
122 struct REGEX_INTERNAL_Automaton *dfa;
124 unsigned int num_transitions;
125 char *filename = NULL;
126 struct IteratorContext ctx = { 0, 0, NULL, 0, NULL, 0 };
130 const struct RegexStringPair rxstr[13] = {
131 {INITIAL_PADDING "ab(c|d)+c*(a(b|c)+d)+(bla)+", 2,
132 {INITIAL_PADDING "abcdcdca", INITIAL_PADDING "abcabdbl"}},
134 "abcdefghixxxxxxxxxxxxxjklmnop*qstoisdjfguisdfguihsdfgbdsuivggsd", 1,
135 {INITIAL_PADDING "abcdefgh"}},
136 {INITIAL_PADDING "VPN-4-1(0|1)*", 2,
137 {INITIAL_PADDING "VPN-4-10", INITIAL_PADDING "VPN-4-11"}},
138 {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,
139 {INITIAL_PADDING "aaaaaaaa", INITIAL_PADDING "aaXXyyyc"}},
140 {INITIAL_PADDING "a*", 1, {INITIAL_PADDING "aaaaaaaa"}},
141 {INITIAL_PADDING "xzxzxzxzxz", 1, {INITIAL_PADDING "xzxzxzxz"}},
142 {INITIAL_PADDING "xyz*", 1, {INITIAL_PADDING "xyzzzzzz"}},
144 "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)",
145 2, {INITIAL_PADDING "abcd:000", INITIAL_PADDING "abcd:101"}},
146 {INITIAL_PADDING "(x*|(0|1|2)(a|b|c|d)+)", 2,
147 {INITIAL_PADDING "xxxxxxxx", INITIAL_PADDING "0abcdbad"}},
148 {INITIAL_PADDING "(0|1)(0|1)23456789ABC", 1, {INITIAL_PADDING "11234567"}},
149 {INITIAL_PADDING "0*123456789ABC*", 3,
150 {INITIAL_PADDING "00123456", INITIAL_PADDING "00000000",
151 INITIAL_PADDING "12345678"}},
152 {INITIAL_PADDING "0123456789A*BC", 1, {INITIAL_PADDING "01234567"}},
153 {"GNUNETVPN000100000IPEX6-fc5a:4e1:c2ba::1", 1, {"GNUNETVPN000100000IPEX6-"}}
156 const char *graph_start_str = "digraph G {\nrankdir=LR\n";
157 const char *graph_end_str = "\n}\n";
159 for (i = 0; i < 13; i++)
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating DFA for regex %s\n",
166 if (GNUNET_YES == REGEX_INTERNAL_ITERATE_SAVE_DEBUG_GRAPH)
168 GNUNET_asprintf (&filename, "iteration_graph_%u.dot", i);
169 ctx.graph_filep = fopen (filename, "w");
170 if (NULL == ctx.graph_filep)
172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
173 "Could not open file %s for saving iteration graph.\n",
175 ctx.should_save_graph = GNUNET_NO;
179 ctx.should_save_graph = GNUNET_YES;
180 fwrite (graph_start_str, strlen (graph_start_str), 1, ctx.graph_filep);
182 GNUNET_free (filename);
186 ctx.should_save_graph = GNUNET_NO;
187 ctx.graph_filep = NULL;
190 /* Iterate over DFA edges */
191 transition_counter = 0;
192 ctx.string_count = rxstr[i].string_count;
193 ctx.strings = rxstr[i].strings;
196 REGEX_INTERNAL_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0);
197 REGEX_INTERNAL_iterate_all_edges (dfa, key_iterator, &ctx);
199 REGEX_INTERNAL_get_transition_count (dfa) - dfa->start->transition_count;
201 if (transition_counter < num_transitions)
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 "Automaton has %d transitions, iterated over %d transitions\n",
205 num_transitions, transition_counter);
209 if (ctx.match_count < ctx.string_count)
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 "Missing initial states for regex %s\n", rxstr[i].regex);
213 error += (ctx.string_count - ctx.match_count);
215 else if (ctx.match_count > ctx.string_count)
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 "Duplicate initial transitions for regex %s\n",
220 error += (ctx.string_count - ctx.match_count);
223 REGEX_INTERNAL_automaton_destroy (dfa);
226 if (GNUNET_YES == ctx.should_save_graph)
228 fwrite (graph_end_str, strlen (graph_end_str), 1, ctx.graph_filep);
229 fclose (ctx.graph_filep);
230 ctx.graph_filep = NULL;
231 ctx.should_save_graph = GNUNET_NO;
236 for (i = 0; i < 13; i++)
238 ctx.string_count = rxstr[i].string_count;
239 ctx.strings = rxstr[i].strings;
243 REGEX_INTERNAL_construct_dfa (rxstr[i].regex, strlen (rxstr[i].regex), 0);
244 REGEX_INTERNAL_dfa_add_multi_strides (NULL, dfa, 2);
245 REGEX_INTERNAL_iterate_all_edges (dfa, key_iterator, &ctx);
247 if (ctx.match_count < ctx.string_count)
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 "Missing initial states for regex %s\n", rxstr[i].regex);
251 error += (ctx.string_count - ctx.match_count);
254 REGEX_INTERNAL_automaton_destroy (dfa);