From 252ed9c8c13ae532d8b54bd2da236bfc20489e53 Mon Sep 17 00:00:00 2001 From: Maximilian Szengel Date: Wed, 18 Apr 2012 14:02:20 +0000 Subject: [PATCH] test update --- src/include/gnunet_regex_lib.h | 16 ++--- src/regex/Makefile.am | 15 +++-- src/regex/regex.c | 25 +++++--- .../{test_regex.c => test_regex_eval_api.c} | 4 +- src/regex/test_regex_iterate_api.c | 61 +++++++++++++++++++ 5 files changed, 99 insertions(+), 22 deletions(-) rename src/regex/{test_regex.c => test_regex_eval_api.c} (98%) create mode 100644 src/regex/test_regex_iterate_api.c diff --git a/src/include/gnunet_regex_lib.h b/src/include/gnunet_regex_lib.h index e1469b25c..aec37c173 100644 --- a/src/include/gnunet_regex_lib.h +++ b/src/include/gnunet_regex_lib.h @@ -112,14 +112,19 @@ GNUNET_REGEX_eval (struct GNUNET_REGEX_Automaton *a, const char *string); /** + * Get the first key for the given 'input_string'. This hashes + * the first x bits of the 'input_strings'. + * + * @param input_string string. + * @param string_len length of the 'input_string'. + * @param key pointer to where to write the hash code. + * * @return number of bits of 'input_string' that have been consumed * to construct the key */ unsigned int -GNUNET_REGEX_get_first_key (const char *input_string, - unsigned int string_len, - GNUNET_HashCode *key); - +GNUNET_REGEX_get_first_key (const char *input_string, unsigned int string_len, + GNUNET_HashCode * key); /** * Check if the given 'proof' matches the given 'key'. @@ -133,7 +138,6 @@ int GNUNET_REGEX_check_proof (const char *proof, const GNUNET_HashCode *key); - /** * Iterator callback function. * @@ -151,7 +155,6 @@ typedef void (*GNUNET_REGEX_KeyIterator)(void *cls, unsigned int num_edges, const struct GNUNET_REGEX_Edge *edges); - /** * Iterate over all edges starting from start state of automaton 'a'. Calling * iterator for each edge. @@ -165,7 +168,6 @@ GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a, GNUNET_REGEX_KeyIterator iterator, void *iterator_cls); - #if 0 /* keep Emacsens' auto-indent happy */ { #endif diff --git a/src/regex/Makefile.am b/src/regex/Makefile.am index 7b9d31095..c6886cb3a 100644 --- a/src/regex/Makefile.am +++ b/src/regex/Makefile.am @@ -19,15 +19,22 @@ libgnunetregex_la_LDFLAGS = \ -version-info 0:0:0 check_PROGRAMS = \ - test_regex + test_regex_eval_api \ + test_regex_iterate_api if ENABLE_TEST_RUN TESTS = $(check_PROGRAMS) endif -test_regex_SOURCES = \ - test_regex.c -test_regex_LDADD = \ +test_regex_eval_api_SOURCES = \ + test_regex_eval_api.c +test_regex_eval_api_LDADD = \ + $(top_builddir)/src/regex/libgnunetregex.la \ + $(top_builddir)/src/util/libgnunetutil.la + +test_regex_iterate_api_SOURCES = \ + test_regex_iterate_api.c +test_regex_iterate_api_LDADD = \ $(top_builddir)/src/regex/libgnunetregex.la \ $(top_builddir)/src/util/libgnunetutil.la diff --git a/src/regex/regex.c b/src/regex/regex.c index 51ceab26a..ae28fb488 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c @@ -927,9 +927,12 @@ dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx, int change; change = 1; - for (i = 0, s1 = a->states_head; i < a->state_count && NULL != s1; + for (i = 0, s1 = a->states_head; + i < a->state_count && NULL != s1; i++, s1 = s1->next) + { s1->marked = i; + } // Mark all pairs of accepting/!accepting states for (s1 = a->states_head; NULL != s1; s1 = s1->next) @@ -980,7 +983,7 @@ dfa_merge_nondistinguishable_states (struct GNUNET_REGEX_Context *ctx, struct GNUNET_REGEX_State *s2_next; - for (i = 0, s1 = a->states_head; NULL != s1; s1 = s1->next) + for (s1 = a->states_head; NULL != s1; s1 = s1->next) { for (s2 = a->states_head; NULL != s2 && s1 != s2; s2 = s2_next) { @@ -1697,8 +1700,10 @@ GNUNET_REGEX_construct_dfa (const char *regex, const size_t len) GNUNET_free (dfa_stack); GNUNET_REGEX_automaton_destroy (nfa); + GNUNET_REGEX_automaton_save_graph (dfa, "dfa_before.dot"); dfa_minimize (&ctx, dfa); - scc_tarjan (&ctx, dfa); + /*GNUNET_REGEX_automaton_save_graph (dfa, "dfa_after.dot");*/ + /*scc_tarjan (&ctx, dfa);*/ return dfa; } @@ -1760,7 +1765,7 @@ GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a, p = fopen (filename, "w"); - if (p == NULL) + if (NULL == p) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not open file for writing: %s", filename); @@ -1788,8 +1793,6 @@ GNUNET_REGEX_automaton_save_graph (struct GNUNET_REGEX_Automaton *a, GNUNET_free (s_acc); } - s->marked = 1; - for (ctran = s->transitions_head; NULL != ctran; ctran = ctran->next) { if (NULL == ctran->to_state) @@ -2009,8 +2012,8 @@ state_get_edges (struct GNUNET_REGEX_State *s, struct GNUNET_REGEX_Edge *edges) { if (NULL != t->to_state) { - edges[count].label = &t->label; - edges[count].destination = t->to_state->hash; + /*edges[count].label = &t->label;*/ + /*edges[count].destination = t->to_state->hash;*/ count++; } } @@ -2041,7 +2044,6 @@ iterate_edge (struct GNUNET_REGEX_State *s, GNUNET_REGEX_KeyIterator iterator, iterator (iterator_cls, &s->hash, NULL, s->accepting, num_edges, edges); - for (t = s->transitions_head; NULL != t; t = t->next) iterate_edge (t->to_state, iterator, iterator_cls); } @@ -2060,5 +2062,10 @@ GNUNET_REGEX_iterate_all_edges (struct GNUNET_REGEX_Automaton *a, GNUNET_REGEX_KeyIterator iterator, void *iterator_cls) { + struct GNUNET_REGEX_State *s; + + for (s = a->start; NULL != s; s = s->next) + s->marked = GNUNET_NO; + iterate_edge (a->start, iterator, iterator_cls); } diff --git a/src/regex/test_regex.c b/src/regex/test_regex_eval_api.c similarity index 98% rename from src/regex/test_regex.c rename to src/regex/test_regex_eval_api.c index c09dc184a..49cdb3931 100644 --- a/src/regex/test_regex.c +++ b/src/regex/test_regex_eval_api.c @@ -18,7 +18,7 @@ Boston, MA 02111-1307, USA. */ /** - * @file regex/test_regex.c + * @file regex/test_regex_eval_api.c * @brief test for regex.c * @author Maximilian Szengel */ @@ -293,7 +293,7 @@ main (int argc, char *argv[]) srand (time (NULL)); for (i = 0; i < 100; i++) - check_rand += test_random (200, 250, 20); + check_rand += test_random (100, 150, 20); return check_nfa + check_dfa + check_rand; } diff --git a/src/regex/test_regex_iterate_api.c b/src/regex/test_regex_iterate_api.c new file mode 100644 index 000000000..913e94f2b --- /dev/null +++ b/src/regex/test_regex_iterate_api.c @@ -0,0 +1,61 @@ +/* + This file is part of GNUnet + (C) 2012 Christian Grothoff (and other contributing authors) + + GNUnet is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3, or (at your + option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNUnet; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ +/** + * @file regex/test_regex_iterate_api.c + * @brief test for regex.c + * @author Maximilian Szengel + */ +#include +#include +#include "platform.h" +#include "gnunet_regex_lib.h" + +void key_iterator(void *cls, const GNUNET_HashCode *key, const char *proof, + int accepting, unsigned int num_edges, + const struct GNUNET_REGEX_Edge *edges) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Iterating...\n"); +} + +int +main (int argc, char *argv[]) +{ + GNUNET_log_setup ("test-regex", +#if VERBOSE + "DEBUG", +#else + "WARNING", +#endif + NULL); + + int error; + const char *regex; + struct GNUNET_REGEX_Automaton *dfa; + + error = 0; + regex = "ab?(abcd)?"; + + dfa = GNUNET_REGEX_construct_dfa (regex, strlen (regex)); + GNUNET_REGEX_automaton_save_graph (dfa, "dfa.dot"); + GNUNET_REGEX_iterate_all_edges (dfa, key_iterator, NULL); + GNUNET_REGEX_automaton_destroy (dfa); + + return error; +} -- 2.25.1