From e5aff270adfaf6ce72d13232ce909968c28baa3f Mon Sep 17 00:00:00 2001 From: Maximilian Szengel Date: Mon, 25 Jun 2012 11:15:41 +0000 Subject: [PATCH] new test for regex --- src/regex/Makefile.am | 9 +++- src/regex/regex.c | 10 ++++- src/regex/test_regex_proofs.c | 84 +++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 src/regex/test_regex_proofs.c diff --git a/src/regex/Makefile.am b/src/regex/Makefile.am index 8c73c607c..cb9bc093a 100644 --- a/src/regex/Makefile.am +++ b/src/regex/Makefile.am @@ -20,7 +20,8 @@ libgnunetregex_la_LDFLAGS = \ check_PROGRAMS = \ test_regex_eval_api \ - test_regex_iterate_api + test_regex_iterate_api \ + test_regex_proofs if ENABLE_TEST_RUN TESTS = $(check_PROGRAMS) @@ -38,5 +39,11 @@ test_regex_iterate_api_LDADD = \ $(top_builddir)/src/regex/libgnunetregex.la \ $(top_builddir)/src/util/libgnunetutil.la +test_regex_proofs_SOURCES = \ + test_regex_proofs.c +test_regex_proofs_LDADD = \ + $(top_builddir)/src/regex/libgnunetregex.la \ + $(top_builddir)/src/util/libgnunetutil.la + EXTRA_DIST = # test_regex_data.conf diff --git a/src/regex/regex.c b/src/regex/regex.c index bf65703b4..b0fb4c175 100644 --- a/src/regex/regex.c +++ b/src/regex/regex.c @@ -919,7 +919,15 @@ remove_epsilon (const char *str) return GNUNET_strdup (str); } - +/** + * Compare 'str1', starting from position 'k', with whole 'str2' + * + * @param str1 first string to compare, starting from position 'k' + * @param str2 second string for comparison + * @param k starting position in 'str1' + * + * @return -1 if any of the strings is NULL, 0 if equal, non 0 otherwise + */ static int strkcmp (const char *str1, const char *str2, size_t k) { diff --git a/src/regex/test_regex_proofs.c b/src/regex/test_regex_proofs.c new file mode 100644 index 000000000..47cc4ee5b --- /dev/null +++ b/src/regex/test_regex_proofs.c @@ -0,0 +1,84 @@ +/* + 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_proofs.c + * @brief test for regex.c + * @author Maximilian Szengel + */ +#include +#include +#include "platform.h" +#include "gnunet_regex_lib.h" + +int +main (int argc, char *argv[]) +{ + GNUNET_log_setup ("test-regex", +#if VERBOSE + "DEBUG", +#else + "WARNING", +#endif + NULL); + + int error; + int i; + const char *regex[21] = { + "ab(c|d)+c*(a(b|c)+d)+(bla)+", + "(bla)*", + "b(lab)*la", + "(ab)*", + "ab(c|d)+c*(a(b|c)+d)+(bla)(bla)*", + "z(abc|def)?xyz", + "1*0(0|1)*", + "a+X*y+c|p|R|Z*K*y*R+w|Y*6+n+h*k*w+V*F|W*B*e*", + "(cd|ab)*", + "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)", + "abc(1|0)*def", + "ab|ac", + "(ab)(ab)*", + "ab|cd|ef|gh", + "a|b|c|d|e|f|g", + "(ab)|(ac)", + "a(b|c)", + "a*a", + "ab?(abcd)?", + "(ab|cs|df|sdf)*", + "a|aa*a" + }; + char *computed_regex; + struct GNUNET_REGEX_Automaton *dfa; + + error = 0; + + for (i = 0; i < 21; i++) + { + dfa = GNUNET_REGEX_construct_dfa (regex[i], strlen (regex[i])); + computed_regex = GNUNET_strdup (GNUNET_REGEX_get_computed_regex (dfa)); + GNUNET_REGEX_automaton_destroy (dfa); + + dfa = GNUNET_REGEX_construct_dfa (computed_regex, strlen (computed_regex)); + error += (0 == strcmp (computed_regex, GNUNET_REGEX_get_computed_regex (dfa))) ? 0 : 1; + GNUNET_free (computed_regex); + GNUNET_REGEX_automaton_destroy (dfa); + } + + return error; +} -- 2.25.1