aboutsummaryrefslogtreecommitdiff
path: root/src/regex
diff options
context:
space:
mode:
authorMaximilian Szengel <gnunet@maxsz.de>2012-09-07 16:38:49 +0000
committerMaximilian Szengel <gnunet@maxsz.de>2012-09-07 16:38:49 +0000
commit88638fd30b3c7898e1209b8128adf42b1e4bd315 (patch)
tree47433205bf0092f5144e4c0b1b2ac3807654582e /src/regex
parent9f81c1a85bb5485bdd2b4dd5a95fc02d2f6deeb4 (diff)
downloadgnunet-88638fd30b3c7898e1209b8128adf42b1e4bd315.tar.gz
gnunet-88638fd30b3c7898e1209b8128adf42b1e4bd315.zip
coverage
Diffstat (limited to 'src/regex')
-rw-r--r--src/regex/Makefile.am12
-rw-r--r--src/regex/regex.c102
-rw-r--r--src/regex/regex_graph.c14
-rw-r--r--src/regex/test_regex_eval_api.c9
-rw-r--r--src/regex/test_regex_graph_api.c157
-rw-r--r--src/regex/test_regex_iterate_api.c21
6 files changed, 197 insertions, 118 deletions
diff --git a/src/regex/Makefile.am b/src/regex/Makefile.am
index 36062272e..cd55d755e 100644
--- a/src/regex/Makefile.am
+++ b/src/regex/Makefile.am
@@ -22,7 +22,8 @@ libgnunetregex_la_LDFLAGS = \
22check_PROGRAMS = \ 22check_PROGRAMS = \
23 test_regex_eval_api \ 23 test_regex_eval_api \
24 test_regex_iterate_api \ 24 test_regex_iterate_api \
25 test_regex_proofs 25 test_regex_proofs \
26 test_regex_graph_api
26 27
27if ENABLE_TEST_RUN 28if ENABLE_TEST_RUN
28TESTS = $(check_PROGRAMS) 29TESTS = $(check_PROGRAMS)
@@ -46,5 +47,12 @@ test_regex_proofs_LDADD = \
46 $(top_builddir)/src/regex/libgnunetregex.la \ 47 $(top_builddir)/src/regex/libgnunetregex.la \
47 $(top_builddir)/src/util/libgnunetutil.la 48 $(top_builddir)/src/util/libgnunetutil.la
48 49
49EXTRA_DIST = 50test_regex_graph_api_SOURCES = \
51test_regex_graph_api.c
52test_regex_graph_api_LDADD = \
53$(top_builddir)/src/regex/libgnunetregex.la \
54$(top_builddir)/src/util/libgnunetutil.la
55
56
57EXTRA_DIST =
50# test_regex_data.conf 58# test_regex_data.conf
diff --git a/src/regex/regex.c b/src/regex/regex.c
index d9c776efd..d60ffdc00 100644
--- a/src/regex/regex.c
+++ b/src/regex/regex.c
@@ -52,104 +52,6 @@ struct GNUNET_REGEX_StateSet
52}; 52};
53 53
54 54
55/*
56 * Debug helper functions
57 */
58
59/**
60 * Print all the transitions of state 's'.
61 *
62 * @param s state for which to print it's transitions.
63 */
64void
65debug_print_transitions (struct GNUNET_REGEX_State *s);
66
67
68/**
69 * Print information of the given state 's'.
70 *
71 * @param s state for which debug information should be printed.
72 */
73void
74debug_print_state (struct GNUNET_REGEX_State *s)
75{
76 char *proof;
77
78 if (NULL == s->proof)
79 proof = "NULL";
80 else
81 proof = s->proof;
82
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84 "State %i: %s marked: %i accepting: %i scc_id: %i transitions: %i proof: %s\n",
85 s->id, s->name, s->marked, s->accepting, s->scc_id,
86 s->transition_count, proof);
87
88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transitions:\n");
89 debug_print_transitions (s);
90}
91
92
93/**
94 * Print debug information for all states contained in the automaton 'a'.
95 *
96 * @param a automaton for which debug information of it's states should be printed.
97 */
98void
99debug_print_states (struct GNUNET_REGEX_Automaton *a)
100{
101 struct GNUNET_REGEX_State *s;
102
103 for (s = a->states_head; NULL != s; s = s->next)
104 debug_print_state (s);
105}
106
107
108/**
109 * Print debug information for given transition 't'.
110 *
111 * @param t transition for which to print debug info.
112 */
113void
114debug_print_transition (struct GNUNET_REGEX_Transition *t)
115{
116 char *to_state;
117 char *from_state;
118 char *label;
119
120 if (NULL == t)
121 return;
122
123 if (0 == t->label)
124 label = "0";
125 else
126 label = t->label;
127
128 if (NULL == t->to_state)
129 to_state = "NULL";
130 else
131 to_state = t->to_state->name;
132
133 if (NULL == t->from_state)
134 from_state = "NULL";
135 else
136 from_state = t->from_state->name;
137
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transition %i: From %s on %s to %s\n",
139 t->id, from_state, label, to_state);
140}
141
142
143void
144debug_print_transitions (struct GNUNET_REGEX_State *s)
145{
146 struct GNUNET_REGEX_Transition *t;
147
148 for (t = s->transitions_head; NULL != t; t = t->next)
149 debug_print_transition (t);
150}
151
152
153/** 55/**
154 * Compare two strings for equality. If either is NULL they are not equal. 56 * Compare two strings for equality. If either is NULL they are not equal.
155 * 57 *
@@ -2320,10 +2222,6 @@ GNUNET_REGEX_construct_nfa (const char *regex, const size_t len)
2320 } 2222 }
2321 nfa_add_question_op (&ctx); 2223 nfa_add_question_op (&ctx);
2322 break; 2224 break;
2323 case 92: /* escape: \ */
2324 regexp++;
2325 count++;
2326 /* fall through! */
2327 default: 2225 default:
2328 if (atomcount > 1) 2226 if (atomcount > 1)
2329 { 2227 {
diff --git a/src/regex/regex_graph.c b/src/regex/regex_graph.c
index 5db3780d0..483cef698 100644
--- a/src/regex/regex_graph.c
+++ b/src/regex/regex_graph.c
@@ -197,11 +197,8 @@ GNUNET_REGEX_automaton_save_graph_step (void *cls, unsigned int count,
197 GNUNET_asprintf (&s_acc, "\"%s\" [shape=circle];\n", name, s->scc_id); 197 GNUNET_asprintf (&s_acc, "\"%s\" [shape=circle];\n", name, s->scc_id);
198 } 198 }
199 199
200 if (NULL == s_acc) 200 GNUNET_assert (NULL != s_acc);
201 { 201
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n", s->name);
203 return;
204 }
205 fwrite (s_acc, strlen (s_acc), 1, ctx->filep); 202 fwrite (s_acc, strlen (s_acc), 1, ctx->filep);
206 GNUNET_free (s_acc); 203 GNUNET_free (s_acc);
207 s_acc = NULL; 204 s_acc = NULL;
@@ -256,12 +253,7 @@ GNUNET_REGEX_automaton_save_graph_step (void *cls, unsigned int count,
256 253
257 GNUNET_free (to_name); 254 GNUNET_free (to_name);
258 255
259 if (NULL == s_tran) 256 GNUNET_assert (NULL != s_tran);
260 {
261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not print state %s\n",
262 s->name);
263 return;
264 }
265 257
266 fwrite (s_tran, strlen (s_tran), 1, ctx->filep); 258 fwrite (s_tran, strlen (s_tran), 1, ctx->filep);
267 GNUNET_free (s_tran); 259 GNUNET_free (s_tran);
diff --git a/src/regex/test_regex_eval_api.c b/src/regex/test_regex_eval_api.c
index a4b183127..7538a25d8 100644
--- a/src/regex/test_regex_eval_api.c
+++ b/src/regex/test_regex_eval_api.c
@@ -252,7 +252,7 @@ main (int argc, char *argv[])
252 int check_rand; 252 int check_rand;
253 char *check_proof; 253 char *check_proof;
254 254
255 struct Regex_String_Pair rxstr[17] = { 255 struct Regex_String_Pair rxstr[18] = {
256 {"ab?(abcd)?", 5, 256 {"ab?(abcd)?", 5,
257 {"ababcd", "abab", "aabcd", "a", "abb"}, 257 {"ababcd", "abab", "aabcd", "a", "abb"},
258 {match, nomatch, match, match, nomatch}}, 258 {match, nomatch, match, match, nomatch}},
@@ -309,14 +309,17 @@ main (int argc, char *argv[])
309 {nomatch}}, 309 {nomatch}},
310 {"d|5kl", 1, 310 {"d|5kl", 1,
311 {"d5kl"}, 311 {"d5kl"},
312 {nomatch}} 312 {nomatch}},
313 {"a()b", 1,
314 {"ab"},
315 {match}}
313 }; 316 };
314 317
315 check_nfa = 0; 318 check_nfa = 0;
316 check_dfa = 0; 319 check_dfa = 0;
317 check_rand = 0; 320 check_rand = 0;
318 321
319 for (i = 0; i < 17; i++) 322 for (i = 0; i < 18; i++)
320 { 323 {
321 if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED)) 324 if (0 != regcomp (&rx, rxstr[i].regex, REG_EXTENDED))
322 { 325 {
diff --git a/src/regex/test_regex_graph_api.c b/src/regex/test_regex_graph_api.c
new file mode 100644
index 000000000..0f1b72b61
--- /dev/null
+++ b/src/regex/test_regex_graph_api.c
@@ -0,0 +1,157 @@
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_graph_api.c
22 * @brief test for regex_graph.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
30/**
31 * Check if 'filename' exists and is not empty.
32 *
33 * @param filename name of the file that should be checked
34 *
35 * @return 0 if ok, non 0 on error.
36 */
37int
38filecheck (const char *filename)
39{
40 int error = 0;
41 FILE *fp = NULL;
42
43 // Check if file was created and delete it again
44 fp = fopen (filename, "r");
45
46 if (NULL == fp)
47 {
48 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not find graph %s\n", filename);
49 error++;
50 }
51
52 fseek (fp, 0L, SEEK_END);
53 if (1 > ftell (fp))
54 {
55 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
56 "Graph writing failed, got empty file (%s)!\n", filename);
57 error++;
58 }
59
60 return error;
61}
62
63int
64main (int argc, char *argv[])
65{
66 GNUNET_log_setup ("test-regex",
67#if VERBOSE
68 "DEBUG",
69#else
70 "WARNING",
71#endif
72 NULL);
73
74 int error;
75 struct GNUNET_REGEX_Automaton *a;
76 unsigned int i;
77 const char *filename = "test_graph.dot";
78
79 error = 0;
80
81 const char *regex[10] = {
82 "ab(c|d)+c*(a(b|c)+d)+(bla)+",
83 "(bla)*",
84 "b(lab)*la",
85 "(ab)*",
86 "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*",
87 "z(abc|def)?xyz",
88 "1*0(0|1)*",
89 "a*b*",
90 "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*",
91 "a"
92 };
93
94 for (i = 0; i < 10; i++)
95 {
96 // Check NFA graph creation
97 a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
98 GNUNET_REGEX_automaton_save_graph (a, filename, GNUNET_REGEX_GRAPH_DEFAULT);
99 GNUNET_REGEX_automaton_destroy (a);
100 error += filecheck (filename);
101
102 a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
103 GNUNET_REGEX_automaton_save_graph (a, filename,
104 GNUNET_REGEX_GRAPH_DEFAULT |
105 GNUNET_REGEX_GRAPH_VERBOSE);
106 GNUNET_REGEX_automaton_destroy (a);
107 error += filecheck (filename);
108
109 a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
110 GNUNET_REGEX_automaton_save_graph (a, filename,
111 GNUNET_REGEX_GRAPH_DEFAULT |
112 GNUNET_REGEX_GRAPH_COLORING);
113 GNUNET_REGEX_automaton_destroy (a);
114 error += filecheck (filename);
115
116 a = GNUNET_REGEX_construct_nfa (regex[i], strlen (regex[i]));
117 GNUNET_REGEX_automaton_save_graph (a, filename,
118 GNUNET_REGEX_GRAPH_DEFAULT |
119 GNUNET_REGEX_GRAPH_VERBOSE |
120 GNUNET_REGEX_GRAPH_COLORING);
121 GNUNET_REGEX_automaton_destroy (a);
122 error += filecheck (filename);
123
124
125 // Check DFA graph creation
126 a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
127 GNUNET_REGEX_automaton_save_graph (a, filename, GNUNET_REGEX_GRAPH_DEFAULT);
128 GNUNET_REGEX_automaton_destroy (a);
129 error += filecheck (filename);
130
131 a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
132 GNUNET_REGEX_automaton_save_graph (a, filename,
133 GNUNET_REGEX_GRAPH_DEFAULT |
134 GNUNET_REGEX_GRAPH_VERBOSE);
135 GNUNET_REGEX_automaton_destroy (a);
136 error += filecheck (filename);
137
138 a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
139 GNUNET_REGEX_automaton_save_graph (a, filename,
140 GNUNET_REGEX_GRAPH_DEFAULT |
141 GNUNET_REGEX_GRAPH_COLORING);
142 GNUNET_REGEX_automaton_destroy (a);
143 error += filecheck (filename);
144
145
146 a = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
147 GNUNET_REGEX_automaton_save_graph (a, filename,
148 GNUNET_REGEX_GRAPH_DEFAULT |
149 GNUNET_REGEX_GRAPH_VERBOSE |
150 GNUNET_REGEX_GRAPH_COLORING);
151 GNUNET_REGEX_automaton_destroy (a);
152 error += filecheck (filename);
153
154 }
155
156 return error;
157}
diff --git a/src/regex/test_regex_iterate_api.c b/src/regex/test_regex_iterate_api.c
index 96f1087e7..afbb548c7 100644
--- a/src/regex/test_regex_iterate_api.c
+++ b/src/regex/test_regex_iterate_api.c
@@ -26,6 +26,9 @@
26#include <time.h> 26#include <time.h>
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_regex_lib.h" 28#include "gnunet_regex_lib.h"
29#include "regex_internal.h"
30
31static unsigned int transition_counter;
29 32
30void 33void
31key_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof, 34key_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
@@ -46,6 +49,7 @@ key_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
46 49
47 for (i = 0; i < num_edges; i++) 50 for (i = 0; i < num_edges; i++)
48 { 51 {
52 transition_counter++;
49 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Edge %i: Label: %s Destination: %s\n", 53 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Edge %i: Label: %s Destination: %s\n",
50 i, edges[i].label, GNUNET_h2s (&edges[i].destination)); 54 i, edges[i].label, GNUNET_h2s (&edges[i].destination));
51 } 55 }
@@ -67,6 +71,7 @@ main (int argc, char *argv[])
67 int error; 71 int error;
68 int i; 72 int i;
69 struct GNUNET_REGEX_Automaton *dfa; 73 struct GNUNET_REGEX_Automaton *dfa;
74 unsigned int num_transitions;
70 75
71 error = 0; 76 error = 0;
72 77
@@ -92,7 +97,23 @@ main (int argc, char *argv[])
92 97
93 for (i = 0; i < 17; i++) 98 for (i = 0; i < 17; i++)
94 { 99 {
100 transition_counter = 0;
101 dfa = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
102 GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, &error);
103 num_transitions = GNUNET_REGEX_get_transition_count (dfa);
104 if (transition_counter != num_transitions)
105 {
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107 "Automaton has %d transitions, iterated over %d transitions\n",
108 num_transitions, transition_counter);
109 }
110 GNUNET_REGEX_automaton_destroy (dfa);
111 }
112
113 for (i = 0; i < 17; i++)
114 {
95 dfa = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i])); 115 dfa = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i]));
116 GNUNET_REGEX_add_multi_strides_to_dfa (NULL, dfa, 2);
96 GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, &error); 117 GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, &error);
97 GNUNET_REGEX_automaton_destroy (dfa); 118 GNUNET_REGEX_automaton_destroy (dfa);
98 } 119 }